jQuery(document).ready(function($){

	jQuery.fn.exists = function(){return jQuery(this).length>0;}

	// Checkout CVN Credit Card Verification Tooltip
	$('#cvn-hint a').live('click', function(){
		$('#cvn-tip').fadeIn('fast');
		return false;
	});
	$('#cvn-tip a').live('click', function(){
		$('#cvn-tip').fadeOut('fast');
		return false;
	});

	//Open Link in New Window
	$('a[rel^=external]').click( function() {
		window.open(this.href);
		return false;
	});

	//Form Elements
	$('input[type="text"]').focus(function() {
		//$(this).removeClass("idleField").addClass("focusField");
 		if (this.value == this.defaultValue){
 			this.value = '';
		}
		if(this.value != this.defaultValue){
   			this.select();
   		}
	});
	$('input[type="text"]').blur(function() {
 		//$(this).removeClass("focusField").addClass("idleField");
 		if ($.trim(this.value) == ''){
  			this.value = (this.defaultValue ? this.defaultValue : '');
		}
 	});

	//Shop Landing Page
	imageArr = $("#thumbs img");
	imageArrLength = imageArr.length;
	randNum = Math.floor(Math.random () * imageArrLength + 1);
	randNum -= 1;
	currentItem = randNum;

	$("#details > div:eq("+ currentItem +"), #thumbs li:eq("+ currentItem +")").addClass('selected');

	$("#thumbs li").click(function(){
		imgtest = $("#thumbs li").index(this);
		$("#details > div:eq("+ currentItem +"), #thumbs li:eq("+ currentItem +")").removeClass('selected');
		$("#details > div:eq("+ imgtest +"), #thumbs li:eq("+ imgtest +")").addClass('selected');
		currentItem = imgtest;
	});

	$("#details .prevItem").click(function(){
		if(currentItem != 0){
			imgtest = currentItem - 1;
		}else{
			imgtest = imageArrLength - 1;
		}
		$("#details > div:eq("+ currentItem +"), #thumbs li:eq("+ currentItem +")").removeClass('selected');
		$("#details > div:eq("+ imgtest +"), #thumbs li:eq("+ imgtest +")").addClass('selected');
		currentItem = imgtest;
		return false;
	});

	$("#details .nextItem").click(function(){
		if(currentItem != 26){
			imgtest = currentItem + 1;
		}else{
			imgtest = 0;
		}
		$("#details > div:eq("+ currentItem +"), #thumbs li:eq("+ currentItem +")").removeClass('selected');
		$("#details > div:eq("+ imgtest +"), #thumbs li:eq("+ imgtest +")").addClass('selected');
		currentItem = imgtest;
		return false;
	});

	//Tool Tip
	$('#details area, .tools').tooltip();
	$(".clickable").click(function() {
		window.location = $(this).find("a").attr("href");
	});


	//Product Listings Page QUICKVIEW  --------------------------- Changed / Fixed by CAK 11/01/2010
	$('.product').mouseenter(function(){
		if ($(this).find('.ajax_results').length>0) { }
		else {
			$(this).children('.details').show();
		}
	});
	$('.product').mouseleave(function(){
		$(this).children('.details').hide();
	});

	$('.product .quickview').click(function(){
		$('.ajax_results').remove();
		$('.details').hide();
		$this = $(this);
		cont_li = $this.closest('li');
		$('<div class="ajax_results"></div>').prependTo(cont_li);
		url_id = $this.attr('rel');
		url = "/catalog/product/ajax/id/"+url_id;
		$('.product:nth-child(3n) > .ajax_results').addClass('left');
		$.get(url, function(data) {
			cont_li.find('.product-image').css('z-index', '60'); // raise this particular product image above the z-index value of the quickview window
		  	$('.ajax_results').html(data);
		  	$('.more-close').click(function(){
	      		$('.ajax_results').remove();
				$('.product-image').css('z-index', '40'); // make sure all product images return to the original z-index value below the quickview window
				return false;
	    	});
		});
		return false;

	});

	//Details Page
	//Disable photo click temporarily
	$('.photo a').click(function(){
		return false;
	});

	//Collections Page
	/*$('#collections li').mouseover(function(){
		var children = $(this).children('.collections-info');
		children.stop(true,true).slideDown('fast');
	});
	$('#collections li').mouseleave(function(){
		var children = $(this).children('.collections-info');
		children.stop(true,true).slideUp('medium');
	});*/

	//Product Detail Page tabs (description and size chart flip flop)
	$('#size-chart-tab a').click(function(){
			// Style selected tab differently
			$('#desc-tab').removeClass('tabs-selected');
			$('#size-chart-tab').addClass('tabs-selected');
			// Actual content change
			$('#description').hide();
			$('#size-chart').fadeIn();
		return false
	});
	$('#desc-tab a').click(function(){
			// Style selected tab differently
			$('#size-chart-tab').removeClass('tabs-selected');
			$('#desc-tab').addClass('tabs-selected');
			// Actual content change
			$('#size-chart').hide();
			$('#description').fadeIn();
		return false
	});

	$('#recently_tab a').click(function(){
			// Style selected tab differently
			$('#complete_tab').removeClass('tabs-selected');
			$('#recently_tab').addClass('tabs-selected');
			// Actual content change
			$('#complete_the_look').hide();
			$('#recently_viewed').fadeIn();
		return false
	});
	$('#complete_tab a').click(function(){
			// Style selected tab differently
			$('#recently_tab').removeClass('tabs-selected');
			$('#complete_tab').addClass('tabs-selected');
			// Actual content change
			$('#recently_viewed').hide();
			$('#complete_the_look').fadeIn();
		return false
	});

	//Listings Filter Nav
	$('.adj-filters li > ul').hide();
	$('.adj-filters > li > a').click(function(){
		if($(this).parent().attr('class')=='current'){
			$(this).parent().children('ul').hide();
			$(this).parent().removeClass('current');
		}else{
			$(this).parent().children('ul').show();
			$(this).parent().addClass('current');
		}
		return false;
	});
	$('ul.adj-filters li .close').click(function(){
		$(this).parent().children('ul').hide();
		$(this).parent().removeClass('current');
    });

	//Customer Service and Privacy Policy
	$('.service .info-section h2').click(function(event) {
      $(this).siblings('ul').slideToggle();
	});
	$('.info-section ul li h3').click(function(event) {
	   	if ($(this).hasClass('active')) {
			$(this).removeClass('active');
			$(this).siblings().slideToggle();
        } else {
			$(this).siblings().slideToggle();
			$(this).addClass('active');
        }
	});

	//Story Page
	$('.story-sub').hover(function(){
		$('.story-sub').css('display','none');
		$(this).css('display','block');
		$(this).css('background-position','top right');
		$(this).parent().css('background-position','top right');
	},function(){
		$('.story-sub').css('display','block');
		$(this).css('background-position','top left');
		$(this).parent().css('background-position','top left');
	});

	//Newsletter
	$('#news-link').colorbox({iframe:true, innerWidth:500, innerHeight:98});
	$('.newsletter-link').colorbox({iframe:true, innerWidth:500, innerHeight:98});

	$('.cb').colorbox({opacity:.75});
	//Divs that want to be image maps… make sure you can click through on the whole div LIKE an image map (used on the magazine / day dreams pages)
//	$('.clickable').click(function() {
//		window.location = $(this).find('a').attr('href');
//	});

	// FROM MAIN.JS START -----------------------------------------------
	$('.drop-down-nav li').hover(function(){
        var $this = $(this);
        $this.find('ul').css('display','block');
    },function(){
        var $this = $(this);
        $this.find('ul').css('display','none');
    });

    $('.container .videos .video').mouseover(function(){
        var $this = $(this);
        $this.find('.detail').css('display','block');
    }).mouseout(function(){
        var $this = $(this);
        $this.find('.detail').css('display','none');
    });

	// END FROM MAIN.JS -------------------------------------------------

		$('.primary-nav li').hoverIntent(
		function(){

      // 201111221544 +
      if (location.href.match(/\/shop\//)) {
     		$('#a-shop-drop-down-nav').hide();
      }

			$(this).find('.drop-down-nav').show().parents('.header').children('.subnav-bg').fadeIn(250);
	  },
		function(){

      // 201111221544 +
      if (location.href.match(/\/shop\//)) {
        if ($(this).find('.drop-down-nav').id == 'a-shop') ;
        else {
          $(this).find('.drop-down-nav').hide().parents('.header');
       		$('#a-shop-drop-down-nav').show().parents('.header');
       	}
      }
      else

      $(this).find('.drop-down-nav').delay(1000).hide().parents('.header').children('.subnav-bg').fadeOut(250);
	  });

});
