fbContent = '<div id="alert-wrapper">';
fbContent += '<div id="alert">';
fbContent += "<h1>Consumer Alert</h1>";
fbContent += '<div class="seperator"></div>';
fbContent += "<p>";
fbContent += "As of March 1, 2011, The Hershey Company will begin producing the HERSHEY'S KISSES Milk Chocolates filled with Cherry Cordial Cr&egrave;me and HERSHEY'S KISSES Milk Chocolates filled with Caramel on a line that manufactures peanut-containing products. As a result, we want to inform our consumers that the following allergen statement is being added to the packages: ALLERGY INFORMATION: MANUFACTURED ON THE SAME EQUIPMENT THAT PROCESSES <b>PEANUTS</b>.<br /><br />";
fbContent += "During a transition period, both the original and relocated products will continue to be in the marketplace. The labels accurately reflect the ingredient and allergen information for the products. Consumers, therefore, should read the labels carefully. It should be noted that the products also continue to contain milk and soy lecithin.<br /><br />";
fbContent += "For more information, please contact The Hershey Company at (800) 468-1714.";
fbContent += "</p>";
fbContent += "</div>"
fbContent += "</div>"

fbLinkCaramel = '<br /><br /><a href="#alert" class="consumer-alert">Consumer Alert: HERSHEY\'S KISSES Milk<br />Chocolates filled with Caramel</a><br /><br />';
fbLinkHoliday = '<br class="cl" /><br /><br /><a href="#alert" class="consumer-alert">*Consumer Alert: KISSES Milk Chocolates filled with Cherry Cordial Cr&egrave;me and<br />HERSHEY\'S KISSES Milk Chocolates filled with Caramel</a><br /><br />';

HERSHEYS.Kisses = {};

HERSHEYS.Kisses.trackKissStep = function(stepID){
	
	switch (stepID){
		case 1:{
			var pageName = 'home';
			s.events = 'event15';
			break;
		}
		case 2:{
			var pageName = 'personalize';
			s.events = 'event18';
			s.prop29 = s.eVar38 = 'kisses:app:send a kiss:step 2';
			break;
		}
		case 3:{
			var pageName = 'names and emails';
			s.events = 'event18';
			s.prop29 = s.eVar38 = 'kisses:app:send a kiss:step 3';
			break;
		}
		case 4:{
			var pageName = 'preview';
			s.events = 'event18';
			s.prop29 = s.eVar38 = 'kisses:app:send a kiss:step 4';
			break;
		}
		case 5:{
			var pageName = 'complete';
			s.events = 'event16';
			s.prop29 = s.eVar38 = 'kisses:app:send a kiss:complete';
			break;
		}
	}

	s.prop26 = s.eVar36 = 'kisses:app:send a kiss';
	s.pageName = 'hersheys:kisses:experience:send a kiss:' + pageName;
	s.channel = s.prop2 = 'hersheys:kisses:experience';
	s.prop3 = 'hersheys:kisses:experience:send a kiss';
	s.prop4 = s.prop1 = s.pageName.toLowerCase();
	s.t();

};

