$(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();
    };

    //hide item function
    function hideItem() {
        jQuery("#guidelinesWrapper .item").hide();
        jQuery("#guidelinesWrapper #item0").slideDown();
    };

    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: "219px",
          height: "306px",
          left: "-10px",
          top: "-10px"
      }, animateTime);
      },
      function() {
          jQuery(this).closest(".scale").find("img").stop()
      .animate({
          width: "199px",
          height: "286px",
          left: "0",
          top: "0"
      }, animateTime);
      }
    );

    //primary navigation

    //hover
    jQuery("#primaryNavigation li").hover(
        function() {
            jQuery(this).addClass("hover");
        }
        ,
        function() {
            jQuery(this).removeClass("hover");
        }
    );

    //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");
    });

    //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("#sietInformation").attr("role", "contentinfo");



    //buttons

    jQuery(".uiButtons input").hover(
      function() {
          jQuery(this).addClass("hover");
      },
      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("Take a look at this amazing TV ad for Go-Cat.");
    jQuery(".pageAdvert #digg").attr("href", diggLink + "submit?url=" + theUrl + "&title=Take a look at this amazing TV ad for Go-Cat");
    jQuery(".pageAdvert #twitter").attr("href", twitterLink + "home?source=ichc&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 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 + "");

    //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
    //jQuery(".tabs #paperClip").hide();
    //jQuery(".tab").addClass("hide"); //removed to BehaviourScripts.ascx for IE6 - crazy
    jQuery("#foodNutrition").removeClass("hide");
    //jQuery("#tabNavigation li:first-child").addClass("current");
    jQuery(".tabs .faq").prepend("<h3 id='hint'>Select a topic</h3>");


    //main function
    jQuery("#tabNavigation a").click(function() {
        jQuery(".tabs #paperClip").show();
        var theTab = jQuery(this).attr("href");
        jQuery("#tabNavigation li").removeClass("current");
        jQuery(".tab").addClass("hide");
        jQuery(theTab).removeClass("hide");
        jQuery(this).closest("li").addClass("current");
        jQuery("#faqIntro").fadeOut();

        return false;
    });

    //Q & A
    jQuery(".tab dt").prepend("<span><acronymn title='Question'>Q</acronymn></span>");
    jQuery(".tab dd p:first-child").prepend("<span><acronymn title='Answer'>A</acronymn></span>");

    //show and hide questions
    jQuery(".question").hide();
    //jQuery(".subCategoryNavigationContainer li:first-child a").addClass("current");
    jQuery(".subCategoryNavigationContainer a").click(function() {
        jQuery(this).closest(".tab").find("#hint").remove();

        var theSection = jQuery(this).attr("href");
        jQuery(".subCategoryNavigationContainer a").removeClass("current");
        jQuery(".question").hide();
        jQuery(theSection).fadeIn();
        jQuery(this).addClass("current");
        return false;
    });

    //hover
    jQuery("#tabNavigation li").hover(
        function() {
            jQuery(this).addClass("hover");
        }
        ,
        function() {
            jQuery(this).removeClass("hover");
        }
    );

    //focus
    jQuery("#tabNavigation li a").bind("focus", function() {
        jQuery(this).closest("li").addClass("hover");
    });

    jQuery("#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>");

    //End common code       
});