$(document).ready(function(){
    
    $(".hoverable").each(function(){
	MakeElementHoverable($(this), "hover")
    });

    $(".product").each(function(){
	MakeElementHoverable($(this), "productHover")
    });

    $(".tweet").addClass("clearer");
 
    $(".clearer").each(function(){
	$(this).append("<div class=\"clear\">.</div>");
    });

    $("tr:odd").addClass("altTableRow");
    
    if($("#HomePageRotator").length > 0)
    {
        var homeCats = $("#HomePageRotator li");
        var homePageRotator = $("#HomePageRotator");
        var homeCatImages = homeCats.find(".image");
        var homeCatSummaries = homeCats.find(".text");
        var homeCatLinks = homeCats.find("a");
	var homeCatHome = homeCats[0];
        
        var homeImageHolder;
        
	$(homeCats[0]).hide();
        homeCatImages.hide();
        homeCatLinks.hide();
        
        homePageRotator.prepend("<div id=\"HomeImageHolder\"></div>");
        homeImageHolder = homePageRotator.find("#HomeImageHolder");
        
        selectHomeCat(homeCats[0]);
        
        homeCats.bind("mouseenter", function(){
            selectHomeCat($(this));
        });

	$("#HomePageRotator").bind("mouseleave", function(){
            deselectHomeCat();
        });
        
        homeCats.click(function(){
            window.location = $(this).find("a").attr("href");
        });
    }

    //Page Section Toggle
    var pageSectionExpandables = $(".pageSectionExpandable");
    
    pageSectionExpandables.each(function(){

	    $(this).find("h3").wrap("<div class=\"toggleHeader\"></div>");
	    $(this).find(".pageSectionInner").wrapInner("<div class=\"toggleContent\"></div>");
	    $(this).find(".pageSectionInner .toggleContent").before($(this).find(".pageSectionInner .toggleHeader"));
	    $(this).find(".pageSectionInner .toggleContent").hide();

	    $(this).find(".pageSectionInner .toggleHeader").click(function(){
		$(this).next(".toggleContent").slideToggle();
            });

    });    

    //wrap things you want rounded in a div
    $(".addTestimonial").wrapInner("<div class=\"roundCornersInner\"></div>");
    $(".pageSummaries .page").wrapInner("<div class=\"roundCornersInner\"></div>");
    $(".parentPageSection1162 .pageSectionInner").wrapInner("<div class=\"roundCornersInner\"></div>");
    $(".tickList").wrap("<div class=\"roundCornersInner\"></div>");
    $(".pageSectionExpandable .pageSectionInner").wrapInner("<div class=\"roundCornersInner\"></div>");
    $("#ContactForm").wrapInner("<div class=\"roundCornersInner\"></div>");
    
    //wrap all the inner divs
    $(".roundCornersInner").wrap("<div class=\"roundCorners\"></div>");
    $(".roundCornersInner").append("<div class=\"clear\">.</div>");

    //fancybox
    $(".pageGallery a").fancybox();
    $("a.pageSectionImage").fancybox();
    $(".pageSectionGallery a").fancybox();
    $("a.fancybox").fancybox();
        
    //Testimonials
    if($("#Testimonials").length > 0)
    {   
    	$("#Testimonials .testimonial .text").hide();
    	$("#Testimonials .testimonial .fullText").hide();
	$("body").append("<div id=\"QuickView\"><div class=\"text\"></div></div>");
    
    	var TestQuickView = $("#QuickView");
    	var TestQuickViewContent = TestQuickView.find(".text");

    	TestQuickView.hide();
    	TestQuickView.fadeTo(0,0);

   	$("#Testimonials .testimonial").bind("mouseenter", function(){        
        	
		TestQuickViewContent.html($(this).find(".text").html());
	
		//get the height of the quickview and the products inner (because of padding)
	    	var fromTop = $(this).offset().top - TestQuickView.outerHeight();
	    	var fromLeft = $(this).offset().left - (TestQuickView.outerWidth() - $(this).outerWidth())/2;

		TestQuickView.css("top", fromTop + "px");
	       	TestQuickView.css("left", fromLeft + "px");
	
		TestQuickView.show();
		TestQuickView.fadeTo(300,1);
	});

	$("#Testimonials .testimonial").bind("mouseleave", function(){
		TestQuickViewContent.html("");
		TestQuickView.hide();
	        TestQuickView.fadeTo(0,0);
	});
    }

    //Testimonial Upload form
    $("#ctl00_ctl00_ctl00_ctl00_ContentPlaceHolderDefault_MasterContentPlaceHolder_StandardPageColumn1_AddTestimonial_4_btnAddTestimonial").bind("click", function(){
    	//show a screen showing spinner and uploading
	 $("body").append("<div id=\"Uploading\">Please wait, uploading image...</div>");
         var Uploading = $("#Uploading");
         Uploading.fadeTo(0,0);
	 Uploading.fadeTo(500,0.8);
         
         Uploading.css('width', $(document).width() + 'px');
         Uploading.css('height', $(document).height() + 'px');
    });

    //Create worm and reposition on scroll if > ie6    
    if((jQuery.browser.msie && jQuery.browser.version.substr(0,1) > 6) || !jQuery.browser.msie)
    {
	    $("body").prepend("<img id=\"WormBack\" src=\"/data/images/wormback.png\" />")
	    $("#WormBack").css('left', $("#bd").position().left-150 + 'px');
	    $(window).bind("resize scroll", function(){
		$("#WormBack").css('left', $("#bd").position().left-150 + 'px');
	    });
    }
    
    if($(".body1076 .pageSummaries").length > 0)
    {
	var ProductsPageSummaries = $(".body1076 .pageSummaries");
	var Products = $(".body1076 .product");

	ProductsPageSummaries.find("h3").hide();
	ProductsPageSummaries.find(".pageSummary").hide();
	ProductsPageSummaries.find(".more").hide();
    }

    function selectHomeCat(homeCat)
    {
        $("#HomePageRotator li").removeClass("selected");
        $(homeCat).addClass("selected");
	homeImageHolder.fadeTo(200,0, function(){
	        homeImageHolder.html($(homeCat).find(".image").html());
		homeImageHolder.fadeTo(200,1);
	});
    }

    function deselectHomeCat(homeCat)
    {
        $("#HomePageRotator li").removeClass("selected");
        homeImageHolder.fadeTo(100,0, function(){
	        homeImageHolder.html( $($("#HomePageRotator li")[0]).find(".image").html());
		homeImageHolder.fadeTo(200,1);
	});
    }

});

