// JavaScript Document
$(document).ready(function(){
  // tab-menu hover for IE 6
	$('#tab-nav-container').children('#nav-list').children('li').each(function(){
		$(this).mouseenter(function(){
			$(this).css('background-color','#186ba7');
			$('body').css('cursor','pointer');   
		});
		$(this).mouseleave(function(){
			$(this).css('background-color','#4295d1');
			$('body').css('cursor','default');   
		});
	});
	$('#selectbox-container').children('div.select-box').each(function(){	
		    /*
			$(this).click(function(){
				$(this).css('background-color','#d9eaf6');
				$('#selectbox-container').children('div.option-box').each(function(){
					$(this).hide();
				});
				$(this).next('div').show();
			});
			*/
			$(this).mouseenter(function(){				
				$('#selectbox-container').children('div.option-box').each(function(){
					$(this).hide();
				});
				if(!($(this).children('span').hasClass('disabled'))){
					$(this).css('background-color','#d9eaf6');
					$(this).next('div').show();				
				}
				$('body').css('cursor','pointer');
			});
			$(this).mouseleave(function(){
					$(this).css('background-color','#fff');
					$(this).next('div').hide();
					$('body').css('cursor','default');
			});
			$(this).next('div').mouseenter(function(){
					$(this).prev('div').css('background-color','#d9eaf6');
					$(this).show();					
			});
			$(this).next('div').mouseleave(function(){													
					$(this).hide();
					$(this).prev('div').css('background-color','#fff');
			});
	});
	
	$('#resource-link').children('.select-box:first').mouseenter(function(){
		$(this).css('background-color','#d9eaf6');
		$(this).next('div').show();
		$(this).mouseleave(function(){
					$(this).css('background-color','#fff');
					$(this).next('div').hide();
		});
		$(this).next('div').mouseenter(function(){
				$(this).show();
				$(this).prev('div').css('background-color','#d9eaf6');
		});
		$(this).next('div').mouseleave(function(){
				$(this).hide();
				$(this).prev('div').css('background-color','#fff');
		});
	});
	
	$('#resource-link').children('.option-box').children('.option-list').children('li').each(function(){
	    $(this).children('a').click(function(){
			window.open($(this).attr('rel'),'resource','');
			$('#resource-link').children('.option-box').hide();
			return false;
		});																								      
	});
	
	$('#starpoint').mouseenter(function(){
		$('body').css('cursor','pointer');
	});
	
	$('#starpoint').mouseleave(function(){
		$('body').css('cursor','default');
	});
	
	$('#starpoint').click(function(){
		var vanityUrl = $(this).attr('rel');				
		get_content(vanityUrl);
	});
	
	$('#selectbox-container').children('div#country-option-box').children('ul').children('li.option-list-item').each(function(){
			$(this).children('a').click(function(){
  			  $selectbox = $(this).parent('li').parent('ul').parent('div').prev('div');
			  $selectbox.children('span').html($(this).html());
			  $selectbox.children('span').attr('rel',$(this).attr('rel'));
			  $selectbox.next('div').hide();
			  $('#state').children('span').attr('rel','').html('SELECT STATE/PROVINCE');
			  $('#state').next('div').children('.option-list').html('');			  
			  $('#city').children('span').attr('rel','').html('SELECT AREA/CITY');
			  $('#city').next('div').children('.option-list').html('');			  
			  
			  $data = 'fx=get_states&country='+$(this).attr('rel');
			  $target = 'state';			 
						
			  $.ajax({
			    type:'POST',
			    data: $data,
			    success:function(msg){
			      $('#'+$target).next('div').children('.option-list').html(msg);
		        },
				complete:function(){
				  $('#'+$target).children('span').removeClass('disabled');
				  $('#city').children('span').addClass('disabled');
				  $('#'+$target).next('div').children('.option-list').children('li').each(function(){
					 $(this).children('a').click(function(){
						$selectbox = $(this).parent('li').parent('ul').parent('div').prev('div');
			            $selectbox.children('span').html($(this).html());
						$selectbox.children('span').attr('rel',$(this).attr('rel'));
						$selectbox.next('div').hide();
						$('#city').children('span').attr('rel','').html('SELECT AREA/CITY');
						
						$data = 'fx=get_cities&country='+$('#country').children('span').attr('rel')+'&state='+$(this).attr('rel');
						$target = 'city';
						$selectbox.next('div').hide();
						
						$.ajax({
							   type:'POST',
							   data: $data,
							   success:function(msg){
								   $('#'+$target).next('div').children('.option-list').html(msg);
							   },
							   complete:function(){
								   $('#'+$target).children('span').removeClass('disabled');
								   $('#'+$target).next('div').children('.option-list').children('li').each(function(){
					                 $(this).children('a').click(function(){
						               $selectbox = $(this).parent('li').parent('ul').parent('div').prev('div');
			                           $selectbox.children('span').html($(this).html());
									   $selectbox.children('span').attr('rel',$(this).attr('rel'));
									   $selectbox.next('div').hide();
									   return false;
								     });
									});
							   }
						});
						return false;
					 });	
				  });
				}
			});
			return false;				
		});
	});
	
	$('#selectbox-container').children('div#brand-option-box').children('ul').children('li.option-list-item').each(function(){
			$(this).children('a').click(function(){
				$selectbox = $(this).parent('li').parent('ul').parent('div').prev('div');
	            $selectbox.children('span').html($(this).html());
				$selectbox.children('span').attr('rel',$(this).attr('rel'));
				$selectbox.next('div').hide();
				return false;
			});																												
	});

	$('#country').change(function(){
		var country = $(this).val();
		$.ajax({
			type:'POST',			
			data: 'fx=get_states&country='+country,
			success:function(msg){
				$('#state').html(msg);
			},
			complete:function(){
				$stateOption = $('#state').children('option');
				if($stateOption.length > 1){
					$('#state').show();
				} else {
					$('#state').hide();
				}
			}
		});
	});
	
	/*
	$('#state').change(function(){
		get_cities();
	});
	
	$('#city').change(function(){
		var country = $('#country').val();
		var state = $('#state').val();
		var city = $('#city').val();
		
		$.ajax({
		  type:'POST',
		  data:'fx=get_brands&country='+country+'&state='+state+'&city='+city,
		  success:function(msg){
			  $('#brand').html(msg);
		  }
		});
	});	
    */
	$('#btn_search').click(function(){
		var country = $('#country').children('span').attr('rel');
		var state = $('#state').children('span').attr('rel');
		var city = $('#city').children('span').attr('rel');
		var brand = $('#brand').children('span').attr('rel');
		var fx = '';
		if(brand){
			fx = 'get_brand_meeting';
		} else if(city || state){
			fx = 'get_destination_meeting';					
		} else {
			return false;
		}
		
		$.ajax({
			type:'POST',
			data:'fx='+fx+'&country='+country+'&state='+state+'&city='+city+'&brand='+brand,
			success:function(msg){
				$('#left-contents').html(msg);
			},
			complete:function(){
				$padding = $('#list-title').children('h2').height() - 15;
				$('#pager').css('padding-top',$padding + 'px');								
			}
		});
		return false;
	});
	
	/*
	$('#resource').change(function(){
			window.open($(this).val(),'resource','');			
	});	

	$('#resource').mouseenter(function(){
		$(this).children('option:selected').next('option').attr('selected','selected');
		$(this).children('option:first').attr('disabled','disabled');
	});

	$('#resource').mouseleave(function(){
		$(this).children('option:first').removeAttr('disabled');
		$(this).children('option:first').attr('selected','selected');
	});
	*/

	$('#tab-nav-container').children('#nav-list').children('li.nav-item').each(function(){
			$(this).click(function(){
				var vanityUrl = $(this).attr('rel');				
				get_content(vanityUrl);
			});			
	});	

	var prop = $('input#deep_prop').val();
	var state = $('input#deep_state').val();
	var city = $('input#deep_city').val();
	var brand = $('input#deep_brand').val();
	var fx = '';
	if(prop){
		fx = 'get_property';
	} else if(state || city){
		fx = 'get_destination_meeting';
	} else if(brand){
		fx = 'get_brand_meeting'
	}
	
	if(fx){
		$.ajax({
		  type:'POST',
		  data:'fx='+fx+'&state='+state+'&city='+city+'&brand='+brand+'&id='+prop,
		  success:function(msg){
			  $('#left-contents').html(msg);
		  }
		});
	} else {
		$vanityUrl = $('#vanityUrl').val()?$('#vanityUrl').val():'home';
		get_content($vanityUrl);
	}
});

