$(document).ready(function () {
    //Start common code
    //jQuery("body").addClass("hasJavascript");


    //Scale kitty!
    //IE doesn't do animation well.  If IE then set animation time to 0
    //all other browsers then use a smooth animation effect
    var animateTime = 100;


    //position item function
    function positionItem(theLink) {
        jQuery("#guidelinesWrapper li.current").removeClass("current");
        jQuery("#guidelinesWrapper .item").hide();
        var theItem = jQuery(theLink).attr("href");
        jQuery("#guidelinesWrapper " + theItem + "").slideDown();
        jQuery("#guidelinesWrapper #item0").hide();
        jQuery("#primaryNavigation").hide();
    };

    //hide item function
    function hideItem() {
        jQuery("#guidelinesWrapper .item").hide();
        jQuery("#guidelinesWrapper #item0").slideDown();
        jQuery("#primaryNavigation").show();
    };

    jQuery("#guidelinesWrapper .scale a").click(function () {
        positionItem(this);
        jQuery(this).closest("li").addClass("current");
        return false;
    });

    jQuery("#guidelinesWrapper .backToSummary").click(function () {
        hideItem();
        return false;
    });

    jQuery("#guidelinesWrapper .feedingCone").click(function () {
        console.log("unbounded");
    });

    //nutrition scaling link animation
    jQuery("#ItemNavigationNutrition a").hover(
      function () {
          jQuery(this).closest(".scale").find("img").stop()
      .animate({
          width: "194px",
          height: "213px",
          left: "-10px",
          top: "-10px"
      }, animateTime);
      },
      function () {
          jQuery(this).closest(".scale").find("img").stop()
      .animate({
          width: "174px",
          height: "193px",
          left: "0",
          top: "0"
      }, animateTime);
      }
    );

    //range scaling link animation
    jQuery("#ItemNavigationRange a").hover(
      function () {
          jQuery(this).closest(".scale").find("img").stop()
      .animate({
          width: "195px",
          height: "250px",
          left: "-10px",
          top: "-10px"
      }, animateTime);
      },
      function () {
          jQuery(this).closest(".scale").find("img").stop()
      .animate({
          width: "175px",
          height: "230px",
          left: "0",
          top: "0"
      }, animateTime);
      }
    );

    //primary navigation

    //hover
    jQuery("#primaryNavigation li").hover(
        function () {
            jQuery(this).addClass("hover");
        }
        ,
        function () {
            jQuery(this).removeClass("hover");
        }
    );

    //hack for feeding guidelines
    $("#primaryNavigation .parent").hover(
        function () {
            $("#guidelinesWrapper .item").css("z-index", "0");
        },
        function () {
            $("#guidelinesWrapper .item").css("z-index", "100");
        }
    );


    //focus
    jQuery("#primaryNavigation li a").bind("focus", function () {
        jQuery(this).closest("li").addClass("hover");
    });

    jQuery("#primaryNavigation li a").bind("blur", function () {
        jQuery(this).closest("li").removeClass("hover");
    });

    //adding current classes to parents in primary navigation
    jQuery("#pageHome #navigationHome").closest(".parent").addClass("current");

    jQuery(".sectionRange #navigationRange").closest(".parent").addClass("current");
    jQuery(".sectionNutrition #navigationAdvice").closest(".parent").addClass("current");
    jQuery(".sectionPlay #navigationPlay").closest(".parent").addClass("current");
    jQuery(".sectionBoboAwards #navigationPlay").closest(".parent").addClass("current");
    jQuery(".sectionStayInTouch #navigationStayInTouch").closest(".parent").addClass("current");


    //wai ria roles
    //This is done with Javascript to avoid invalid XHTML - Stuart Johnston 11/12/09
    //Support for Firevox, Window Eyes Screen Reader, JAWS Screen Reader  - Peace and love :)

    jQuery("#primaryContent").attr("role", "main");
    jQuery("#primaryNavigation ul").attr("role", "navigation");
    jQuery("#siteInformation").attr("role", "contentinfo");



    //buttons

    jQuery(".uiButtons input").hover(
      function () {
          jQuery(this).addClass("hover");
          hideNutritionItem();
      },
      function () {
          jQuery(this).removeClass("hover");
      }
    );

    //nutrition page behaviour

    //initialize the page
    jQuery("#nutrition .item").hide();
    jQuery("#nutrition #item0").show();

    //position item function
    function positionNutritionItem(theLink) {

        jQuery("#nutrition .item").hide();
        var theItem = jQuery(theLink).attr("href");
        jQuery("#nutrition " + theItem + "").fadeIn();
        jQuery("#nutrition #item0").hide();
        jQuery(theLink).addClass("current");

    };

    //hide item function
    function hideNutritionItem() {
        jQuery("#nutrition .item").hide();
        jQuery("#nutrition #item0").fadeIn();
    };

    jQuery("#nutrition a").click(function () {
        jQuery("#nutrition ul *").removeClass("current");
        positionNutritionItem(this);
        return false;
    });

    jQuery("#nutrition .backToSummary").click(function () {
        hideNutritionItem();
        return false;
    });

    // digg twitter facebook
    var diggLink = jQuery("#digg").attr("href");
    var twitterLink = jQuery("#twitter").attr("href");
    var facebookLink = jQuery("#facebook").attr("href");

    var theUrl = document.location.href;
    var pageTitle = document.title;
    var encodedUrl = encodeURIComponent(theUrl);
    var encodedPageTitle = encodeURIComponent(pageTitle);
    //cat name
    var catName = pageTitle;
    catName = catName.replace(/BOBO AWARDS - /g, "");

    var encodedWebsite = encodeURIComponent('http://www.go-cat.co.uk/');

    //for tv advert page
    var encodedTitleTvAdvert = encodeURIComponent("New video - Go-Cat Kittens live life to the full!");
    jQuery(".pageAdvert #digg").attr("href", diggLink + "submit?url=" + theUrl + "&title=Take a look at this brilliant ad for Go-Cat");
    jQuery(".pageAdvert #twitter").attr("href", twitterLink + "intent/tweet?status=Take a look at this amazing TV ad for Go-Cat.  Watch it at " + theUrl + "");
    jQuery(".pageAdvert #facebook").attr("href", facebookLink + "sharer.php?u=" + encodedUrl + "&t=" + encodedTitleTvAdvert + "");

    //for Lifestyle guide page
    var encodedTitleLifestyleguide = encodeURIComponent("The official home of Purina Go-Cat - the UK's number 1 dry cat food. Now with compact kibbles. Get you FREE personalised lifestyle guide.");
    jQuery(".pageLifestyleGuide #digg").attr("href", diggLink + "submit?url=" + theUrl + "&title=Take a look at this FREE Lifestyle guide from Go-Cat");
    jQuery(".pageLifestyleGuide #twitter").attr("href", twitterLink + "home?source=ichc&status=You can create a free personalised lifestyle guide for your cat. Get yours at " + theUrl + "");
    jQuery(".pageLifestyleGuide #facebook").attr("href", facebookLink + "sharer.php?u=" + encodedUrl + "&t=" + encodedTitleLifestyleguide + "");

    //for view entry page
    var encodedSummary = encodeURIComponent("Go-Cat is looking for the UK's most adventurous cat! Have a peep at www.go-cat.co.uk to see all the cats in action.  The top prize is a safari to see some really big cats!");
    var encodedPageTitle = encodeURIComponent(pageTitle);
    jQuery(".pageViewEntry  #digg").attr("href", diggLink + "submit?url=" + encodedUrl + "&title=" + catName + " has been entered into the Go-Cat BOBO AWARDS!");
    jQuery(".pageViewEntry  #twitter").attr("href", twitterLink + "home?source=ichc&status=" + catName + " to win the Go-Cat BOBO AWARDS! %23WIN a safari " + encodedUrl + " %23competition");
    jQuery(".pageViewEntry  #facebook").attr("href", facebookLink + "sharer.php?u=" + encodedUrl + "&t=" + catName + " has been entered into the Go-Cat BOBO AWARDS! " + encodedSummary + "");

    //for ecards
    var encodedTitleEcards = encodeURIComponent("Send personalised greetings with new e-cards from Go-Cat. The e-cards contain unique animations of Bobo and his crew that your friends and family will love.");
    jQuery(".chooseAnEcard #twitter").attr("href", twitterLink + "home?source=ichc&status=Send personalised greetings with new e-cards from Go-Cat. " + theUrl + "");
    jQuery(".chooseAnEcard #facebook").attr("href", facebookLink + "sharer.php?u=" + encodedUrl + "&t=" + encodedTitleEcards + "");

    //for indoor cat page
    var encodedIndoor = encodeURIComponent("Bobo's back in another TV ad for Go-Cat.  Watch it now!");
    jQuery(".pageIndoor #digg").attr("href", diggLink + "submit?url=" + theUrl + "&title=Bobo's back!");
    jQuery(".pageIndoor #twitter").attr("href", twitterLink + "home?source=ichc&status=Bobo's back in another TV ad for Go-Cat.  Watch it at " + theUrl + "");
    jQuery(".pageIndoor #facebook").attr("href", facebookLink + "sharer.php?u=" + encodedUrl + "&t=" + encodedIndoor + "");

    //email with cat name
    jQuery(".pageViewEntry #email").attr("href", "mailto:?subject=" + catName + " has been entered into the Go-Cat BOBO AWARDS!&body=Go-Cat%20has%20launched%20a%20nationwide%20search%20for%20the%20UK%27s%20most%20adventurous%20cat%20and%20" + catName + "%20is%20my%20favourite%20to%20win%21%20Take%20a%20look%20at%20" + encodedUrl + "%20to%20see%20" + catName + "%20and%20all%20the%20other%20cats%20in%20action.%0A%0AThere%20are%20some%20great%20prizes%20to%20be%20won%20including%20an%20amazing%20safari%20to%20Africa.%20%20The%20closing%20date%20is%207th%20May%202010%2C%20so%20there%27s%20still%20time%20to%20enter%20if%20you%20want%20to%21%0A%0ATake%20a%20look%20at%20" + encodedWebsite + "%20for%20more%20info%20on%20the%20Go-Cat%20BOBO%20AWARDS.%0A");

    //tabs in faq
    //initial set up

    //load content
    jQuery('.tabs #foodNutrition').load('Food-and-nutrition/Default.aspx .FAQPrimaryContent');
    jQuery('.tabs #catHealth').load('Cat-health/Default.aspx .FAQPrimaryContent');
    jQuery('.tabs #generalCare').load('General-care/Default.aspx .FAQPrimaryContent');

    //set tab navigation hrefs
    jQuery(".pageFAQ #navFoodNutrition").attr("href", "#foodNutrition");
    jQuery(".pageFAQ #navCatHealth").attr("href", "#catHealth");
    jQuery(".pageFAQ #navGeneralCare").attr("href", "#generalCare");

    //main function
    jQuery(".tabs #tabNavigation a").click(function () {
        jQuery(".tabs #paperClip").show();
        var theTab = jQuery(this).attr("href");
        jQuery(".tabs #tabNavigation li").removeClass("current");
        jQuery(".tabs .tab").addClass("hide");
        jQuery(theTab).removeClass("hide");
        jQuery(this).closest("li").addClass("current");
        jQuery(".tabs #faqIntro").fadeOut();
        jQuery(".tabs .question").hide();
        return false;
    });

    //show and hide questions

    //jQuery(".subCategoryNavigationContainer li:first-child a").addClass("current");
    jQuery(".tabs .subCategoryNavigationContainer a").live("click", function () {
        jQuery(this).closest(".tab").find("#hint").hide();

        //ie6 hack-nasty
        var theSection = jQuery(this.href.split('/FAQ/')[1]);

        jQuery(".tabs .subCategoryNavigationContainer a").removeClass("current");
        jQuery(".tabs .question").hide();
        jQuery(theSection).fadeIn();
        jQuery(this).addClass("current");
        return false;
    });

    //hover
    jQuery(".tabs #tabNavigation li").hover(
        function () {
            jQuery(this).addClass("hover");
        }
        ,
        function () {
            jQuery(this).removeClass("hover");
        }
    );

    //toptip
    function topTip(obj) {
        jQuery(".tabs #topTip a").attr("href", "#generalCare07");
        jQuery(".tabs .question").hide();
        jQuery(".tabs #paperClip").show();
        jQuery(".tabs #tabNavigation li").removeClass("current");
        jQuery(".tabs .tab").addClass("hide");
        jQuery(".tabs #generalCare").removeClass("hide");
        jQuery(".tabs #navGeneralCare").closest("li").addClass("current");
        jQuery(".tabs #faqIntro").fadeOut();
        jQuery(".tabs #generalCare").closest(".tab").find("#hint").hide();
        jQuery(".tabs #generalCare07").fadeIn();
        jQuery(".tabs .subCategoryNavigation .tip").addClass("current");
    };

    jQuery(".tabs #topTip a").click(function () {
        topTip(this);
        return false;
    });

    //focus
    jQuery(".tabs #tabNavigation li a").bind("focus", function () {
        jQuery(this).closest("li").addClass("hover");
    });

    jQuery(".tabs #tabNavigation li a").bind("blur", function () {
        jQuery(this).closest("li").removeClass("hover");
    });



    //Zebra tables
    jQuery(".genericForm table.zebra").each(function () {
        jQuery(this).find("tr:even").addClass("even");
        jQuery(this).find("tr:first-child").addClass("headers");
    });

    //TV advert suggestions
    //hide all messages
    jQuery(".message").hide();

    //give all messages an id
    var messageCount = 1;
    jQuery(".message").each(function () {
        jQuery(this).attr("id", "message" + messageCount + "");
        messageCount++;
    });

    //show the first one
    jQuery("#message1").show();
    jQuery("#previous").fadeTo("fast", 0.5);

    //set the current message count to 1
    var currentMessageCount = 1;

    //get the total number of messages
    var totalNumberOfMessages = jQuery(".message").length;

    //Going up
    function messageCounterIncrease(theLink) {
        if (currentMessageCount < totalNumberOfMessages) {

            jQuery("#previous").fadeTo("fast", 1.0);
            jQuery(".message").hide();
            currentMessageCount++;
            if (currentMessageCount == totalNumberOfMessages) {
                fadeLink(theLink);
            };
            jQuery("#message" + currentMessageCount + "").fadeIn();

        }
        else {
            fadeLink(theLink);
        }
    }

    //Going down
    function messageCounterDecrease(theLink) {
        if (currentMessageCount > 1) {
            jQuery("#next").fadeTo("fast", 1.0);
            jQuery(".message").hide();
            currentMessageCount--;
            if (currentMessageCount == 1) {
                fadeLink(theLink);
            };
            jQuery("#message" + currentMessageCount + "").fadeIn();
        }
        else {
            fadeLink(theLink);
        }
    }

    //fade the link
    function fadeLink(theLink) {
        jQuery(theLink).fadeTo("fast", 0.5);
    }

    // next function
    jQuery("#next").click(function () {
        //increase message count
        messageCounterIncrease(this);
        return false;
    });

    // previous function
    jQuery("#previous").click(function () {
        //decrease the message count
        messageCounterDecrease(this);
        return false;
    });

    //featured entires panel image vertical align
    jQuery(".featureCategory").each(function () {
        var heightCategorgyLink = jQuery(this).find(" a").height();
        var heightOfImage = jQuery(this).find("a img").height();

        if (heightOfImage < heightCategorgyLink) {
            var marginTop = (heightCategorgyLink - heightOfImage) / 2
            jQuery(this).find("a img").css("margin-top", marginTop);
        }
        else {
            //make a cup of tea and relax
        }

    });

    //listings background image fix
    jQuery(".entries table td.first img").wrap("<div></div>");
    jQuery(".entries table td.second").wrapInner("<div></div>");

    //Satifaction filter
    $("#satisfaction .option").addClass("hide");

    $("#satisfaction #filter input").change(function () {
        var content = $(this).attr("rel");
        $("#satisfaction .option").addClass("hide noPrint");
        $("#" + content).removeClass("hide noPrint");
    });

    //End common code       
});
