/// <reference path="jquery-1.4.3-vsdoc.js" />

var slideshow = new SlideShow();

function SlideShow() {

    this.container = this.sliders = this.buttons = this.shiftWidth = null;
    this.activeButtons = true;

    this.sliders = null;

    this.activeSlide = 0;
    this.nextSlide = 1;
    this.moved = 0;

    this.slideShowInterval = false;

    this.setShow = function (inContainer) {
        this.container = inContainer;
        this.sliders = this.container.find('.slider');

        this.container.find('h1,h2,p').css({ background: 'transparent' });

        if (this.sliders.length > 1)
            this.container.find('#slideshowButtons').css({ display: 'block', zIndex: 101 });
        else return false;

        this.buttons = this.container.find('#slideshowButtons li');
        this.buttons.eq(0).attr('class', 'slideshowBtnActive');

        this.buttons.find('a').click(function () {
            if (!slideshow.activeButtons || $(this).attr('rel') == slideshow.activeSlide) return false;
            slideshow.activeButtons = false;
            slideshow.wipeInterval();
            $('#slideshowBtnPause').attr('class', 'play');
            slideshow.nextSlide = $(this).attr('rel');
            slideshow.showNext();
            return false;
        });

        $('<div id="slideshowBtnPause" class="play"></div>')
            .appendTo(this.container)
            .css({ zIndex: 101 })
            .click(function () {
                if (slideshow.slideShowInterval) {
                    slideshow.wipeInterval();
                    $(this).attr('class', 'play');
                }
                else {
                    slideshow.showNext();
                    slideshow.startSlideShow();
                    $(this).attr('class', 'pause');
                }
            });

        this.startSlideShow();
    }

    this.wipeInterval = function() {
        clearInterval(this.slideShowInterval);
        this.slideShowInterval = false;
    }

    this.showNext = function() {

        this.activeButtons = false;

        this.buttons.find('a[rel="' + this.activeSlide + '"]').parent().attr('class', 'slideshowBtn');
        this.buttons.find('a[rel="' + this.nextSlide + '"]').parent().attr('class', 'slideshowBtnActive');

        this.sliders.eq(this.nextSlide).css({ zIndex: 100 }).fadeIn('slow',
            function() {
                slideshow.sliders.eq(slideshow.activeSlide).hide().css({ zIndex: 0 });

                slideshow.activeSlide = slideshow.nextSlide;
                if (slideshow.nextSlide == slideshow.sliders.length - 1) slideshow.nextSlide = 0;
                else slideshow.nextSlide++;

                slideshow.activeButtons = true;
            });
    }

    this.startSlideShow = function() {
        $('#slideshowBtnPause').attr('class', 'pause');
        this.slideShowInterval = setInterval(function() { slideshow.showNext(); }, 4000);
    }
}



