
$(document).ready( function () {
    
    //the new golf popup 
    
    //openGolfPop
    //golf_pop_content
    
    var thisHole = 1;
    var totalPos = ''; 
    var t_int = '';
    var cHole = ''; 
    
    $("#openGolfPop").click( function () {
        
        $.colorbox({
            inline:true, 
            href:"#golf_pop_content", 
            open: true, 
            title: false,
            onComplete: function() {
                $("#cboxTitle").css("display", "none");
            }, 
            onLoad: function() { 
                $("#golf_pop_content").css("display", "block");
            }, 
            onCleanup: function() {
                $("#cboxTitle").css("display", "block");
                $("#golf_pop_content").css("display", "none");
                clearInterval(t_int);
            }
        } );         
        
        totalPos = $("#hole_"+thisHole+" ul").children().length-1;
        
        if ( totalPos == 0 ) {
            $(".img-nav").css("display", "none");   
        }
        else {
            $(".img-nav").css("display", "block"); 
        }
        
        t_int = setInterval( delegate , 4500   ); 
        
    }); 
    
    //takes care of scope problems in IE 
    function delegate( ) {
        
        move(    "#hole_"+thisHole+" ul" ); 
    }
    
    
    //animation for the holes 
    var perPos = "480";
    var cPosition = parseInt(0);
    
 
    function move ( element   ) {
        
        if (   cPosition  <=  parseInt( totalPos ) ) {
            
            // var thisLi = $(element).children("li").get(  cPosition  );
           
            var thisLi = $(element).children(':first');  
            
            // alert( thisLi[0].children('img').attr('src') );
       
            // $(thisLi).fadeOut(function() {
            var t =  $(thisLi).clone(true);
            
            $(thisLi).detach();
 
            $(element).append( t );
            $(t).css("display" , "block");  
            
            var dotPos = ''; 
            
            if ( cPosition +1 > totalPos ) {
             
                dotPos = 0;
            }
            else {
                dotPos = cPosition+1;
            }
          
                
            // small dots 
            $(element).next().children("a").each( function( index ) {
                    
                if (  index == parseInt(dotPos)   ) {
                    
                    $(this).addClass("active");
                }
                else {
                      
                    $(this).removeClass("active");
                }
            });
               
              
            cPosition++; 
        }
        else {
            
            cPosition = parseInt( 0 ); 
        }
    }

    
    $("#pop_next").click( function() {
        
        if ( parseInt( thisHole  ) <=  17 ) {
        
            $("#hole_"+(thisHole)).css("display", "none");
            $("#hole_"+(thisHole+1)).css("display", "block");
        
            thisHole++;
             
            clearInterval( t_int ); 
             
            cPosition = parseInt( 0 );
            
            totalPos = $("#hole_"+thisHole+" ul").children().length-1;
        
            $("#hole_"+thisHole+" ul").next().children("a").not(":first-child").removeClass("active");
            $("#hole_"+thisHole+" ul").next().children("a").first().addClass("active");
            
            t_int = setInterval(  delegate, 4500  ); 
        
       
            $.colorbox({
                inline:true, 
                href:"#golf_pop_content", 
                open: true, 
                title: false,
                onComplete:  function() {
                    $("#cboxTitle").css("display", "none");
                },
                onLoad: function() { 
                    $("#golf_pop_content").css("display", "block");
                }, 
                onCleanup: function() {
                    $("#cboxTitle").css("display", "block");
                    $("#golf_pop_content").css("display", "none");
                }
             
            } );  
            
            if ( totalPos == 0 ) {
                $(".img-nav").css("display", "none");   
            }
            else {
                $(".img-nav").css("display", "block");  
            }
    
        }
    });
    
    $("#pop_prev").click( function() {
        
        if ( thisHole > 1 ) {
            $("#hole_"+(thisHole)).css("display", "none");
            $("#hole_"+(thisHole-1)).css("display", "block");
        }
        
        thisHole--;
             
        clearInterval( t_int ); 
             
        cPosition = parseInt( 0 );
            
        totalPos = $("#hole_"+thisHole+" ul").children().length-1;
            
        //$("#hole_"+thisHole+" ul").next().children("a").first().addClass("active");
        
        $("#hole_"+thisHole+" ul").next().children("a").not(":first-child").removeClass("active");
        $("#hole_"+thisHole+" ul").next().children("a").first().addClass("active");
            
        t_int = setInterval(  delegate, 4500 ); 
        
        if ( parseInt( thisHole  ) >=  1  ) {
            $.colorbox({
                inline:true, 
                href:"#golf_pop_content", 
                open: true, 
                title: false,
                onComplete: function() {
                    $("#cboxTitle").css("display", "none");
                },
                onLoad: function() { 
                    $("#golf_pop_content").css("display", "block");
                }, 
                onCleanup: function() {
                    $("#cboxTitle").css("display", "block");
                    $("#golf_pop_content").css("display", "none");
                }
             
            } );  
    
            if ( totalPos == 0 ) {
                $(".img-nav").css("display", "none");   
            }
            else {
                $(".img-nav").css("display", "block");   
            }
    
        }
    });
    
    
    // attach colorbox
    
    $("a[rel='colorbox_gallery']").colorbox();
     
    $("#triggerPhotos").click( function () {
        
        $("a[rel='colorbox_gallery']").colorbox({
            open : true
        });
         
    });

    var Total = parseInt( $("ul.topImg").children().length );

    var aActive = new Array();

    aActive.push( 'topImg1' );

    var aPassive = new Array();

    $.each( $("ul.topImg").children() , function ( i, n ) {

        if ( $(this).attr("id")  != aActive[0] ) {

            aPassive.push( $(this).attr("id")  );
        }
    });


    var aLoaded = new Array();

    function loaded ( img ) {

        aLoaded.push( img );

    }

    function unblock() {

        $.each( aLoaded , function( i , n ) {

            $("#"+n).css("display", "block");
        });
    }


    var z = 1;

    for ( var i = 0 ; i < aPassive.length ; i++ ) {

        var image = new Image();

        //image.onload = loaded( aPassive[i] )  ;
        image.onLoad = loaded( aPassive[i] );

        image.src = $("#"+aPassive[i]).children("img").attr("src");

        if ( z == parseInt( Total -1 ) ) {

            play_animation();
        }
        
        z++;
    }

   
  
    // play_animation();

    var iterate = 1;


    function play_animation() {


        var cToElement = aPassive.reverse().pop();

        var cFromElement = aActive[0].toString();

        aActive.length = 0;

        aActive[0] = cToElement;

        aPassive.reverse();

        aPassive.push( cFromElement );

        $("#"+cFromElement).delay(5000).animate({

            opacity: 0
                    
        }, {
            step: function( now, fx ) {
                if ( now == 1 )  {
                    unblock();
                }
            } ,
            duration: 2000,
            easing: 'linear',
            complete : function(){

                $(this).css({
                    zIndex: 1,
                    opacity: 1
                });

                $("#"+cToElement).css("zIndex", parseInt( Total ) );

                     
                for ( var z in aPassive ) {

                    if ( $("#"+aPassive[z]).attr("id") != $(this).attr("id") ) {

                        var cIndex = $("#"+aPassive[z]).css("zIndex");

                        cIndex = parseInt( cIndex ) +1 ;

                        $("#"+aPassive[z]).css("zIndex", cIndex  ) ;

                    }
                }

                play_animation();
            }
        });

    }


    /// mail form holder

    $("#mailform_holder a.close").click( function() {
        $(this).parent().css("display", "none");
    });

    $("#showForm").click(function() {

        $("#mailform_holder").css("display", "block");

        if ( $.data( document.body , "contact-done") == 1 ) {

            $("#mailform_holder").delay(3000).fadeOut("slow");
        }

    });

    $("#arrive").datepicker({
        showOn: "button",
        buttonImage: "/media/images/icons/calendar.gif",
        buttonImageOnly: true,
        minDate: new Date(),
        maxDate: "+2 weeks"
    
    });

    //little booking form submit
    /*
    $("#book-btn").click( function() {

        var aErrors = new Array();

        if ( $("#arrive").val() == "" ) {

            aErrors.push( "Please select arrival date" );
        }

        if ( $("#type").val() == "" && $("#typeselect").val() == "phone"  ) {

            aErrors.push( "Please add a phone number ");
        }

        if ( $("#type").val() == "" && $("#typeselect").val() == "e-mail"  ) {

            aErrors.push( "Please add an e-mail address");
        }

        if ( $("#type").val() != "" && $("#typeselect").val() == "e-mail" && !isValidEmailAddress($("#type").val() )  ) {

            aErrors.push( "Please add valid e-mail address");
        }


        if ( aErrors.length > 0 ) {

            $("#contact-error").empty();

            $.each( aErrors, function ( i , n ) {

                $("#contact-error").append( n + "<br />");
            });

            $("#contact-error").css("display", "block");

        }
        /*
        else {

            ntptEventTag('ev=booking_form');

            $.get( "/patterns/contact", {
                data:  decodeURIComponent( $("#room-chain").serialize() )
            }, function( databack ) {

                //alert ( databack.arrive );
                $.data( document.body, "contact-done", 1 );
                
                $("#mailform_holder").empty().css("height", "300px").append( "Thank you for contacting us! We will contact you back during the hours of 6am and 9pm PST").delay(6000).fadeOut("slow");
               

            }, 'json' );

        }

    });
    */


    // subscribe email form on the index page

    $("#subscribeEmail").focus( function() {
        $(this).attr("value", "")
    } );
    
    
    $("#subscribeEmailCoba").focus( function() {
        $(this).attr("value", "")
    } );

    $("#subscribeEmailBtn").click( function() {

        if ( $("#subscribeEmail").val() == "" || !isValidEmailAddress( $("#subscribeEmail").val() ) ) {

            $("#subscribe-errors").append("Please enter valid email address").css("display", "block");
        }
        else {

            $.post( "/patterns/contact/email" , {
                data : $("#subscribeEmail").val()
            } , function ( data ) {

                if( data.status == 1 ) {

                    window.location = "thank-you-subscriber";
                }

            }, 'json' );
        }


    });
    
    
    $("#subscribeEmailBtnCoba").click( function() {

        if ( $("#subscribeEmailCoba").val() == "" || !isValidEmailAddress( $("#subscribeEmailCoba").val() ) ) {

            $("#subscribe-errors").append("Please enter valid email address").css("display", "block");
        }
        else {

            $.post( "/patterns/contact/coba" , {
                data : $("#subscribeEmailCoba").val()
            } , function ( data ) {

                if( data.status == 1 ) {
					
                    $("#subscribe-errors").empty();
                    $("#subscribe-errors").append("Thank you for subscribing!").css("display", "block");
                    $("#subscribeEmailCoba").attr("disabled", "disabled");
                    $("#subscribeEmailBtnCoba").unbind('click');
                }

            }, 'json' );
        }


    });



    /// equestrian


    $("#btn_show_1, #btn_show_2, #btn_show_3, #btn_show_4").click( function() {

        var thisID = $(this).attr("id").split("btn_show_")[1];

        if ( $("#table-widget-left-"+thisID).css("display") != "none" ) {

            $("#table-widget-left-"+thisID).css({
                display: "none"
            });

            $(this).css({
                backgroundImage: ""
            });
        }
        else {
      
            $("#table-widget-left-"+thisID).css({
                display : "block"
            });

            $(this).css({
                backgroundImage: 'url("/media/images/elements/btn_hide_table.png")'
            });
        }
    });



});


function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}
