// JavaScript Document (jQuery)

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

function introComplete() {
	if (configuredDisplay === true) {
		$('#main-flash').get(0).loadView('list');
		getResult();
		$('#view-toggle-list').removeClass('view-toggle-list-unselected').addClass('view-toggle-list-selected').addClass('view-toggle-selected');
	} else {
		$('#main-flash').get(0).loadView('gallery');
		$('#view-toggle-gallery').removeClass('view-toggle-gallery-unselected').addClass('view-toggle-gallery-selected').addClass('view-toggle-selected');
	}
}

function loadView(value) {
	if (value != 'list') {
		$('#result-box').fadeOut();
	}
	$('#main-flash').get(0).loadView(value);
	update();
}

    function showDescriptionLB(prop_id,offer_id) {
        $('#LBprop'+prop_id+'_offer'+offer_id).parent().show();
        $('#LBprop'+prop_id+'_offer'+offer_id).show();
        $('#LBprop'+prop_id+'_offer'+offer_id).siblings().hide();
    }
	function modifyLightboxBookURL(el, id) {

        bookUrl = $('#' + id).attr('orighref');

        //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 showProp(value) {
	var url = campaign_name = $('#campaign-name').val() + '/' + value;
	var $lightbox = $('#lightbox');
	if(!$lightbox.hasClass('loading')){
		$lightbox.addClass('loading');
	  $.ajax({
		  url:url,
		  type:'POST',
		  data:'fx=get_lightbox',
		  success: function(content) {
			 $lightbox.html(content);
		  },
		  complete: function() {
			 if($('input#campaign-name').val() == 'starwoodresorts' && ($('input#brand').val() == 'SI' || $('input#brand').val() == 'ST' || $('input#brand').val() == 'WI' || $('input#brand').val() == 'WH')) {
			   //var path = ($('input#brand').val() == 'SI')?'sheraton':'westin';
				switch($('input#brand').val()) {
					case 'SI': var path = '/sheraton'; break;
					//case 'ST': var path = '/stregis'; break;
					case 'WH': var path = '/whotels'; break;
					case 'WI': var path = '/westin'; break;
					default: var path = '';
				}			   
			   $("img[alt='Book Now']").each(function(){
					$(this).attr('src','assets/images/' + path + '/button-booknow.jpg');
			   });
			 }
			 $lightbox.jqmShow();
			 $lightbox.removeClass('loading');
		  }
	  });
	}
}

function updateCity() {	
	if ($('#main-flash').get(0).setDestination)	{
		$('#main-flash').get(0).setDestination();
	}
	update();
}

function getFullURL(filter_prefix,filter,value) {
	if (filter_prefix == null) return false;
	if (filter == null) filter = '';
	if (value == null) value = 0;
	var campaign_name = $('input#campaign-name:hidden').val();
	var brand = $('input#brand:hidden').val();
	var country = $('select#' + filter_prefix + '-country').val();
	var state = $('select#' + filter_prefix + '-state').val();
	var city = $('select#' + filter_prefix + '-city').val();
	if (filter == 'country') {
		country = value;
		state = 0;
		city = 0;
	} else if (filter == 'state') {
		state = value;
		city = 0;
	} else if (filter == 'city') {
		city = value;		
	}
	return	(campaign_name != '0' ? campaign_name + '/' : '') +
			(brand != '' ? brand + '/' : '') +
			(country != '0' ? country + '/' : '') +
			(state != '0' ? state + '/' : '') +
			(city != '0' ? city + '/' : '');
}

function updateFilter(url,filter_prefix,filter_type) {
	if (filter_prefix == null) return false;
	if (filter_type == null) return false;
	if (url == null || url == '') url = 'index.php';
	var filter_return = 0;
	var filter_text = $('input#text-' + filter_type + ':hidden').val();
	var dataString = 'fx=get_' + filter_type + 's';
	$.ajax({
		type: 'POST',
		url: url,
		data: dataString,
		success: function(content) {
			if (content.length > 1) {
				$('select#' + filter_prefix + '-' + filter_type).removeAttr('disabled').html(content);
			} else {
				$('select#' + filter_prefix + '-' + filter_type).attr('disabled','disabled').html('<option value="0">'+filter_text+'</option>');
			}
		},
		complete: function() {
			if ((filter_type == 'state') && ($('select#' + filter_prefix + '-state option').size() <= 1)) {
				updateFilter(url,filter_prefix,'city');
			}
		}
	});
}

function update(){
	var selected = $('#view-toggle').children('.view-toggle-selected').attr('id');
	if(selected == 'view-toggle-list'){
		getResult();
	} else if (selected == 'view-toggle-gallery') {
		getCoverFlow();
	} else {
		getMap();
	}

	var brands = getSelectedBrands();
	var interests = getSelectedInterests();

	var url = getFullURL('explore');
	url += brands.join('-') + '/';
	url += interests.join('-');	
	
	var dataString = 'fx=get_count';
	$.ajax({
		type: 'POST',
		url: url,
		data: dataString,		   
		success: function(data){
        	$('#result-count').html(data);
		}
    });
}
debounce_update = $.debounce(update,1000);