window.onload = function () {

    if ($('#shoppingPod .rightCol').length > 0) {
        var images = $('#shoppingPod #rightColImages img').hide();
        var links = $('#shoppingPod #rightColLinks a').hide();
        var leftArrow = $('<img src="/images/arrow-left-grey.gif" alt="Previous" class="leftArrow" rel="0"/>').click(function() { nextImage(this, images, -1, links); });
        var rightArrow = $('<img src="/images/arrow-right-grey.gif" alt="Next" class="rightArrow" rel="0" />').click(function() { nextImage(this, images, 1, links); });
        $('#shoppingPod #rightColImages img[rel="0"]').fadeIn('slow');
        $('#shoppingPod #rightColLinks a[rel="0"]').css({ display: 'block' });
        if (images.length > 1) $('#shoppingPod .rightCol').append(leftArrow).append(rightArrow);
    }

    function nextImage(arrow, jqueryCol, dir, links) {
        var newIndex = ($(arrow).attr('rel')) ? $(arrow).attr('rel') : 0;
        var nextIndex = parseInt(newIndex) + dir;
        if (nextIndex < 0) nextIndex = jqueryCol.length - 1;
        else if (nextIndex >= jqueryCol.length) nextIndex = 0;
        jqueryCol.filter(':visible').fadeOut('slow', function() {
            links.filter(':visible').css({ display: 'none' });
            links.filter('[rel="' + nextIndex + '"]').css({ display: 'block' });
            jqueryCol.filter('[rel="' + nextIndex + '"]').fadeIn('slow');
            $(arrow).parent().find('.leftArrow,.rightArrow').attr('rel', nextIndex);
        });
    }

    $('.shoppingEatingPod .imageSwitchControl').each(function() {
        var images = $(this).find('.images img').hide();
        var links = $(this).find('.imageLinks a').hide();
        var leftArrow = $('<img src="/images/arrow-left-bluebg.gif" alt="Previous" rel="0" class="leftArrow" />').click(function() { nextImage(this, images, -1, links); });
        var rightArrow = $('<img src="/images/arrow-right-bluebg.gif" alt="Next" rel="0" class="rightArrow" />').click(function() { nextImage(this, images, 1, links); });
        $(this).find('.images img[rel="0"]').fadeIn('slow');
        $(this).find('.imageLinks a[rel="0"]').css({ display: 'block' });
        if (images.length > 1) $(this).append(leftArrow).append(rightArrow);
    });

    $('#investment .imageSwitchControl').each(function() {
        var images = $(this).find('.images img').hide();
        var links = $(this).find('.imageLinks a').hide();
        var leftArrow = $('<img src="/images/backwardBtn.jpg" alt="Previous" rel="0" class="leftArrow" />').click(function() { nextImage(this, images, -1, links); });
        var rightArrow = $('<img src="/images/forwardBtn.jpg" alt="Next" rel="0" class="rightArrow" />').click(function() { nextImage(this, images, 1, links); });
        $(this).find('.images img[rel="0"]').fadeIn('slow');
        $(this).find('.imageLinks a[rel="0"]').css({ display: 'block' });
        if (images.length > 1) $(this).append(leftArrow).append(rightArrow);
    });

    if ($('#weatherPod').length > 0) {
        $('#weatherPod .day5Forecast, #weatherPod  .worldWeather').hide();
        $('#weatherPod .expandLink').toggle(
            function() {
                $(this).prev().slideDown('fast');
                $(this).attr('class', 'shrinkLink');
            },
            function() {
                $(this).prev().slideUp('fast');
                $(this).attr('class', 'expandLink');
            }
        );
        $('#weatherPod a.openTab').click();
    }

    $('.greyButton, .yellowButton').hover(
        function() { $(this).css({ backgroundPosition: '0 -17px' }); },
        function() { $(this).css({ backgroundPosition: '0 0' }); }
    );

    $('#slideshowButtons li').each(
        function() {
            $(this).hover(
                function() { if ($(this).attr('class') == 'slideshowBtn') $(this).attr('class', 'slideshowBtnHover'); },
                function() { if ($(this).attr('class') == 'slideshowBtnHover') $(this).attr('class', 'slideshowBtn'); }
            );
        }
    );

    //slideshow.setShow($('.slideshowLinks'));

    if ($("#txtArrivalRoute, #txtDepartureRoute").length > 0) {
        var autoSuggestUrl = "/Resources/Code/json/api.ashx?op=listroutes";

        $("#txtArrivalRoute, #txtDepartureRoute").attr("autocomplete", "off");

        $.getJSON(autoSuggestUrl, function (routes) {
            $("#txtArrivalRoute, #txtDepartureRoute").autocomplete(routes, {
                formatItem: function (route) {
                    return route.name;
                }
            });
        });
    }
};

