var carouselItems = new Array;
var currentScreen;
var items;
var tweenSpeed;
var showTime;


jQuery(document).ready(function() 
{	

	var i;
	for (i in document.images) {
	    if (document.images[i].src) {
	        var imgSrc = document.images[i].src;
	        if (imgSrc.substr(imgSrc.length-4) === '.png' || imgSrc.substr(imgSrc.length-4) === '.PNG') {
	            document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
	        }
	    }
	}





	$('a[rel*=facebox]').facebox();
	
	$(document).bind('afterReveal.facebox', function() {
	   
	});


	if($.browser.msie && $.browser.version.substr(0,1)== 6)
	{
		return;
	}
	else
	{
		flashMovieWithVars = $.flash.create(
			{
				swf: $('#flash span.videoFlash').html(),
				width: $('#flash span.videoWidth').html(),
				height: $('#flash span.videoHeight').html(),
				flashvars: {
					video: $('#flash span.videoFile').html()
				}
			}
		);

		$(document).ready(
			function () {
				$('#flash').html(flashMovieWithVars);
			}
		);

	
	
		
	
		currentScreen = 0;
		tweenSpeed = 400;
		opacityMinimal = 0.0;
		opacityMiddle = 0.4;
		opacityMaximal = 1.0;
		showTime = 5000;
		
		$("#carousel li").each(function()
		{
			carouselItems.push($(this));
		});
		
		items = carouselItems.length;
		
		
		carouselTweener();
		
		$("li.intro").find("div").addClass("clickable");
		
		$("li.intro").hover(function()
		{
			$(this).find("img").stop().animate({ opacity: opacityMinimal}, tweenSpeed * opacityMinimal);
			$(this).find("div").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			

			

			

		},function()
		{
			$(this).find("img").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			$(this).find("div").stop().animate({ opacity: opacityMinimal}, tweenSpeed);
		});
		
		$("li.intro").click(function()
		{	
			if (!$(this).find("a").attr("href")) return;
			window.location=$(this).find("a").attr("href"); return false;
		});
		
		
		
		// init overview pages
		
		$("#overview li").hover(function()
		{
			$(this).find("img").stop().animate({ opacity: opacityMiddle}, tweenSpeed * opacityMiddle);
			$(this).find("span").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);	
		},function()
		{
			$(this).find("img").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			$(this).find("span").stop().animate({ opacity: opacityMinimal}, tweenSpeed);
		});
		
		
		$("a.video").hover(function()
		{
			$(this).find("img").stop().animate({ opacity: opacityMiddle}, tweenSpeed * opacityMiddle);	
		},function()
		{
			$(this).find("img").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
		});
		
		
			
		// init detail pages
		
		$("#thumb01").addClass("clickable");
		$("#thumb02").addClass("clickable");
		$("#thumb03").addClass("clickable");
		
		$("#thumb01").stop().animate({ opacity: opacityMiddle}, tweenSpeed * opacityMiddle);
		$("#thumb02").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
		$("#thumb03").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMinimal);


		$("#thumb01").click(function()
		{
			$("#visual01").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			$("#visual02").stop().animate({ opacity: opacityMinimal}, tweenSpeed * opacityMinimal);
			$("#visual03").stop().animate({ opacity: opacityMinimal}, tweenSpeed * opacityMinimal);
			
			$("#thumb01").stop().animate({ opacity: opacityMiddle}, tweenSpeed * opacityMiddle);
			$("#thumb02").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			$("#thumb03").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			
		});

		$("#thumb02").click(function()
		{
			$("#visual01").stop().animate({ opacity: opacityMinimal}, tweenSpeed * opacityMinimal);
			$("#visual02").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			$("#visual03").stop().animate({ opacity: opacityMinimal}, tweenSpeed * opacityMinimal);
			
			$("#thumb01").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			$("#thumb02").stop().animate({ opacity: opacityMiddle}, tweenSpeed * opacityMiddle);
			$("#thumb03").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
		});

		$("#thumb03").click(function()
		{
			$("#visual01").stop().animate({ opacity: opacityMinimal}, tweenSpeed * opacityMinimal);
			$("#visual02").stop().animate({ opacity: opacityMinimal}, tweenSpeed * opacityMinimal);
			$("#visual03").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			
			$("#thumb01").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			$("#thumb02").stop().animate({ opacity: opacityMaximal}, tweenSpeed * opacityMaximal);
			$("#thumb03").stop().animate({ opacity: opacityMiddle}, tweenSpeed * opacityMiddle);
		});




		// ================================
		// == EMBROIDER
		// ================================
		
		var product = decodeURIComponent(getUrlVars()['product']);
		var font    = decodeURIComponent(getUrlVars()['font']);
		var color   = decodeURIComponent(getUrlVars()['color']);
		var text    = decodeURIComponent(getUrlVars()['text']);
		
		if (product) $("#embroiderProduct").val(product);
		if (font) $("#embroiderFont").val(font);
		
		if (color != undefined)
		{
			var color = rgb2hex($("#embroiderColor li").last().css('background-color'));
			$("#exampleText").css('color' ,color);
		}
		
		if (text != "undefined" )
		{
			alert(text);
		}	
		else
		{	
			text = "Little Company";
		}

		$("#embroiderText").val(text);
		$("#exampleText").text(text);
		
		Cufon.refresh();
		
		
		
		
		$(".embroiderVisual").each(function ()
		{
			var selected = 'img_' + $('#embroiderProduct').attr("value");
			var item     = $(this).attr('id');
		
			if (selected != item) $(this).fadeOut();
		});

		$("#embroiderProduct").change(function ()
		{
			var product = $(this).attr("value");
			var item    = $(this).attr('id');
			var visual  = '#img_' + product;
			
			$(".embroiderVisual").fadeOut();
			$(visual).fadeIn();
			
			var colors = $(visual).find(".embroider_colors").html();
			var colors_array = colors.split('||')			
			
			$("#embroiderColor").slideUp(function()
			{
				$("#embroiderColor li").removeClass('active');
				$("#embroiderColor li").last().addClass('active');
			
				$("#embroiderColor li").each(function()
				{
					var item_color = $(this).text();

					if (jQuery.inArray(item_color, colors_array) > -1) $(this).show();
					else $(this).hide();
				});

				$("#embroiderColor").slideDown();
			});
			
			$("#exampleText").fadeOut(function ()
			{
				var color = rgb2hex($("#embroiderColor li").last().css('background-color'));
			
				$("#exampleText").css('color' ,color);
				Cufon.refresh();
				$(this).fadeIn();
			});
		});
		
		$("#embroiderFont").change(function ()
		{
			var font = $(this).attr("value");
			
			$("#exampleText").fadeOut(function ()
			{
				$(this).attr("class", font);
				Cufon.refresh();
				$(this).fadeIn();
			})
		});
		
		$("#embroiderColor li").addClass('clickable');
		
		$("#embroiderColor li").click(function()
		{
			var color = rgb2hex($(this).css('background-color'));
			
			$("#exampleText").fadeOut(function ()
			{
				$("#exampleText").css('color' ,color);
				Cufon.refresh();
				$(this).fadeIn();
			});
			
			$("#embroiderColor li").removeClass('active');
			$(this).addClass('active');
			
		});
		
		$("#embroiderText").change(function ()
		{
			var text = $(this).attr("value");
			
			$("#exampleText").fadeOut(function ()
			{
				$("#exampleText").text(text);
				Cufon.refresh();
				$(this).fadeIn();
			});
		});








		
	}
	
});


function carouselTweener()
{
	$(carouselItems[currentScreen]).removeClass("hidden").addClass("hiddenAlpha").animate({ opacity: 1}, tweenSpeed).delay(showTime).animate({ opacity: 0}, tweenSpeed);
	
	if(currentScreen < items-1) currentScreen++;
	else currentScreen = 0;
	
	carouselTimer();
}


function carouselTimer()
{
	var t=setTimeout("carouselTweener()",showTime+tweenSpeed);
}


function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}


function rgb2hex(rgb)
{
    rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
    
    function hex(x)
    {
        return ("0" + parseInt(x).toString(16)).slice(-2);
    }
    return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

