/*	SONY | SONY STYLE.COM - GLOBAL FOOTER EVENT LISTENERS
 *	Author(s): 	Branden Thompson	| Front End Architect , Branden.Thompson at am dot sony dot com
 *				Jonathan Cheung 	| Sr. Flash Developer , Jonathan.Cheung at am dot sony dot com
 *
 *	Date:		April 17, 2009
 *
 *	JS FILE PRE-CONDITIONS:
 *	-------------------------------------------------------------------------------------------------
 *
 *		- Prototype must be included before this .js (or included functions) is referenced.
 *		- Cookies.js must be included before this .js
 *
 *
 *	FUNCTIONALITY THAT THIS JS WILL PROVIDE
 *	-------------------------------------------------------------------------------------------------
 *	
 *	[DESCRIPTION HERE]
*/

/* =================================================================================================== 
 * BEGIN CRM Footer replacement
 * =================================================================================================== 
*/

//dependancies
// we need to load the light window library if not already loaded.

Event.observe(window, 'load', function() {
	
	if(typeof(lightwindow)!=='function') {
		ssCoreUtil.dynamicLoader('/wcsstore/SonyStyleStorefrontAssetStore/javascript/lightwindow.js','js');
			ssCoreUtil.dynamicLoader('/wcsstore/SonyStyleStorefrontAssetStore/css/lightwindow.css','css');
	
	} 
});

function closeEmailErrorPopup(){
 if($('signUpResultsNotificationSection')){
	 $('signUpResultsNotificationSection').innerHTML = "";
	 $('signUpResultsNotificationSection').hide();
	 		
	 }
	 $('emailSignUpSection').setStyle({ visibility: 'visible'});
 }

 function displayZipDesc(){
 	$('signUpResultsNotificationSection').style.display = "block";
 	$('signUpResultsNotificationSection').innerHTML = "<h3 class='zipCodeTitle'>Why do we need your zip?</h3><ul class='input-set'><li><span class='quickEmailErrorMsg'>Giving us your zip code will allow us to let you know about Sony events and Sony Style store openings in your area.</span></li></ul><div id='okDisplayZipBtn' class='okButton'/>";
	// onClick='closeEmailErrorPopup();']
	Event.observe($('okDisplayZipBtn'), "click", function(e){ 
		closeEmailErrorPopup();		
																  });
	
 } 

