﻿$(document).ready(function() {

    // cycle
    if ($('#spotlight').length) {
        $('#spotlight').cycle({
            speed: 'fast',
            timeout: 10000,
            pause: 1,
            pager: '#spotlight_page',
            cleartype: 1,
            pagerAnchorBuilder: function(idx, slide) {
                return '<li><a class="' + slide.title + '_link" href="#no_link">' + slide.title + '</a></li>';
            }
        });
    };

    // Accomodation Popups

    $('a[rel="popInfo"]').click(function() {
        var leftMargin = (screen.width / 2) - 320;
        var topMargin = (screen.height / 2) - 275;
        window.open($(this).attr('href'), '_blank', 'height=550, width=640, status=no, toolbar=no, menubar=no, location=no, scrollbars=yes, resizable=yes, left=' + leftMargin + ', top=' + topMargin + '', '');
        return false;
    });

    $('a[rel="mapInfo"]').click(function() {
        var screenWidth = screen.width - 100;
        var screenHeight = screen.height - 140;
        var leftMargin = (screen.width / 2) - (screenWidth / 2);
        var topMargin = (screen.height / 2) - (screenHeight / 2);
        window.open($(this).attr('href'), '_blank', 'height=' + screenHeight + ', width=' + screenWidth + ', status=no, toolbar=no, menubar=no, location=no, scrollbars=yes, resizable=yes, left=' + leftMargin + ', top=' + topMargin + '', '');
        return false;
    });

    // Property page

    $('ul#ul-img li:even').addClass('odds');
    
    function updateMedia(content) {
        $('#l-img').html(content);    
    };

    $('ul#ul-img li img').click(function() {
        var src = $(this).attr('src'), title = $(this).attr('alt');
        updateMedia('<img src="' + src + '" title="' + title + '" alt="' + title + '">');
    });

    $('#tour').click(function() {
        var src = $(this).attr('href'), arr = src.split('.'), ext = arr[arr.length - 1];
        if (ext == 'mov') {
            updateMedia('<object classid="clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b" width="376" height="266" codebase="http://www.apple.com/qtactivex/qtplugin.cab"> <param name="src" value="' + src + '" /><param name="autoplay" value="true" /><param name="kioskmode" value="false" /><param name="controller" value="true" /><param name="loop" value="false" /><embed src="' + src + '" width="376" height="266" autoplay="true" kioskmode="false" controller="true" loop="false" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>');
        } else {
            window.open(src);
        }
        return false
    });

    $('#map').click(function() {
    var address = $(this).attr('rel');
        var src = 'http://maps.google.com/maps/api/staticmap?zoom=16&size=376x250&maptype=roadmap&markers=color:red|' + encodeURIComponent(address) + '&sensor=false';
        if (address != null || '') {
            updateMedia('<img src="' + src + '" title="Area map" alt="Area map">');
        }
        return false
    });

    // external links    
    $('a[class*="external"]').attr('title', function() { return this.title + ' (new window - ' + this.href + ' )' });

    $('a[class*="external"]').click(function() {
        window.open($(this).attr('href'));
        return false;
    });

    //$('ul.link_list li').not(':last-child').after(' | ');


    //Checkout page
    if ($('#bookingForm').length) {

        $('#billingAddress').hide();

        $('input[name*="UseLeadPassenger"]').click(function() {
            if ($('input[name*="UseLeadPassenger"]').is(':checked')) {
                $('#billingAddress').hide();
                $('#billingAddress .validate').removeClass('required');
                $('#billingSwitch > h4').attr('class', 'b1_subtitle')
            }
            else {
                $('#billingSwitch > h4').attr('class', 'b1_title')
                $('#billingAddress .validate').addClass('required');
                $('#billingAddress').show();
            };
        });

        $('#creditCardExpiry').mask('99 / 99', { placeholder: '_' });
        $('#creditCardStart').mask('99 / 99', { placeholder: '_' });

        function setCardType() {
            var theCard = $('#Payment_CardDetails_CardType option:selected').val()

            function setMask(maskFormat) {
                $('#creditCardNumber').unmask();
                if (maskFormat != null || '') {
                    $('#creditCardNumber').mask(maskFormat, { placeholder: '_' });
                };
            };

            switch (theCard) {
                // Visa                                 
                case (theCard = 'Visa'):
                    setMask('?9999 9999 9999 9999');
                    $('#debitCardFields').hide();
                    break;
                // Master Card                                 
                case (theCard = 'MasterCard'):
                    setMask('?9999 9999 9999 9999');
                    $('#debitCardFields').hide();
                    break;
                // American Express                                  
                case (theCard = 'AmericanExpress'):
                    setMask('?9999 999999 99999');
                    $('#debitCardFields').hide();
                    break;
                //Maestro                                 
                case (theCard = 'Maestro'):
                    setMask();
                    $('#debitCardFields').show();
                    break;
                //Solo                                 
                case (theCard = 'Solo'):
                    setMask();
                    $('#debitCardFields').show();
                    break;
                //Visa Debit                                  
                case (theCard = 'VisaDebit'):
                    setMask('?9999 9999 9999 9999');
                    $('#debitCardFields').show();
                    break;
                //Visa Electron                                 
                case (theCard = 'VisaElectron'):
                    setMask();
                    $('#debitCardFields').show();
                    break;
            };
        };
        setCardType();

        $('#Payment_CardDetails_CardType').change(setCardType);


        $('#bookingForm').validate({
            errorClass: "input-validation-error",
            errorPlacement: function(error, element) { }
        });

        $('#bookingForm').submit(function() {
            if ($('#termsChecked').is(':checked')) {
                return true;
            }
            else {

                alert('You must read and agree to accept the Terms & Conditions before proceeding.')
                return false;
            }
        });

    };

});

