/*NOTES
for this .js to work the following components need to be installed
	1 - updated headercart.cfm
	2 - ajaxheadercart.cfm
	3 - updated dsplayouts.cfm
	4 - ajax-loader.png and blackback.png in the correct folder
	5 - updated circuit files and all their required templates
*/
	
	//browser detection
	function detectMacXFF() {
  		var userAgent = navigator.userAgent.toLowerCase();
  		if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    		return true;
  		}
	}


	function detectFF() {
  		var userAgent = navigator.userAgent.toLowerCase();
  		if (userAgent.indexOf('firefox')!=-1) {
    		return true;
  		}
	}

	var isGenFF = detectFF();
	var isMacFF = detectMacXFF();
	
								
	//window properties
	var myWindowWidth = window.getWidth();
	var myWindowHeight = window.getScrollHeight();
	var myWindowHeightNonScroll = window.getHeight();
	var myCenterWidth = myWindowWidth/2;
	var myCenterHeight = myWindowHeight/3;
	var heightPercent = myWindowHeight*.02;
	var myCenterHeightReal = myWindowHeightNonScroll/2;
	var myWinowUrl;
	
/*===========================================================================================================
-------------------------------------header cart------------------------------------------
===========================================================================================================*/
	
	//PROPS
		
	
	
/*===========================================================================================================
-------------------------------------header login status----------------------------------
===========================================================================================================*/
	//PROPS
	
		//DOM variables
		var cartMenu = document.getElementById('CartMenu');
	
	//METHODS
	
		//delays the implementation of header update
		function checkAndRun() {
			setTimeout ( 'myAjaxGetLogin.request();', 1000 );
		}
		
		//updates the contents of the headerCart
		var myAjaxHeaderUpdate = new Ajax(myBurl(urlShowAjaxLogin), {method: 'get', update: $('CartMenu'), 
							onComplete: function(){
								}
						});
	
	
	
