$(function() {

    // Image cycling
    var slideNo = getStartingSlideNo();
    var imageDivSelector = '#indexpageBottom > div';

    $(imageDivSelector).children().each(function() {
        if ($(this).is("a")) {
            var hrefVal = $(this).attr("href");
            $(this).children("img").appendTo(imageDivSelector).click(function() {
                window.location.href = hrefVal;
            }).css("cursor", "pointer").end().end().remove();
        }
    });

    $('#indexpageBottom > div').cycle({timeout: 5000, speed: 4000,
        startingSlide: slideNo, before: function() {

            if (slideNo > 3) slideNo = 0;
            setStartingSlideNo(slideNo);   
            slideNo++;
        }, containerResize: 0 });


    // Sliding Menu
    var slideSpeed = 1;
    var isHovering = false;

    var timeOutInstances = 0;

    $('#headerTop ul li:has(ul)').parent().removeClass("noJS").end().hover(
        function() {
                isHovering = true;
                hover(this);
        },
        function() {
                isHovering = false;
                hover(this);
        });

    function hover(jqueryObj, isTimeoutCallback) {
        
        if (isTimeoutCallback) timeOutInstances--;

        if ($(jqueryObj).children("ul").is(":animated") && timeOutInstances < 1) {
            setTimeout(function() {hover(jqueryObj, true);}, 50);
            timeOutInstances++;
            return;
        }

        if ($(jqueryObj).children("ul").is(":hidden") && isHovering) {
            $(jqueryObj).children("ul").slideDown(slideSpeed);
        }
        else if ($(jqueryObj).children("ul").is(":visible") && !isHovering) {
            $(jqueryObj).children("ul").slideUp(slideSpeed);
        }
    }

    function getStartingSlideNo() {
        var cookieName = "slideNo";

        if (document.cookie.length > 0) {
          c_start = document.cookie.indexOf(cookieName + "=");

          if (c_start != -1) {
            c_start = c_start + cookieName.length + 1;
            c_end = document.cookie.indexOf(";", c_start);

            if (c_end == -1) c_end = document.cookie.length;

            return unescape(document.cookie.substring(c_start,c_end));

          }
        }

        return 0;
    }

    function setStartingSlideNo(slideNo) {
     
        if (slideNo != null) {

            var exdate = new Date();

            exdate.setDate(exdate.getDate()+365);

            document.cookie = "slideNo=" + escape(slideNo) +
                ";expires=" + exdate.toGMTString() + ";path=/";
        }
    }

});






