
function switcher(photos, img_holder, dir) {	
	
	var counter = 0;	
	
	function switch_it() {
		var img1 = new Image();		
		$("#" + img_holder).attr("class","switch_loading");
		$(img1).attr("class","switcher_pic");
		$(img1).hide();
		$(img1).load(function() {
				$(this).click(function() {
						 pop_image(dir + photos[counter]);
					});
				$("#" + img_holder).append(this);								
				$("#" + img_holder).attr("class","");
				$(this).fadeIn(600).delay(10000).fadeOut(400, function() {
						counter ++;
						if (counter >= photos.length) counter = 0;
						$(this).remove();
						switch_it();
					});
			}).attr("src", dir + "thumbs/" + photos[counter]);		
	}
	
	switch_it();
	
}

function pop_image(filename) {			
	var winWidth = $(document).width();
	var winHeight = $(document).height();
	var scrollTop = $(window).scrollTop();		
	var viewHeight = $(window).height();
	var left = (winWidth / 2) - (32 / 2);
	var top = (viewHeight / 2) - (32 / 2) + scrollTop;	
	
	$("#popup_bg").css({"width": winWidth + "px", "height": winHeight + "px"});	
	$("#popup_bg").fadeTo("fast", 0.9, function() {					
			left = (winWidth / 2) - ($("#popup_loader").width() / 2);
			top = ((viewHeight / 2) - ($("#popup_loader").height() / 2)) + scrollTop;	
			$("#popup_loader").css({"left": left + "px", "top": top + "px"});			
			$("#popup_loader").show();
		});		
	$("#popup_bg").click(function() {
			pop_out();
		});			
	
	$("#popup").click(function() {
			pop_out();
		});	
	
	$("#img").remove();		
	
	var img = new Image();
	$(img).load(function() {	
		var w = $(this).attr("width");
		var h = $(this).attr("height");
		left = (winWidth / 2) - (w / 2);
		top = (viewHeight / 2) - (h / 2);			
		top += scrollTop;
		$(this).attr("id", "img");
		$("#popup").css({"left": left + "px", "top": top + "px", "width": w + "px", "height": h + "px"});		
		$("#popup").append(this);	
		$("#popup").fadeIn();	
	}).attr("src",filename);
}

function pop_out() {	
	$("#popup_loader").hide(); 	
	$("#popup").fadeOut();
	$("#popup_bg").fadeOut(function() {
			$("#img").remove();
		});	
}

