var IMAGE_DESTINATION = 45;
var IMAGE_SIZE = 36;
var IMAGE_SELECTED_SIZE = 32;

$(document).ready(function(){
	
	//Easing for animations	
	$.extend(jQuery.easing, {
		easeOutElastic: function (x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t===0) {return b;}  
			if ((t/=d)==1) {return b+c;}  if (!p){ p=d*0.3;}
			if (a < Math.abs(c)) { a=c;  s=p/4; }
			else {s = p/(2*Math.PI) * Math.asin (c/a);}
			return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
		},
		easeOutExpo: function (x, t, b, c, d) {
			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		},
		easeInCubic: function (x, t, b, c, d) {
			return c*(t/=d)*t*t + b;
		},
		easeOutCubic: function (x, t, b, c, d) {
			return c*((t=t/d-1)*t*t + 1) + b;
		}
	
	});
	
	/* Splash Page Animation Sequence */
	
	$.fn.cycle.transitions.tmpSlideY = function($cont, $slides, opts) {
		var h = $cont.css('overflow','hidden').height();
		opts.before.push(function(curr, next, opts) {
			$.fn.cycle.commonReset(curr,next,opts);
			opts.animIn.height = next.cycleH;
			opts.animOut.top   = curr.cycleH;
			opts.cssBefore.opacity = 0;
		});
		opts.cssBefore = { top: h, left: 0 };
		opts.animIn = { top: 0, opacity: 1 };
		opts.animOut  = { top: h, opacity: 0 };
	};

	$('#graphicsContainer img:first').css("display","none").fadeIn(1000, function() {
		$('#graphicsContainer').cycle({
			fx:     'tmpSlideY', 
			sync: 0,
			continuous:0,
			speedIn: 1000,
			speedOut: 1200,
			delay: -2000,
			timeout: 8000,
			easeIn : 'easeOutCubic',
			easeOut: 'easeInCubic'
		});
	});
	
	/* Button slide effects */
	
	(function($) {
			$.extend($.fx.step,{
				backgroundPosition: function(fx) {
					if (fx.state === 0 && typeof fx.end == "string") {
						var start = $.curCSS(fx.elem,"backgroundPosition");
						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];
		
				   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]];
				   }
				}
			});
		})(jQuery);

	
	function slideFX(mnuName, xPos, yPos, xPosAni, yPosAni) {
		$(mnuName)
			.css({ backgroundPosition:xPos+" "+yPos })
			.mouseenter(function(e){
				$(this).hoverFlow(e.type, { backgroundPosition:"("+xPosAni+" "+yPosAni+")"}, 400, "easeOutExpo");
			})
			.mouseleave(function(e){
				$(this).hoverFlow(e.type, { backgroundPosition:"("+xPos+" "+yPos+")"}, 400, "easeOutExpo");
			})
	}
	
	
	slideFX("a.btnSplash, a.btnIdeas", "0px", "0px", "0px", "-33px");
	slideFX("a.button", "0px", "0px", "0px", "-29px");

	$form_fields = $('.FormPanel input[type="text"], .FormPanel textarea');
	$form_labels = $('.FormPanel label');
	
	$form_fields.each(function(){
		
		if($(this).val().length>0){
			$label = $(this).parents('fieldset:eq(0)').find('label');
			$label.fadeOut(400);
		}
	});
	
	$form_labels.click(function(e){
		$(this).parents('fieldset:eq(0)').find('input,textarea').focus();
	});
	
	$form_fields.focus(function(e){
		$label = $(this).parents('fieldset:eq(0)').find('label');
		if($(this).val().length==0){
			$label.fadeTo(400,'0.2');
		}
	});
	
	$form_fields.blur(function(e){
		$label = $(this).parents('fieldset:eq(0)').find('label');
		if($(this).val().length==0)
			$label.fadeTo(400,'1.0');
	});

	$form_fields.keyup(function(e){
		if($(this).val().length>0)
			$label.fadeOut(100);
		else
			$label.fadeIn(500);
	});

	$('body').addClass("js");	
	$('.#artifactDetailsContainer.flashAsset #artifactTranscript').css({'overflow-y':'scroll'});
	
	if (!jQuery.browser.msie){	

		$('.FormPanel span.validationError').css({
			'-moz-background-clip':'border',
			'-moz-background-inline-policy':'continuous',
			'-moz-background-origin':'padding',
			'-moz-border-radius-bottomleft':'6px',
			'-moz-border-radius-bottomright':'6px',
			'-moz-border-radius-topleft':'6px',
			'-moz-border-radius-topright':'6px',
			'background-color':'rgba(255, 30, 30, 0.3) !important'
		});
		$('#contentSecContainer div.gallery.detailed li').css({ opacity: 0.6 });
		$('#contentSecContainer div.gallery.detailed li.selected').css({ opacity: 1});
		$('#contentSecContainer div.gallery.detailed li:not(.selected)').hover(function(){$(this).css({ opacity: 1 });}, function() {$(this).css({ opacity: 0.6 });});


		$(".overThumbCaption").hide();
		$(".overThumbCaption").mouseover(function(){$(this).hide()});
		$("#contentSecContainer div.gallery li > img").width(IMAGE_SIZE).height(IMAGE_SIZE)
		$("#contentSecContainer div.gallery li > a > img").hover(
			//Over
			function(e){
				
				var target = $(e.target);
				var a = target.parent();
				var li = a.parent();
	
				li.find(".overThumbCaption").hide();
				li.find(".overThumbCaption").fadeIn(300);
				target.css("position","relative");
				
				target.stop().animate({
					width:IMAGE_DESTINATION+"px",
					height:IMAGE_DESTINATION+"px",
					top:"-5px"
				},100);
	
			},
			
			//Out
			function(e){
				
				var target = $(e.target);
				var a = target.parent();
				var li = a.parent();
				
				if (!li.hasClass("selected")){
					target.stop().animate({
						width:IMAGE_SIZE+"px",
						height:IMAGE_SIZE+"px",
						top:"0px"
					},1000,'easeOutElastic');
				}else{
					
					target.stop().animate({
						width:IMAGE_SELECTED_SIZE+"px",
						height:IMAGE_SELECTED_SIZE+"px",
						top:"0px"
					},1000,'easeOutElastic');
				}
				
				target.css({width:IMAGE_SELECTED_SIZE+"px",height:IMAGE_SELECTED_SIZE+"px",top:"0px"});
				li.find(".overThumbCaption").css({opacity:"1"});
				li.find(".overThumbCaption").stop().fadeOut(0);
			}
		);
	
	} else {
		$("#contentSecContainer div.gallery li a").hover(
			function(e){
				$(this).parent().addClass("hover").children(".overThumbCaption").addClass("hover");
			},
			function(e){
				$(this).parent().removeClass("hover").children(".overThumbCaption").removeClass("hover");
			}
		);
	}
	
});
