﻿$(document).ready(function () {



    //This is cahce bug in IE6/IE7 - Have to forcefully show image after a short time because javascript things the image did not load in those modes
    //if (parseInt($.browser.version, 10) == 7) { setTimeout(function () { $('.bgs-current img').show() }, 500) };

    //All IE is strange with caches background. Just always force to show image regardless if loading or cached.
    if ($.browser.msie) { setTimeout(function () { $('.bgs-current img').show(); }, 500); };


    var skin = ReadCookie('Skin');

    if (skin == '') {
        skin = $("body").attr("class");
    }
    else { ChangeSkin(skin); };

    //Moved this to the respective .js...
    //var CompsToShow = 3;
    //var HolesToShow = 5;
    //var HolesToShow = 4;

    if (skin == "Traditional") { CompsToShow = 2; HolesToShow = 4 };

    if ($("#Skins")) { $("#Skins ul li").click(ChangeSkinClick); }

    if ($("#Clock")) { ClockUpdate(); }
    if ($("#Calendar")) { InitCalendar(); }
    //if ($("#News")) { $("#News").bxSlider({ controls: true, displaySlideQty: 2 }); }

    if ($("#News")) { $("#News").bxSlider({ controls: true, displaySlideQty: 1, auto: true, pause: 10000, autoHover: true }); }


    //if ($("#Holes")) { $("#Holes").bxSlider({ controls: true, displaySlideQty: 5 }); }

    //Moved this to the course.js as it needed to be directly accessed to control the slider.
    //if ($("#Holes")) { $("#Holes").bxSlider({ controls: true, displaySlideQty: HolesToShow }); }


    //if ($("#Content")) { $("#Content").bxSlider({ controls: true, displaySlideQty: 1 }); }http://localhost:56938/images/backgrounds/gototee.jpg

    //Moved this to the competitions.js as it needed to be directly accessed to control the slider.
    //if ($("#Competitions")) {    $("#Competitions").bxSlider({ controls: true, displaySlideQty: CompsToShow });     }


    if (skin == "Traditional") {


        $(".Menu ul li").each(function (i) {
            if ($(this).attr("class") == "active") {
                $(this).children("ul").show();

            } else {
                //$(this).children("ul").hide();
            }
        });




        $(".Menu ul li span a").click(function (e) {

            if ($(this).parents('li').children("ul").length >= 1) {

                e.preventDefault();

                if ($(this).parents('li').children("ul").is(':hidden')) {
                    //$(".Menu ul li ul").show();
                    $(this).parents('li').children("ul").show("slow");
                }
                else {
                    $(this).parents('li').children("ul").hide("slow");
                }
            }

            //else {
            //No child menu on this element!;
            //Do nothing
            //}

        });

    }


    /////$(document).bgStretcher({ images: ['images/backgrounds/gototee.jpg'], imageWidth: 1080, imageHeight: 720 });


    //Handles the click of submitting a newsletter- will add a cookie not to show to user again and send an ajax call
    $('.RequestNewsletter').click(function () {
        
        
        var reg = /^([a-zA-Z0-9._-]+@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)?([\.]{1}[a-zA-Z]{2,4}){1,4}$)/;
        var thisEmail = $('#newsletterEmail').val();

        if (reg.test(thisEmail)) {
            var args = '{"email":' + '"' + thisEmail + '"}';
            $.ajax({
                type: "POST",
                cache: false,
                url: "../Ajax.asmx/AddGuestNewsletter",
                data: args,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (Req, txt, err) {
                    alert(err);
                },
                success: function (msg) {
                    //Add a cookie
                    $('.NewsLetterContainer').slideUp('slow');
                }

            });
        }
        else {
            alert("The email address you entered seems to be incorrect.");
        }




    });

    //Handles the login page for the forgot password section to change it dynamically
    $('.forgotPasswordButton').click(function () {
        $('#loginReset').slideDown('slow');
        $('#normalLogin').slideUp('slow');

        $('#loginInfo').html('<strong>Please enter the Login E-Mail address used across the Clubsite and HDID - not the E-Mail address held at this club.</strong>');
        $('#passwordReset').val('yes');
    });


    //Login Box Expand
    $(".Dropdown .Slider").click(function () {
        if ($(this).hasClass('Open')) {
            $(this).removeClass('Open');
            $(this).addClass('Closed');
            //            $(this).closest('div.Dropdown').find('.Content').slideUp();

            $('#DDContent').slideUp();
        }
        else {
            $(this).removeClass('Closed');
            $(this).addClass('Open');
            $('#DDContent').slideDown();
        }

    });



});

