var carouselTimer = 10000;
var carouselPauseTimer = 12000;

$(document).ready(function() {
    jQuery.fn.exists = function() { return this.length > 0; }
    // Top Menu
    $('#topMenu li ul').addClass('hide');
    $('#topMenu > li.sub').hover(
	function() { $(this).addClass('active'); $('ul', this).removeClass('hide'); },
	function() { $(this).removeClass('active'); $('ul', this).addClass('hide'); });

    $(".transform").jqTransform();

    $(".fromDate").datepicker({
        showOn: "button",
        buttonImage: "/assets/Images/ico_calendar.gif",
        buttonImageOnly: true
    });

    $(".toDate").datepicker({
        showOn: "button",
        buttonImage: "/assets/Images/ico_calendar.gif",
        buttonImageOnly: true
    });

    $('ul.haveLast li:last').addClass('last');
    $('ul.zebra li:odd').addClass('odd');
    $('ul.zebra li:even').addClass('even');

    // tooltip grades
    $('span.gradeToolTip').hover(function() {
        var par = $(this).parent().parent();
        var tip = $('.gradeToolTipContent', par);

        $(par).css('position', 'relative');

        var cntWidth = $(tip).width();
        var cntHeight = $(tip).height();


        var newTop = 0 - cntHeight - 35;
        var newLeft = 0 - (cntWidth / 2) + 75;


        $(tip).css({
            'top': newTop + "px",
            'left': newLeft + "px",
            'display': 'block',
            'opacity': 1,
            'z-index': 9999
        });

        $(tip).show();


    }, function() {

        var par = $(this).parent().parent();
        var tip = $('.gradeToolTipContent', par);

        $(tip).fadeOut();

    });

    // Maps
    var mapId = '#googleMap';
    var mapSmallClass = '.googleMapSmall';
    if ($(mapId).exists()) {

        var mapTrigger = '.viewGoggleMap';
        var latitude = parseFloat($('#latitude').val());
        var longitude = parseFloat($('#longitude').val());
        var locName = $('#locationName').val();
        $(mapId).gMap({ markers: [{ latitude: latitude,
            longitude: longitude,
            html: locName,
            popup: true}], zoom: 16
        });

        $(mapSmallClass).gMap({ controls: false,
            scrollwheel: false,
            markers: [{ latitude: latitude,
                longitude: longitude}],
                zoom: 8
            });

            $(mapId).dialog({
                autoOpen: false,
                width: 600,
                height: 450
            });

            $(mapTrigger).click(function() {
                $(mapId).dialog('open');
                return false;
            });
        }
    });

    // header rotator
    $(document).ready(function() {
        var $curr = $(".frameRotator  ul li:first").fadeIn();
        var $totSlide = $('.frameRotator  ul li').size();
        var cycleTimer;

        function setNavButtons() {
            if ($curr.index() > 0 && $curr.index() < ($totSlide - 1)) {
                $(".navRotator a.prev").show();
                $(".navRotator a.next").show();
            }
            else if ($curr.index() == 0) {
                $(".navRotator a.prev").hide();
                $(".navRotator a.next").show();
            }
            else if ($curr.index() == ($totSlide - 1)) {
                $(".navRotator a.prev").show();
                $(".navRotator a.next").hide();
            }
        }

        $('.headerRotator .navRotator')
            .prepend('<a href="#" class="pngFix prev" title="Prev">Prev</a>')
            .append('<a href="#" class="pngFix next" title="Next">Next</a>');

        $(".navRotator a.prev").click(function() {
            if (cycleTimer) clearTimeout(cycleTimer);
            cycleTimer = setTimeout(CycleCarousel, carouselPauseTimer);
            $curr = $curr.prev();
            if ($curr.index() == -1) return false;

            $(".frameRotator  ul li").fadeOut("slow");
            $curr.fadeIn("slow");

            setNavButtons();
        });
        $(".navRotator a.next").click(function() {
            if (cycleTimer) clearTimeout(cycleTimer);
            cycleTimer = setTimeout(CycleCarousel, carouselPauseTimer);
            $curr = $curr.next();
            $(".frameRotator  ul li").fadeOut("slow");
            $curr.fadeIn("slow");
            //.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {$curr.removeClass('active last-active');});
            setNavButtons();

        });

        if ($totSlide > 1) {
            $(".navRotator a.next").show();
        } else {
            $(".navRotator a.next").hide();
        }

        if ($curr.index() == 0) {
            $(".navRotator a.prev").hide();
        }

        function CycleCarousel() {
            var currentIndex = $curr.next().index();
            if (currentIndex == -1) {
                $curr = $(".frameRotator  ul li:first");
                $(".frameRotator  ul li").fadeOut("slow");
                $curr.fadeIn("slow");
                setNavButtons();
            } else if (currentIndex < $totSlide) {
                $curr = $curr.next();
                $(".frameRotator  ul li").fadeOut("slow");
                $curr.fadeIn("slow");
                setNavButtons();
            }
            cycleTimer = setTimeout(CycleCarousel, carouselTimer);
        }

        cycleTimer = setTimeout(CycleCarousel, carouselTimer);
    });  

    // Fade in & Fade Out Map Parks
    $(document).ready(function() {
        $(".animFadeImage").fadeTo("slow", 0.3);
        $(".animFadeImage").hover(function() {
            $(this).fadeTo("slow", 1.0).addClass("imgHover");
        },
                                          function() {
                                              $(this).fadeTo("slow", 0.3).removeClass("imgHover");
                                          });
        $("ul.nswParksMap").mouseout(function() { $(".animFadeImage").stop().fadeTo("slow", 0.3); });
    });


    $(document).ready(function() {
        $('input[type="text"].searchTextBox').addClass("idleField");
        $('input[type="text"].searchTextBox').focus(function() {
            $(this).removeClass("idleField").addClass("focusField");
            if (this.value == this.defaultValue) {
                this.value = '';
            }
            if (this.value != this.defaultValue) {
                this.select();
            }
        });
        $('input[type="text"].searchTextBox').blur(function() {
            $(this).removeClass("focusField").addClass("idleField");
            if (this.value == '') {
                this.value = (this.defaultValue ? this.defaultValue : this.defaultValue);
            }
        });
    });

    function clearContent() {
        $(".parkWrapper > div > select > option:not(:first-child)").remove();
    }
    function writeNewValue(value, text) {
        $(".parkWrapper > div > select").append('<option value="' + value + '">' + text + '</option>');

    }


    // This function for add max length to Multiline Textbox ===================
    // add this to you textbox properties
    // [onKeyPress="return AddMaxLength(this,500);" onChange="return AddMaxLength(this,500);" onKeyDown="return AddMaxLength(this,500);"]
    function AddMaxLength(text, long) {
        var maxlength = new Number(long);
        if (text.value.length > maxlength) {
            text.value = text.value.substring(0, maxlength);
            return false;
        }
        else
            return true;
    }






    var tooltipTimer;
    var currentTip;

    // MAP INTRO
    $(document).ready(function() {
        var i = 1;
        $('.mapHomeIntro area').each(function(index) {
            $(this).attr('title', '');
            var alt = $(this).attr('alt');
            $(this).attr('rel', alt);
            $(this).attr('alt', '');

            $(this).attr('class', 'mapReg' + i);
            $(this).addClass('toolMap');
            i++;
        });

        $('.toolMap').mouseover(function(e) {
            var tip = $(this).attr('rel');
            $(this).after('<div class="ttipWrapper"><div class="ttipHeader pngFix"></div><div class="ttipBody pngFix">' + tip + '</div><div class="ttipFooter pngFix"></div></div>');
            $('.ttipWrapper').css('opacity', 0.999);

            if (currentTip) $(currentTip).remove();
            currentTip = $(this).parent().find('div.ttipWrapper');

            if (tooltipTimer) clearTimeout(tooltipTimer);

        });

        $('.toolMap').mousemove(function(e) {

            if (tooltipTimer) clearTimeout(tooltipTimer);

            var left = $(window).width();
            var wLeft = (left / 2) - 202;
            var top = $(window).height();

            var tipTop = e.pageY - 394 - $('.ttipWrapper').height();

            $('.ttipWrapper').css('top', tipTop + "px");
            $('.ttipWrapper').css('left', e.pageX - wLeft);
            $('.ttipWrapper').css('display', 'block');
            $('.ttipWrapper').css('z-index', '99999');
        }).mouseout(function() {
            //$(this).attr('alt', $('.ttipBody').html());

            tooltipTimer = setTimeout(function() { $(currentTip).remove(); }, 100);

        });
    });


