var $j = jQuery.noConflict();

$j(document).ready(function() {
	if ($j('.page-title h1').length) {
		
		var brandtitle = $j('.page-title h1').text();
		brandtitle = brandtitle.substr(20);
		var titlelength = brandtitle.length - 1;
		brandtitle = brandtitle.substr(0, titlelength);

		$j('.page-title h1').text(brandtitle);
		
		$j("#manufacturer option:contains('"+brandtitle+"')").attr('selected', 'selected');
		//$('#'+select_id+' option:selected').removeAttr('selected');
	}	
     
     $j("#search_mini_form").submit(function() {
     	$j("#search").val($j("#manufacturer option:selected").text());
      });
      
     $j('.footer').mouseover(
     			function() {
            		$j('#more_stockists').css("visibility", "visible");
            	}
         	);
         	
     $j('.footer').mouseleave(
         function() {
     		$j('#more_stockists').css("visibility", "hidden");
     	}
     );
     
     $j(".product_advice").css("display","none");
     $j(".product_size").css("display","none");
     $j('#descripition').css("font-weight","bold");
     
     
     $j('#style_advice').click(function() {
     	
     	$j('#style_advice').css("font-weight","bold");
     	//$j('#sizing').css("font-weight","normal");
     	$j('#descripition').css("font-weight","normal");
     	
     	
     	$j(".product_size").hide("slow");
     	$j(".product_desc").hide("slow");
  		$j(".product_advice").show("slow", function() {
    		// Animation complete.
  			});
	});    
     
     $j('#descripition').click(function() {
     	
     	$j('#descripition').css("font-weight","bold");
     	//$j('#sizing').css("font-weight","normal");
     	$j('#style_advice').css("font-weight","normal");
     	
     	$j(".product_size").hide("slow");
     	$j(".product_advice").hide("slow");
  		$j(".product_desc").show("slow", function() {
    		// Animation complete.
  			});
	});
	
	$j("#preorder_btn").click(function(){
		$j("#preorder_dropdown").fadeTo(500, 1);
		$j("#signup_bg").fadeTo(500, 0.5);
	});
	
	$j("#close_btn").click(function(){
		$j("#preorder_dropdown, #signup_bg").fadeTo(500, 0, function() {
			$j("#preorder_dropdown, #signup_bg").hide();
		});
	});

    
    $j("#makethis_btn").click(function(){
		$j("#makethisyours").fadeTo(500, 1);
		$j("#signup_bg").fadeTo(500, 0.5);
	});
	
	$j("#close_btn_mty").click(function(){
		$j("#makethisyours, #signup_bg").fadeTo(500, 0, function() {
			$j("#makethisyours, #signup_bg").hide();
		});
	});

    
    //$j("#makethis_btn").toggle(fnClick1, fnClick2);
    
    // function fnClick1() {
    	//$j("#makethisyours").slideToggle("normal");
    	//$j("#upsell_products").removeClass("prefix_2");
    	//$j("#upsell_products").addClass("prefix_6");
    //};
    
    //function fnClick2() {
    	//$j("#makethisyours").slideToggle("normal", function() {
    		//$j("#upsell_products").removeClass("prefix_6");
    		//$j("#upsell_products").addClass("prefix_2");
  		//});

    //};
    
	
	$j(".twitterbtn").mouseover(
            	function() {
            		$j('img:last',this).css("visibility", "visible");
            	}
         	);
         	
    $j(".twitterbtn").mouseleave(
    	function() {
        	$j('img:last',this).css("visibility", "hidden");
    	}
    );
    
    $j(".facebookbtn").mouseover(
    	function() {
    		$j('img:last',this).css("visibility", "visible");
    	}
    );
    
    $j(".facebookbtn").mouseleave(
    	function() {
        	$j('img:last',this).css("visibility", "hidden");
    	}
    );
    
    
    
         
     //$j(".postholder:last").css("border-bottom", "none");   

     //$j("document").ready(function() {
     		
     		//$j("#postcategories ul li:not(li:last-child)").append(", ");
     		
     		
     		//$j(".twitterbtn").mouseover(
            	//function() {
            		//$j('img:last',this).css("visibility", "visible");
            	//}
         	//);
         	
         	//$j(".twitterbtn").mouseleave(
            //	function() {
            //    	$j('img:last',this).css("visibility", "hidden");
            //	}
         	//);
         	//
         	//$j(".facebookbtn").mouseover(
            //	function() {
            //		$j('img:last',this).css("visibility", "visible");
            //	}
         	//);
         	//
         	//$j(".facebookbtn").mouseleave(
            //	function() {
            //    	$j('img:last',this).css("visibility", "hidden");
            //	}
         	//);
			//
			//$j(".post-twitterbtn").mouseover(
            //	function() {
            //		$j('img:last',this).css("visibility", "visible");
            //	}
         	//);
         	//
         	//$j(".post-twitterbtn").mouseleave(
            //	function() {
            //    	$j('img:last',this).css("visibility", "hidden");
            //	}
         	//);
			//
			//$j(".post-facebookbtn").mouseover(
            //	function() {
            //		$j('img:last',this).css("visibility", "visible");
            //	}
         	//);
         	//
         	//$j(".post-facebookbtn").mouseleave(
            //	function() {
            //    	$j('img:last',this).css("visibility", "hidden");
            //	}
         	//);
    //});
    
    
    
   // ---- start coming-soon slider --- 
    
    var currentImage = 0;
    
    $j("#cs-back").click(function(e){
    	e.preventDefault();
    	if(currentImage > 0){
    		currentImage--;
    		scrollImages();
    	}
	   	clearInterval(timer);
    });
    
    $j("#cs-next").click(function(e){
    	e.preventDefault();
    	if(currentImage < $j(".coming-soon-list li").size()-3){
	    	currentImage++;
    	} else {
    		currentImage = 0;
    	}
    	
	    scrollImages();
	   	clearInterval(timer);
    });
    

	function scrollImages(){
    	var newPos = 0;
	    if(currentImage !== 0){
	    	var newPos = -(currentImage * 320);
	    }
	    $j(".coming-soon-list").stop(true,false).animate({left: newPos});
    }    
    
    var searchBar = $j("#search_bar");
    $j("#search_bar").remove();
    $j("#search-bar-holder").append(searchBar);
    
    function nextImage(){
    	if(currentImage < $j(".coming-soon-list li").size()-3){
	    	currentImage++;
    	} else {
    		currentImage = 0;
    	}
    	
	    scrollImages();
    }
    var timer = setInterval(nextImage, 3000); 
    
    
    // ---- start homepage slider ---
    
    
    $j("#feature-1 .widget").cycle({
    	fx: "scrollHorz",
    	easing: "easeInOutExpo",
    	timeout: 5000,
    	speed: 1000
    	
    });
    
    $j("#feature-1 .widget").mouseover(function(){
    	$j(this).cycle("pause");
    });
    
    $j("#feature-1 .widget").mouseout(function(){
    	$j(this).cycle("resume");
    });
        
    
    
    //------- newsletter script -----------------
    
    $j(".block-subscribe").each(function(){
	    $j("#newsletter", this).val("Subscribe to our newsletter");
	    $j("#newsletter", this).click(function(){
	    	$j(this).val("");
	    });
    });
    
    
    
    // ------- messages script -------------------
    if($j(".messages").length){
	    var mheight = $j(".messages").outerHeight();
	    $j(".messages").prepend($j("<div>").attr("id", "close-container").append($j("<a>").attr({"id": "close", "href": "#"})))
			.css({
		    	"position": "fixed",
		    	"top": 0,
		    	"left": 0,
		    	"width": "100%",
		    	"display": "none"
		    })
		    .slideDown(200);
		  $j("#close").click(function(){
			$j(".messages").slideUp(200);
			$j("body").animate({
		    	"margin-top": 0
		    }, 200);
		});
	    $j("body").animate({
	    	"margin-top": mheight
	    }, 200);
    }
    
    
    
    //----------- terms-browser ---------
    $j("#terms-close").click(function(){
    	$j("#terms-menu ul li a").each(function () {
    		$j(this).removeClass("active");
		});
    	$j("#terms-browser, #terms-container").fadeOut(400);
    });
    
    
    $j("#footer-links ul li a").not("#footer-links ul li a.nopopup").click(function(e){
    	e.preventDefault();
    	$j("html,body").stop().animate({scrollTop: 0}, 1000, "easeInOutExpo");
    	$j("#terms-window").empty();
    	$j("#terms-loader").fadeTo(400,1);
    	var targets = $j(this).attr("href");
    	var targetname = $j(this).text();
    	$j("#terms-browser").fadeTo(400,0.5);
    	$j("#terms-container").fadeTo(400,1);
    	console.log("loading " + targets);
    	$j("#terms-window").load(targets + " .std", function() {
			$j("#terms-loader").hide();
		});
    	
		$j("#terms-menu ul li a").each(function () {
    		if ($j(this).text() == targetname) {
				$j(this).addClass("active");
       		}
		});

	
    });
    
    $j("#terms-menu ul li a").click(function(e){
    	e.preventDefault();
    	$j("#terms-window").empty();
    	$j("#terms-loader").fadeTo(400,1);
    	$j("#terms-menu ul li a").each(function () {
    		$j(this).removeClass("active");
		});
    	$j(this).addClass("active");
    	var targets = $j(this).attr("href");
    	$j("#terms-window").load(targets + " .std", function() {
    		$j("#terms-loader").hide();
    	});
    });
    
    //----------- call to action ---------
    
    if($j.cookie("df-offer-show") == null){
    	$j("#subscription-offer").fadeIn(100);
    }
    
    
    $j("#subscription-close").click(function(e){
    	e.preventDefault();
    	$j("#subscription-offer").fadeOut(100);
    	subscriberCookie();
    });
    
    $j("#subscription-offer form").submit(subscriberCookie);
    
    function subscriberCookie(){
    	$j.cookie('df-offer-show', 1, { expires: 365 });
    }
    
    
    
    // ---------------- sizing-------------
    
    $j("#sizing").click(function(){
    	$j("#sizing-info").fadeIn(100);
    });
    
    $j("#sizing-info div").prepend($j("<a id='sizing-close'>"));
    
    $j("#sizing-info").click(function(){
    	$j("#sizing-info").fadeOut(100);
    });
    
    
    // -------------- fade in images when they load --------
    
    $j("img").fadeTo(0,0).each(function(){
		if(this.complete){
			$j(this).fadeTo(100,1);
		} else {
			$j(this).one("load", function(){
				$j(this).fadeTo(100,1);
			});
		}
	});
	
	
	
	// ------------ our boutiques
	
	$j(".fader").cycle({
		fx: 'fade'
	});

});
