/*globals jQuery, ND, window */
(function(ND, $){
	$(function($){

		var Mossimo = {
			
			initialize: function(){
				if($(".tabs").length > 0) Mossimo.tabs();
				if($(".category-products").length > 0) Mossimo.productHover();
				if($(".popup").length > 0) Mossimo.popup();
				if($(".carousel-scroll").length > 0) Mossimo.carousel();
				if($(".accordian").length > 0) Mossimo.accordian();
				if($(".moreview").length > 0) Mossimo.accordianText();
				if($("span.tip").length > 0) Mossimo.tipPopup();
				if($(".slider-show").length > 0) Mossimo.sliderShow();
				if($(".products-list .product-image ").length > 0) Mossimo.imgPosition();
                if($(".size-chart-link").length > 0) Mossimo.sizeChartLink();
				if($("#shopping-cart-table").length > 0) Mossimo.tableItemPosition();
				if($("#keyLook").length > 0) Mossimo.keyLookInt();
                },
			
			bannerSlider: function(){
				$(".banner-container .slideshow").jshow({
					controls: ['previous', 'items', 'next'],
					transitionInterval: 4000
				});
			},
            tabs: function(){
                $(".tabs").tabs();
            },

	
		sizeChartLink: function() {
			$('a.size-chart-link').fancybox({
				'easingIn'      : 'easeOutBack',
				'easingOut'     : 'easeInBack',
				'speedIn'		: 800,
				'autoDimensions': false,
				'overlayColor'	: '#000',
				'overlayOpacity': 0.5,
				'centerOnScroll': true,
				'titleShow'		: false,
				'scrolling'		: 'no'
			});
			},		
			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);
				});
			
			},
			
			tableItemPosition: function(){
				var tableCell = $("#shopping-cart-table .subtotal");
				var container = $("div",tableCell);
				console.log(tableCell.height());
				container.css("height",tableCell.height()+'px');
			},
			
			keyLookInt: function(){
				var slide = $("#keyLook ul li div.slide");
				var keyItem = $("#keyLook ul li");
				
				slide.hide();	
				
				keyItem.hover(function(a) {
					var el = $(this).find('div.slide');
					el.stop(true, true).slideToggle(200, function(){
					});
				}, function() {
					var el = $(this).find('div.slide');
					el.stop(true, true).slideToggle(200, function(){
						keyItem.bind('hover');
					});
				});
			}
			
		}
		
	$(document).ready(Mossimo.initialize);			
		
	});
}(window.ND || {}, jQuery));

