$(function(){

	function imagefade(){
		$('.product.open .product-image-container img:not(":first")').hide();
		var count=0;
		var countImages = $('.product.open .product-image-container img').size();

		if(countImages>1){
			setInterval(nextImage,7000);
		}

		function nextImage() {
			$('.product.open .product-image-container img:eq('+count+')').fadeOut(1500);
			if(count<countImages-1){
				count++;
			} else {
				count=0;
			}
			$('.product.open .product-image-container img:eq('+count+')').fadeIn(1500);
		}	
	}

	//footer
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		var height = $(window).height() - 50;
		var top = $(document).scrollTop() + height;

		$(window).scroll(function(){
			var top = $(document).scrollTop() + height;
				$(".footer").css({
				"position": "absolute",
				'top': + top + "px",
				"left": 0
			});	
		});

		$(".footer").css({
			"position": "absolute",
			'top': + top + "px",
			"left": 0
		});
	 }
		
	//accordian
	$(".accordian h2").click(function(){		
		$(".accordian").animate({
			"height": "43px"
		});	
		$(".accordian").removeClass("open");
		$(this).parent(".accordian").animate({
			"height": $(this).next(".content").height()+63
		});
		$(this).parent(".accordian").addClass("open");
	});

	//Home Page content
	$("a.read-more").click(function(){
		$(".content-second").slideDown("slow", function(){
			$("a.read-more").fadeOut();
		});		
		return false;
	});
	$("a.read-less").click(function(){
		$(".content-second").slideUp("slow", function(){
			$("a.read-more").fadeIn();
		});		
		return false;
	});

	// Footer
	$(".footer-control").mouseenter(function(){
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			var height = $(window).height() - 50;
			var top = $(document).scrollTop() + height;		

			$(".footer").animate({
				"height": "245px",
				"top": top-175+"px"			
			}, {queue: false});
		} else{
			$(".footer").animate({
				"height": "225px"
			}, {queue: false});
		}
	});

	$(".footer").mouseleave(function(){
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			var height = $(window).height() - 50;
			var top = $(document).scrollTop() + height;
			$(".footer").animate({
				"top": top+"px",
				"height": "50px"
			}, {queue: false});
		} else{
			$(".footer").animate({
				"height": "50px"
			}, {queue: false});
		}
	});

	//order-sidebar

	$(".order-now").mouseenter(function(){
		$(this).animate({
			"right": 0
		}, {queue: false});
	});

	$(".order-now").mouseleave(function(){
		$(this).animate({
			"right": "-236px"
		}, {queue: false});
	});

	// products

	$(".product-control").click(function(){

		if($(this).parent().hasClass("closed")){

			$(this).parent().siblings(".open").children("p").fadeOut();

			$(".product.open").animate({
				"height": "188px",
				"width": "205px"
			}, {
				duration: "10000",
				complete: function(){					
					$(this).removeClass("open");
					$(this).addClass("closed");
				}				
			});	
			
			$(".product.open").find(".product-image").hide();
			$(".product.open").find(".product-image:first").show();		

			if($(".product.open").length){

				var imageSrc = $(".product.open").find(".product-image:first");
				$(imageSrc).animate({
					"height": "124px",
					"width": "205px"
				}, {
					duration: "10000"
				});	
				var src = imageSrc.attr("src").replace("sml/", "");
				$(imageSrc).attr("src", src);		
				
			}			

			$(this).parent(".product").animate({
				"height": "406px",
				"width": "675px"
			}, {
				duration: "10000",
				complete: function(){
					$(this).children("p").fadeIn();
					$(this).addClass("open");
					$(this).removeClass("closed");
					imagefade();
				}
			});		
			
			var imageSrc = $(this).parent().find(".product-image:first");
			$(imageSrc).animate({
				"height": "347px",
				"width": "675px"
			}, {
				duration: "10000"
			});	
			var src = imageSrc.attr("src").replace("sml/", "");
			$(imageSrc).attr("src", src);	

			return false;
		} else{			
			$(this).parent().animate({
				"height": "188px",
				"width": "205px"
			}, {
				duration: "10000"			
			});			


			if($(".product.open").length){			  
				
				var imageSrc = $(".product.open").find(".product-image:first");
				$(imageSrc).animate({
					"height": "124px",
					"width": "205px"
				}, {
					duration: "10000"
				});	
				var src = imageSrc.attr("src").replace("sml/", "");
				$(imageSrc).attr("src", src);						
			}
			$(".product.open").find(".product-image").hide();
			$(".product.open").find(".product-image:first").show();
			$(this).parent().children("p").fadeOut();						
			$(this).parent().addClass("closed");
			$(this).parent().removeClass("open");
		}
		
		return false;
	});

	$(".product-control").hover(function(){
		$(this).parent().siblings().animate({
			"opacity": "0.5"
		}, { queue: false });
	});

	$(".product-control").mouseleave(function(){
		$(".product").animate({
			"opacity": "1"
		}, { queue: false });
	});

	$(".order-form").fancybox({			
		'width' : '75%',
		'height' : '75%',
		'overlayColor' : '#000',
		'autoScale' : false,
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'type' : 'iframe'
	});

});