HERSHEYS.Kisses.initializations = {

    /* HOMEPAGE */
    'kisses': function () {

		$('#main').hide();
//		HERSHEYS.Kisses.Flash.init();
		HERSHEYS.Kisses.Flash.videoComplete(); // temporarily disabling the flash animation
    },

    /* PRODUCTS */
    'kisses/products': function () {
        HERSHEYS.TabbedBrowsing.init({
            navSelector: '.products-box a.product-bottom',
            contentSelector: '.tabbedBrowsing .tabbedContent'
        });

        $(HERSHEYS.TabbedBrowsing).bind('NewItemShowing', function (e, data) {
            var productName = $(data).attr('title');
            HERSHEYS.Analytics.doProductView(productName);
        });
		$(".prod-desc:eq(4)").append(fbLinkCaramel);
		$("body").append(fbContent);
		$("a.consumer-alert").fancybox();
    },

	'kisses/products/filled-with-caramel': function () {
		$(".prod-desc").append(fbLinkCaramel);
		$("body").append(fbContent);
		$("a.consumer-alert").fancybox();
	},
    
	'kisses/seasonal/holiday-products': function () {
		$("#recipes-content").append(fbLinkHoliday);
		$("body").append(fbContent);
		$("a.consumer-alert").fancybox();
	},
    
	/*EXPERIENCE OUR STORY*/
    'kisses/experience/our-story': function () {
        HERSHEYS.TabbedBrowsing.init({
            navSelector: '#our-story-tabbed-nav .nav a',
            contentSelector: '#our-story-tabbed-content .tabbedContent',
            'nextSelector': '.next-story',
            'previousSelector': '.prev-story'
        });

        $(HERSHEYS.TabbedBrowsing).bind('NewItemShowing', function (e, data) {
            var name = $(data).attr('title').toLowerCase();
            HERSHEYS.Analytics.doPageView('our story', name);
        });

    },
    /*SEND A KISS*/
    'kisses/experience/send-a-kiss': function () {

        $('#main').css('visibility', 'hidden');

        var id = window.location.href.split('?id=').length == 2 ? window.location.href.split('?id=')[1] : 0;

        if (id) {
            $.getJSON('/AjaxServices.ashx?method=getKiss&kissId=' + id, function (data) {
                HERSHEYS.Kisses.SendToAFriend.init(data);
            });
        } else {
            HERSHEYS.Kisses.SendToAFriend.init({});
        }
    }
    /* ETC */
};

HERSHEYS.Utility.doPageInits(HERSHEYS.Kisses.initializations);

