// JavaScript Document (jQuery)

/***
*
* Functions & Global Variables
*
*/

//path to the assets folder
//var baseUrl = '/templates/newcastle/hamptoninn/';
//var assetsPath = baseUrl + 'assets/';

//flash parameters
var params = {
    quality : 'best',
    scale : 'noscale',
    salign : 'tl',
    wmode : 'transparent',
    allowscriptaccess : 'always'
};
var attributes = {
    align : 'top'
};

/* Suckerfish dropdowns */

$.fn.hoverClass = function(c) {
    return this.each(function() {
        $(this).hover(
            function() {
                $(this).addClass(c);
            },
            function() {
                $(this).removeClass(c);
            }
        );
    });
};

/* Lightbox functions */

function hideLightbox() {
    $('.jqmWindow').css('left','-999em');
    $('.jqmOverlay').remove();
}
function showLightbox() {
    $('.jqmWindow').css('left','50%');
}
function triggerLightbox(url, dataString) {
    if (url == null) url = '';
    if (dataString == null) dataString = '';
    var $lightbox = $('#lightbox');
    $.ajax({
        url:url,
        type:'POST',
        data:dataString,
        success: function(content) {
            $lightbox.html(content);
        },
        complete: function() {
            $lightbox.jqmShow();
        }
    });
}

/***
*
* Page Events
*
*/

$(function() {

    $('.date-pick').datePicker({
        clickInput:true
    });

    $('#booking-widget-submit').click(function() {
       var url = $(this).attr('href');
       var checkIn = $('#check-in').val();
       if (checkIn.length) {
           checkIn = $('#check-in').val().split('/');  //checkIn[0] is day, checkIn[1] is month, checkIn[2] is year
           var checkOutDate = new Date(parseInt(checkIn[2]),parseInt(parseInt(checkIn[1])-1),parseInt(checkIn[0]));
           checkOutDate.setDate(checkOutDate.getDate() + parseInt($('#number-nights').val()));
           var queryString = (url.match('/\?/')) ? '&' : '?';
           queryString += 'arrivalMonth=' + checkIn[1] + '&arrivalDay=' + checkIn[0] + '&arrivalYear=' + checkIn[2];
           queryString += '&departureMonth=' + eval(parseInt(checkOutDate.getMonth()) + 1) + '&departureDay=' + checkOutDate.getDate().toString() + '&departureYear=' + checkOutDate.getFullYear().toString();
           queryString += '&numAdults=' + $('#number-adults').val() + '&numChildren='  + $('#number-children').val();
           window.open(url + queryString,'_blank');
           return false;
       }
       alert('Please choose a check in date!');
       return false;
    });

    $('#search').focus(function() {
        if ($(this).hasClass('empty-search')) {
            $(this).removeClass('empty-search').attr('value','');
        }
    });
    $('#search').blur(function() {
        if ($(this).attr('value').length == 0) {
            $(this).addClass('empty-search').attr('value','Search');
        }
    });

    $('.navigation li').hover(
        function(){
            $('ul', this).fadeIn('fast');
        },
        function() { }
    );
    if (document.all) {
        $('.navigation li').hoverClass ('sfHover');
    }

    $('#banner').cycle();

    if ($('#lightbox').length > 0) {
        $('#lightbox').jqm({
            overlay:70,
            onShow:showLightbox,
            onHide:hideLightbox
        });
		
		$('#lightbox').draggable();

    }

    if ($('.gallery-image').length > 0) {
        $('.gallery-image').click(function() {
            //identifiers[0] is galleryIdentifier, identifiers[1] is imageId
            var identifiers = $(this).attr('id').split('-');
            triggerLightbox('ajax/' + identifiers[0] + '/' + identifiers[1]);
        });
    }

});
