var greensmart = {
	
	init:function()
	{
		if (!($.browser.msie && parseInt(jQuery.browser.version)==6))
			$("#menu a").hover(greensmart.menuHover, greensmart.menuOut);//.bind("click",greensmart.menuClick);
		else
			try{ 
				document.execCommand('BackgroundImageCache', false, true);
			}catch(x){};
		
		
		
	}
	
	,menuHover: function() {
		var el = $(this);
		if (!el.hasClass('current') && !el.hasClass('home'))
			el.animate({ height: "48" }, 150);
			
	}
	
	,menuOut: function() {
		var el = $(this);
		if (!el.hasClass('current') && !el.hasClass('home'))
			el.animate({ height: "14" }, 150);
	}
	
	,menuClick: function(e) {
		
	}
	
	,thirdLevelClick: false
	
	,initSubmenu : function() {
		$('.thirdlevel').hover(
			function(){
				// Nothing
			},
			function(){
				if(! greensmart.thirdLevelClick) {
					$(this).hide();
				}
			}
		);
		
		$("#submenu a").click(greensmart.submenuClick).hover(
			function(){
				mouseOver = function(){};
				$(this).animate({ paddingLeft: '3px' }, 150);
				if (($(this).attr("id")=='submenu_41'))
				{
					if(! greensmart.thirdLevelClick) {
						$(".thirdlevel").show();
						var h = $("#subcontent").height()+30;
			        	if (h < 200)
			         		$('#pagecontent').slideDown();

					}
					return false;
				}
			},
			function(){
				if($(this).attr("id")=='submenu_41' && !greensmart.thirdLevelClick) {
				}
				$(this).animate({ paddingLeft: '0px' }, 150);				
			}
		);	
	}
	
	,submenuClick : function(e) {
		if ($(this).attr("id")=='submenu_41') {
			return false;
		}
		
		var $target = $(e.target);
		e.preventDefault();
		
		if($target.parent().hasClass('thirdlevel')) {
			greensmart.thirdLevelClick = true;
		} else {
			$('.thirdlevel').hide();
			greensmart.thirdLevelClick = false;
		}
		
		
		
		$("#submenu a").each(function(){
			if ($(this).attr('id')==$target.attr('id'))
				$(this).addClass('current');
			else
				$(this).removeClass('current');
		});
		
		
		$('#pagecontent').animate({width:490},500);
		$("#subcontent").html("<img src='/images/ajax-loader.gif'/>");
		$.getJSON($target.attr("href"),{json:true},
	         function(data){
	         	$("#subcontent").html(data.content);
	        	$("#subcontent").slideDown("slow");
	       
	         });
		
		
		return false;
	}
	
	,faqinit : function() {
		jQuery('<div class="Q">Q:</div>').prependTo($('#subcontent'));
		//jQuery('<div id="Answer" style="display:none"><div class="A">A:</div><span></span></div><div class="clear"><div>').appendTo($('#subcontent'));
		
		$('#subcontent blockquote').hide();
		$('#subcontent h3').click(
				function(){
		                        $('#subcontent blockquote').hide();
                                        var p = $(this).next("blockquote");
					p.show();
					//$("#Answer").show();
					//$("#Answer span").html(p.html());
					$('#pagecontent').slideDown();
		         	$('#subcontent h3').each(function(){ $(this).removeClass('current'); });
		         	$(this).addClass('current');
		         	
				});
	}
	
	,storeLoad : function(product) {
	
	}
	
	
	,storeSetup : function(product, styles)
	{
		this.store = { product: product, styles:styles};
		if (product==null && styles==null)
		{
			window.location = $("#ranges a")[0].href;
		}
		
		this.storeImagesInit();
	}
	
	,storeImagesInit : function ()
	{
		$(".images_wrap img").each(function() {
			var $this = $(this); 
			$this.css({position:'absolute',top:0,left:0,display:(this.id=='image1'?'block':'none')});
			
		});
		$(".images_nav").show();
	}
	
	,showImage : function (index)
	{
		$(".images_wrap img").each(function() {
			if (this.id=='image'+index)
				$(this).fadeIn('slow');
			else
				$(this).fadeOut('slow');		
			
		});
		$(".images_nav a").each(function() {
			
			if (this.id=='image_a'+index)
				$(this).addClass('current');
			else
				$(this).removeClass('current');		
		});
	}
	
	,showCartoptions : function()
	{
		this.showPanel(2);
		return false;
	}
	
	,addToCart : function(product) {
		var errors = [];
		var qty = $('#qty').val();
		if (!qty.match("^[0-9]+$"))
			errors.push("The selected Quantity is not a valid number");
		var style = false;
		$(":radio").each(function(){
			if (this.checked)
				style = this.value;
		});
		if (style==false)
			errors.push("You must select a colour from the list");
			
		if (errors.length>0)
		{
			$('#cartoptionserror').html(errors.join("<br/>"));
			return false;
		}
		this.showPanel(3);
		$.post("/online-store/?product="+product,{
			style: style,
			qty:qty,
			addtocart:1
		},this.addToCartResult, "json")
		return false;
	}
	
	,addToCartResult : function(data, textStatus)
	{
		if (textStatus=='success')
		{
			if (data.success==true)
			{
				greensmart.showPanel(4);
				$('.cart').html(data.cart).animate({color:"#da2d28",opacity:0.5},300).animate({color:"#746057",opacity:1},300);
				$('.cart a').animate({color:"#da2d28",opacity:0.5},300).animate({color:"#746057",opacity:1},300);
				
			}
			else
				greensmart.showCartError(data.error);
		}
		else
			greensmart.showCartError("Unable to add product to your cart at this time. Please try again.");
	}
	
	,showCartError : function (error)
	{
		this.showPanel(2);
		$('#cartoptionserror').html(error);;
	}
	
	,showPanel : function(page)
	{
		$('.details').animate({scrollLeft:380*(page-1)},250);
	}
	
	,submitpayment : function()
	{
		if($('#step4_form').length)
		{
			$('#step4_form').submit(function(){
				$("input[@name=submit]").attr('disabled', true);
				$('#loader').show();
			});
		}
	}
}

$(document).ready(greensmart.init);