//Send a Kiss
HERSHEYS.Kisses.SendToAFriend = (function(){
	
	var steps = ['step1', 'step2', 'step3', 'step4', 'step5'];
	
	var values = {
		FoilId:1, 
		FlavorId:1, 
		PlumeMessage:'',
		UserMessage:'',
		ToName:'',
		ToEmail:'',
		FromName:'',
		FromEmail:''
	}
	
	unlockedStep = 'step2';
	currentStep = 'step1';
	
	var  gotoStep = function( stepID )
	{
		currentStep = stepID;
		updateValues();
		updateNav();
	}
	
	function updateNav()
	{
		//current step index
		var cIndex = steps.indexOf(currentStep);

		//unlocked step index		
		var uIndex = steps.indexOf(unlockedStep);
		
		var i;
		var len = steps.length;
		var sID;
		var step;
		var html;
		
		// Unlock the steps
		if(cIndex>uIndex)
		{
			if(currentStep == 'step3')
			{
				unlockedStep = 'step5';
			}
			else
			{
				unlockedStep = currentStep;
			}
			uIndex = steps.indexOf(unlockedStep);
		}
		
		// Update the menu
		for(i=0; i<len; i++)
		{
			sID = steps[i];
			step = $('#'+steps[i]);
			
			if( i<=uIndex)
			{
				
				html = step.html().replace('<div', '<a');
				html = html.replace('</div', '</a');
				step.html(html);

				if(i==cIndex)
				{
					step.find('a').attr('class', 'send-kiss-'+sID+' active');
				}
				else
				{
					step.find('a').attr('class', 'send-kiss-'+sID);
				}
				step.find('a').attr('href', '#');
				step.find('a').unbind('click', onNavClick);
				step.find('a').bind('click', onNavClick);
				
				if(i==cIndex)
				{
					$('#'+currentStep+'-content').show();
				}
				else
				{
					$('#'+steps[i]+'-content').hide();
				}	
			}
			else
			{
				html = step.html().replace('<a', '<div');
				html = html.replace('</a', '</div');
				step.html(html);
				step.find('div').attr('class', 'send-kiss-'+sID+' disabled');
				$('#'+steps[i]+'-content').hide();
			}
		}
		
		// Hide Preview Button
		if(currentStep == 'step3')
		{
			$('#step4').show();
			$('#stepNext').hide();
		}
		else
		{
			$('#step4').hide();
			$('#stepNext').show();
		}
		
		// Display for step 4
		if(currentStep == 'step4')
		{
			$('#step1').hide();
			$('#step2').hide();
			$('#step3').hide();
			$('#step4').hide();
			$('#stepNext').hide();
			$('#goBack').show();
			$('#step5').show();
			
			$('.final-message-plume').html(values.PlumeMessage);
			$('.final-message').html(values.UserMessage);
			$('.final-message-name').html('&ndash;'+values.FromName);
		}
		else if(currentStep == 'step5')
		{
			$('#step1').hide();
			$('#step2').hide();
			$('#step3').hide();
			$('#goBack').hide();
			$('#stepNext').hide();
			$('#step5').hide();
			
			var post = {
				
				'PlumeMessage' 	: values.PlumeMessage,
		        'UserMessage' 	: values.UserMessage,
	            'ToName' : values.ToName,
	            'ToEmail' : values.ToEmail,
	            'FromName' : values.FromName,
	            'FromEmail' : values.FromEmail,
	            'FoilId' : values.FoilId,
	            'FlavorId' : values.FlavorId
			}
		
			$.post('/AjaxServices.ashx?method=sendKiss', post, function(data){		
				
				if (data == 1){
					// Call Analytics
					HERSHEYS.Kisses.trackKissStep(5);
				}
				
			});
			
		}
		else if(currentStep == 'step6')
		{
			$('#step1').hide();
			$('#step2').hide();
			$('#step3').hide();
			$('#goBack').hide();
			$('#stepNext').hide();
			$('#step5').hide();
			
		}
		else
		{
			$('#step1').show();
			$('#step2').show();
			$('#step3').show();
			$('#goBack').hide();
			$('#step5').hide();
			$('#step6').hide();
		}
	}
	
	function updateValues()
	{
		values.PlumeMessage = $('#PlumeMessage').val();
		values.UserMessage = $('#UserMessage').val();
		values.ToName = $('#ToName').val();
		values.ToEmail = $('#ToEmail').val();
		values.FromName = $('#FromName').val();
		values.FromEmail = $('#FromEmail').val();
	}
	
	function reset()
	{
		$('#PlumeMessage').val('');
		$('#UserMessage').val('');
		$('#ToName').val('');
		$('#ToEmail').val('');
		$('#FromName').val('');
		$('#FromEmail').val('');
		
		setFoilByID(1);
		setChocolateByID(1);
		values.PlumeMessage = $('#PlumeMessage').val();
		values.UserMessage = $('#UserMessage').val();
		values.ToName = $('#ToName').val();
		values.ToEmail = $('#ToEmail').val();
		values.FromName = $('#FromName').val();
		values.FromEmail = $('#FromEmail').val();
		
		$('#step6-content').hide();
	}
	
	function onNavClick()
	{
			var cleanTitle = HERSHEYS.Utility.titleHash($(this).attr('title'));
			location.hash = '/' + cleanTitle;
			gotoStep($(this).parent().attr('id'));
			return false;
	}
	
	function onFoilClick()
	{
		var foilID = $(this).attr('class').replace('foil-button foil-', '');
		setFoilByID( foilID );
	}
	
	function onChocolateClick()
	{
		var chocolateID = $(this).attr('class').replace('chocolate-button chocolate-', '');
		setChocolateByID( chocolateID );
	}
	
	function onNextClick()
	{
		var cIndex = steps.indexOf(currentStep);
		var item = $('#'+steps[cIndex+1]);
		var title = item.find('div').attr('title') ? item.find('div').attr('title') : item.find('a').attr('title');
		
		var cleanTitle = HERSHEYS.Utility.titleHash(title);
		location.hash = '/' + cleanTitle;
		gotoStep(steps[cIndex+1]);
		return false;
	}
		
	function onGoBackClick()
	{
		gotoStep('step3');
	}
	
	function onCreateAndShareClick()
	{
		reset();
		gotoStep('step1');
	}
	
	function setFoilByID( foilID )
	{
		$('#step1-back').attr('class', 'color'+foilID);
		$('#step2-back').attr('class', 'color'+foilID);
		$('#step3-back').attr('class', 'color'+foilID);
		$('#step4-back').attr('class', 'color'+foilID);
		$('#step5-back').attr('class', 'color'+foilID);
		$('#step6-back').attr('class', 'color'+foilID);
		values.FoilID = foilID;
	}
	
	function setChocolateByID( chocolateID )
	{
		$('#step1-front').attr('class', 'type'+chocolateID);
		$('#step4-front').attr('class', 'type'+chocolateID);
		$('#step5-front').attr('class', 'type'+chocolateID);
		$('#step6-front').attr('class', 'type'+chocolateID);
		values.FlavorID = chocolateID;
	}
	
	return {		
		init : function(values){
			
			$.each(values, function(i,v){
				
				values[i] = v;
				
			});
			
			var flashvars = {};
			var attributes = {};
			var param = {wmode : 'transparent'};
				
			var altContent = function(){
				
				onCreateAndShareClick();
				
				if( values.PlumeMessage != '' )
				{
					currentStep = 'step1';
					ulockedStep = 'step1';
					setFoilByID( values.FoilId );
					setChocolateByID( values.FlavorId );
					$('.final-message-plume').html(values.PlumeMessage);
					$('.final-message').html(values.UserMessage);
					$('.final-message-name').html(values.FromName);
					
					gotoStep('step6');
					$('#step6-content').show();
					
				}
				else
				{
					reset();
				}
				
				$('#stepNext').find('a').bind('click', onNextClick);
				$('#goBack').find('a').bind('click', onGoBackClick);
				$('.foil-button').bind('click', onFoilClick);
				$('.chocolate-button').bind('click', onChocolateClick);
				$('.create-and-share-but').find('a').bind('click', onCreateAndShareClick);
				updateNav();
				$('#main').css('visibility', 'visible');
			};

			var params = {allowFullScreen : true, allowScriptAccess: "always", wmode: "transparent"};
			var attributes = {id: "send_kiss"};
			var flashVars = {};
			flashVars.inBrowser = "true";

			// absolute *directory* path to the application SWF file. Will append "sendKissApp.swf" to path
			// if not present, will use relative path to page that shell is located
			flashVars.SwfPath = "/assets/swf/kisses/";
			//flashVars.SwfPath = location.href.substring( 0, location.href.lastIndexOf("/") )+ "/flash/";

			// absolute path to the "send" service
			var base = window.location.href.split(".com")[0];
			flashVars.ServiceURL = base + ".com/AjaxServices.ashx?method=sendKiss";
			
			// if any of below variables are missing, it will not display the kiss
			flashVars.FoilID = values.FoilId;
			flashVars.FlavorID = values.FlavorId;
			flashVars.UserMessage = values.UserMessage;
			flashVars.PlumeMessage = values.PlumeMessage;
			flashVars.FromName = values.FromName;
			
			swfobject.embedSWF("/assets/swf/kisses/sendKissShell.swf", 
								"flash_alternative", "1024", "560", "9.0.0", 
								"/assets/swf/expressInstall.swf",flashVars, params, attributes, function(e){
									if (!e.success){
										altContent();
									}else{
										$('#main').css('visibility', 'visible');
									}
			});
			
		}
	}
})();


