$(document).ready(function(){
	callBack();
    lastBlock = $("#a1");
    maxWidth = 589;
    minWidth = 11;	
	xOffset = 450;
	yOffset = 300;
	popupStatus = 0;
	load_image = new Image();
	
	
	$(".itemLink").css({ color: "#D80880", cursor: "pointer"});
	
	
    $("#colorChooser ul li a").click(
      function(){
	  	$("#colorChooser .itemHolder").css({
		"width":589,
		"height":258,
		"overflow":"hidden"
		});
		$("#colorChooser .item").css({
		"overflow":"hidden"
		});
        $(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:600 });
		lastBlock = this;
		$(this).animate({width: maxWidth+"px"}, { queue:false, duration:600, complete:callBack});

      }
    );

	 $("#colorChooser .itemLink").click(
      function(){
	  	window.open('pdf/'+($(this).attr('meta')));
		return false;
      }
    );

	$("#colorChooser .button").click(
      function(){
	  	window.open('foto/'+($(this).attr('meta')));
		return false;
      }
    );
	$("#colorChooser .preview").click(function(e)
	{
		if(popupStatus == 0)
		{
			xOffset = 0;
			yOffset = 0;
			
			popupStatus = 1;
			var windowWidth = document.documentElement.clientWidth;
			var windowHeight = document.documentElement.clientHeight;
			this.t = this.title;
			this.title = "";	
			var c = (this.t != "") ? "<br/>" + this.t : "";
			
			load_image = new Image();
			load_image.src = "fotos/"+ ($(this).attr('id')) +".jpg"; 
			
			var path = load_image.src;
			
			if(load_image.complete){ 
				donePic(windowWidth, windowHeight, path, c); 
			}else{ 
				load_image.onLoad = donePic(windowWidth, windowHeight, path, c); 
			}
			
		}
		else
		{
			popupStatus = 0;
			this.title = this.t;	
			$("#container").remove();
			$("#preview").remove();	
		}
		$("#preview").click(function(e)
		{
			popupStatus = 0;
			this.title = this.t;	
			$("#container").remove();
			$("#preview").remove();
		});
		$("#colorChooser #container").click(function(e)
		{
			popupStatus = 0;
			this.title = this.t;	
			$("#container").remove();
			$("#preview").remove();
		});
		
		
    });	

	
	$("#colorChooser ul li").css('overflow', 'hidden');
	$("#colorChooser ul").css('overflow', 'hidden');
	$("#colorChooser").css('overflow', 'hidden');

});


function donePic(wWidth, wHeight, path, c)
{	
	if(load_image.height == 0) yOffset = (wHeight - 240)/2;
	else yOffset = (wHeight - load_image.height)/2;
	
	if(load_image.width == 0) xOffset = xOffset = 900/2;
	else xOffset = load_image.width/2;
	
	if(xOffset == 'undefined' || xOffset == 0) xOffset = 900/2;
	if(yOffset == 'undefined' || yOffset == 308) yOffset = (wHeight - 240)/2;
		
	$("body").append("<div id='container'>&nbsp;</div>");								
	$("body").append("<div id='preview'><img src='"+path+"' alt='Image preview' />"+ c +"</div>");	
	
	// check scroll position
	var ScrollTop = document.body.scrollTop;

	if (ScrollTop == 0)
	{
	    if (window.pageYOffset)
	        ScrollTop = window.pageYOffset;
	    else
	        ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	
	$("#preview")
		.css("top",yOffset + ScrollTop + "px")
		.css("left",(wWidth/2 - xOffset) + "px")
		.fadeIn("slow");		
	$("#container")
		.css("top",ScrollTop + "px")
		.css("left",0 + "px")
		.css("display", "none")
		.css("position","fixed")
		.css("position","absolute")
		.css("width",99.8+"%")
		.css("height",wHeight-2)
		.css("opacity", "0.7")
		.fadeIn("slow");
}

function callBack()
{
	$(".itemHolder").css({
		"width":589,
		"height":258,
		"overflow":"auto"
		});
	$(".item").css({
		"overflow":"hidden"
		});
}
