var agent=navigator.userAgent.toLowerCase();
var isIphone = (agent.indexOf('iphone')!=-1);
soundManager.debugMode = false;
soundManager.url = '/skins/fwy/soundmanager/swf/';
jQuery(
	function ($) {
		//start the party
		init();
		bindEvents();
		
		function init(){
	   		var body = $('html,body').attr('scrollTop',0);
			$(window).bind('load',doLoad);
			
	   	}

	   	function doLoad(e){
			//sound manager
			soundManager.onready(function(oStatus) {
				if (!oStatus.success) {
				  return false;	
				}
				if(!isIphone){
					var navSound = soundManager.createSound({
					 id: 'mySound',
					 url: '/skins/fwy/sounds/click1.mp3'
					});
				}
				
			});
	   	}
	
		function bindEvents(){
			//top nav
			$('nav ul#mainNav li a').bind('mouseenter',function(){
				var $navItem = $(this).parent('li');

				$navItem.find('a').addClass('on');
				$navItem.find('.navGlow').addClass('on');
				//make sure our glow is in affect ya all!
				$navItem.css({zIndex:'3'});
				$navItem.find('a').animate({left:'-7px',top:'0px',height:'34px',width:'211px'}, {speed:3000,duration:100,queue:'nav',scope:'topNav'},null,function(){
					if(!isIphone) {
						var sound = soundManager.getSoundById('mySound');
						sound.play();
					}
				});

				
			}).bind('mouseleave',function(){
				
				var $navItem = $(this).parent('li');
				//-3px left tricks the human eye of the text falling back in
				$navItem.find('a').animate({left:'0px',top:'8px',height:'26px',width:'204px'}, {speed:3000,duration:100,queue:'nav',scope:'topNav'},null,function(){
					$navItem.find('a').removeClass('on');
					$navItem.find('.navGlow').removeClass('on');
					$navItem.css({zIndex:'2'});
				});

			});
			
			$('a.closer').bind('click', function(){
				$(this).parent().fadeOut(600);
				if($('#overlay').is(':visible')){
					$('#overlay').fadeOut(600);
				}
				return false;
			});
			
			$('.blurIt').bind('blur',fieldBlur);
			$('.blurIt').bind('focus',fieldFocus);
			
			$('a.loveThis').bind('click', function(){
				$('span.loveTxt').css({'left':'-3px','font-size':'11px'}).html('LOVED');
				$('span.loveThisTxt').css({'left':'16px','font-size':'11px'}).html('IT!!!!!!!');
				return false;
			});
			
		}
		
	}
);

function CartTotal(){
	
	this.load = function(){
		loadTotal();
	}
		
	function loadTotal(){
		jQuery.ajax({
	   		type: "POST",
	   		url: '/store/scripts/cartTotal.php',
			dataType: 'html',
	   		success: loadSuccess,
			error: loadError,
			complete: loadComplete 
	 	});
	}
		
	//for section load
	function loadSuccess(data){
		var cart = data.split(',');
		jQuery('#cartItemCount').html(cart[0]);
	}
	
	function loadError(data){
		// alert('Sorry we couldn\'t find any recipes. Lame.');
	}
	
	function loadComplete(data){
		
	}
		
}


function CartAdd(){
	var h =jQuery('html').height();
	
	
	this.load = function(){
		Add();
	}
		
	function Add(){
		var formData = jQuery('form#product_addtocart_form').serialize();
		jQuery.ajax({
	   		type: "POST",
	   		url: '/store/scripts/addToCart.php',
			data: formData,
			dataType: 'html',
	   		success: loadSuccess,
			error: loadError,
			complete: loadComplete 
	 	});
	}
		
	//for section load
	function loadSuccess(data){
		
		if(data !== "success"){
			jQuery('#ajaxmsg').html(data).fadeIn(600);
			var func = function() {
				jQuery('#ajaxmsg').fadeOut(600);
	        }
	        window.setTimeout(func, 3000);
			
		} else {
			jQuery('#overlay').css({"height": h+"px" }).fadeIn(600);
			jQuery('#addPopUp').fadeIn(600);	
		}
	}
	
	function loadError(data){

	}
	
	function loadComplete(data){
		new CartTotal().load();
		
		var func = function()
        {
			jQuery('#overlay').fadeOut(600);
            jQuery('#addPopUp').fadeOut(600);
        }

        window.setTimeout(func, 3000);
		
	}
}