HERSHEYS.Kisses.Flash = {

	airDelight : function(){

		var flashvars = {};
		var attributes = {};
		var param = {wmode : 'opaque', base : '.' };

		flashvars.swfpath = "/assets/swf/kisses/airDelightIntro_Main.swf";
		flashvars.wmode = 'opaque';

		swfobject.embedSWF("/assets/swf/kisses/airDelightIntro_Preloader.swf", 
							"flash_wrap_airdelight", "1024", "552", "10.0.0", 
							"/assets/swf/expressInstall.swf",flashvars, param, attributes,function(e){								
								if (e.success){
								}else{
									$('#main').show();
									$('#flash_wrap_holder_airdelight').hide();
									// trigger the tabbed browsing behavior
									HERSHEYS.Kisses.Flash.videoComplete();
								}
		});

	},
	videoComplete : function(){
		// this function is used as an end result of the AirDelight sequence. is called via the swf
		
		setTimeout(function(){
			swfobject.embedSWF("/assets/swf/kisses/carousel_video_shareKiss.swf",
									"flash_wrap", "800", "395", "9.0.0",
									"/assets/swf/expressInstall.swf", { 'wmode': 'opaque' }, { 'wmode': 'opaque' }, { 'wmode': 'opaque' }, function (e) {
			});
			$('#flash_wrap_holder_airdelight').hide();
			$('#main').removeClass('hidden').fadeIn();
			
		}, 500);


		HERSHEYS.TabbedBrowsing.init({
			navSelector: '#home-tabbed-nav .nav a',
			contentSelector: '#home-tabbed-content .tabbedContent',
			'nextSelector': '.tabbedNext',
			'previousSelector': '.tabbedPrevious',
			'autoRotate' : 10000,
			ignoreShow: true,
			callback: function () {
				var that = HERSHEYS.TabbedBrowsing;
				var index = !that.config.ignoreShow ? that.currentIndex : arguments[0]; // being sloppy by hardcoding and assuming

				that.contentItems.removeClass('showflashnice').addClass('noshowflashnice');
				if (that.contentItems.hasClass(that.config.tabContentClass)) {
					that.contentItems.removeClass(that.config.tabContentClass);
				}
				$(that.contentItems[index]).removeClass('noshowflashnice').addClass(that.config.tabContentClass + ' showflashnice');

				var fw = document.getElementById('flash_wrap');

				if (index == 2 && fw != null && fw.nodeName == 'OBJECT') {
					// call the swf's internal method each time the slide is shown.
					// note: on first load, the method is undefined
					if (typeof fw.unwrap == 'function') { fw.unwrap(); }
				}
			}
		});
	},
	init : function(){
		this.airDelight();
	}

};