/* ! Next we setup the onLoad listener to replace CRM breadcrumb */
Event.observe(window, 'load', function(){
									   						   							   

	if(isCRMUser())
	{
		var bct = document.getElementById('bctlink1');
		if(bct == null)
		{
			//alert('bct is null again!! should not fall in this condition');
		}
		
		if(bct !=null)
		{
			var breadCrumbHTML = bct.innerHTML;
			var breadCrumbTitle = bct.title;
			var breadCrumbHREF = bct.href;
									
			// replace function Sony style with CRM Portal Home
			breadCrumbHTML = breadCrumbHTML.replace(/Sony Style/i,"Backstage Access");
			breadCrumbHREF = breadCrumbHREF.replace("home","backstageportal");
			breadCrumbHREF = breadCrumbHREF.replace("StoreCatalogDisplay","CRMPortalHome");
			if(breadCrumbTitle == "Sony Style")
			{
				breadCrumbTitle = breadCrumbTitle.replace("Sony Style",'Backstage Access');
			}	

			bct.innerHTML = breadCrumbHTML ;
			bct.title =  breadCrumbTitle;
			bct.href = breadCrumbHREF;			
		}
	}
	
	/* Initialize email signup ajax*/
	if ($('emailSignUpSection'))
	{
		ajaxEngine.registerRequest("SYQuickEmailAjaxSignUp", "SYQuickEmailAjaxSignUp");
		ajaxEngine.registerAjaxElement("signUpResultsNotificationSection");
		//ajaxEngine.registerAjaxElement("emailSignUpSection");
	
		var QuickEmailClassChange = {};
		QuickEmailClassChange.ajaxUpdate = function(ajaxResponse) {			
			
			$('signUpResultsNotificationSection').setStyle({ display: 'block' }); 
			$('signUpResultsNotificationSection').setStyle({ height: 'auto' }); 
			
			//$('quickEmailSignupParentContainer').hide();
			$('emailSignUpSection').setStyle({ visibility: 'hidden'});
		}
		
		ajaxEngine.registerAjaxObject("QuickEmailClassChange", QuickEmailClassChange);
		
	
		if($('emailSignUpSubmitButton')) {
	        /* Trigger Email Signup */      
	        Event.observe($('emailSignUpSubmitButton'), "click", function(e){
	                ajaxEngine.sendRequest('SYQuickEmailAjaxSignUp', {method:'post', parameters: 'emailAddress='+escape($("emailSignUpEmailAddress").value) + '&zipCode='+escape($("emailSignUpZipCode").value) + '&newsletter_0='+$("emailSignUpNewsletterInput").value+'&storeId=10151&catalogId=10551&langId=-1'});
	        })
   		}
	}
	
	/* Initiate and Clear Email Signup Fields */	
	$$('.emailFormField').each(function(element){

		Event.observe(element, 'blur', function(e){		
			var element = Event.element(e)			
			if(element.value=='' && element.id=='emailSignUpEmailAddress'){
				element.value='Enter E-mail Address';
			}
			if(element.value=='' && element.id=='emailSignUpZipCode'){
				element.value='Zip Code';
			}
			Event.stop(e);
		})

		Event.observe(element, 'click', function(e){		
			var element = Event.element(e)
			if(element.value=='Enter E-mail Address'){
				element.value='';
			}
			if(element.value=='Zip Code'){
				element.value='';
			}
			Event.stop(e);
		})
	})
	
	
	
	
	// ================================= 
	// ! Clear Search Box values
	// ================================= 	
	if ($('WC_CachedSidebarDisplay_FormInput_searchTerm_In_CatalogSearchForm_1'))
	{
		Event.observe($('WC_CachedSidebarDisplay_FormInput_searchTerm_In_CatalogSearchForm_1'), "click", function(e){
			var element = Event.element(e)
			if(element.value=='Search'){
				element.value='';
			}
		})
	}
	
	if($('termsOfServiceFooterLink')){
		// BRT 7.14.09 - Terms and Conditions Pop-up
		Event.observe($('termsOfServiceFooterLink'), 'click', function(){
		
			var ourURL = window.location.href;
			if(ourURL.indexOf('storeId=10151', ourURL) != -1)
			{
				displayTermsCondPopup('10151');
			}
			else
			{
				displayTermsCondPopup('10152');
			}
			
		
		});// end termsAndConditionsPopup Event Listener
	}
	
	if($('bottomFooterLegalLink')){
		// BRT 7.16.90 - Legal Footer Popup
		Event.observe($('bottomFooterLegalLink'), 'click', function(){
		
			var ourURL = window.location.href;
			if(ourURL.indexOf('storeId=10151') != -1)
			{
				displayLegalInfoPopup('10151');
			}
			else
			{
				displayLegalInfoPopup('10152');
			}
		
		});
	}
	
	if($('bottomFooterFeedbackLink')){
		// BRT 7.16.09 - Stripping out the CallFeedbackURL behavior from the footer markup
		Event.observe($('bottomFooterFeedbackLink'), 'click', function(){
			callFeedBackURL();
		});
	}
	
	if($('viewSampleEmailLink')){
		//Added Lightwindow for
		Event.observe($('viewSampleEmailLink'), 'click', function(evt) {

			if(myLightWindow==null) {
				myLightWindow = new lightwindow();
			}
			myLightWindow.activateWindow({
				href: '/wcsstore/SonyStyleStorefrontAssetStore/img/account/newsletters/hdna_sample.jpg',
				iframeEmbed: false,
				width: 600 
			});

				Event.stop(evt);

		});
	}
	
	if($('footerWhyZipCodeLink'))
	{
		Event.observe($('footerWhyZipCodeLink'), 'click', function(){
			
			displayZipDesc();
			
		})
	}
	
}, false);
/* =================================================================================================== 
 * END CRM Footer Setup
 * =================================================================================================== 
*/

