/**
 * The BookingBuddy Deals lib module
 * Exists primarily to carve out a namespace for BBD JS functions
 */
var BBDeals = {

	
 	/**
	 * Handles the search from filter list
	 * Assumes that at this point we have both destination and traveltype, so only end in /deals/
	 * @param string id Name of the element that stores the value
	 */
	handleFilterSearch: function(id) {

		var e = $j("#"+id);
		
		if (!e.length > 0) {
			return;
		}
		
		var url_piece = e.val();
		if ($j.emptyString(url_piece)) {
			return;
		}
		
		var url = '/travel-deals/' + url_piece + '/deals/?filter=' + id;
		window.location = url;
	}

	
};

$j(document).ready(function() {
	
	// Deals Page
	if($j("#container.dealsPageTemplate").length > 0) {
		
		$j("#left_nav ul li").each( function() {
			$j(this).hover( function(e) {
				$j(this).addClass("highlight");
			}, function(e) {
				$j(this).removeClass("highlight");
				
			});
		});
		$j("#filterResults").submit( function(e) { 
			e.stopPropagation();
			e.preventDefault();
			BBDeals.handleFilterSearch("filterResultsType");
			return false;
		});
		$j("#filterBy").change( function(e) { 
			$j("#filterResultsType").val($j("#filterBy").val());
			$j("#filterResults").submit();
		});
		
		// Deal listings
		$j(".deal_list_item").each( function(e) {
			var _deal_list_id = $j(this).attr("id").split("_");

			// Get the ID
			var _id = _deal_list_id[1];

			// Get the CPC code
			var _code = _deal_list_id[2];
			
			// Get the title
			var _title = $j(this).find("span.dealTitle").html();
			_title = _title.replace(/\'/g,'');
			
			// Get the URL
			var _link = $j(this).find("td.deal a");
			var _url = _link.attr("href");
			
			$j(this).click( function(e) {
				var _track_click = true;
				if(!$j.emptyString(_url)) {
					_track_click = openDealWindow(_url, this); 
				}
				if(_track_click) {
					omnitureDealClick(this, "bbdn_" + _id, _title, _code);
				}
				e.stopPropagation();
				e.preventDefault();
				return false;
			});
			$j(_link).click( function(e) {
				var _track_click = true;
				if(!$j.emptyString(_url)) {
					_track_click = openDealWindow(_url, this); 
				}
				if(_track_click) {
					omnitureDealClick(this, "bbdn_" + _id, _title, _code);
				}
				e.stopPropagation();
				e.preventDefault();
				return false;
			});
		});
		
		
		// Suggestion forms
		$j("form.dealSearch").each( function() {

			$j(this).smartFormFactory( {
				submitHandler: function(form ,target, e) {
					e.stopPropagation();
					e.preventDefault();
					var _input = $j(form).find("input.locationSuggest");
					var _smartElement = $j.data(_input.get(0), "smartElement");
					var _hidden_id = "#" + _input.attr("id") + "_hidden";
					var _val = $j(_hidden_id).val();
					if($j.emptyString(_val)) {
						_val = _smartElement.value();
						if(_val === _smartElement.defaultVal) {
							_val = null;
						}
					}
					if(!$j.emptyString(_val)) {
						_smartElement.clearError();
						var url = '/travel-deals/' + _val + '/travel/deals/?s=1';
						window.location = url;
					} else {
						_smartElement.valid = false;
						_smartElement.showError();
						_input.blur();
					}
					return false;
				}
			});
			$j.data(this, "smartForm").debug();

			// enable auto submit?
			// use <div class="hide" id="disable_auto_submit">true</div>
			// in the bbd_destination_suggest chunk to disable
			if ($j("#disable_auto_submit").length < 1) {
				$j("form.dealSearch input.locationSuggest").bind('LocationSuggest:valid', function() {
					BBDebug.log(" LocationSuggest:valid triggered -- submitting suggestion form");
					var _hidden_id = "#" + $j(this).attr("id") + "_hidden";
					var _val = $j(_hidden_id).val();
					var url = '/travel-deals/' + _val + '/travel/deals/?s=1';
					window.location = url;

				} );
			}
			
		});
	}

});
