$(document).ready(function(){
    // Add pseudoclasses to lists
    $("li:last-child").addClass("last");
    $("li:first-child").addClass("first");
    // Add hover support for ie
    $("#language-switch, #restaurant-links-container a, #main-menu li ul li, #our-hotels ul li, #hotel-quick-links a").hover(function(){
        $(this).addClass('hover');
    }, function(){
        $(this).removeClass('hover');
    });
    // Main menu and hotels gadget click fix
    $("#main-menu li ul li, #our-hotels ul li").click(function(){
        window.location = $(this).children("a").first().attr('href');
    });
    // Show main menu subpoints
    $("#main-menu ul").css({visibility: 'visible'});
    // Prepapare to wrap main menu
    $("#main-menu ul").wrap('<div class="rounded gray" />');
    // Add wrappers to rounded
    $.each($(".rounded"), function(){
        $(this).children().wrapAll('<div class="wrapper-d" />');
    });
    $(".wrapper-d").wrap('<div class="wrapper-c" />');
    $(".wrapper-c").wrap('<div class="wrapper-b" />');
    $(".wrapper-b").wrap('<div class="wrapper-a" />');
    // Add wrapping to language switch
    $("#language-switch").removeClass('rounded');
    $("#language-switch").hover(function(){
        $(this).addClass('rounded');
    }, function(){
        $(this).removeClass('rounded');
    });
    // Restaurants links
    if (true || jQuery.browser.msie && jQuery.browser.version == '9.0') {
        $('#restaurant-links-container img').each(function(){
            $(this).attr('src', $(this).attr('src') + '?random=' + (new Date()).getTime());
        });
    }
    $('#restaurant-links-container img').load(function(){
        $(this).parent().height($(this).height()/2);
        $(this).css({top: -$(this).height()/2 + 'px'});
        $(this).parent().hover(function(){
            $(this).children().css({top: 0});
        }, function(){
            $(this).children().css({top: -$(this).children().height()/2 + 'px'});
        });
    });
    // Add expander to main-menu
    $("#main-menu li").hover(function(){
        if ($(this).children("div").length == 0) return false;
        $(this).children("div").css({
            'min-width': $(this).width() + 20
        });
        $(this).children("div").find("div").css({
            width: '100%'
        });
        $(this).children("div").fadeIn(400, function(){
            if ($.browser.msie && $.browser.version.substr(0,1)<7) {
                $("select").css({
                    display: 'none'
                });
            }
            if ($.browser.msie && $.browser.version.substr(0,1) == 7) {
                $(this).width(Math.max($(this).width(), $(this).parent().width() + 20));
            }
        }).show();
        $(this).addClass("hover");
    }, function(){
        $(this).children("div").fadeOut(400, function(){
            if ($.browser.msie && $.browser.version.substr(0,1)<7) {
                $("select").css({
                    display: 'block'
                });
            }
        }).hide();
        $(this).removeClass("hover");
    });
    // Add expand pattern
    $(".expand-button").parent().hover(function(){
        $(this).addClass("hover");
    }, function(){
        $(this).removeClass("hover");
    });
    // Init slideshow
    var slideShow = new SlideShow($("#slides-container .slide"), 'slideShow');
    // Hovering banner links
    $("#slide-controls-container li").hover(function(){
        $(this).addClass('active');
    }, function(){
        $(this).removeClass('active');
    });
    // Init jflow
    for (var i = 0; i < $('#dyna-banner-slides-container').children().length; i++) {
        $('#dyna-banner-controller').append('<span class="jFlowControl">No 1 </span>');
    }
    $('#dyna-banner-controller').jFlow({
        slides: "#dyna-banner-slides-container",
        width: "252px",
        height: "187px",
        duration: 1000
    });
    // Init lightbox
    $('#gallery a').lightBox({
        imageLoading: '/public/images/lightbox-ico-loading.gif',
        imageBtnClose: '/public/images/lightbox-btn-close.gif',
        imageBtnPrev: '/public/images/lightbox-btn-prev.gif',
        imageBtnNext: '/public/images/lightbox-btn-next.gif',
        imageBlank: '/public/images/lightbox-blank.gif'
    });
    // Prepare subscribe form
    $('#subscribe-form #subscribe-submit').click(function() {
        // validate
        var error = false;
        $('#subscribe-form :input').each(function() {
            if ($(this).attr('name') == 'email') {
                if (! /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/.test($(this).val())) {
                    $(this).addClass('error');
                    $(this).bind('change', function(){
                        $(this).removeClass('error')
                    });
                    error = true;
                }
            }
        });
        // Stop propogation if error acquired
        if (error) {
            return false;
        }
        // submit the form
        $('#subscribe-form').ajaxSubmit({
            success: function(responseValue) {
                $('#subscribe-form').fadeOut(300, function(){
                    if (/.*ERROR.*/.test(responseValue)) {
                        $('#subscribe-error').fadeIn(300);
                    } else {
                        $('#subscribe-success').fadeIn(300);
                    }
                });
            }
        });
        // return false to prevent normal browser submit and page navigation
        return false;
    });
    // GMap implementation
    $('a.gmap').click(function(){
        $('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"></div>');
        // Get page sizes
        var arrPageSizes = ___getPageSize();
        // Style overlay and show it
        $('#jquery-overlay').css({
            backgroundColor: '#000',
            opacity: '0.8',
            width: arrPageSizes[0],
            height: arrPageSizes[1]
        }).fadeIn();
        // Get page scroll
        var arrPageScroll = ___getPageScroll();
        // Calculate top and left offset for the jquery-lightbox div object and show it
        $('#jquery-lightbox').css({
            top: arrPageScroll[1] + (arrPageSizes[3] / 10),
            left: (arrPageSizes[0] - 530) / 2,
            width: 530,
            height: 500,
            padding: '15px 0',
            background: '#fff'
        }).append('<iframe width="500" height="500" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+$(this).attr('href')+'"></iframe>').show();
        // Assigning click events in elements to close overlay
        $('#jquery-overlay').click(function() {
            $(this).remove();
            $('#jquery-lightbox').remove();
        });

        return false;
    });
    // Init datepicker
    $("#dateIn").attr('value', 'mm/dd/yyyy').datepicker({
        dateFormat: 'mm/dd/yy'
    });
    $("#dateOut").attr('value', 'mm/dd/yyyy').datepicker({
        dateFormat: 'mm/dd/yy'
    });
});




/* THIRD FUNCTION
* getPageSize() by quirksmode.com
*
* @return Array Return an array with page width, height and window width, height
*/
function ___getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
};
/**
/ THIRD FUNCTION
* getPageScroll() by quirksmode.com
*
* @return Array Return an array with x,y page scroll values.
*/
function ___getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }
    arrayPageScroll = new Array(xScroll,yScroll);
    return arrayPageScroll;
};