var SetupTabs = function () {
    $(".Tab").click(SelectTab);
    //This just clicks the first 'Tab' and displays the relevant data. In some cases not always Div1, sometimes it could start from 2nd or 3rd Div.. 
    $("li.Tab:first").trigger("click");
};

        var SelectTab = function () {
            //Hide all rows
            $(".ResultsRow").addClass("Hidden");
            $(".Tab").removeClass("Selected");
    
            //This is for Competitions
            $(".DivisionNumber" + this.id.substring(3, 4)).removeClass("Hidden");
            $("#Tab" + this.id.substring(3, 4)).addClass("Selected");

            //This is for course statitics
            $(".TabPage").removeClass("Selected");
            $("#TabPage" + this.id.substring(3, 4)).addClass("Selected");

            //alert("#DivisionNumber" + this.id.substring(3, 4));
        };

        var ClockUpdate = function() {


            var today = new Date();
            var h = today.getHours();
            var m = today.getMinutes();
            //var s = today.getSeconds();
            // add a zero in front of numbers<10
            m = checkTime(m);
            //s = checkTime(s);
            //$("#Clock").html(h + ":" + m + ":" + s);
            $("#Clock").html(h + ":" + m);
            //setTimeout('startTime();', 500);

            setTimeout(function() { ClockUpdate(); }, 1000);
        };

        var checkTime = function (i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        };

        var InitCalendar = function () {

            $("#Calendar")
					.datePicker({ inline: true,  dateFormat: 'yy-mm-dd' })
					.bind(
						'dateSelected',
						function (e, selectedDate, $td) {
						    SetBookingURL(selectedDate);
						    $td.addClass("selected");
						}
					);

            SetBookingURL(new Date());
        };

        var SetBookingURL = function (selectedDate) {
            //Get Calendar Selected Date in BRS Format
            var seldate = selectedDate.getFullYear() + '-' + ('0' + (selectedDate.getMonth() + 1)).slice(-2) + '-' + selectedDate.getDate();

            if ($("#lnkVis.Visitors").length > 0) { SetURLLink($("#lnkVis.Visitors"), seldate); }
            //if ($("#lnkMem.Members").length > 0) { SetURLLink($("#lnkMem.Members"), seldate); }
            if ($("#lnkCalendar").length > 0) { SetURLLink($("#lnkCalendar"), seldate); }
            if ($("#lnkBooking.Book").length > 0) { SetURLLink($("#lnkBooking.Book"), seldate); }
        };

        var SetURLLink = function (button, seldate) {
            //alert('href=' + button.attr("href"));

            //alert('set url link');

            //Find where date param starts in href of booking url
            var visd_date = button.attr("href").indexOf('d_date');
            visd_date = visd_date + 7; //First Character Of Date String

            //Find where data param end in href of booking url
            var visd_dateEnd = button.attr("href").indexOf('&', visd_date);
            var currUrldate = button.attr("href").substring(visd_date, visd_dateEnd);

            //Change Booking URL's
            button.attr("href", button.attr("href").replace(currUrldate.toString(), seldate));
        };


        var goToMobileSite = function () {

            if (document.location.href.indexOf("showmobile=no") != -1) { document.cookie = "goToMobileSite=no"; }
            
            var redirectToMobile = ReadCookie('goToMobileSite');
            if (redirectToMobile != 'no') {
                if ((screen.width <= 699) || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
                    document.location = "/mobile/home.aspx";
                }
            }
            
        };
        
        var ChangeSkin = function (s) {
         $("Body").removeClass("Classic");
          $("Body").removeClass("Traditional");
           $("Body").removeClass("Modern");
           $("Body").addClass(s);
           document.cookie = "Skin=" + s.toString();
       };

       var ChangeSkinClick = function () {
           $("Body").removeClass("Classic");
           $("Body").removeClass("Traditional");
           $("Body").removeClass("Modern");
           $("Body").addClass(this.id);
           document.cookie = "Skin=" + this.id;
       };

      

        //Cookie Reader
       var ReadCookie = function (cookieName) {
           var theCookie = "" + document.cookie;
           var ind = theCookie.indexOf(cookieName);
           if (ind == -1 || cookieName == "") return "";
           var ind1 = theCookie.indexOf(';', ind);
           if (ind1 == -1) ind1 = theCookie.length;
           return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
       };


       

