$(document).ready(function () {
	Z.Hotel = {
		firstURL : 'post/hotel-post.php',
		staticURL : '',
		ie : 'Microsoft Internet Explorer',
		//ie : 'Netscape',
		dealsURL : 'http://'+document.domain+'/advertise/index2.php',
		contentEl: null,
		contentId: null,		
		selectedLink :0,
		selectedAdvertiser : '',
		browser : (navigator.appName == 'Microsoft Internet Explorer') ? 'ie' : '',
		city1 : null,
		date1 : null,
		date2 : null,
		adults : null,
		btnSearch : null,
		init : function(tab){
			var El = this;
			this.contentEl = tab;
			this.contentId = $(tab)[0].id;
			
			//for testing
			this.firstURL = 'post/hotel-post.php';
			this.staticURL = '';
			//ie = 'Netscape';
			
			var currentTime = new Date();
			currentTime.setDate(currentTime.getDate()+20);
			var month = currentTime.getMonth() + 1;
			var day = currentTime.getDate();
			var year = currentTime.getFullYear();
			if(currentTime.getMonth() > month){
				year++;
			}
			var today = month + "/" + day + "/" + year;
			currentTime.setDate(currentTime.getDate()+2);
			var month2 = currentTime.getMonth() + 1;
			var day2 = currentTime.getDate();
			var year2 = currentTime.getFullYear();
			if(currentTime.getMonth() > month2){
				year2++;
			}
			var today2 = month2 + "/" + day2 + "/" + year2;
			$("#"+this.contentId+' #hdate1').val(today);
			$("#"+this.contentId+' #hdate2').val(today2);
			var try1=false;
			
			El.city1 = $("#"+this.contentId+' #city1');
			El.date1 = $("#"+this.contentId+' #hdate1');
			El.date2 = $("#"+this.contentId+' #hdate2');
			El.adults = $("#"+this.contentId+' #adults');
			El.btnSearch = $("#"+this.contentId+' #search-btn');
			$("#"+this.contentId+' #city1').autocomplete(
				"post/hotel-post.php",
				{
					delay:10,
					minChars:3,
					matchSubset:1,
					matchContains:1,
					cacheLength:10,
					selectFirst: true,
					width:'450px',
					errormsg: 'Enter City'
					,onItemSelect: function(){
						$("#hdate1").focus();
					}
				}
			);
				
			var dates = $("#"+this.contentId+" #hdate1, #"+this.contentId+" #hdate2" ).datepicker({
				defaultDate: "+1w",
				showButtonPanel: true,
				minDate: 0,
				changeMonth: true,
				changeYear: true,
				numberOfMonths: 2,
				
				showOn: "both",
				buttonImage: "http://hotels.asia.com/images/icon-cal.gif",
				buttonImageOnly: true,
				
				onSelect: function( selectedDate ) {
					if(this.id=="hdate1" && !try1){ //try1 add 10days only on the first set of hdate1
						var dd = selectedDate.split('/');
						var tmpdate = new Date(dd[2], parseInt(dd[0],10)-1, dd[1]);
						tmpdate.setDate(tmpdate.getDate()+7);
						var m = tmpdate.getMonth() + 1;
						var d = tmpdate.getDate();
						var y = tmpdate.getFullYear();
						var t = m + "/" + d + "/" + y;
						$("#"+El.contentId+' #hdate2').val(t);
					}
					if(this.id == "hdate1"){
						//try1 = true;
						var option = "minDate",
							instance = $( this ).data( "datepicker" ),
							date = $.datepicker.parseDate(
								instance.settings.dateFormat ||
								$.datepicker._defaults.dateFormat,
								selectedDate, instance.settings );
						dates.not( this ).datepicker( "option", option, date );
					}
				},
				beforeShow: function(input, inst) {
					var dateMin = 0;
					if(input.id === "date2") {
						var dd =  $("#"+El.contentId+' #hdate1').val().split('/');
						var dd2 =  $("#"+El.contentId+' #hdate2').val().split('/');
						var tmpdate = new Date(dd[2], parseInt(dd[0],10)-1, dd[1]);
						tmpdate.setDate(tmpdate.getDate()+1);
						dateMin = tmpdate;
							
						//added to always show current seleted date month at leftside------------------------------
						inst.drawMonthEx = 0;
						if(parseInt(dd[2],10) < parseInt(dd2[2],10)){
							inst.drawMonthEx = -1;
							inst.txtboxclick = true;
						}else if(parseInt(dd[0],10) < parseInt(dd2[0],10)){
							inst.drawMonthEx = -1;
							inst.txtboxclick = true;
						}
						//---------------------------------------------------------------------------------------------------------

					}
					return {
						minDate: dateMin
					}; 
				},
				beforeShowDay: function(date) {
					var dd1 = dates[0].value.split('/');
					var dd2 = dates[1].value.split('/');
					
					var dates1 = [new Date(dd1[2], parseInt(dd1[0],10)-1, dd1[1]), new Date( dd2[2],  parseInt(dd2[0],10)-1,  dd2[1])];
					if (dates1[0] <= date && dates1[1] >= date) {
					
						return [true, 'ui-state-highlight'];
					}else{
					
					}
					//ui-datepicker-close
					return [true, ''];
				},
				onClose: function(input, inst){
					if(!date2Popup){
						date2Popup = true;
						if(inst.id == 'hdate1'){
							setTimeout(function(){
								$("#"+El.contentId+" #hdate2").datepicker( "show" );
							},300);
						}
					}
				}
			});
			
			var date2Popup = false;
			
			$("#"+El.contentId+' .search-back').click(function(){
				$("#"+El.contentId+' .trip_inside3').slideUp('fast',function(){
					$("#"+El.contentId+' .trip_inside').fadeIn('slow');
				});
				return false;
			});
			$("#"+El.contentId+' #hdate1').blur(function(){
				var currentTime = new Date();

				var dd = $("#"+El.contentId+' #hdate1').val().split('/');
				var tmpdate = new Date(dd[2], parseInt(dd[0],10)-1, dd[1]);
				
				if(tmpdate<currentTime) $("#"+El.contentId+' #hdate1').val(today);
			});
			$("#"+El.contentId+' #hdate2').blur(function(){
				var currentTime = new Date();
			
				var dd = $("#"+El.contentId+' #hdate2').val().split('/');
				var tmpdate = new Date(dd[2], parseInt(dd[0],10)-1, dd[1]);
				
				if(tmpdate<currentTime) $("#"+El.contentId+' #hdate2').val(today2);
			});
			
			$("#"+El.contentId+' #city1').dblclick(function(){
				$(this).val('');
			});
			
			$("#"+El.contentId+' #city1').click(function(){
				if($(this).val()!='Enter a City or Airport Code'&&$(this).val()!=''){
					$(this).focus();
					$(this).select();
				}
			});
			
			$("#"+El.contentId+' #search-btn').click(function(){
				var exit = false;
			   $("#"+El.contentId+' .vtip').each(function(){
					
					if($(this).is(":visible")){
						$(this).fadeOut('fast',function(){
							$(this).fadeIn('fast');
						});
						exit = true;
					}
			   });
			   if(exit){
				   exit = false;
				   return exit;
			   }
				var city1 = $("#"+El.contentId+' #city1').val();
				date1 = $("#"+El.contentId+' #hdate1').val();
				date2 = $("#"+El.contentId+' #hdate2').val();
				var adults = $("#"+El.contentId+' #adults').val();
				
				var rooms = $("#"+El.contentId+' #rooms').val();
				
				if(city1=='' ||city1.length<3||city1=='type in City'){
					$("#"+El.contentId+' #city1').fadeOut();
					$("#"+El.contentId+' #city1').fadeIn();
					$("#"+El.contentId+' #city1').focus();
					$("#"+El.contentId+' #city1').select();
					return false;
				}
				
				if(!globalObj.isDate(date1)){
					date1 = today;
					$("#"+El.contentId+' #hdate1').val(date1);
					$("#"+El.contentId+' #hdate1').focus();
					$("#"+El.contentId+' #hdate1').select();
				}
				if(!globalObj.isDate(date2)){
					date2 = today;
					$("#"+El.contentId+' #hdate2').val(date2);
					$("#"+El.contentId+' #hdate2').focus();
					$("#"+El.contentId+' #hdate2').select();
				}

				var d1 = new Date(date1.split('/')[2],date1.split('/')[0]-1, date1.split('/')[1]);
				var d2 = new Date(date2.split('/')[2],date2.split('/')[0]-1, date2.split('/')[1]);

				if((d2.getTime()-d1.getTime())<0){
					$("#"+El.contentId+' #hdate1').val(date2);
					$("#"+El.contentId+' #hdate2').val(date1);
					date1 = $("#"+El.contentId+' #hdate1').val();
					date2 = $("#"+El.contentId+' #hdate2').val();
				}
				
				if((d2.getTime()-d1.getTime())==0){
					$("#"+El.contentId+' #errorEqual').show();
					$("#"+El.contentId+' #hdate2').focus();
					setTimeout(function(){
						$("#"+El.contentId+' #errorEqual').fadeOut('slow');
					},3500);
					return false;
				}

				El.to = city1;
				El.search();
				return false;
		   });
		   
			var sArray = new Array();
			var winId = new Array();
			var supplierClick = new Array();
			
			$("#"+El.contentId+' #compare').click(function(){
				El.compare();
				return false;
			});
			
			$("#"+El.contentId+' #edit-btn').click(function(){
				$("#"+El.contentId+' .trip_inside3').slideUp();
				$("#"+El.contentId+' .trip_inside1').slideDown();
				$("#"+El.contentId+' #cancel-btn').show();
				return false;
			});
			$("#"+El.contentId+' #cancel-btn').click(function(){
				$("#"+El.contentId+' .trip_inside1').slideUp();
				$("#"+El.contentId+' .trip_inside3').slideDown();
				return false;
			});
			
			$("#"+El.contentId+' .error-notification').click(function(){
				$(this).fadeOut();
				return false;
			});
			
			$("#"+this.contentId+" #city1").focus();
		},
		
		search: function (){
			var El = this;
			var adults = $("#"+El.contentId+' #adults').val();
					
			var rooms = $("#"+El.contentId+' #rooms').val();
			var exit = false;
		   $('.vtip').each(function(){
				
				if($(this).is(":visible")){
					$(this).fadeOut('normal',function(){
						$(this).fadeIn('normal');
					});
					exit = true;
				}
		   });
		   if(exit){
			   exit = false;
			   return exit;
		   }
		   $("#"+El.contentId+' #div-compare-btn').hide();
			$("#"+El.contentId+' .front-banner').css({background: 'url("http://fliasia.s3.amazonaws.com/widget-wide/images/advertisers/desc-trip-mama2.png") no-repeat scroll 0 0 transparent'});
			
		   var city1 = $("#"+El.contentId+' #city1').val();
		   $('#trip-to').html(city1.split(',')[0]);
		   
		   $('#trip-to-date').html('Checkin: '+$("#"+El.contentId+' #hdate1').val()+'&nbsp;&nbsp;'+'Checkout: '+$("#"+El.contentId+' #hdate2').val());
			var children = '';
		   $("#"+El.contentId+' #search-result').html('');
		
			$("#"+El.contentId+' .trip_inside2').show();
			$("#"+El.contentId+' .trip_inside1').slideUp();
		
			$("#"+El.contentId+' #edit-btn').hide();
			$("#"+El.contentId+' #btn-back').hide();
			selectedLink = 0;
		
			$("#"+El.contentId+' #block-bar-title').html('<b>Checking fares from top travel sites...</b>Please be patient while we update your new itinerary');
		
			$("#"+El.contentId+' #div-btn-back').hide();
			$("#"+El.contentId+' #your-trip').html('Your '+city1.split('-')[0]+' Trip:');
			$("#"+El.contentId+' #your-trip-date').html('Checkin: '+date1+'&nbsp;&nbsp;'+'Checkout: '+date2);
			var ttt = ' Hotels ';

			$("#"+El.contentId+' #sub-heading-2-title').html('Compare '+city1.split('-')[0]+ttt);
			$("#"+El.contentId+' #selected').html('<div id="" style="" class="box"><br class="clear" /></div><div id="" style="" class="box"><br class="clear" /></div><div id="" style="" class="box"><br class="clear" /></div><div id="" style="" class="box"><br class="clear" /></div>');
			$("#"+El.contentId+' #msg-compare1').hide();
			$("#"+El.contentId+' .flight-info').hide();
			$("#"+El.contentId+' #trip-title').html('Your '+city1+' Trip');
			$("#"+El.contentId+' #l-from-to').html('City: '+city1);
			$("#"+El.contentId+' #l-tickets').html('Checkin: '+date1+' Checkout: '+date2);
			
			$("#"+El.contentId+' #l-travellers').html('Travellers: '+adults+' Rooms: '+rooms);
			
			$("#"+El.contentId+' #od').html("Your "+city1+' Trip');
			
			$("#"+El.contentId+' #trip_inside2').show();
			
			var xId = 0;
			
			var firstData = '';
			var firstDataCount = '';
			var guid = globalObj.guidGenerator();
			function setFirst(res){
				var div = '<div id="" style="height: 60px;background:url(http://fliasia.s3.amazonaws.com/fliasia-2/images/selectbutton-small.png) #EFEFEF no-repeat center center !important;" class="result-box box"></div>';
					div += '<div id="" style="height: 60px;background:url(http://fliasia.s3.amazonaws.com/fliasia-2/images/selectbutton-small.png) #EFEFEF no-repeat center center !important;" class="result-box box"></div>';
					div += '<div id="" style="height: 60px;background:url(http://fliasia.s3.amazonaws.com/fliasia-2/images/selectbutton-small.png) #EFEFEF no-repeat center center !important;" class="result-box box"></div>';
					div += '<div id="" style="height: 60px;background:url(http://fliasia.s3.amazonaws.com/fliasia-2/images/selectbutton-small.png) #EFEFEF no-repeat center center !important;" class="result-box box"></div>';
					
				$("#"+El.contentId+' #selected').html(div);
				//for select site--
				$("#"+El.contentId+' .box').click(function(){
					$($('#search-result .result-box .result-box-icon:visible')[0]).trigger('click');
				});
				//----
				$("#"+El.contentId+' #msg-compare1').show();
				var ret = jQuery.parseJSON(res);
				$("#"+El.contentId+' #edit-btn').show();
				$("#"+El.contentId+' #btn-back').show();
				$("#"+El.contentId+' .trip_inside2').hide();
				
				$("#"+El.contentId+' .trip_inside1').hide();
				
				$("#"+El.contentId+' .trip_inside3').show();
				$("#"+El.contentId+' #icon-arrow').show();
				$("#"+El.contentId+' .edit-btn').show();
				
				var pattern = new RegExp(/\(([^}]+)\)/);
				
				if(res.indexOf('Please correct the items below in red')<0){
					var child = ret.channel.supplier;
					firstDataCount = $(child).length;
					if($(child).length <1){
						$("#"+El.contentId+' #div-compare-btn').hide();
						$("#"+El.contentId+' .front-banner').css({background: 'url("http://fliasia.s3.amazonaws.com/widget-wide/images/advertisers/desc-trip-mama2.png") no-repeat scroll 0 0 transparent'});
						$("#"+El.contentId+' #search-result').append('<span style="text-align: left;"> <h2>No results found.</h2></span>');
						$("#"+El.contentId+' #block-bar-title').html('<b></b>');
					}else{
						$("#"+El.contentId+' #div-compare-btn').show();
						$("#"+El.contentId+' .front-banner').css({background: 'url("http://fliasia.s3.amazonaws.com/widget-wide/images/desc-trip-mama6.png") no-repeat scroll 0 0 transparent'});
						$("#"+El.contentId+' #div-btn-back').show();
						$("#"+El.contentId+' #block-bar-title').html('<b>Select 3 or more site to compare</b>Pick from your favorite travel site');
						
						var country = city1.split('-');
						country = country[0].split(',');
						country = country[country.length-1];
						country = country.replace(/^\s+|\s+$/g,"");
						
						var pop_url = window.location.protocol+'//'+document.domain+"/loader-bw.php?template="+encodeURIComponent(category_name)+"&country="+encodeURIComponent(country)+"&widget_type=hotel&memberid="+memberid+"&visitor="+guid+"&to="+encodeURIComponent(city1);
						
						xId = globalObj.displayResult(El,child,'Hotels',xId,'search-result',pop_url);
					}
					$("#"+El.contentId+' #search-result input[type=submit]').fadeOut('normal');
					if($("#"+El.contentId+' .check').length<3){ //automatic check
						$("#"+El.contentId+' .check').trigger('click');
						$("#"+El.contentId+' #block-bar-title').html('<b>You are now ready to compare.</b>');
					}
				}else{
					$("#"+El.contentId+' #search-result').append('<span style="text-align: left;"> <h2>No results found.</h2></span>');
					$("#"+El.contentId+' .edit-btn').show();
					$("#"+El.contentId+' #div-btn-back').show();
				}
			}
			function getFirst(){
				return $.ajax({
				  url: El.firstURL,
				  data:{
						q:0,
						city1:city1,
						date1:date1,
						date2:date2,
						adults:adults,
						children:'',
						rooms:rooms
					},
					success: function(res1){
						firstData = res1;
						return;
						
					}
				});
			}
			
			function done(){
				setFirst(firstData);
			  
				var c = city1.split(' - ')[0];
				$("#"+El.contentId+' #sec-hotels').html(""+c+"<b> Hotels</b>("+firstDataCount+') sites found');
				
				globalObj.setResultBox(El);
				
		   }
		
			$.when( getFirst())
			   .then(done)
			   .fail(function(){
			});
			
			return false;
	   },
	   compare: function(el){
			var El = el!=undefined?el:this;
			$('#more-supplier').hide();
			var city1=$("#"+El.contentId+' #city1').val();
			$('#trip-to').html(city1.split(',')[0]);
	   
			$('#trip-to-date').html('Checkin: '+$("#"+El.contentId+' #hdate1').val()+'&nbsp;&nbsp;'+'Checkout: '+$("#"+El.contentId+' #hdate2').val());
			
			globalObj.compare(El);
	   }
	}
});