function exploreResults(filter_prefix, url) {
	
	if (filter_prefix == null) filter_prefix = 'explore';
	if (url == null) url = 'http://www.starwoodhotels.com/preferredguest/search/results/standard.html?localeCode=en_US&language=en_US&filterType=Amenity&filterCriteria=Resort';
	
	if (filter_prefix == 'explore') {
		url += '&numberOfRooms=1';
		url += '&numberOfAdults=2';
		url += '&arrivalDate=';
		url += '&departureDate=';
	} else {
		var $filter_country = $('select#' + filter_prefix + '-country');
		var $filter_state = $('select#' + filter_prefix + '-state');
		var $filter_city = $('select#' + filter_prefix + '-city');
		url += '&city=';
		if ($filter_city.val() != 0) url += $filter_city.val().replace('_','+');
		url += '&stateCode=';
		if ($filter_state.val() != 0) url += $filter_state.val().toUpperCase();
		url += '&countryCode=';
		if ($filter_country.val() != 0) url += $filter_country.val().toUpperCase();
		url += '&numberOfRooms='+$('#rooms').val();
		url += '&numberOfAdults='+$('#adults').val();
		url += '&arrivalDate='+$('#checkin').val();
		url += '&departureDate='+$('#checkout').val();
	}
	window.open(url,'_blank');
	
}

function showLightbox() {
	$('.jqmWindow').css('left','50%');
}

function hideLightbox() {
	$('.jqmWindow').css('left','-999%');
	$('.jqmOverlay').remove();
}


function getSelectedBrands(){
	var brands = new Array();
	var tmp = $('#brands-options');
	if(tmp.length > 0){
	$('#brands-options').children('.checkboxes').each(function(){
		$(this).children('.checked').each(function(){
			brands.push($(this).attr('rel'));
		});
	});
	  if (brands.length == 7) {
		brands.push('gx');
	}
	} else {
		if($('input#brand').val() != '') brands.push($('input#brand').val().toLowerCase());
		else brands.push('none');
	}
	return brands;
}

function getSelectedInterests(){
	var interests = new Array();
	$('#interest-options').children('.checkboxes').each(function(){
		$(this).children('.checked').each(function(){
			interests.push($(this).attr('rel'));
		});
	});
	if (interests.length == 0) { interests.push('none'); }
	return interests;
}

function getCoverFlow() {
	var url = getFullURL('explore');
	var brands = getSelectedBrands();
	var interests = getSelectedInterests();

	url = 'xml/properties/' + url;
	url += brands.join('-') + '/';
	url += interests.join('-');	
	if ($('#main-flash').get(0).loadResults) {
		$('#main-flash').get(0).loadResults(url);
	}
}

function getMap(){
	var url = getFullURL('explore');
	var brands = getSelectedBrands();
	var interests = getSelectedInterests();
	url = 'xml/properties/' + url;
	url += brands.join('-') + '/';
	url += interests.join('-');	
	$('#main-flash').get(0).loadResults(url);
}

function getResult(){
	
	var url = getFullURL('explore');
	var brands = getSelectedBrands();
	var interests = getSelectedInterests();
	url = 'xml/properties/' + url;
	url += brands.join('-') + '/';
	url += interests.join('-');		
	
	var $prefix = '';
	var $reserve = $('#widget-reserve');
	if($reserve.css('display') != 'none'){
		$prefix = '#widget-';
	} else {
		$prefix = '#explore-';
	}
	$.post(url,{'fx':'get_content'},function(content){
		var imgPath = 'assets/images/%BRAND%/';
		switch($('#brand').val()){
			case 'SI' : imgPath = imgPath.replace('%BRAND%','sheraton'); break;
			//case 'ST' : imgPath = imgPath.replace('%BRAND%','stregis'); break;
			case 'WH' : imgPath = imgPath.replace('%BRAND%','whotels'); break;
			case 'WI' : imgPath = imgPath.replace('%BRAND%','westin'); break;
			default : imgPath = imgPath.replace('%BRAND%/','');
		}
		$('#result-box').html(content);
		$('#result-box').show();
		$('#property-box').children('.property').each(function(){
			$(this).children('.property-title').mouseenter(function(){
				$('body').css('cursor','pointer');
			});
			$(this).children('.property-title').click(function(){
				var $info = $(this).next('.property-info');
				var $display = $info.css('display');
				$('.property').each(function(){
					$(this).children('.property-info').hide();
					$(this).children('.property-title').children('.collapse-icon').html('<img src="' + imgPath + 'collapse-plus.png" />');
				});
				if($display != 'none'){
					$info.hide();
					$(this).children('.collapse-icon').html('<img src="' + imgPath + 'collapse-plus.png" class="png" />');
				} else {
					$info.show();
					$(this).children('.collapse-icon').html('<img src="' + imgPath + 'collapse-minus.png" class="png" />');
				}
			});
			$(this).children('.property-title').mouseleave(function(){
				$('body').css('cursor','default');
			});
		});
		$('#close').mouseenter(cursorLink);
		$('#close').mouseleave(cursorDefault);
		$('#close').bind('click', closeResult);
	},'html');
	
}

function getDesc(id){
	$('#shortDesc_'+id).hide();
	$('#fullDesc_'+id).show();
}

function cursorLink(){$('body').css('cursor','pointer');}

function cursorDefault(){$('body').css('cursor','default');}

function openResult(){
	$('#close').html('CLOSE');
	$('#close').unbind('click', openResult);
	$('#close').bind('click', closeResult);
	$('#property-box').show();
}

function closeResult(){
	$('#close').html('OPEN');
	$('#close').unbind('click', closeResult);
	$('#close').bind('click', openResult);
	$('#property-box').hide();
}

function photoPopup(url){
  window.open(url,'_blank','height=400,width=600,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no')
}