function switchNav(direction){
	this.load = function(){
		var c = jQuery('.section.active').attr('id');
		// console.debug(c);
		jQuery('ul.top-progress li').removeClass('on');
		
		if(direction == 'back'){
			switch(c){
				case 'opc-login':
					jQuery('li.menuSign').addClass('on');
			  	break;
				case 'opc-billing':
					jQuery('li.menuBilling').addClass('on');
			  	break;
				case 'opc-shipping':
					jQuery('#shipping a.back-link').show();
					jQuery('li.menuShiping').addClass('on');
				break;
				case 'opc-shipping_method':
					// jQuery('#shipping a.back-link').show();
					jQuery('li.menuShipping').addClass('on');
				break;
				case 'opc-payment':
					jQuery('li.menuPay').addClass('on');
				break;

				default:
					return false;
			}
			
		} else {
			switch(c){
				case 'opc-login':
					jQuery('li.menuBilling').addClass('on');
			  	break;
				case 'opc-billing':
					jQuery('li.menuShip').addClass('on');
			  	break;
				case 'opc-shipping':
					jQuery('li.menuShip').addClass('on');
				break;
				case 'opc-shipping_method':
					jQuery('li.menuPay').addClass('on');
				break;
				case 'opc-payment':
					jQuery('li.menuRev').addClass('on');
				break;

				default:
					return false;
			}
		}
	}
	
}

function Progress(){
	this.load = function(){
		var b1 = jQuery('dt.billing').html(),
		b2 = jQuery('dd.billing').html(),
		s1 = jQuery('dt.shipping').html(),
		s2 = jQuery('dd.shipping').html(),
		m1 = jQuery('dt.method').html(),
		m2 = jQuery('dd.method').html();
		
		jQuery('li.shipFill').html(s2+s1);
		if(s2 == b2){
			var b2 = "SAME AS SHIPPING ADDRESS";
		} 
		jQuery('li.billFill').html(b2+b1);
		
		jQuery('li.methFill').html(m2+m1);
		
		var func = function(){
			var p1 = jQuery('dt.payment').html(),
			p2 = jQuery('dd.payment').html();
			jQuery('li.payFill').html(p2+p1);
			new ArrowGiggle().load();
		}
		
		setTimeout(func, 2000);
		
	}
	
}

function ArrowGiggle(){
	this.load = function(){
		if (jQuery('.revArrow').is(':visible')){
			for (var i=0; i < 10; i++) {
				jQuery('.revArrow').animate({'left': '155px'}, {speed:200, duration:1200,easing:'easeOutBounce', preDelay:800});
				jQuery('.revArrow').animate({'left': '0px'}, {speed:200, duration:1000,easing:'easeInOutExpo'});
			};			
			jQuery('.revArrow').animate({'left': '155px'}, {speed:200, duration:1200,easing:'easeOutBounce'});
		}
	}
}

function EqualBottoms(){
	this.load = function(){
		jQuery(document).ready(function () {
			
			// equalHeight(jQuery('.eq'));

			
			function equalHeight(group) {
				var tallest = 0;
				group.each(function() {
					var thisHeight = jQuery(this).height();
					if(thisHeight > tallest) {
						tallest = thisHeight;
					}
				});
				group.height(tallest);
			}
			
		});
		
		
		
	}
}

function fieldFocus(e){
	jQuery(this).css('color','black');
    if (jQuery(this).val() == jQuery(this)[0].title)
    {
    	jQuery(this).val("");
    }
}

function fieldBlur(e){
	jQuery(this).css('color','#cccccc');
    if (jQuery(this).val() == "")
    {
    	jQuery(this).val(jQuery(this)[0].title);
    }
}