/*===========================================================================================================
-------------------------------------login box--------------------------------------------
===========================================================================================================*/
	
	//PROPS

		//login box DOM variables
		var myLoginHeader = document.getElementById('logIn');
		var myLoginLeftNav = document.getElementById('logInLeftNav'); 
		var myLoginCheckout = document.getElementById('logInCheckOut'); 
		var myLoginClub = document.getElementById('logInClub');
		var myForgetLeftNav = document.getElementById('forgetLeftNav');
		var myElement = document.getElementById('myElement');
		var innerBlockHolder = document.getElementById('innerBlockHolder');
	
	
		//commonly used url variables
		var myURL;
		var urlShowLogin = 'index.cfm?method=members.showLoginAjax&A=';
		var urlProcessLogout = 'index.cfm?method=members.processLogoutAjax&A=';
		var urlShowForgot = 'index.cfm?method=members.showForgotAjax&A=';
		var urlShowAjaxLogin = 'index.cfm?method=layouts.showAjaxLogin&A=';
		var urlSubscribe = 'index.cfm?method=newsletter.subscribeAjax&A=';
		var urlUnsubscribe = 'index.cfm?method=newsletter.unsubscribeAjax&A=';
	
	
		//ajax get request
		var myAjaxGetShowLogin = new Ajax(myBurl(urlShowLogin), {method: 'get', update: $('innerBlockHolder')});
		var myAjaxGetShowForgot = new Ajax(myBurl(urlShowForgot), {method: 'get', update: $('innerBlockHolder')});
		var myAjaxGetProcessLogout = new Ajax(myBurl(urlProcessLogout), {method: 'get', update: $('innerBlockHolder')});
		var myAjaxGetShowAjaxLogin = new Ajax(myBurl(urlShowAjaxLogin), {method: 'get', update: $('innerBlockHolder')});
		var myAjaxSubscribe = new Ajax(myBurl(urlSubscribe), {method: 'get', update: $('innerBlockHolder')});
		var myAjaxUnsubscribe = new Ajax(myBurl(urlUnsubscribe), {method: 'get', update: $('innerBlockHolder')});
	

		//black background mootools set-up
			var myBigBlack = document.getElementById('bigBlack');
		var myBlackW = new Fx.Style('bigBlack','width',{duration:1800});
		var	myBlackH = new Fx.Style('bigBlack','height',{duration:1800});
		if (isMacFF) {
			myBigBlack.style.backgroundImage= "url(../assets/images/global/bigBlack.png)";
    		myBigBlack.style.backgroundRepeat="repeat";
		} else {
			myBigBlack.style.backgroundColor="#000000";
			var myBigBlackFade = new Fx.Style('bigBlack', 'opacity', {duration:500});
		}
		
	
		//my Element Mootools variables (box that we use to move around the login box - no width or height)
		var marginChangeTop = new Fx.Style('myElement', 'top', {duration:600});
			var marginChangeLeft = new Fx.Style('myElement', 'left', {duration:600});
			var myFx = new Fx.Style('myElement', 'opacity', {duration:500});
	
	
		//innerBoxHolder (where the actual content goes)
		var myInnerBoxHeight = new Fx.Style('innerBlockHolder', 'height', {duration:600});
		var myInnerBoxWidth = new Fx.Style('innerBlockHolder', 'width', {duration:600});
		var myInnerBoxFade =  new Fx.Style('innerBlockHolder', 'opacity', {duration:100});
		var myInnerBoxTop = new Fx.Style('innerBlockHolder', 'top', {duration:600});
		var myInnerBoxLeft = new Fx.Style('innerBlockHolder', 'left', {duration:600});
	
	
		//set initial window values
		myElement.style.display = 'none';
		innerBlockHolder.style.display = 'none';
		marginChangeTop.set(window.getScrollTop()+100);
		marginChangeLeft.set(myCenterWidth);
		if (isMacFF) {
			myBigBlack.style.display = 'none';
		} else {
			myBigBlackFade.set(0);
		}	
	

		/*if (myLoginLeftNav != null) {
			addLoginEvents('logInLeftNav', myAjaxGet);
		}

		if (myLoginCheckout != null) {
			addLoginEvents('logInCheckOut', myAjaxGet);
		}

		if (myLoginClub != null) {
			addLoginEvents('logInClub', myAjaxGet);
		}

		if (myForgetLeftNav != null) {
			addLoginEvents('forgetLeftNav', null);
		}*/
		//myFx.set(0);

	//METHODS

		//generate random number
		function generateRandomNumber() {
			var randomNumber2=Math.floor(Math.random()*9000);
			return randomNumber2;
		}
	
		//appends random number to the end of a URL
		function myBurl (baseUrl) {
			var burl = baseUrl + generateRandomNumber();
			return (burl);
		}
	
		
		//adds login event handler to specified element
		function addLoginEvents (element, nowAjax) {
			$(element).addEvent('click', function() {
				showBlackBack();
				showWindow();
				if (nowAjax != null){
					nowAjax.request();
				}
			});
		}


		//submits forms for login or newsletters
		function formSubmitLogin(myForm) {
			window.scrollTo(0,0);
			$(myForm).addEvent('submit', function(e) {
				new Event(e).stop();
				var myLogLoader = $('logLoader').addClass('ajax-loading');
				this.send({
					update: $('innerBlockHolder'),
					onComplete: function() {
									myWindowUrl = location.href;
									if (myWindowUrl.match('JoinClub')) {
										window.location.reload( false );
									}
									if (myWindowUrl.match('personalinformation')) {
										window.location.reload( false );
									}
									loadAjaxLogin('index.cfm?method=layouts.showAjaxLogin');
									setTimeout ( 'closeWindow();', 1500);
									/*if (isMacFF) {
										myBigBlack.style.display = 'none';
									} else {
										myBigBlackFade.set(0);
									}	
									setTimeout('myFx.start(1,0);', 500);*/
									
							}
							
				});
			});
		}


		//general ajax get request for login box
		function submitAjaxGet(newUrl) {
			new Ajax(newUrl, {method: 'get', update: $('innerBlockHolder')}).request();
		}

		//closes login window
		function closeWindow () {
			myElement.style.display = 'none';
			innerBlockHolder.style.display = 'none';
			//$('log').style.display = 'none';
			myInnerBoxWidth.set(0);
			//myInnerBoxHeight.set(0);
			marginChangeTop.set(myCenterHeight);
			marginChangeLeft.set(myCenterWidth);
			myBlackW.set(myWindowWidth);
			myBlackH.set(myWindowHeight);
			if (isMacFF) {
				myBigBlack.style.display = 'none';
			} else {
				myBigBlackFade.set(0);
			}	
			myElement.style.display = 'none';
		}

		//show login window
		function showWindow() {
			$('innerBlockHolder').empty();
			myInnerBoxWidth.set(320);
			myElement.style.display = 'block';
			if (isGenFF) {
				window.getScrollTop();
				marginChangeLeft.set(myCenterWidth-150);
			} else {
				marginChangeLeft.set(myCenterWidth-620);
			}
			innerBlockHolder.style.display = 'block';
			//setTimeout('myFx.start(0,1);', 500);

		}

		//show blackbackground
		function showBlackBack() {
			myWindowWidth = window.getWidth();
			myWindowHeight = window.getScrollHeight();
			myWindowHeightNonScroll = window.getHeight();
			myCenterWidth = myWindowWidth/2;
			myCenterHeight = myWindowHeight/3;
			heightPercent = myWindowHeight*.02;
			myCenterHeightReal = myWindowHeightNonScroll/2;
			myBlackW.set(myWindowWidth);
			myBlackH.set(myWindowHeight);
			if (isMacFF) {
				myBigBlack.style.display = 'block';
			} else {
				myBigBlackFade.set(.4);
			}
		}

		//main function for showing the ajax LOGIN window
		function ajaxLogin(forgot) {
			window.scrollTo(0,0);
			showBlackBack();
			showWindow();
			if(forgot) {
				myAjaxGetShowForgot.request();
			} else {
				myAjaxGetShowLogin.request();
			}
		}

		//Performs the logout procedure
		function ajaxLogout(url) {
			window.scrollTo(0,0);
			/*showBlackBack();
			showWindow();*/
			///
			myAjaxGetProcessLogout.request();
			if (cartMenu != null) {
				randomNumber=Math.floor(Math.random()*9000);
				cartMenu.innerHTML = 'loading...'
				url = url + '&A=' + randomNumber;
				new Ajax(url, {method: 'get', update:('CartMenu'), onComplete: function() {
																			myWindowUrl = location.href;
																			if (myWindowUrl.match('JoinClub')) {
																				window.location.reload( false );
																			}
																			if (myWindowUrl.match('personalinformation')) {
																				window.location.reload( false );
																			}
																			
																						}}).request();
			}
		}

		//changes login status in the header
		function loadAjaxLogin(url) {
			randomNumber=Math.floor(Math.random()*9000);
			cartMenu.innerHTML = 'loading...'
			url = url + '&A=' + randomNumber;
			//myURL = url;
			new Ajax(url, {method: 'get', update:('CartMenu'), onComplete: function() { 
																			} 
					}).request();
		}

		//load subscribe page into the ajax window
		function subscribe(){
			showBlackBack();
			showWindow();
			myAjaxSubscribe.request();
		}


		//loads unsubscribe page into ajax window
		function unsubscribe(){
			showBlackBack();
			showWindow();
			myAjaxUnsubscribe();
		}


		//windows scroll event to keep the cart and the login centered on the screen
		window.addEvent('scroll', function(e) {
			thisScroll = window.getScrollTop();
			loginScrollOld = $('myElement').style.top;
			loginScrollNew = thisScroll+150;
			setTimeout('marginChangeTop.start(loginScrollOld, loginScrollNew)',500);
		});
		
		
		
		
/*===========================================================================================================
-----------------------------------------STORE---------------------------------------------------------
===========================================================================================================*/
	
	//PROPS
	
		

		