/** Custom jQuery Plugins **/
(function($) {
    $.fn.createDropdown = function(options) {
        var opts = $.extend({}, $.fn.createDropdown.defaults, options);
        return this.each(function() {
            var newSelect = $('<select name="' + $(this).attr('id') + '" />').css({ display: 'none' });
            var newOption = null;
            var children = $(this).find('a');
            for (var i = 0, len = children.length; i < len; i++) {
                var newOption = $('<option value="' + $(children[i]).attr('href') + '">' + children[i].innerHTML + '</option>');
                newSelect.append(newOption);
            }
            var buttonClass = $(this).attr('rel') ? $(this).attr('rel') : 'greyButton';
            var newInput = $('<span class="' + buttonClass + '">Go </span>').click(
                function() { window.location.href = newSelect.val(); }
            ).css({ display: 'none' });
            $(this).before(newSelect).before(newInput).after($('<div class="clear"></div>')).remove();
            newSelect.fadeIn();
            newInput.fadeIn();
            //$(this).animate({height:'20px'},'slow','swing',function(){});
        });
    }
    $.fn.createDropdown.defaults = {}
    /** Create Tabs plugin **/
    $.fn.createTabs = function(options) {
        var opts = $.extend({}, $.fn.createTabs.defaults, options);
        return this.each(function() {
            var count = 0;
            $(this).parent().find('.form2').hide();
            $(this).parent().find('.form').eq(0).show();
            $(this).parent().find('a[rel="tab1"]').hide();
            $(this).find('a:not(.disabledTab)').each(function() {

                // remove tracking code
                $(this).removeAttr('onclick');

                $(this).attr('rel', count);
                $(this).click(
                    function() {
                        $(this).parent().parent().parent().find('.tabButton').hide();
                        var rel = $(this).attr('rel');
                        $(this).parent().parent().parent().find('a[rel="tab' + rel + '"]').show();
                        var form = $(this).parent().parent().parent().find('.form').eq(rel);
                        if (form.css('display') == 'block') return false;
                        else {
                            $(this).parent().parent().parent().find('.form:visible').hide();
                            form.show();
                            $(this).parent().parent().find('.selected').attr('class', '');
                            $(this).parent().attr('class', 'selected');
                        }
                        return false;
                    }
                );
                count++;
            });
        });
    }
    $.fn.createTabs.defaults = {}
    /** Vertical alignment **/
    $.fn.vAlign = function() {
        return this.each(function(i) {
            $(this).css({ height: 'auto', display: 'block' });
            $(this).parent().css({ backgroundPosition: '7px center' });
            var topPad = ($(this).parent().height() - $(this).height()) / 2;
            $(this).css('padding-top', topPad);
        });
    };
    /** remove last underline **/
    $.fn.remLastBorder = function() {
        return this.each(function(i) {
            $(this).find('li:last').css({ border: '0' });
        });
    };
    /** jQuery centerContent (modal pop-up) plugin **/
    $.fn.centerContent = function () {
        return this.each(function () {
            var windowTop = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
            var top = windowTop + ($(window).height() / 2) - ($(this).outerHeight() / 2);
            
            /*var top = ($(window).height() - $(this).outerHeight()) / 2;*/
            var left = ($(window).width() - $(this).outerWidth()) / 2;
            $(this).css({ position: 'absolute', margin: 0, top: (top > 0 ? top : 0) + 'px', left: (left > 0 ? left : 0) + 'px' });
        });

    } 
    /** jQuery makes an element the full height of the document **/
    $.fn.fullScreen = function() {
        return this.each(function(){
            var fullHeight = $(document).height();
            var fullWidth = $(document).width();
            $(this).css({ height: fullHeight, width: fullWidth });
        });
    };
        
})(jQuery);


