function photoPopup(url) {
	window.open(url,'photosPopUp','width=600,height=416,resizable=no,scrollbars=no,status=no');
}
function callback(item) {
	$('input#search-id').attr('value',item.id);
	$.ajax({
		type: 'POST',
		data: 'fx=get_link&marketid='+item.id,
		success: function(content){
			if(content!=''){
				$('#btn-search').attr('href', content);
				$('#btn-search').attr('target',"_blank"); 
			}
		}
	});	
}
function get_states(){
	var country=$('#country').val();
	$('#search').val('Enter your City/Area');
	$('#btn-search').attr('href', 'javascript:void(0);');
	$('#btn-search').removeAttr('target'); 
	$.ajax({
		type: 'POST',
		data: 'fx=get_states&country='+country,
		success: function(content) {
			$('#state').html(content);
		}
	});
	$.ajax({
		type: 'POST',
		data: 'fx=get_cities&country='+country,
		success: function(content) {
			$('#city').html(content);
		}
	});
}
function get_cities(){
	var country=$('#country').val();
	var state=$('#state').val();
	$('#search').val('Enter your City/Area');
	$('#btn-search').attr('href', 'javascript:void(0);');
	$('#btn-search').removeAttr('target'); 
	$.ajax({
		type: 'POST',
		data: 'fx=get_cities&country='+country+'&state='+state,
		success: function(content) {
			$('#city').html(content);
		}
	});
	if($(this)!=''){
		$.ajax({
			type: 'POST',
			data: 'fx=get_link&country='+country+'&state='+state,
			success: function(content){
				if(content!=''){
					$('#btn-search').attr('href', content);
					$('#btn-search').attr('target',"_blank"); 
				}
			}
		});
	}
}
function get_link(){
	var country=$('#country').val();
	var state=$('#state').val();
	var city=$('#city').val();
	$('#search').val('Enter your City/Area');
	if(country!=''&&city!=''){
		$.ajax({
			type: 'POST',
			data: 'fx=get_link&country='+country+'&state='+state+'&city='+city,
			success: function(content){
				if(content!=''){
					$('#btn-search').attr('href', content);
					$('#btn-search').attr('target',"_blank"); 
				}
			}
		});
	}
}
function get_offer(current_id)
{

	$.ajax({
			type: 'POST',
			data: 'fx=get_offer&current_id='+current_id,
			success: function(content){
				$('#offer_container').fadeOut('1000', function() {
					$('#offer_container').html(content);											  
				});
				$('#offer_container').fadeIn('1000');
				
			}
		});
	return false;
}
$(function(){
	$('input#search').jsonSuggest(propertyData.properties, {maxResults:5, onSelect:callback});
	$('#search').focus(function(){
		$(this).val('');
		$('#country option[value=""]').attr('selected', 'selected');
		$('#state').html('<option value="" selected="selected">State/Province</option>');
		$('#city').html('<option value="" selected="selected">City/Area</option>');
		$('#btn-search').attr('href', 'javascript:void(0);');
		$('#btn-search').removeAttr('target')
	});
	$('.checkIn').focus(function(){
		if($(this).val()=='MM/DD/YYYY'){
			$(this).val('');	
		}
	});
	$('.checkIn').blur(function(){
		if($(this).val()==''){
			$(this).val('MM/DD/YYYY');
		}
	});
	$('#search').blur(function(){
		if($(this).val()==''){
			$(this).val('Enter your City/Area');
		}
	});
	$('#btn-search').click(function(){
			
			 var orig_href=$(this).attr('href');
			 if(orig_href=="javascript:void(0);"){
				alert("Please type in a destination to perform your search");
			 }
			 if($('#in-date').val()!='MM/DD/YYYY' && $('#out-date').val()!='MM/DD/YYYY' && orig_href!='javascript:void(0);'){
				 var in_date=$('#in-date').val();
				 var out_date=$('#out-date').val();
				 inArr=in_date.split('/');
				 /*if(inArr[0]<10){
					temp=parseInt(inArr[0]);
					inArr[0]='0'+temp; 
				 }
				 if(inArr[1]<10){
					temp=parseInt(inArr[1]);
					inArr[1]='0'+temp; 
				 }*/
				 if(!orig_href.match(/&arrivalDate=/i)){
				 	orig_href+='&arrivalDate='+inArr[2]+'-'+inArr[0]+'-'+inArr[1];
				 }else{
					orig_href=orig_href.replace(/&arrivalDate=[^\&]*/, '&arrivalDate='+inArr[2]+'-'+inArr[0]+'-'+inArr[1]);
				 }
				 outArr=out_date.split('/');
				 /*if(outArr[0]<10){
					temp=parseInt(outArr[0]);
					outArr[0]='0'+temp; 
				 }
				 if(outArr[1]<10){
					temp=parseInt(outArr[1]);
					outArr[1]='0'+temp; 
				 }*/
				 if(!orig_href.match(/&departureDate=/i)){
				 	orig_href+='&departureDate='+outArr[2]+'-'+outArr[0]+'-'+outArr[1];
				 }else{
					orig_href=orig_href.replace(/&departureDate=[^\&]*/, '&departureDate='+outArr[2]+'-'+outArr[0]+'-'+outArr[1]);
				 }
				 				 
			 }
			 /*orig_href+='&numberOfChildren='+$('#sel-children').val();*/
			 $(this).attr('href', orig_href);							
	});
/*		$('#dates').datepick({
			showOnFocus: true, 
			rangeSelect:true, 
			showTrigger: '#calImg', 
			dateFormat: 'mm/dd/yyyy', 
			minDate:'0',
			onSelect: function(dates){
				//alert(dates[1]);
				if(dates[0]==dates[1]){
					var indate = dates[0];
					var outdate = dates[1];
					var inmonth = indate.getMonth()+1;
					var inday = indate.getDate();
					var inyear = indate.getFullYear();
					
					outdate.setDate(outdate.getDate()+1);
					var outmonth = outdate.getMonth()+1;
					var outday = outdate.getDate();
					var outyear = outdate.getFullYear();
				}else{
					var indate = dates[0];
					var outdate = dates[1];
					var inmonth = indate.getMonth()+1;
					var inday = indate.getDate()-1;
					var inyear = indate.getFullYear();
					
					var outmonth = outdate.getMonth()+1;
					var outday = outdate.getDate();
					var outyear = outdate.getFullYear();
				}
				
				
				$('#in-date').val(inmonth+'/'+inday+'/'+inyear);
				$('#out-date').val(outmonth+'/'+outday+'/'+outyear);
			}
		});*/
		$('#in-date').datepick({showOnFocus: true, showTrigger: '#calImg', dateFormat: 'mm/dd/yyyy', minDate:0});
		$('#out-date').datepick({showOnFocus: true, showTrigger: '#calImg', dateFormat: 'mm/dd/yyyy', minDate:0});
		
		$('#out-date').change(function(){
			//alert('yes');
			/* var orig_href=$(this).attr('href');
			 if($('#in-date').val()!='MM/DD/YYYY' && $('#out-date').val()!='MM/DD/YYYY'){
				 var in_date=$('#in-date').val();
				 var out_date=$('#out-date').val();
				 inArr=in_date.split('/');
				 if(inArr[0]<10){
					temp=parseInt(inArr[0]);
					inArr[0]='0'+temp; 
				 }
				 if(inArr[1]<10){
					temp=parseInt(inArr[1]);
					inArr[1]='0'+temp; 
				 }
				 orig_href+='&arrivalDate='+inArr[2]+'-'+inArr[0]+'-'+inArr[1];
				 outArr=out_date.split('/');
				 if(outArr[0]<10){
					temp=parseInt(outArr[0]);
					outArr[0]='0'+temp; 
				 }
				 if(outArr[1]<10){
					temp=parseInt(outArr[1]);
					outArr[1]='0'+temp; 
				 }
				 orig_href+='&departureDate='+outArr[2]+'-'+outArr[0]+'-'+outArr[1];				 
			 }
			 orig_href+='&roomOccupancyTotal='+$('#sel-rooms').val();
			 orig_href+='&numberOfAdults='+$('#sel-adults').val();
			 $(this).attr('href', orig_href);				*/		  
		});
});

