// JavaScript Document (jQuery)

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

//var lang_code = 'en';  //defined in head.phtml
//var path_assets = 'application/views/assets/';  //defined in head.phtml
var preloader = path_assets + 'flash/preloader.swf';

var flashvars = {
    introURL : path_assets + 'flash/intro-' + lang_code.replace(/\/$/,'') + '.swf',
    galleryURL : path_assets + 'flash/gallery.swf',
    mapURL : path_assets + 'flash/map.swf',
    listURL : path_assets + 'flash/list.swf',
    galleryXML : base_url + '/xml/coverflow/' + lang_code,
    mapXML : base_url + '/xml/map/' + lang_code,
    propertyXML : base_url + '/xml/property/' + lang_code,  //additional query string is sent through by the map on this call
    //api_key : 'ABQIAAAA8kYNiCD2wxasVNry2M0JXBTJB3r09bwitJTNzRaGnpRe7C8rpBQdqUHY0fe19jkYI2Ze2XG1qiTC5g'  //http://draft.starwoodafrica2010.com
    //api_key : 'ABQIAAAA8kYNiCD2wxasVNry2M0JXBRugisQHSQgEsAd4u74yv894_aRdhQUGt1TXF-3gOpXfcWkExCX5REhJA'  //http://starwoodafrica.michael-copeland.dev.webcanada.com
    api_key : 'ABQIAAAA8kYNiCD2wxasVNry2M0JXBTQ2CJRM1DFQi4wks1aze4gL5rh0hSwOVj-pNgB-4KwuepzJXfwHUE04w'  //http://www.starwoodafrica.com
};
var params = {
    quality : 'best',
    scale : 'noscale',
    salign : 'tl',
    wmode : 'transparent',
    allowscriptaccess : 'always'
};
var attributes = {
    align : 'top'
};

/***
 *
 * Page Events
 *
 */

$(function() {

    swfobject.embedSWF(preloader, 'main-flash', '100%', '523', '10.0.0', 'expressInstall.swf', flashvars, params, attributes);

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

    $('#select-language').change(function() {
        document.location.href = base_url + '/' + $(this).val();
    });

    $('.view-toggle').click(function() {
        var toggle_id =  $(this).attr('id');
        var view_id = toggle_id.replace(/view-toggle-/,'');
        if (!$(this).hasClass('view-toggle-selected')) {
            $(this).siblings('.view-toggle').each(function () {
                var sibling_toggle_id = $(this).attr('id');
                if ($(this).hasClass('view-toggle-selected')) {
                    $(this).removeClass(sibling_toggle_id+'-selected').removeClass('view-toggle-selected').addClass(sibling_toggle_id+'-unselected');
                }
            });
            $(this).removeClass(toggle_id+'-unselected').addClass('view-toggle-selected').addClass(toggle_id+'-selected');
            loadView(view_id);
        }
    });

    var brand = getURLBrandSegment();
    var location = getURLLocationSegment();
    var url = base_url + '/ajax/propertycount/';
    $.ajax({
        type:'POST',
        url:url,
        success: function(data){
            $('#total-count').html(data);
        }
    });
    url = url + brand + location;
    $.ajax({
        type:'POST',
        url:url,
        success: function(data){
            $('#result-count').html(data);
        }
    });

    $('select#country').change(function() {
        var url = base_url + '/ajax/cities/' + lang_code;
/*		
		if ($(this).val() == 'EG') {
			var msg = 'The safety, security and well-being of our guests and associates are of paramount priority at Starwood Hotels & Resorts. We have committed significant resources to ensure a safe and comfortable environment at our hotels in Egypt. Starwood\'s global safety and security policies and procedures are followed consistently and we will continue to monitor the situation closely.'
						+ '\r\n\r\n'
						+ 'The hotels are prepared to accommodate travellers who are unable to depart from Egypt due to flight cancellations. Guests who have to cancel their stay at should contact the hotels directly for guidance on special cancellation policies that apply during this time. Guests planning to travel to Egypt should contact their respective governments or tour operators for more information.'
			alert(msg);
		}
*/		
        if ($(this).val() != 0) {
            var dataString = '';
            $.ajax({
                type: 'POST',
                url: url + $(this).val(),
                data: dataString,
                success: function(content) {
                    $('select#city').removeAttr('disabled').html(content);
                }
            });
        } else {
            $('select#city').attr('disabled','disabled').html('<option value="0">Select a City</option>');
        }
        updateView();
    });
    $('select#city').change(function() {
        updateView();
    });

    $('.filter').click(function() {
        var $filter_options = $(this).nextAll('.filter-options:first');
        if ($(this).hasClass('filter-open') && ($filter_options.length > 0)) {
            $filter_options.slideUp();
            $(this).removeClass('filter-open').addClass('filter-closed');
        } else if ($(this).hasClass('filter-closed') && ($filter_options.length > 0)) {
            $filter_options.slideDown();
            $(this).removeClass('filter-closed').addClass('filter-open');
        }
    });

    $('.checkbox').click(function() {
        if ($(this).hasClass('checked')) {
            $(this).removeClass('checked').addClass('unchecked');
        } else {
            $(this).removeClass('unchecked').addClass('checked');
        }
        debounce_updateView();
    });

    $('#reset-brands').click(function() {
        $('span.checkbox').removeClass('unchecked').addClass('checked');
        debounce_updateView();
    });

    $('#clear-brands').click(function() {
        $('span.checkbox').removeClass('checked').addClass('unchecked');
        debounce_updateView();
    });

});