/** This runs at the bottom of the page before the ads have a chance to load **/
function contentLoaded() {
    Cufon.replace('#picPos p,#picPos h2,#picPos h3,#slideshow h1, .dw h2, .wd  h3, .default h2,.default h3, .slideshowText p, .fullImagePod .dark h3, .fullImagePod .dark h4, #socialMediaPod h3', { textShadow: '1px 1px rgba(255,255,255,0.5)' });
    Cufon.replace('.white h2,.white h3,.dw h3,.wd h2,.fullImagePod .light h3,.fullImagePod .light h4', { textShadow: '1px 1px rgba(0,0,0,0.5)' });
    Cufon.replace('#pageHeading h1');
    Cufon.now();
    $('.convertDropdown').createDropdown();
    $('.tabs').createTabs();
    $('.tabs .active').not('.selected').find('a').click();
    $('#QypePod li.selected').find('a').click();
    $('a .pad').vAlign();
    $('.links').remLastBorder();

    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    function findIndex(arr, obj) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == obj) return ++i;
        }
    }

    slideshow.setShow($('#slideshow'));

    if ($('.datePicker').length > 0) {
        if (!($.browser.msie && $.browser.version <= 7)) {
            $('.datePicker').each(
                function () {
                    var day = $($(this).find('select')[0]).val();
                    var monthYear = $($(this).find('select')[1]).val().split(' ');
                    var month = monthYear[0];
                    var year = monthYear[1];
                    $(this).html('');
                    var closeEvent = function () { };
                    var datePickerObjectIni = {};
                    if ($(this).attr('rel') == 'parkingEntryDay') {
                        closeEvent = function (dateText, inst) {
                            var dateParts = dateText.split(" ");
                
                            switch (dateParts[1]) {
                                case "Jan":
                                    dateParts[1] = 0;
                                    break;
                                case "Feb":
                                    dateParts[1] = 1;
                                    break;
                                case "Mar":
                                    dateParts[1] = 2;
                                    break;
                                case "Apr":
                                    dateParts[1] = 3;
                                    break;
                                case "May":
                                    dateParts[1] = 4;
                                    break;
                                case "Jun":
                                    dateParts[1] = 5;
                                    break;
                                case "Jul":
                                    dateParts[1] = 6;
                                    break;
                                case "Aug":
                                    dateParts[1] = 7;
                                    break;
                                case "Sep":
                                    dateParts[1] = 8;
                                    break;
                                case "Oct":
                                    dateParts[1] = 9;
                                    break;
                                case "Nov":
                                    dateParts[1] = 10;
                                    break;
                                case "Dec":
                                    dateParts[1] = 11;
                                    break;
                            }

                            var entryDate = Date.UTC(dateParts[2], dateParts[1], dateParts[0]);
                            var exitDate = entryDate + 604800000;

                            var exitPicker = $('#parkingExitDay');
                            exitPicker
                                .datepicker("option","minDate", null)
                                .datepicker("setDate", new Date(exitDate))
                                .datepicker("option","minDate", new Date(entryDate));
                        }
                        datePickerObjectIni = { onClose: closeEvent, dateFormat: 'd M y', buttonImageOnly: true, buttonImage: '/images/icon-datepicker.gif', showOn: 'both',minDate: 0};
                    }
                    else{
                        datePickerObjectIni = { dateFormat: 'd M y', buttonImageOnly: true, buttonImage: '/images/icon-datepicker.gif', showOn: 'both', minDate: 0 };
                    }
                    var dp = $('<input class="datepicker" value="' + day + ' ' + month + ' ' + year + '" id="' + $(this).attr('rel') + '"  name="dp' + $(this).attr('rel') + '" />')
                        .appendTo($(this))
                        .datepicker(datePickerObjectIni);
                }
            );
        }
    }

    /**** Default value in input box ****/
    $('input[rel|=defaultVal]').each(
        function() {
            var input = $(this);
            var defaultVal = input.attr('rel').split('-')[1];
            if (input.val() == '') input.val(defaultVal);
            input
                .focus(function() { if ($(this).val() == defaultVal) $(this).val(''); })
                .blur(function() { if ($(this).val() == '') $(this).val(defaultVal); });
        }
    );

    if ($('#mainSitemap').length > 0) {
        $('#mainSitemap ul ul').css({ display: 'none' });
        $('#mainSitemap .spacer').css({ display: 'block' });
        $('#mainSitemap .plus')
          .css({ cursor: 'pointer', display: 'block' })
          .html('<img src="/images/icon-plus.gif" alt="Expand sitemap" />')
          .toggle(
            function() {
                $(this).parent().find('>ul').slideDown('fast');
                $(this).html('<img src="/images/icon-minus.gif" alt="Collapse sitemap" />');
            },
            function() {
                $(this).parent().find('>ul').slideUp('fast');
                $(this).html('<img src="/images/icon-plus.gif" alt="Expand sitemap" />');
            }
        );
    }
/*
    if ($('table.liveflights').length > 1) {
        $('table.liveflights').each(
            function() {
                var columns = $(this).find('th');
                columns.eq(0).css({ width: '40px' });
                columns.eq(1).css({ width: '55px' });
                columns.eq(4).css({ width: '40px' });
            }
        );
    }
*/
//.net helpers
    $('#javascriptOn').val('true');
}

/* The accordian for the qype reviews page */
$(function () {
    $('.slideUp .content').hide();
    $('.slideUp h2').click(function () {
        $(this).parent().find('.content').slideToggle('fast');
    });
})
function contentLoadedBlog() {
    Cufon.replace('#headerCopy h1,#headerCopy h2', { fontFamily: 'gatwick-sans-bold' });
    Cufon.replace('#socialMediaPod h3', { fontFamily: 'Frutiger CE 55 Roman', textShadow: '1px 1px rgba(255,255,255,0.5)' });
    Cufon.now();
}


/* Function to use tracking and open link in a new window */
function openWindow(link, goal) {
    _gaq.push(function () {
        var tracker = _gaq._getAsyncTracker();  //add name param if needed
        if (goal.length != 0) {
            _gaq.push(['_trackPageview', goal]);
            window.open(tracker._getLinkerUrl(link.href));
        }
        else
            window.open(tracker._getLinkerUrl(link.href));
    });
}

function checkForNewWindow(link, goal) {

    if ($(link).attr('target') == '_blank')
        openWindow(link, goal);
    else {
        _gaq.push(['_link', link.href]);
        if (goal.length != 0) {
            _gaq.push(['_trackPageview', goal]);
        }
    }
}

