/*
 * review vote
 */
ha.ajax.reviews = {
	notVoted: function(reviewId) {
		//check for the cookie for the review being passed in
		if (document.cookie.match("HATravelerReviewHelpful"+ reviewId )) {
			// cookies matched, return the message
			$j('#reviewMessage_' + reviewId).html($j("#alreadyVotedMessage").html());
			return false;
		}
		//set the cookie
		$j.cookie("HATravelerReviewHelpful" + reviewId, 0, {expires: 18250, path:'/'});
		return true;
	},

	updateVotes: function(reviewId, helpfulVote, totalVote, showThankYou) {
		if(showThankYou) {
			$j('#reviewMessage_' + reviewId).html($j("#thankYouMessage").html());
		}
		votesContent = $j('#helpfulVotesLabel').html() + ' <span class="helpfulVotes">' + helpfulVote + '</span>/<span class="totalVotes">' + totalVote +'</span>';
		$j('#reviewVote_' + reviewId).html(votesContent);
	},
	
	voteHelpful: function(reviewId, helpful) {
		
		if(ha.ajax.reviews.notVoted(reviewId)) {
			$j.ajax({ 
				type: "GET", 
				url: "/ajax/reviews/vote", 
				data: ({helpful : helpful, reviewId: reviewId}),
				dataType: "json",  
	  	        success:function(data){
				 	ha.ajax.reviews.updateVotes(reviewId, data.helpful, data.total, true);
				}, 
				error:function(xhr,err,e){ alert("Error: " + err ); }
			});
		}
	}
};

/*
 * Property
 */
var ajaxInquirySubmittedSuccessfully = new Observer;
var ajaxInquirySubmittedWithErrors = new Observer;
 
ha.ajax.property = {
	getMonth: function(propertyId, systemId, unitId, offset) {
		$j.ajax({
			url: "/propertyAvailabilityNextPrevious.htm?propertyId=" + propertyId + "&uni_id=" + unitId + "&systemId=" + systemId + "&offset=" + offset,
			type: "get", 
			success: function(html){ $j("#availabilityCalendars").html(html);},
			error: function(){alert(ha.strings.errorMessage);}
		});
	},

	getCurrency: function(systemId, propertyId, unitId, currency){
		$j.ajax({
			url: "/propertyCurrencyChange.htm?systemId=" + systemId + "&propertyId=" + propertyId + "&uni_id=" + unitId + "&currency=" + currency,
			type: "get",
			success: function(html){$j("#rates").html(html);}, 
			error: function(){alert(ha.strings.errorMessage);}
		});
	}
};

/*
 * Inquiry
 */
