/* Author: Andrew Norell
*/
$(document).ready(function() {
	//randomize punyteers
	$("#main aside ul").randomize("li");
	
	if (window.PIE) {
        $("#name span").each(function() {
            PIE.attach(this);
        });
    }
	
	var bolIE678 = false;
	if ($(".ie6, .ie7, .ie8").length > 0) {
		bolIE678 = true;
	}
	
	//puny staff rollover
	var intHover;
	var intLeft = 0;
	var intTop = 0;
	//$("#name").css({opacity: 0});
	function punyteerHoverOut() {
		intHover = setTimeout(function() {
			if (bolIE678) {
				$("#name").stop(true, false).hide();
			} else {
				$("#name").stop(true, false).fadeOut(200, "easeOutSine");
			}
		}, 150);
	}
	$("#main aside ul, #name").mousemove(function(e) {
		/*
		clearTimeout(intHover);
		$("#name span").text($("img", this).attr("alt"));
		if ($("#name").is(":visible") == false) {
			$("#name").stop(true, false).delay(40).css({opacity: 0}).show();
		}
		*/
		intLeft = (e.pageX - $("#container").offset().left) - (($("#name").width()/2))
		intTop = (e.pageY - $("#container").offset().top - 40);
		$("#name").css({left:intLeft + "px", top:intTop + "px"});
		//$("#name").stop(true, false).animate({top:($(this).position().top - 27) + "px", left:intLeft + "px", opacity:1}, 300, "easeOutSine");
	});
	
	$("#main aside ul li").hover(function() {
		clearTimeout(intHover);
		$("#name span").text($("img", this).attr("alt"));
		if ($("#name").css("opacity") < 1 || $("#name").is(":visible") == false) {
			//$("#name").stop(true, false).css({opacity: 0}).show();
			if (bolIE678) {
				$("#name").stop(true, false).show();
			} else {
				$("#name").stop(true, false).fadeTo(300, 1);
			}
		}
		//intLeft = ($(this).position().left + 22) - (($("#name").width()/2))
		//$("#name").stop(true, false).animate({top:($(this).position().top - 27) + "px", left:intLeft + "px", opacity:1}, 300, "easeOutSine");
		
	}, function() {
		punyteerHoverOut();
	});
	
	$("#name").hover(function() {
		clearTimeout(intHover);
		//$("#name span").text($("img", this).attr("alt"));
		//$("#name").fadeTo(400, 1, "easeOutSine");
		if ($("#name").css("opacity") < 1 || $("#name").is(":visible") == false) {
			//$("#name").stop(true, false).css({opacity: 0}).show();
			if (bolIE678) {
				$("#name").stop(true, false).show();
			} else {
				$("#name").stop(true, false).fadeTo(300, 1);
			}
		}
	}, function() {
		punyteerHoverOut();
	});
	

	
	
	var cssGradValue = $("#grad").css("background-image");
	$.support.linearGradient =
	cssGradValue.indexOf( "-webkit-gradient" )  > -1 ? '-webkit-gradient' :
	(cssGradValue.indexOf( "-webkit-linear-gradient" )  > -1 ? '-webkit-linear-gradient' :
	(cssGradValue.indexOf( "-moz-linear-gradient" )  > -1 ? '-moz-linear-gradient' :
	(cssGradValue.indexOf( "-ms-linear-gradient" )  > -1 ? '-ms-linear-gradient' :
	(cssGradValue.indexOf( "-o-linear-gradient" )  > -1 ? '-o-linear-gradient' :
	(cssGradValue.indexOf( "linear-gradient" )  > -1 ? 'linear-gradient' : false)))));

	if ($.support.linearGradient != false && $(".ie7").length < 1)
    {
		$.cssHooks['linearGradientColor'] = { 
			get: function(elem) {
			  var currentStyle=$.css(elem, 'backgroundImage'),gradient,colors=[];
			  gradient=currentStyle.match(/gradient(\(.*\))/g);
			  if(gradient.length)
			  {
				gradient=gradient[0].replace(/(linear|radial|from|\bto\b|gradient|top|left|bottom|right|\d*%)/g,'');
				colors= jQuery.grep(gradient.match(/(rgb\([^\)]+\)|#[a-z\d]*|[a-z]*)/g),function (s) { return jQuery.trim( s )!=''})
			  }
			  return colors;
			}, 
			set: function(elem, value){
				$(elem).css("background-image", $.support.linearGradient + '(top, ' + value[0] + ', ' + value[1] + ')');
			}
		};
		
		var topDest = "#ffe98e";
		//var btmDest = "#ffa48e";
		//var topDest = "#FFEE3B";
		var btmDest = "#EA6272";
		var bolUp = true;
		var intDistance = 0;
		var iAdjustColor = setInterval(function() {
			$("#grad").css("linearGradientColor", [$.Color("#ffa48e").transition(topDest, intDistance), $.Color("#ffe98e").transition(btmDest, intDistance)]);
			
			if (bolUp) {
				intDistance += 0.05;
			} else {
				intDistance -= 0.05;
			}
			
			if (intDistance >= 1) {
				bolUp = false;
			} else if (intDistance <= 0) {
				bolUp = true;
			}
		}, 250);
	}
	function randSwirlBGY() {
		return "-" + (Math.floor(Math.random() * (105 - 50 + 1)) + 50) + "px";
	}
	var strColor = "#ea6272";
	var strColorHover = "#cd4857";
	
	$("#navigation #older, #navigation #newer").data("color", "ed632d");
	$("#navigation #older, #navigation #newer").data("color-hover", "d45726");
	
	$("#main aside .btn, #navigation #older, #navigation #newer").hover(function() {
		if ($(this).data("color-hover") != null) {
			strColorHover = "#" + $(this).data("color-hover");
		} else {
			strColorHover = "#cd4857";
		}
		$(this).stop(true, false).animate({"background-color":strColorHover, "background-position":"0px " + randSwirlBGY()}, 300, "easeOutSine");
	}, function() {
		if ($(this).data("color") != null) {
			strColor = "#" + $(this).data("color");
		} else {
			strColor = "#ea6272";
		}
		$(this).stop(true, false).animate({"background-color":strColor, "background-position":"0px 45px"}, 300, "easeOutSine");
	});
	
	//clouds for scrolling
	var intTop;
	var intDest1;
	var intDest2;
	var intDest3;
	var intDest4;
	var intDest5;
	var intDest6;
	var intSpeed;
	var strEasing = "easeInOutSine";
	function onScrollEvent() {
		intTop = $(window).scrollTop();
		intDest1 = (216 - Math.round(intTop*0.3));
		intDest2 = (255 - Math.round(intTop*0.7));
		intDest3 = (334 - Math.round(intTop*0.5));
		intDest4 = (177 - Math.round(intTop*0.4));
		intDest5 = (278 - Math.round(intTop*0.8));
		intDest6 = (323 - Math.round(intTop*0.2));
		intSpeed = Math.abs(parseInt($("#cl1").css("top"), 10) - intDest1) * 30;
		if (intSpeed < 800) {
			intSpeed = 800;
		} else if (intSpeed > 4500) {
			intSpeed = 4500;
		}
		$("#cl1").stop(true, false).animate({top:intDest1 < -200 ? intDest1 = -200 : intDest1 = intDest1 + "px"}, intSpeed, strEasing);
		$("#cl2").stop(true, false).animate({top:intDest2 < -200 ? intDest2 = -200 : intDest2 = intDest2 + "px"}, intSpeed, strEasing);
		$("#cl3").stop(true, false).animate({top:intDest3 < -200 ? intDest3 = -200 : intDest3 = intDest3 + "px"}, intSpeed, strEasing);
		$("#cr1").stop(true, false).animate({top:intDest4 < -200 ? intDest4 = -200 : intDest4 = intDest4 + "px"}, intSpeed, strEasing);
		$("#cr2").stop(true, false).animate({top:intDest5 < -200 ? intDest5 = -200 : intDest5 = intDest5 + "px"}, intSpeed, strEasing);
		$("#cr3").stop(true, false).animate({top:intDest6 < -200 ? intDest6 = -200 : intDest6 = intDest6 + "px"}, intSpeed, strEasing);
	}
	$(window).scroll(onScrollEvent);
	onScrollEvent();
	
	//hack for youtube embed width. Tumblr doesn't allow youtube videos to be greater than 500px, this JS "fixes" that ;)
	var contentWidth = 640;
	
	$("object").each(function () {
	  if ($(this).find("embed[src^='http://www.youtube.com']").length > 0) {
		// Identify and hide embed(s)
		var parent = $(this).parent();
		parent.css("visibility","hidden");
		var youtubeCode = parent.html();
		var params = "";
		if (youtubeCode.toLowerCase().indexOf("<param") == -1) {
		  // IE doesn't return params with html(), so…
		  $("param", this).each(function () {
			params += $(this).get(0).outerHTML;
		  });
		}
		// Set colours in control bar to match page background
		var oldOpts = /rel=0/g;
		var newOpts = "rel=0&amp;color1=0xeeeeee&amp;color2=0xeeeeee;hd=1";
		youtubeCode = youtubeCode.replace(oldOpts, newOpts);
		if (params != "") {
		  params = params.replace(oldOpts, newOpts);
		  youtubeCode = youtubeCode.replace(/<embed/i, params + "<embed");
		}
		// Extract YouTube ID and calculate ideal height
		var youtubeIDParam = $(this).find("embed").attr("src");
		var youtubeIDPattern = /\/v\/([0-9A-Za-z-_]*)/;
		var youtubeID = youtubeIDParam.match(youtubeIDPattern);
		var youtubeHeight = Math.floor(contentWidth * 0.75 + 25);
		var youtubeHeightWide = Math.floor(contentWidth * 0.5625 + 25);
		// Test for widescreen aspect ratio
		$.getJSON("http://gdata.youtube.com/feeds/api/videos/" + youtubeID[1] + "?v=2&alt=json-in-script&callback=?", function (data) {
		  oldHeight = /height="?([0-9]*)"?/g;
		  oldWidth = /width="?([0-9]*)"?/g;
	
		  newWidth = 'width="' + contentWidth + '"';
		  if (data.entry.media$group.yt$aspectRatio != null) {
			newHeight = 'height="' + youtubeHeightWide + '"';
		  } else {
			newHeight = 'height="' + youtubeHeight + '"';
		  }
		  youtubeCode = youtubeCode.replace(oldHeight, newHeight);
		  youtubeCode = youtubeCode.replace(oldWidth, newWidth);
		  if (params != "") {
			params = params.replace(oldWidth, newWidth);
			params = params.replace(oldHeight, newHeight);
			youtubeCode = youtubeCode.replace(/<embed/i, params + "<embed");
		  }
		  // Replace YouTube embed with new code
		  parent.html(youtubeCode).css("visibility","visible");
		});
	  }
	});
	
});
