current = 0;
last = 0;
timeout=0;
currentLink = ['','','','','','',''];

$(document).ready(function() {
	initial();
	$('#playerWrap a').each(function(index){
		$(this).bind("click", function(){
			if (currentLink[index] != ''){
				window.location.href = currentLink[index];
			}
		});
	});
});
(function(jQuery) {
    jQuery.extend(jQuery.fx.step, {
        backgroundPosition: function(fx) {
            function toArray(strg) {
                strg = strg.replace(/left|top/g, '0px');
                strg = strg.replace(/right|bottom/g, '100%');
                strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g, "$1px$2");
                var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
                return [parseFloat(res[1], 10), res[2], parseFloat(res[3], 10), res[4]];
            }

            if (parseInt(fx.state) === 0 && typeof fx.end ==='string') {
                var start = jQuery.curCSS(fx.elem,'backgroundPosition');
                if (!start) {
                    start = jQuery.curCSS(fx.elem,'backgroundPositionX') + ' ' + jQuery.curCSS(fx.elem,'backgroundPositionY');
                }
                start = toArray(start);
                fx.start = [start[0], start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0], end[2]];
                fx.unit = [end[1], end[3]];
            }
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0] + ' ' + nowPosX[1];
        }
    });
})(jQuery); 

jQuery.fn.idle = function(time){
	return this.each(function(){
		var i = $(this);
		i.queue(function(){
			setTimeout(function(){
				i.dequeue();
			}, time);
		});
	});
};

function showProduct(delay) {
	current = (current)%2+1;
	topPos = "(0 " + current * -251 + ")";
	lowPos = "(0 " + current * -161 + ")";
	$("#layer1").idle(delay).hide(1).animate({backgroundPosition:topPos},3).fadeIn(500, function() {
		currentLink[0] = urls[current][0];
		$("#layer2").animate({backgroundPosition:topPos},3).hide().fadeIn(500, function() {
			currentLink[1] = urls[current][1];
			$("#layer3").animate({backgroundPosition:topPos},3).hide().fadeIn(500, function() {
				currentLink[2] = urls[current][2];
				$("#layer4").animate({backgroundPosition:topPos},3).hide().fadeIn(500, function() {
					currentLink[3] = urls[current][3];
					$("#layer7").animate({backgroundPosition:lowPos},3).hide().fadeIn(500, function() {
						currentLink[4] = urls[current][4];
						$("#layer6").animate({backgroundPosition:lowPos},3).hide().fadeIn(500, function() {
							currentLink[5] = urls[current][5];
							$("#layer5").animate({backgroundPosition:lowPos},3).hide().fadeIn(500, function() {
								currentLink[6] = urls[current][6];
								showProduct(5000);
							});
						});
					});
				});
			});
		});
	});
}

function initial() {
	$("#layer2").fadeIn(500, function(){
		$("#layer6").fadeIn(500, function(){
			$("#layer4").fadeIn(500, function() {
				$("#layer5").fadeIn(500, function() {
					$("#layer1").fadeIn(500, function() {
						$("#layer3").fadeIn(500, function() {
							$("#layer7").fadeIn(500, function(){
								showProduct(2000);
							});
						});
					});
				});
			});
		});
	});
}