$(document).ready(function() {
	
	/* Wire up search result button to field and search result page. */
	HERSHEYS.Utility.stealSearchButton({
		'site' : 'kisses'
	});

	/* Enable leaving notice binding */
	HERSHEYS.Utility.leavingNotice({
		'transparency' : HERSHEYS.transparency,
		'title' : 'KISSES'
	});
	
	HERSHEYS.ShareThis.init();
	
	
	if (typeof CURRENT_PROMOTION_ID != 'undefined'){
		switch(CURRENT_PROMOTION_ID){
			case 'kisses-familygamenight':
			
				$("#pr_content").append('<a href="#" style="display: none;" class="pr_close_button"></a>');
				
				$("h2#pr_promoheader").bind('click', function(){
					window.location.reload();
					return false;
				});
				
				$('#pr_nav_gamenightideas a').unbind('click').bind('click', function(){
					HERSHEYS.Analytics.trackPromotionPage($(this).attr('title'));
					$('.pr_close_button').show();
					$('#pr_promostatus_content').hide();
					$('#pr_content').css('background-image', 'url("/assets/images/pr_images/kisses/game-night/gamenightcombo_bg.jpg")');
				
					return false;
				});

				// show game ideas from the "promo expired message"
				$('a.pr_desc_gamenightideas').unbind('click').bind('click', function(){
					HERSHEYS.Analytics.trackPromotionPage($(this).attr('title'));
					$('.pr_close_button').show();
					$('#pr_promostatus_content').hide();
					$('#pr_content').css('background-image', 'url("/assets/images/pr_images/kisses/game-night/gamenightcombo_bg.jpg")');
				
					return false;
				});

				/* sloppy one off... sorry! thom */
				if (window.location.href.indexOf('#pr_gamenightideas') != -1) {
					HERSHEYS.Analytics.trackPromotionPage($('#pr_nav_gamenightideas a').attr('title'));
					$('.pr_close_button').show();
					//$('#pr_promostatus_content, #pr_promoenter, #replay_animation').hide();
					$('#pr_promostatus_content').hide();
					$('#pr_content').css('background-image', 'url("/assets/images/pr_images/kisses/game-night/gamenightcombo_bg.jpg")');
				} else {
					$('.pr_promo_description').show();
				}
				
				$('#pr_primarynav a').bind('click', function(){
					$('#pr_primarynav a').removeClass('active');
					$(this).addClass('active');
				});
				
				$('.pr_play_ratuki_btn').bind('click', function(){
					$('#pr_nav_playratuki a:first').click();
					return false;
				});
				
				$('.pr_close_button').bind('click', function(){
					$.fancybox.close();
					$('.pr_gamenight #pr_content ').css('background-image', 'url("/assets/images/pr_images/kisses/game-night/home-1.jpg")');
					$('#pr_promostatus_content').show();
					$(this).hide();
					return false;
				});
					
				HERSHEYS.Kisses.prepareRatuki = function(){
					
					var title = 'Please enter your date of birth to continue:';
				
					var newDOB = $('.pr_form_fieldset_dob').first().clone().css('width', 300).attr('id', 'ratuki_agecheck_form');
					newDOB.find('select').css({'float' : 'left', 'margin-right' : 10});
					newDOB.prepend('<span>' + title + '</span><br /><br />');
					var goButton = $('<div class="pr_button"><a href="#" id="age_check_ratuki" class="override_leaving">Continue</a>');
					newDOB.append(goButton);
					
					goButton.click(function(){
						var error = false;
						var month = $('#ratuki_agecheck_form [name=dob_month]').val(),
							day = $('#ratuki_agecheck_form [name=dob_day]').val(),
							year = $('#ratuki_agecheck_form [name=dob_year]').val();
							
							if (month == ''){
								$('#ratuki_agecheck_form [name=dob_month]').addClass('error');
								error = true;
							}
							if (day == ''){
								$('#ratuki_agecheck_form [name=dob_day]').addClass('error');
								error = true;
							}
							if (year == ''){
								$('#ratuki_agecheck_form [name=dob_year]').addClass('error');
								error = true;
							}
							
							if (!error){
							
								HERSHEYS.Services.agecheck({
									year : year,
									month : month,
									day : day,

									success : function(){ 
										$.fancybox({
										        	'autoDimensions'	: false,
													'width'         		: 780,
													'height'        		: 650,
													'transitionIn'		: 'none',
													'transitionOut'		: 'none',
													'type' : 'iframe',
													'href' : '/assets/swf/kisses/ratuki/index.html',
													'onStart': function() {
														if($('.pr_page').length) { $('#fancybox-wrap').addClass('pr_fancybox'); }
														if($('.pr_fancybox_noborder').length) { $('#fancybox-outer').addClass('pr_fancybox_noborder'); }
													}
										});
									},
									failure : function(){
										$("#pr_playratuki .pr_node_title_container").remove();
										$("#pr_playratuki .pr_node_main_inner").html("We're sorry, you are ineligible to participate in this game. <br />Please see our <a href='http://thehersheycompany.com/privacy-policy.aspx'>privacy policy</a> for details.");
										$.fancybox.resize();
									}
								});
								
							}else{
								
							}
						return false;
						
					});
				
					$("#pr_playratuki .pr_node_title_container").hide();
					$('#pr_playratuki .pr_node_main_inner').html(newDOB);
				};
				
				//if (window.location.hash.indexOf('ratuki_enabled') != -1){
					HERSHEYS.Kisses.prepareRatuki();
				//}

				$('#fancybox-close').live('click', function(){
				   $("#pr_primarynav a").removeClass('active');
				});
				
			break;
		}
	}
	
	
});
