/*globals jQuery, ND, window */
(function(ND, $){
	$(function($){

		var Sheridan = {
			
			initialize: function(){
				if($(".banner-container").length > 0) Sheridan.bannerSlider();
				if($(".category-products").length > 0) Sheridan.productHover();
				if($(".popup").length > 0) Sheridan.popup();
				if($(".carousel-scroll").length > 0) Sheridan.carousel();
				if($(".accordian").length > 0) Sheridan.accordian();
				if($(".moreview").length > 0) Sheridan.accordianText();
				if($("span.tip").length > 0) Sheridan.tipPopup();
				if($(".slider-show").length > 0) Sheridan.sliderShow();
				if($(".products-list .product-image ").length > 0) Sheridan.imgPosition();
				Sheridan.checkOutPopUp();
			},
			
			checkOutPopUp: function() {
				
				var contInner = $("#xmas-delivery").html();
				// grab innerhtml .length > 0
				if( contInner!=null && contInner.length > 0) {
					$(".btn-proceed-checkout").colorbox({opacity:0.7, inline:true, href:"#xmas-delivery", onClosed:function(){ checkbeforecheckout(); }});
				}	
				else {
					$(".btn-proceed-checkout").click(function(){
						checkbeforecheckout();	
					});
				}			
			},
			
			bannerSlider: function(){
				$(".banner-container .slideshow").jshow({
					controls: ['previous', 'items', 'next'],
					transitionInterval: 4000
				});
			},
			
			productHover: function(){
				$(".category-products .item").hover(function(){
					$(this).find(".cover").hide();
					$(this).find(".wrap").addClass("wrap-over");
				},
				function(){
					$(this).find(".cover").show();
					$(this).find(".wrap").removeClass("wrap-over");	
				});
			},
			
			popup: function(){
				if($(".enews").length > 0) $(".enews").colorbox({opacity:0.7, inline:true, href:"#enews"});
				if($(".payment-option").length > 0) $(".payment-option").colorbox({opacity:0.7, inline:true, href:"#paymentoptions"});
				
				if($(".shipping_returns").length > 0) $(".shipping_returns").colorbox({opacity:0.7, inline:true, href:"#sheridan_shipping_returns"});

				if($(".secure_shopping").length > 0) $(".secure_shopping").colorbox({opacity:0.7, inline:true, href:"#sheridan_secure_shopping"});

				if($(".privacy_policy").length > 0) $(".privacy_policy").colorbox({opacity:0.7, inline:true, href:"#sheridan_privacy_policy"});
			},
			
			accordian: function(){
				var $this = $(".accordian");
				$(".tab-content", $this).jScrollPane({
					
						showArrows: true,
						horizontalGutter: 16,
						verticalGutter: 16
					
				});
				$(".tab-content", $this).hide();
				$(".tab:first", $this).addClass("active");
				$(".tab-content:first", $this).show();
				
				$(".action", $this).click(function(){
					var currentItem = $(this);
					var currentParent = currentItem.parent();
					var currentContent = currentParent.next().children();
					if (currentParent.hasClass("active")){
						currentParent.removeClass("active");
						currentContent.slideUp();
					}
					else{
						var activeItem = $("dt.active", $this);
						activeItem.removeClass("active");
						activeItem.next().children().slideUp();
						currentParent.addClass("active");
						currentContent.slideDown();
					}					
					return false;
				});
				
			},
			
			accordianText: function(){
				$(".moreview").parent(".textwrap").find(".text").addClass("showless");
				$(".moreview").click(function(){
					$(this).parent(".textwrap").find(".text").toggleClass("showless");
					if($(this).text() == "...read more"){
						$(this).text("...read less");
					}else{
						$(this).text("...read more");
					};
				});
			},

			carousel: function(){
				$('.carousel-scroll').jcarousel({
					scroll:1
				});
			},
			
			tipPopup: function(){
			
				$("span.tip").hover(function(){
					$(".tip-box").addClass("tip-box-over");
				},
				function(){
					$(".tip-box").removeClass("tip-box-over");
				});
			
			},
			
			sliderShow: function(){
				var imgLink = $(".slider-show .scroller li a");
				var tabContent = $(".slider-show .content");
				var target;
				/*
				if ($(".image img",tabContent).length > 0){
					$(".image img",tabContent).hide();
				}
				*/
				if($(".imgClickable").length > 0){
					$(".imgClickable").click(function(){
						var target = $(this).attr("href").split("#")[1];
						if (targetImg.is(':visible')){
							targetImg.fadeOut();
						}
						targetImg.fadeIn();
						return false;
					});
				}
				
				tabContent.children().hide();
				tabContent.children(":first").fadeIn();
				imgLink.eq(0).addClass("active");
				
				imgLink.click(function(){
					var index = imgLink.index(this);
					tabContent.children(":visible").hide();
					tabContent.children().eq(index).fadeIn();
					imgLink.removeClass("active");
					$(this).addClass("active");
					var tabtitle = tabContent.children().eq(index).find(".tab");
					tabtitle.each(function(){
						var tabcontentVal = $(this).next().find(".tab-content").css("display");
						if ( tabcontentVal == "block"){
							$(this).addClass("active");
						}
					});
					return false;
				});
			},
			
			imgPosition: function(){
				$(".products-list .item .wrap").each(function(){
					var $this = $(this);
					var height = $this.height();
					$(".product-image",$this).css("top",height/2 - 129/2);
				});
			
			}
			
		}
		
	$(document).ready(Sheridan.initialize);			
		
	});
}(window.ND || {}, jQuery));

