/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 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;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: 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*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	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*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var 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;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});


// Use 'windowslivekarneval' as namespace for all site specific functions
var windowslivekarneval = (function()
{
	var str_ContentSelector = "#carnivalGreetings ";
	var int_pagerAnimationSpeed = 500;
	var int_sheetFadeInSpeed = 500;
	var int_pinFadeSpeed = 120;
	var int_textFadeInSpeed = 400;
	var bol_animationRun = false;
	
	function tabBehaviors()
	{
		jQuery(str_ContentSelector + "ul.blackboardTabs li").hover(function()
		{
			if (! bol_animationRun) jQuery(this).addClass("hover");
		},
		function()
		{
			if (! bol_animationRun) jQuery(this).removeClass("hover");
		});
		
		jQuery(str_ContentSelector + "ul.blackboardTabs li a").click(function()
		{
			if (! bol_animationRun) 
			{
				var str_trackingCode = windowslivekarneval.data.tabstracking[jQuery(this).parent().attr("id").substr(7)];
				gm_tracker.ManualTrackClick(str_trackingCode);
				
				// Disable current tab
				jQuery(str_ContentSelector + "ul.blackboardTabs li.current").removeClass("current");
				
				// Select new tab
				jQuery(this).parent().addClass("current");
				windowslivekarneval.data.page = jQuery(this).parent().attr("id").substr(7);
				
				// First fade-out / clear old sheets and then trigger the fly-in of the new sheets
				pagerFlyOutAnimation();
			}
			
			return false;
		});
	}
	
	function pagerFlyOutAnimation()
	{
		var int_pagerAnimationCount = 5;
		var int_fallDownOffset = 310;
		var int_currentSheet = 0;
		var int_localTop = 0;
		
		if (! bol_ie6)
		{
			bol_animationRun = true;
			
			jQuery(str_ContentSelector + "div.blackboard li").css("cursor", "default");
			jQuery(str_ContentSelector + "ul.blackboardTabs li a").css("cursor", "default");
			jQuery(str_ContentSelector + "div.blackboard li.selected").removeClass("selected");
			
			jQuery(str_ContentSelector + "p.hotmailBtn").animate({
				top: "368px",
				left: "220px"
			}, 300, "easeOutQuad").fadeOut(120, function()
			{
				jQuery(str_ContentSelector + "div.clouds").fadeOut(200);
			});
			
			var hdl_animation = window.setInterval(function()
			{
				// Remove pin
				jQuery(str_ContentSelector + "div.blackboard li.sheet" + int_pagerAnimationCount + " span.pin").fadeOut(int_pinFadeSpeed, function()
				{
					// FadeOut sheet 
					jQuery(this).parent().fadeOut(300, function()
					{						
						jQuery(this).find("p").css(
						{
							backgroundImage: "none"
						});
						
						// Clear text
						jQuery(this).find("p span").html("");
						
						// Get current element
						int_currentSheet = parseInt(jQuery(this)[0].className.substr(5, 1)) -1;
						if (int_currentSheet < 1)
						{
							window.clearInterval(hdl_animation);
							
							jQuery("#carnivalGreetings div.blackboard").fadeOut(400, function()
							{
								// Show new page entries
								pagerFlyInAnimation();
							});
						}
					});
				});
				
				int_pagerAnimationCount -= 1;
			}, 10);
		}
		else
		{
			// No animation in main area for IE6
			jQuery(str_ContentSelector + "div.blackboard li.selected").removeClass("selected");
			
			jQuery(str_ContentSelector + "p.hotmailBtn").animate({
				top: "368px",
				left: "260px"
			}, 300, "easeOutQuad", function()
			{
				jQuery(str_ContentSelector + "div.clouds").fadeOut(700, function()
				{
					jQuery(str_ContentSelector + "p.pleaseSelect").fadeIn(350); 
				});
			}).fadeOut(120);
			
			// Remove pin
			jQuery(str_ContentSelector + "div.blackboard li.sheet" + int_pagerAnimationCount + " span.pin").hide();
			jQuery(str_ContentSelector + "div.blackboard li.sheet" + int_pagerAnimationCount).hide(); 
			
			// Reset css values for new entries
			jQuery(str_ContentSelector + "div.blackboard li.sheet" + int_pagerAnimationCount + " p").css(
			{
				backgroundImage: "none"
			});
						
			// Clear text
			jQuery(str_ContentSelector + "div.blackboard li.sheet" + int_pagerAnimationCount + " p span").html("");
						
			// Show new page entries
			pagerFlyInAnimation();
		}
	}
	
	function pagerFlyInAnimation()
	{
		var int_pagerAnimationCount = 1;
		var int_currentSheet = 1;
		var int_starCounter = 0;
		var int_animationFinished = 0;
		
		if (! bol_ie6)
		{
			bol_animationRun = true;
			jQuery(str_ContentSelector + "div.blackboard li").css("cursor", "default");
			jQuery(str_ContentSelector + "ul.blackboardTabs li a").css("cursor", "default");
			
			jQuery("#carnivalGreetings div.blackboard")[0].className= "blackboard blackboard_" +  jQuery("#carnivalGreetings ul.blackboardTabs li.current").attr("id").substr(7);
			jQuery("#carnivalGreetings div.blackboard").fadeIn(400, function()
			{
				var hdl_animation = window.setInterval(function()
				{
					// Fade in sheet
					jQuery(str_ContentSelector + "div.blackboard li.sheet" + int_pagerAnimationCount).fadeIn(int_sheetFadeInSpeed, function()
					{
						// Set staranimation counter
						int_starCounter = parseInt(jQuery(this)[0].className.substr(5, 1));
						if (int_starCounter > 3) int_starCounter = int_starCounter - 3;
		
						// Append animated gif (starts)
						jQuery(this).append('<img width="177" height="130" alt="" style="display:none; "/>');
						
						jQuery(this).find("img").load(function()
						{
							
							jQuery(this).css("display", "block").fadeIn(200, function() 
							{
								jQuery(this).fadeOut(2000, function() 
								{
									// Remove animated gif image
									jQuery(this).parent().find("img").remove();
								});
							});
						
							// Add pin
							jQuery(this).parent().find("span.pin").fadeIn(int_pinFadeSpeed, function()
							{
								// Get current sheet
								int_currentSheet = parseInt(jQuery(this).parent()[0].className.substr(5, 1));
						
								// Set text and graphic
								jQuery(this).next("p").css({
										top: windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].top,
										left: windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].left,
										width: windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].width,
										height: windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].height,
										backgroundImage: "url(" + windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].image + ")"
								});
								jQuery(this).next("p").find("span").html(windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].shorttext);
			
								// Show text
								jQuery(this).next("p").fadeIn(int_textFadeInSpeed, function() 
								{
									int_animationFinished += 1;
									if (int_animationFinished == 5) 
									{
										jQuery(str_ContentSelector + "div.blackboard li").css("cursor", "pointer");
										jQuery(str_ContentSelector + "ul.blackboardTabs li a").css("cursor", "pointer");
										
										bol_animationRun = false;
									}
								});
							});
						});
						//jQuery(this).find("img").attr("src", "../_sub/karnevalsgruesse/img/sterne" + int_starCounter + ".gif?t=" + Math.random());
						jQuery(this).find("img").attr("src", "../_sub/karnevalsgruesse/img/animation.gif?t=" + Math.random());
						
						if (int_pagerAnimationCount > 5)
						{
							window.clearInterval(hdl_animation);
						}
						else 
						{
							int_currentSheet += 1;
						}
					});
					int_pagerAnimationCount += 1;
					
				}, int_pagerAnimationSpeed);
			});
		}
		else
		{
			// No animation in main area for IE6
			// Fade in sheet
			jQuery(str_ContentSelector + "div.blackboard li").show();
			
			// Add pin
			jQuery(str_ContentSelector + "div.blackboard li span.pin").show();
					
			// Set text and graphic
			jQuery(str_ContentSelector + "div.blackboard li").each(function()
			{
				// Get current sheet
				int_currentSheet = parseInt(jQuery(this)[0].className.substr(5, 1));
							
				jQuery(this).find("p").css({
					top: windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].top,
					left: windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].left,
					width: windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].width,
					height: windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].height,
					backgroundImage: "url(" + windowslivekarneval.data[windowslivekarneval.data.page][int_currentSheet - 1].image + ")"
				}).show();
			});
		}
	}
	
	function entries() 
	{
		jQuery(str_ContentSelector + "div.blackboard li").hover(function()
		{
			if ((! jQuery(this).hasClass("selected")) && (! bol_animationRun)) jQuery(this).find("div.hover").show();
		},
		function()
		{
			if ((! jQuery(this).hasClass("selected")) && (! bol_animationRun)) jQuery(this).find("div.hover").hide();
		})
		
		jQuery(str_ContentSelector + "div.blackboard li").click(function()
		{
			if (! bol_animationRun)
			{
				var str_trackingCode = windowslivekarneval.data[windowslivekarneval.data.page][parseInt(jQuery(this)[0].className.substr(5, 1) - 1)].trackingCode;
				gm_tracker.ManualTrackClick(str_trackingCode);
				
				// Remove old entry selection and hide hover
				jQuery(str_ContentSelector + "div.blackboard li.selected").removeClass("selected");
				jQuery(this).find("div.hover").hide();
				
				// Select new entry
				jQuery(this).addClass("selected");
				
				if (jQuery(str_ContentSelector + "p.hotmailBtn").css("display") == "none") var bol_animate = true;
				else var bol_animate = false;
				
				changeButtons(parseInt(jQuery(this)[0].className.substr(5, 1) - 1), bol_animate);
			}
		})
	}
	
	function instructions()
	{
		jQuery("#carnivalGreetings div.instructions p").hover(function() 
		{
			jQuery("#carnivalGreetings div.instructions p.help").fadeTo(300, 0);
			jQuery("#carnivalGreetings div.instructions p.moresnow").css("display", "block").fadeTo(300, 1);
			
			jQuery("#carnivalGreetings .instructionpanel").fadeIn(300);
		},
		function()
		{
			jQuery("#carnivalGreetings div.instructions p.help").fadeTo(300, 1);
			jQuery("#carnivalGreetings div.instructions p.moresnow").fadeTo(300, 0);
			
			jQuery("#carnivalGreetings .instructionpanel").fadeOut(300);
		});
	}
	
	function changeButtons(int_pCurrentSheet, bol_animate)
	{
		var str_mailText = "";
		
		if (bol_animate)
		{
			jQuery(str_ContentSelector + "div.clouds").fadeIn(200, function()
			{
				jQuery(str_ContentSelector + "p.hotmailBtn").css({
					top: "368px",
					left: "220px"
				});
				
				jQuery(str_ContentSelector + "p.hotmailBtn").fadeIn(120).animate({
					top: "314px",
					left: "211px"
				}, 200, "easeOutQuad", function()
				{
					str_mailText = windowslivekarneval.data.mailtemplate.replace(/##SUBJECT##/, encodeURI(windowslivekarneval.data[windowslivekarneval.data.page][int_pCurrentSheet].mailSubject));
					str_mailText = str_mailText.replace(/##BODY##/, encodeURI(windowslivekarneval.data[windowslivekarneval.data.page][int_pCurrentSheet].mailBody)) + "<p><br><br><img src='http://windowslivede.gimas.net/_sub/karnevalsgruesse/img/m.jpg'></p>";
					
					jQuery(this).find("a").attr("href", (str_mailText));
				});
			});
		}
		else
		{
			str_mailText = windowslivekarneval.data.mailtemplate.replace(/##SUBJECT##/, encodeURI(windowslivekarneval.data[windowslivekarneval.data.page][int_pCurrentSheet].mailSubject));
			str_mailText = str_mailText.replace(/##BODY##/, encodeURI(windowslivekarneval.data[windowslivekarneval.data.page][int_pCurrentSheet].mailBody)) + "<p><br><br><img src='http://windowslivede.gimas.net/_sub/karnevalsgruesse/img/m.jpg'></p>";
			
			jQuery(str_ContentSelector + "p.hotmailBtn a").attr("href", (str_mailText));
		}
	}
	
	return {
		init: function()
		{
			tabBehaviors();
			pagerFlyInAnimation();
			entries();
			instructions();
		}
	}
})();


jQuery(document).ready(function()
{
	windowslivekarneval.init();
});

