// JavaScript Document

$(function() {

    /* rounding of corner on LP section 1 
    -----------------------------------------*/
    //if (!$.browser.msie() ) {
    /*if ($.browser.firefox() && ($.browser.version.number() > 2)) {
    $("#lp-section-1 .list-elements div").corners("8px bottom");
    $("#lp-section-1 .list-elements a span:last-child").corners("8px bottom");
    $("#content #lp-section-2 a").corners("5px");
    }*/

    /* landing page photo events extend mouseover 
    --------------------------------------------*/
    $("#lp-section-1 .list-elements div").mouseover(function() {
        $(this).find("a").addClass("hover");
    }).mouseout(function() {
        $(this).find("a").removeClass("hover");
    });

    /* calendar hide/show by events
    -----------------------------------------*/
    $("#c-hide-show input").click(function() {

        var currentlySelected = $(this).attr("id");
        var checked = false;

        // ensure at least one venue is selected.	
        $("input:checkbox").each(function() { if ($(this).is(":checked")) { checked = true; } });
        if (!checked) {
            alert("At least one venue must be selected");
            $(this).attr("checked", "true");
        }

        $("input:checkbox").each(function() {
            currentlySelected = $(this).attr("id");
            if ($(this).attr("checked")) {
                $("#calendar tbody td").each(function() {
                    if ($(this).hasClass(currentlySelected)) {
                        $(this).children("a").removeClass("fade");
                    }
                });
            } else {
                $("#calendar tbody td").each(function() {
                    if ($(this).hasClass(currentlySelected)) {
                        $(this).children("a").addClass("fade");
                    }
                });
            }
        });


        /*		$("input:checkbox").each( function() {
        if ($(this).attr("checked")) {
        currentlySelected = $(this);
        $().each( function() { 
        if ($(this).hasClass(currentlySelected)) { 
						
					};
        })
        }
        });*/

    });

    /* landing page events hide/show by venue 
    -----------------------------------------*/

    // function to handle hide/show of venues
    function fnHideShowVenue(me, pageLoad) {

        var checked = false;

        // ensure at least one venue is selected.	
        $("input:checkbox").each(function() { if ($(this).is(":checked")) { checked = true; } });
        if (!checked) {
            alert("At least one venue must be selected");
            $(me).attr("checked", "true");
        }

        // fix element heights to avoid page height changing
        var strHeightTag = ($.browser.msie && Number($.browser.version) <= 6) ? "height" : "min-height";
        //$("#lp-section-1").css(strHeightTag, $("#lp-section-1").height());
        //$("#lp-section-2").css(strHeightTag, $("#lp-section-2").height());
        //$("#lp-section-3").css(strHeightTag, $("#lp-section-3").height());

        // get racetracks
        var racetrackOn = new Array();
        var racetrackOff = new Array();
        var racetrackAll = new Array();
        racetrackOn.push("all-sites")
        $("input:checkbox").each(function() {
            racetrackAll.push($(this).attr("id"));
            $(this).is(":checked") ? (racetrackOn.push($(this).attr("id")))
                : (racetrackOff.push($(this).attr("id")));
            racetrackOn.push("the-races");
        });

        // hide show panels/panel elements
        $(".panel").each(function(i) {
            var panel = String("#" + $(this).attr("id")); // panel id
            var count = 0;

            // show events
            $.each(racetrackOn, function() {
                $(panel).slideDown("slow");
                $("." + this).fadeIn("slow");
                $("." + this).addClass("visible");
            });

            // hide events
            $.each(racetrackOff, function() {
                pageLoad ? ($("." + this).hide()) : ($("." + this).fadeOut("slow"));
                $("." + this).removeClass("visible");
            });

            // determine if all events for panel are hidden
            $.each(racetrackAll, function() {
                count = $(panel).find(".visible").length;
            });

            // if all events for panel are hidden then close
            if (count == 0) {
                $(panel).addClass("closed");
                pageLoad ? ($(panel).hide()) : ($(panel).slideUp("slow"));
            }

        });
    }

    //if ($("#lp-section-5").length > 0) {
    //    fnHideShowVenue(null, true);
    //}

    // onclick listener to handle hide/show of venues
    $("#lp-section-5 input").click(function() {
        fnHideShowVenue(this, false);
    });

    /* alternate row shading punter guide table
    ---------------------------------------------*/
    $(".template-2T table tr:even").addClass("even");
    $(".template-T tr:even").addClass("even");
    if ($.browser.msie) {
        $(".template-2T td:last-child").addClass("last");
        $(".template-T td:last-child").addClass("last");
        if (Number($.browser.version) <= 6) {
            $(".template-2T table td:first-child").addClass("first");
        }
    }

    /* prettyPhoto - lightBox clone
    ---------------------------------------------*/
    $("a[rel^='prettyPhoto']").prettyPhoto();


    /* Slideshow
    ---------------------------------------------*/
    var play = true;
    var pause = false;
    if ($("#home").length > 0) { $("div.nav-controls").show(); }

    Slideshow.setElement('slideshow');
    Slideshow.showTime = 4;
    Slideshow.fadeTime = 1;
    Slideshow.start();

    $('#slideshow .prev').click(function() {
        //console.log("previous");
        Slideshow.prev();
    });
    $('#slideshow .next').click(function() {
        //console.log("next");
        Slideshow.next();
    });

    // if mouse over image stop slideshow
    $('#slideshow li img.hero').mouseover(function() {
        if (play) {
            //console.log("pause after mouseover image");
            Slideshow.stop();
            play = false;
        }
    });

    // if mouse out image start slideshow
    $('#slideshow li img.hero').mouseout(function() {
        if (!play && !pause) {
            //console.log("play after mouseout on image");
            Slideshow.start();
            play = true;
        }

    });

    // toggle pause button
    $('#slideshow .pause').click(function() {
        play = play ? false : true;
        if (play) {
            //console.log('play after pause');
            Slideshow.start();
            $(this).children("img").attr("src", $(this).children("img").attr("src").replace("play", "pause"));
            pause = false;
        } else {
            //console.log('pause from play');
            Slideshow.stop();
            $(this).children("img").attr("src", $(this).children("img").attr("src").replace("pause", "play"));
            pause = true;
        }
        return false;
    });


});

