// JavaScript Document (jQuery)

/***
*
* Page Events
*
*/

function getTheParamFromUrlByName(name, url){
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( url );
    if( results == null )
        return '';
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

    var bookUrl = $('a.results-booknow').attr('href');

    function modifyBookURL(el, id) {

        bookUrl = $('#' + id).attr('href');

        //process radio selections
        var promoCode = (getTheParamFromUrlByName('promotionCode', bookUrl));
        var rateName = (getTheParamFromUrlByName('ratePlanName', bookUrl));
        var needle = '';

        if (promoCode == '') {
            needle = rateName;
        } else {
            needle = promoCode;
        }

        if (needle != '' && (el.value != 'undefined'))
        {
            var re = new RegExp(needle,"g");
            bookUrl = bookUrl.replace(re, el.value);
        }
        $('#' + id).attr('href', bookUrl);
    }

function radioSelectionsExist(id) {
    if ($("#radio_selection" + id).size())
    {
        return true;
    }
    return false;
}

function lightboxRadioSelectionsExist(id) {
    if ($("#lightbox_radio_selection" + id).size())
    {
        return true;
    }
    return false;
}

function viewAll(){
    $('span.checkbox').each(function(){
        if($(this).hasClass('unchecked')){
            $(this).removeClass('unchecked');
            $(this).addClass('checked');
        }
    });
}

function resetAll(){
    $('span.checkbox').each(function(){
        if($(this).hasClass('checked')) {
            $(this).removeClass('checked');
            $(this).addClass('unchecked');
        }
    });
}

$(function() {
		   
    var campaign_name = $('input#campaign-name:hidden').val();
    var intro_swf = $('input#intro-swf:hidden').val();
    var brand = $('input#brand').val();
    var intro = 'assets/u/' + intro_swf;
    var gallery = 'assets/flash/gallery.swf';
    var map = 'assets/flash/map.swf';
    var list = 'assets/flash/list.swf';
    var	gallery_feed = 'xml/coverflow/' + campaign_name;
    var preloader = 'assets/flash/preloader.swf';
    if(campaign_name == 'starwoodresorts' && (brand == 'SI' || brand == 'WH' || brand == 'WI')) {
        var path = '';
        switch (brand) {
            case 'SI':
                path = 'shr';
                break;
            //case 'ST': path = 'str'; break;
            case 'WH':
                path = 'who';
                break;
            case 'WI':
                path = 'wst';
                break;
        }
        //var path = (brand == 'SI')?'shr':'wst';
        intro = 'assets/u/intro-' + path + '.swf';
        gallery = 'assets/flash/gallery-' + path + '.swf';
        map = 'assets/flash/map-' + path + '.swf';
        list = 'assets/flash/list-' + path + '.swf';
        gallery_feed = 'xml/coverflow/' + brand + '/' + campaign_name;
        preloader = 'assets/flash/preloader-' + path + '.swf'
    }
		   
    var flashvars = {
        introURL : intro,
        galleryURL : gallery,
        mapURL : map,
        listURL : list,
        galleryXML : gallery_feed,
        mapXML : 'xml/map/' + campaign_name,
        propertyXML : 'xml/property',  //additional query string is sent through by the map on this call
        api_key : 'ABQIAAAA8kYNiCD2wxasVNry2M0JXBT4oVjpfPDEzeNheH1TIs2B8cctPBQpJGjVXeWNTkOewxlvnzl-HivEUA'  //live site
    //api_key : 'ABQIAAAA8kYNiCD2wxasVNry2M0JXBRQqOFFmW_FC1x0oaiQOkMWKpgqVxQxCE5Bv_2YKH4PHzUKTCXVkQPOrg'  //draft.livecms.biz
    };
    var params = {
        quality : 'best',
        scale : 'noscale',
        salign : 'tl',
        wmode : 'transparent',
        allowscriptaccess : 'always'
    };
    var attributes = {
        align : 'top'
    };
		   
    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
        });
    }
	
    if ($.browser.msie && $.browser.version.substr(0,1)<7) {
    //$(document).pngFix();
    }

    //get the result count and total property count for the explore widget
    var brands = getSelectedBrands();
    var interests = getSelectedInterests();
    var url = campaign_name + '/' + brands.join('-') + '/' + interests.join('-');
    $.ajax({
        type: 'POST',
        url: url,
        data: 'fx=get_count',
        success: function(data){
            $('#total-count').html(data);
        }
    });
	
    var url = getFullURL('explore');
    url += brands.join('-') + '/';
    url += interests.join('-');
    $.ajax({
        type: 'POST',
        url: url,
        data: 'fx=get_count',
        success: function(data){
            $('#result-count').html(data);
        }
    });
	
    $('select[id$=-country]').change(function() {
        var filter_info = $(this).attr('id').split('-');
        var value_country = $(this).val();
        if (value_country != 0) {
            var url = getFullURL(filter_info[0],'country',value_country);
            updateFilter(url,filter_info[0],'state');
        } else {
            $('select#' + filter_info[0] + '-state').attr('disabled','disabled').html('<option value="0">State/Province</option>');
            $('select#' + filter_info[0] + '-city').attr('disabled','disabled').html('<option value="0">City</option>');
        }
        update();
    });
	
    $('select[id$=-state]').change(function() {
        var filter_info = $(this).attr('id').split('-');
        var value_state = $(this).val();
        if (value_state != 0) {
            var url = getFullURL(filter_info[0],'state',$(this).val());
            updateFilter(url,filter_info[0],'city');
        } else {
            $('select#' + filter_info[0] + '-city').attr('disabled','disabled').html('<option value="0">City</option>');
        }
        update();
    });

    $('#explore-city').change(updateCity);  //check if this is needed anymore???
	
    $('.tab').click(function() {
        if ($(this).hasClass('tab-unselected')) {
            $(this).removeClass('tab-unselected').addClass('tab-selected');
            $(this).siblings('.tab').removeClass('tab-selected').addClass('tab-unselected');
            $('.widget-middle-container').toggle();
        }
    });

    /*
	$('a[id^=reset-]').click(function() {
		var reset_info = $(this).attr('id').split('-');
		if (reset_info[1] == 'location') {
			$('#explore-country').children().first().attr('selected','selected').trigger('change');
		} else {
			$('#' + reset_info[1] + '-options .checkbox:not(:first)').removeClass('unchecked').addClass('checked');
			$('#' + reset_info[1] + '-options .checkbox:first').removeClass('checked').addClass('unchecked').trigger('click');
		}
		return false;
	});
	*/
	
    $('a[id^=reset-]').click(function() {
        var reset_info = $(this).attr('id').split('-');
        if (reset_info[1] != 'location') {
            $('#' + reset_info[1] + '-options .checkbox:not(:first)').removeClass('unchecked').addClass('checked');
            $('#' + reset_info[1] + '-options .checkbox:first').removeClass('checked').addClass('unchecked').trigger('click');
        }
        return false;
    });
	
    $('a[id^=clear-]').click(function() {
        var clear_info = $(this).attr('id').split('-');
        $('#' + clear_info[1] + '-options .checkbox:not(:first)').removeClass('checked').addClass('unchecked');
        $('#' + clear_info[1] + '-options .checkbox:first').removeClass('unchecked').addClass('checked').trigger('click');
        return false;
    });
	
    $('.filter').click(function() {
        var $filter_options = $(this).nextAll('.filter-options:first');
        if ($(this).hasClass('filter-open') && ($filter_options.length > 0)) {
            $filter_options.slideUp();
            //$filter_options.hide();
            $(this).removeClass('filter-open').addClass('filter-closed');
        } else if ($(this).hasClass('filter-closed') && ($filter_options.length > 0)) {
            $filter_options.slideDown();
            //$filter_options.show();
            $(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_update();
    });
	
    Date.format = 'mm/dd/yyyy';
    $('.date-pick').datePicker();
	
    $('a#booking-widget-booknow').click(function() {
        exploreResults('widget',$(this).attr('href'));
        return false;
    });

    $('.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);
        }
    });

    $('#promobar-middle div[class^=column-]').each(function() {
        var margin_top = Math.round(($(this).parent().height() - $(this).height())/2);
        $(this).css('margin-top',margin_top+'px');
    });
	
});