$(window).load(function () 
{

    //PageGallery
    if($(".pageGallery").length > 0)
    {
	var PageGalleryList = $(".pageGallery ul");
	var GalleryOriginalHeight = $(".pageGallery ul").outerHeight();
	var PageGalleryListHider;
	var PageGalleryMoreButton;
	
	if(PageGalleryList.outerHeight() > 66)
	{
		PageGalleryList.wrap("<div class=\"pageGalleryHider\"></div>");
		PageGalleryListHider = PageGalleryList.parent();
		PageGalleryListHider.after("<div class=\"morePageGallery\">more...</div>");
		PageGalleryMoreButton = PageGalleryListHider.parent().find(".morePageGallery");

		PageGalleryMoreButton.bind("click", function(){
			
			if(PageGalleryListHider.outerHeight() == 66)
			{	
				PageGalleryListHider.animate({
					height: GalleryOriginalHeight + "px"},
					200,
					function(){PageGalleryMoreButton.addClass("morePageGalleryOpen");}
				);
			}
			else
			{	
				PageGalleryListHider.animate({
					height: "66px"}, 
					200, 
					function(){PageGalleryMoreButton.removeClass("morePageGalleryOpen");}
				);
			}

		});
	}
    }
});

//Gets an internal link and makes the whole item clickable
function MakeElementClickable(item, hideLink)
{
    var link = item.find("a").attr("href");
    item.bind("click", function(){window.location = link;});
    
    if(hideLink)
    {
        item.find("a").hide();
    }
}

//Gets an internal link and makes the whole item clickable
function MakeElementHoverable(item, mouseOverClass)
{
    item.bind("mouseenter", function(){$(this).addClass(mouseOverClass)});
    item.bind("mouseleave", function(){$(this).removeClass(mouseOverClass)});
}