function get_cities(){
	var country = $('#country').val();
	var state = $('#state').val();
	$.ajax({
		 type:'POST',
		 data:'fx=get_cities&country='+country+'&state='+state,
		 success:function(msg){
			 $('#city').html(msg);
		 }
	});
}

function get_content(vanityUrl){
	var queryStr = 'fx=get_content';	
	$.ajax({
	  type:'POST',
	  url:vanityUrl,
	  data:queryStr,
	  success:function(msg){
		  $('#left-contents').html(msg);
	  }
	});
}

function photoPopup(url) {
	window.open(url,'photosPopUp','width=600,height=416,resizable=no,scrollbars=no,status=no');
}

function goNextPage(perpage){
	var cur_page = $('#cur_page').html();
	var max_page = $('#max_page').html();
	//var last_item = cur_page*10 + 10;
	var last_item = cur_page*perpage + perpage;
	if(cur_page == max_page){
		return false;
	}	
	$('#list-container').children('.property-list').children('li').each(function(){
		$(this).addClass('nodisplay');
	});
	for(var i = cur_page * perpage + 1; i <= last_item; i++)
	{
		$('#list-container').children('.property-list').children('li#list_'+i).removeClass('nodisplay');
	}
	var next_page = parseInt(cur_page) + 1;
	$('#cur_page').html(parseInt(cur_page) + 1);	
	if(cur_page == (max_page - 1)){
		$('#list-container').children('div').children('a.next').children('img').attr({'src':'http://www.starwoodpromos.com/nasppoffers/assets/images/pager_next_deactivated.jpg'});
	}
	if(next_page > 1){
		$('#list-container').children('div').children('a.prev').children('img').attr({'src':'http://www.starwoodpromos.com/nasppoffers/assets/images/pager_prev_activated.jpg'});
	}
	return false;
}

function goPrevPage(perpage){
	var cur_page = $('#cur_page').html();
	var max_page = $('#max_page').html();
	if(cur_page == 1){
		return false;
	}
	cur_page = cur_page - 1;	
	
	$('#list-container').children('.property-list').children('li').each(function(){
		$(this).addClass('nodisplay');
	});
	
	var start = (cur_page*perpage) - (perpage - 1);
	for(start;start<=cur_page*perpage;start++){
		$('#list-container').children('.property-list').children('li#list_'+start).removeClass('nodisplay');
	}
	$('#cur_page').html(cur_page);	
	$('#list-container').children('div').children('a.next').children('img').attr({'src':'http://www.starwoodpromos.com/nasppoffers/assets/images/pager_next_activated.jpg'});
	if(cur_page == 1){
		$('#list-container').children('div').children('a.prev').children('img').attr({'src':'http://www.starwoodpromos.com/nasppoffers/assets/images/pager_prev_deactivated.jpg'});
	}	
	return false;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}