window.itineary = new Object();
window.itineary.collection = new Array();

$(document).ready(function() {
	bindCtrls();
   });
 //blueprint for itin objects
 var itinObject = { iType:0, recid:0 };

	window.itineary.add = function(recid,iType,fn) {
		itinObject.recid = recid;
		itinObject.iType = iType;
		window.itineary.ajaxAdd(itinObject);
		if (window.itineary.find(itinObject) == 0) {
			this.collection.push(itinObject);
		}
	
		this.executeCallBack(fn);
	}
	
	window.itineary.remove = function(recid,iType,fn) {
		var index = window.itineary.find(recid,iType);
		this.collection
		this.collection.splice(index);
		window.itineary.ajaxRemove({recid:recid,iType:iType});
		this.executeCallBack(fn);
	};

	//trip builder modification
	window.itineary.removeTrip = function(recid,iType,fn) {
		var index = window.itineary.find(recid,iType);
		this.collection
		this.collection.splice(index);
		window.itineary.ajaxRemoveTrip({recid:recid,iType:iType});
		this.executeCallBack(fn);
	};
	
	window.itineary.removeFromDay = function (recid,iType,linkobject){
		var id = $(linkobject).parent().children('.id').html();
		var type = $(linkobject).parent().children('.itemtype').html();
		var day = $(linkobject).parent().parent().parent().attr('id');
		day = day.replace("day","");
		window.itineary.ajaxRemoveFromDay({recid:id,iType:type,day:day});
		$(linkobject).parent().parent().remove();
	return false;
	}

	
	
	window.itineary.find = function(recid,type){
		//return the index of the object in the collection
		for(var i =0; i < this.collection.length;i++ ){
			var o = this.collection[i];
			if(o.iType == type && o.recid==recid){
				return i;
			}
		}
	return 0;
	}
	window.itineary.executeCallBack = function(fn){
		call = (typeof call == 'undefined') ? function(x){return x;} : fn;
		return fn;	 	
	}
	window.itineary.size = function(){
		return this.collection.length;
	};

	//bind each
	function bindCtrls(){
		$('a.iconAddItin').each(function(){
			$(this).bind("click",function(){		
				var recid =$(this).get(0).id.split('_')[1];
				var type = $(this).get(0).id.split('_')[0];
				
				
				if(type.toUpperCase() =='LISTINGITIN'){
					var iType = 1;
				} 
				else if(type.toUpperCase() =='EVENTITIN'){
					var iType = 2;		   	
				}
				else if(type.toUpperCase() =='COUPONITIN'){
					var iType = 3;		   	
				}
				
				window.itineary.add(recid,iType);
				return false;
			});
		});
		
		$('a.iconRemoveItin').each(function(){
			$(this).bind("click",function(){		
				var recid =$(this).get(0).id.split('_')[1];
				var type = $(this).get(0).id.split('_')[0];
				if(type.toUpperCase() =='LISTINGITIN'){
					var iType = 1;
				} 
				else if(type.toUpperCase() =='EVENTITIN'){
					var iType = 2;
				}
				else if(type.toUpperCase() =='COUPONITIN'){
					var iType = 3;
				}
				window.itineary.remove(recid,iType);
				return false;
			});
		});
		
		//trip builder modification
		$('a.iconRemoveTrip').each(function(){
			$(this).bind("click",function(){		
				var recid =$(this).get(0).id.split('_')[1];
				var type = $(this).get(0).id.split('_')[0];
				if(type.toUpperCase() =='LISTINGITIN'){
					var iType = 1;
				} 
				else if(type.toUpperCase() =='EVENTITIN'){
					var iType = 2;
				}
				else if(type.toUpperCase() =='COUPONITIN'){
					var iType = 3;
				}
				window.itineary.removeTrip(recid,iType);
				return false;
			});
		});
	}
		
		window.itineary.typeToClass = function(iType){
			switch (iType){//itinListing
				case 1:
					return 'itinListing';
				break;
				case 2:
					return 'itinEvent';
				break;
				case 3:
					return 'itinCoupon';
				break;
			}
		}
		
		window.itineary.ajaxAdd = function(itinObject) {
			var thisUrl  = siteURL+'itinerary/ajax/index.cfm';
			var thisData = 'action=ajax_addItin&recid='+itinObject.recid  +'&iType='+itinObject.iType; 
			thisData += '&avoideCatch=' + myTimestamp(); 

			
			$.ajax({
			   type: "GET",
			   url: thisUrl,
			   data: thisData,
			   success: function (response){
						$('#'+window.itineary.typeToClass(itinObject.iType)+'_' + itinObject.recid).html('<a href="javascript:window.itineary.remove('+itinObject.recid+','+itinObject.iType+')" class="iconRemoveItin">Remove From Trip</a>');
			   		if (itinObject.iType != 3) {
						$('.' + window.itineary.typeToClass(itinObject.iType) + '_' + itinObject.recid).parent().html('<a id="listingItin_' + itinObject.recid + '" href="javascript:window.itineary.remove(' + itinObject.recid + ',' + itinObject.iType + ')" class="iconRemoveItin itinListing_' + itinObject.recid + '" title="Remove From Trip Planner"><img src="' + imgRoot + 'shell/listings/added-to.gif" /></a>');
					}						
					//} else {
					//	$('.'+window.itineary.typeToClass(itinObject.iType)+'_' + itinObject.recid).html('<a href="javascript:window.itineary.remove('+itinObject.recid+','+itinObject.iType+')" id="couponItin_'+itinObject.recid+'" title="Remove From Trip Planner">Remove from Trip</a>');
					//}
					updateItin();
				},
			   failure: handlerFailuer
			 });
		}
		
		window.itineary.ajaxRemove = function(itinObject) {
			var thisUrl  = siteURL+'itinerary/ajax/index.cfm';
			var thisData = 'action=ajax_update&recid='+itinObject.recid+'&iType='+itinObject.iType;
			thisData += '&avoideCatch=' + myTimestamp(); 

			$.ajax({
				type: "GET",
				url: thisUrl,
				data: thisData,
				success: function (response){
						$('#'+window.itineary.typeToClass(itinObject.iType)+'_' + itinObject.recid).html('<a href="javascript:window.itineary.add('+itinObject.recid+','+itinObject.iType+')" class="iconAddItin">Add To Trip Planner</a>');
					if (itinObject.iType != 3) {
						$('.' + window.itineary.typeToClass(itinObject.iType) + '_' + itinObject.recid).parent().html('<a id="listingItin_' + itinObject.recid + '" href="javascript:window.itineary.add(' + itinObject.recid + ',' + itinObject.iType + ')" class="iconAddItin itinListing_' + itinObject.recid + '" title="Add To Trip Planner"><img src="' + imgRoot + 'shell/listings/add-to.gif" /></a>');
					}						
					///}else{
					//	$('.'+window.itineary.typeToClass(itinObject.iType)+'_' + itinObject.recid).child().html('<a href="javascript:window.itineary.add('+itinObject.recid+','+itinObject.iType+')" id="couponItin_'+itinObject.recid+'">Add To Trip Planner</a>');
					//}
					updateItin();
				},
				failure: handlerFailuer
			});
		}
		
		/* Trip Builder Modifications */
		window.itineary.ajaxRemoveTrip = function(itinObject) {
			var thisUrl  = siteURL+'itinerary/ajax/index.cfm';
			var thisData = 'action=ajax_update&recid='+itinObject.recid+'&iType='+itinObject.iType;
			thisData += '&avoideCatch=' + myTimestamp(); 

			$.ajax({
				type: "GET",
				url: thisUrl,
				data: thisData,
				success: function (response){
					$('#'+window.itineary.typeToClass(itinObject.iType)+'_' + itinObject.recid).parent().parent().remove();
					updateItin();
				},
				failure: handlerFailuer
			});
		}
		
		window.itineary.ajaxRemoveFromDay = function(itinObject)
		{
			var thisUrl  = siteURL+'itinerary/ajax/index.cfm';
			var thisData = {action:'removeFromDay',recid:itinObject.recid, iType:itinObject.iType, day:itinObject.day, avoidCatch: myTimestamp()};
			$.ajax({
				type: "GET",
				url: thisUrl,
				data: thisData,
				success: function (response){
					updateItin();
				},
				failure: handlerFailuer
			});
			
			
			//$('.' + window.itineary.typeToClass(itinObject.iType) + '_' + itinObject.recid).remove();
			return false;
		}//end ajaxRemove function	




/* This method is used to update a Itin widget with the changes.
 * !important, it will ONLY run if a element #itinListHolder (the widget itin) is present
 */
 
function updateItin(){
	if ($('#itinListHolder').length != 0) {
		$.get(siteURL + '/includes/cftags/itinerary/itineraryDisplay.cfc', {
			method: "updateItin",
			fuse_root: siteURL,
			avoideCache: myTimestamp()
		}, function(data){
			$('#itinListHolder').fadeOut().empty();
			$('#itinListHolder').html(data).fadeIn();
		});
		$.get(siteURL + '/includes/cftags/itinerary/itineraryDisplay.cfc', {
			method: "countItin",
			avoideCache: myTimestamp()
		}, function(data){
			$('#itinCount').empty();
			$('#itinCount').html(data);
			
		});
	}
}

function handleSuccess(response)
{
	document.body.innerHTML = response.responseText;
			return false;
}

function handlerFailuer(response)
{
	alert(response);
}

function myTimestamp(){
    tstmp = new Date();    
    return tstmp.getTime();
} 