ha.ajax.inquiry = {
	init: function(){
		ha.ajax.inquiry.initInquiryComments();
		var submitViaAjax = false;
		if (jsEnabled.s && $j('#jsEnabled').length > 0) { 
			submitViaAjax = true;
			$j("input[name=jsEnabled]").val("true");
		}
		
		if($j('#propertyInquiryForm').length > 0) {
			
			var now = new Date();
			now = now.getTimezoneOffset()/60;
			$j('#localGMTOffset').val(now);
				
			if(!submitViaAjax || submitViaAjax == 'false') {
				$j('#submitButton').click(function(e) { 
					ha.ajax.inquiry.inquiryStatus(true);
					$j("#propertyInquiryForm").submit();
				});
			} else {
				var ajaxFormOptions = {
				    	success: ha.ajax.inquiry.inquiryComplete,
				    	error: ha.ajax.inquiry.inquiryFailure
					};
				
				$j('#submitButton').click(function(e){
					var comments = $j("#comments").val();
					if (comments.indexOf(ha.strings.propertyCommentsLangTitle) == 0) { $j("#comments").val(""); }
						
					ha.ajax.inquiry.inquiryStatus(true);
				});
				
				$j('#propertyInquiryForm').ajaxForm(ajaxFormOptions);
			}
		}
	},
	
	initInquiryComments: function(){
		if ($j('#comments').length == 0) return;

		var maxChars = $j('#comments').attr('maxlength');
		var hasDefaultTxt = ($j('#comments').val().indexOf(ha.strings.propertyCommentsLangTitle) == 0) ? true : false;
		
		//onload: set the default text and style; then set the counter.
		if ($j("#comments").val() == "") {
			$j("#comments").val(commentsLangTxt.s);
			$j("#comments").css("color","#ccc");
			hasDefaultTxt = true;
		}
		else if (hasDefaultTxt){
			$j("#comments").css("color","#ccc");
		}
		
		//onfocus: clear default text and set font color
		var setAttribute = function(){
			if ($j(this).val().indexOf(ha.strings.propertyCommentsLangTitle) == 0){
				$j(this).val("");
				$j(this).css("color","#333");
			}
		}
		$j('#comments').focus(setAttribute);
		
		$j('#comments').keyup(ha.util.limiter);
		if (hasDefaultTxt) {
			$j('#remainder').text(maxChars);
		}
		else{
			$j('#comments').change(ha.util.limiter).change();
		}
	},
	
	inquiryStatus: function (show) {
    	if (show) {
    		if ($j('#submitButton').length > 0) {
    			$j('#submitButton').onclick = null;
    		}
            
    		$j('#inquiry-status').css("height", $j('#enquiry-form').attr("offsetHeight") - 40 + "px");
    		$j('#inquiry-status').css("visibility","visible");
    		return; // force a return because the debugger jumped down to line 194!!
    	} else {
    		$j('#inquiry-status').css("visibility","hidden");
    	}					
    },
    
	inquiryComplete: function(responseText, statusText){
    	$j("#inquiryReplacementDiv").html(responseText);

		datePickerController.create(); ha.util.datePickControl.linkDates('startDateInput','endDateInput'); 
    	
    	if (statusText == "success" && $j("#jsEnabled").length == 0){
    		if (jQuery.browser.mozilla && navigator.userAgent.toLowerCase().indexOf("mac") != -1) { 
    			$j("#propertyInquiry").css({position:"static"});
    		}
    		$j("#owner-contact-info").css("display","none");
			$j("#propertyInquiry .propertySubHead").css("display","none");
    		$j("#enquiry-form .form").css("width","100%");
    		
    		// fire off any tracking events
    		ajaxInquirySubmittedSuccessfully.fire();

    		// Fire off the inquiry confirmation vbis tracking data
    		submitInqTracker();

			// load iframe ads
			$j("iframe[rel]").each(function(){
				$j(this).attr("src", $j(this).attr("rel"));
			});
    	}
    	else {
    		ha.ajax.inquiry.inquiryError();
    		//force a change to linkdates
    		$j("#startDateInput").change();
    		ha.ajax.inquiry.init();
    		if (ha.settings.omnitureOn){
    			var errs="";
		    	$j(".form-error-inline", "#enquiry-form").each(function(i){
			    	if($j(".body p", this).text() != ""){
			    		errs+=$j(".body p", this).text() + " ";
			    	}
			    });
    		
    			ajaxInquirySubmittedWithErrors.fire(errs);
    		}
    		
    	}
    	       
    	ha.ajax.inquiry.inquiryStatus(false);
    },
    
    inquiryFailure: function() {
    	var newHtml = '<div style="text-align:center">';
    	newHtml += '<p class="formError">' + ha.strings.errorMessage + '</p>';
    	newHtml += '<p>&nbsp;</p>';
    	newHtml +='</div>';
    	$j("#inquiryReplacementDiv").html(newHtml);
    	
    	ha.ajax.inquiry.inquiryStatus(false);
    },
    
    inquiryError: function(){
    	if(!document.getElementById("enquiry-form"))
    		return;
    	// if there is not an error message then suppress the error divs
    	// this occurs when triphomes returns errors that we are handling
    	// at times we want to display our error and suppress the triphome response
    	$j(".form-error-inline", "#enquiry-form").each(function(i){
	    	if($j(".body p", this).text() == ""){
	    		$j(this).css("display","none");
	    	}
	    });
    	// global close popups
    	$j("#enquiry-form").click(function(e){
    		var type = '';
    		
			if($j(e.target).hasClass("form-error-icon")){
				type = "icon";
			}
			
			$j(".form-error-msg", "#enquiry-form").each(function(i){
		    	if(!$j(this).hasClass("hidden")){
		    		$j(this).addClass("hidden");
		    	}
		    });
			
   			if(type == "icon"){
   				$j(e.target).next().removeClass("hidden");
   			}
    	});
    }
};
