Array.prototype.shuffle = function (){ 
	for(var rnd, tmp, i=this.length; i; rnd=parseInt(Math.random()*i), tmp=this[--i], this[i]=this[rnd], this[rnd]=tmp);
};

jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

var intervalID;
var colors = ["#22C2FF","#FFB800","#922CE1","#D0204E","#82CC20"];

currentColor = $("#wrapper").data("startcolor") || 0;

var changeColor = function() {
	newColor = colors[currentColor];
	$("#wrapper").css({"background-color":newColor});
	
	currentColor = currentColor + 1;
	if (currentColor > 5) {
		currentColor = 0;
	}
}

var jump=function(e) {
	e.preventDefault();
	var target = $(this).attr("href");
	$('html,body').animate({
		scrollTop: $(target).offset().top
	},1000,function() {
		location.hash = target;
	});
}

function flash(num, delay) {
	window.setTimeout(function() {  	
		$(".dancer"+num).show();
		window.setTimeout(function() {  
			$(".dancer"+num).hide();
			window.setTimeout(function() {  
				$(".dancer"+num).show();
				window.setTimeout(function() {  
					$(".dancer"+num).hide();						
				}, 300);			
			}, 300);		
		}, 300);
	}, delay);
}

function twinkle(jQObject) {

	jQObject.animate({
	    opacity: 0
	  }, 500+Math.floor(Math.random()*400), function() {
		jQObject.animate({
		    opacity: 1
		  }, 500+Math.floor(Math.random()*400));
		
		window.setTimeout(function(){twinkle(jQObject)}, 2000+Math.floor(Math.random()*5000));					
	  });
}

$(document).ready(function() {
	
	twinkle($("#logostar"));
	twinkle($("#logostar1"));	
	
	if ($("#splash").length > 0 && !$("html").hasClass("ios")) {
		
		if (!$.cookie('seen_splash')) {
			$("#wrapper").hide();
			$.cookie('seen_splash', 'true', { expires: 1 });

			$("#splash").show(function(){
				flash(1, 0);
				flash(2, 900);
				flash(3, 1800);
				window.setTimeout(function() {  
					$("#dancers img").show();
					$("#wrapper").show();
					window.setTimeout(function() { 
						$("#splash").fadeOut();
						
						if (Modernizr.csstransitions && Modernizr.csstransforms) {
							$("#dancers").css("-webkit-transition","all ease-in-out 300ms");
							$("#dancers").css("-moz-transition","all ease-in-out 300ms");
							$("#dancers").css("-ms-transition","all ease-in-out 300ms");
							$("#dancers").css("-o-transition","all ease-in-out 300ms");																		
							$("#dancers").css("transition","all ease-in-out 300ms");						
							window.setTimeout(function(){
								$("#dancers").css({"-webkit-transform":"scale(0.5)", "-moz-transform":"scale(0.5)", "-ms-transform":"scale(0.5)", "-o-transform":"scale(0.5)", "transform":"scale(0.5)", "left":"60px", "top":"-198px"});
							},1);							
						}

					}, 1000);			
				}, 3000);
			});			
		}

	}
	
	if ($(".image").length > 0) {
		var imagesArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
		imagesArray.shuffle();
		
		var imageNumber = 0;
		var zindex = $(".image").length + 1;
		
		$(".image").each(function(){
			$(this).css("z-index", zindex);
			
			$(this).children("img").attr("src","/media/img/components/side_images/photo"+imagesArray[imageNumber]+".jpg")
			imageNumber++;
			zindex--;
		});
			
	}
	
	// var audioPlayer = document.getElementById("audioPlayer");
	// $("#eq").bind("touch click", function() {
	// 	if ($("#eq").attr("class") == "on") {
	// 		audioPlayer.pause();
	// 		$("#eq").removeClass("on");
	// 	} else {
	// 		audioPlayer.play();
	// 		$("#eq").addClass("on");			
	// 	}
	// });

	$("#jplayer").jPlayer( {
		swfPath: "/media/js/jPlayer.swf",
		ready: function () {
			$(this).jPlayer("setMedia", {
					mp3: "/media/audio/streamsound.mp3"
			});
		}	
	});
	
	$("#eq").bind("touch click", function() {
		if ($("#eq").attr("class") == "on") {
			$("#jplayer").jPlayer("pause");
			$("#eq").removeClass("on");
		} else {
			$("#jplayer").jPlayer("play");
			$("#eq").addClass("on");			
		}
	});

	if (Modernizr.csstransitions) {
		intervalID = setInterval(changeColor, 8000);		
	}

	$('a[href*=#]').bind("click", jump);
	
});
