// JavaScript Document (jQuery)

var splash = '';

/***
*
* Functions
*
*/

function getFullURL(filter,value) {
	if (filter == null) filter = '';
	if (value == null) value = 0;
	var lang_code=$('input#lang_code').val();
	var hotel_type = (filter == 'hotel_type') ? value : $('select#hotel_type').val();
	var brand = (filter == 'brand') ? value : $('select#brand').val();
	var country = $('select#country').val();
	var state = $('select#state').val();
	var city = $('select#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	lang_code+'/'+
			(hotel_type !='0' ? hotel_type + '/' : '') +
			(brand != '0' ? brand + '/' : '') +
			(country != '0' ? country + '/' : '') +
			(state != '0' ? state + '/' : '') +
			(city != '0' ? city + '/' : '');
}
function nothing(){
  var myfont = { src:'http://www.starwoodpromos.com/openingoffers-new/templates/default/assets/flash/eidetic.swf' };
  sIFR.activate(myfont);
  sIFR.replace(myfont, {
      selector:'.sifr-h1',
      wmode:'transparent',
	  css:['.sIFR-root{color:#4882d0;font-size:28px;}']
  });
  sIFR.replace(myfont, {
      selector:'.sifr-hotel-name',
      wmode:'transparent',
	  css:['.sIFR-root{color:#4882d0;font-size:26px;}']
  });
	
}
function updateFilter(url,filter_type) {
	if (filter_type == null) return false;
	var lang_code=$('input#lang_code').val();
	if (url == '') url = 'index.php';
	var filter_text = $('input#text-' + filter_type + ':hidden').val();
	var dataString = 'fx=get_' + filter_type + 's';
	if(lang_code=='en'){
		if(filter_type=='state'){
			var dataString = 'fx=get_' + filter_type + 's' + '&state='+ $('select#state').val();
		}else if(filter_type=='city'){
			var dataString = 'fx=get_' + filter_type + 's' + '&state='+ $('select#state').val() + '&city='+$('select#city').val();
		}else if(filter_type=='brand'){
			var dataString = 'fx=get_' + filter_type + 's' + '&state='+ $('select#state').val() + '&city='+$('select#city').val();
		}else if(filter_type=='hotel_type'){
			var dataString = 'fx=get_' + filter_type + 's' + '&state='+ $('select#state').val() + '&city='+$('select#city').val();
		}else if(filter_type=='country'){
			var dataString = 'fx=get_' + filter_type + 's';
		}
	}
	$.ajax({
	  type: 'POST',
	  url: url,
	  data: dataString,
	  success: function(content) {
		if (content.length > 1) {
			$('select#' + filter_type).removeAttr('disabled').html(content);
		} else {
			$('select#' + filter_type).attr('disabled','disabled').html('<option value="0">'+filter_text+'</option>');
		}
	  }
	});
}

function updateContent(url) {
	if (url == null) return false;
	var lang_code=$('input#lang_code').val();
	if (url == '') url = 'index.php';
	var dataString = 'fx=get_content';
	if(lang_code=='en'){
		var dataString = 'fx=get_content&state=' + $('select#state').val() + '&city=' + $('select#city').val() + '&offer=' + $('select#offer_type').val() + '&region=' + $('select#region').val();
	}else{
		var dataString = 'fx=get_content'+'&region='+$('select#region').val() + '&offer=' + $('select#offer_type').val() ;
	}
	if (splash=='') splash = $("#column-right").html();
    $.ajax({
		type: 'POST',
		url: url,
		data: dataString,
		success: function(content) {
		  $("#column-right").html(content).show();
		},
		complete: function(){
		  $('#column-right').removeAttr('style');
		  newHeight=$('#column-right').height();
		  //alert(newHeight);
		  if(newHeight<$('#column-left').height()){
			  $('#column-right').height($('#column-left').height());
		  }
		}
	});
}

function showProperty(propid, lang) {
	var url = lang+'/'+propid;
	var dataString = 'fx=get_content';
	if (splash=='') splash = $("#column-right").html();
    $.ajax({
		type: 'POST',
		url: url,
		data: dataString,
		success: function(content) {
		  $("#column-right").html(content).show();
		}
	});	
}

function closeResults() {
	$('#column-right').html(splash);
	$('select#city').val(0);
	$('select#state').val(0);
	$('select#country').val(0);
	$('select#brand').val(0);
	$('select#hotel_type').val(0);
	
}

/***
*
* Page Events
*
*/

$(function() {
  $('select#language-selection').change(function() {
	var url = 'http://www.starwoodpromos.com/openingoffers-new/';
	url+=$(this).val();
	top.location = url;
  });

  $('select#region').change(function(){
	var lang_code=$('input#lang_code').val();
	    url = lang_code;
		dataString = 'fx=get_countrys_by_region&region='+$(this).val();
	$.ajax({
	  type: 'POST',
	  url: url,
	  data: dataString,
	  success: function(content) {
		if (content.length > 1) {
			$('select#country').removeAttr('disabled').html(content);
			$('select#state').attr('disabled','disabled').html('<option value="0">'+$('input#text-state:hidden').val()+'</option>');
			$('select#city').attr('disabled','disabled').html('<option value="0">'+$('input#text-city:hidden').val()+'</option>');
			$('select#brand').attr('disabled','disabled').html('<option value="0">'+$('input#text-brand:hidden').val()+'</option>');
			$('select#hotel_type').attr('disabled','disabled').html('<option value="0">'+$('input#text-hotel_type:hidden').val()+'</option>');
		} else {
			$('select#country').attr('disabled','disabled').html('<option value="0">'+$('input#text-region:hidden').val()+'</option>');
		}
	  }
	});
		
  });
  $("select#country").change(function() {
	var url = getFullURL('country',$(this).val());	
	//alert(url);
	//alert(lang_code);
	updateFilter(url,'state');
	updateFilter(url,'city');
	updateFilter(url,'brand');
	updateFilter(url,'hotel_type');
	//updateContent(url);
	return false;
  });
  $('select#state').change(function() {
	var url = getFullURL('state',$(this).val());								  
	updateFilter(url,'country');
	updateFilter(url,'city');
	updateFilter(url,'brand');
	updateFilter(url,'hotel_type');
	//updateContent(url);
	return false;
  });
  $('select#city').change(function() {
	var url = getFullURL('city',$(this).val());
	updateFilter(url,'country');
	updateFilter(url,'state');
	updateFilter(url,'brand');
	updateFilter(url,'hotel_type');
	//updateContent(url);
	return false;
  });
  $('select#brand').change(function() {
	var url = getFullURL('brand',$(this).val());
	updateFilter(url,'country');
	updateFilter(url,'state');
	updateFilter(url,'city');
	updateFilter(url,'hotel_type');
	//updateContent(url);
	return false;
  });
  $('select#hotel_type').change(function() {
	var url = getFullURL('hotel_type',$(this).val());
	updateFilter(url,'country');
	updateFilter(url,'state');
	updateFilter(url,'city');
	updateFilter(url,'brand');
	//updateContent(url);
	return false;
  });
  $('#button-search').click(function() {
	updateContent(getFullURL());
	return false;
  });
  
  if($('#column-left').height()<480){
	  $('#column-left').height(480);
  }
  if($('#column-right').height()<$('#column-left').height()){
	  $('#column-right').height($('#column-left').height());
  }
/* if($('#column-right').height()>$('#column-left').height()){
	  diff=$('#column-right').height()%$('#column-left').height()+10;
	  $('#main').height($('#column-right').height()+diff);
	  footer_top=10+diff;
	  $('#footer').css('margin-top', footer_top+'px');
  }*/
  //alert($('#column-left').height());
  //setTimeout('nothing()', 1000);
  
});
  var myfont = { src:'templates/default/assets/flash/eidectic.swf' };
  sIFR.activate(myfont);
  sIFR.replace(myfont, {
      selector:'.sifr-h1',
      wmode:'transparent',
	  css:['.sIFR-root{color:#4882d0;font-size:28px;}']
  });
  sIFR.replace(myfont, {
      selector:'.sifr-hotel-name',
      wmode:'transparent',
	  css:['.sIFR-root{color:#4882d0;font-size:24px;}']
  });