/* Banner slideshow with controls */
function SlideShow(slides, varName) {
    /* Slides container */
    this.slides = slides;
    /* Active slide number container */
    this.active_slide = 0;
    /* Interval handler */
    this.interval = null;
    /* Variable of the slideshow */
    this.varName = varName;

    /* Changes active slide to new_slide. new_slide is INT */
    this.changeSlide = function(new_slide) {
        if (this.active_slide == new_slide) return false;

        $(this.slides[this.active_slide]).fadeOut(500);
        this.active_slide = new_slide;
        $(this.slides[this.active_slide]).fadeIn(500);
        return true;
    }

    /* Change to next */
    this.changeToNext = function() {
        
    }

    /* Timed executioner */
    this.runTimer = function() {
        //        return false;
        if (this.slides.length < 2) return false;
        var self = this;
        this.interval = setInterval(function(){
            var next_slide = self.active_slide + 1;
            if (next_slide == self.slides.length) next_slide = 0;
            self.changeSlide(next_slide);
        }, 7000);
        return true;
    }


/* Apply height of image to slides container */
    this.applyHeight = function() {
        var maxHeight = 0;
        this.slides.each(function(slide){
            if(slide.getHeight() > maxHeight) maxHeight = slide.getHeight();
        });
        this.slides.first().up().setStyle({
            height: maxHeight+'px'
        });
    }


    /* Initialize */
    //    this.applyHeight();
    this.runTimer();
}
