/*
$(document).ready(function(){
	$('a.addToListingItin').click(function(){
		
		var id = this.id.split('_')[1];//the listing id
		alert(id);
		ajaxAddItin(id);
		return false;
	});
});
*/
 window.itineary = new Object();
 window.itineary.collection = new Array();
 
 //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);
        };

	 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
	 $(document).ready(function() {
        $('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;
		    });//end click 
		});//end each
		
		//bind reamove each on click button
		 $('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;
        }); 
		});	
		
		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('<span class="addeditin"><a href="javascript:window.itineary.remove('+itinObject.recid+','+itinObject.iType+')" class="iconRemoveItin">Remove From Itinerary</a></span>');
					updateItin();
				},
			   failure: handlerFailuer
			 });
		}//end ajax add function	
		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('<span class="addItin"><a href="javascript:window.itineary.add('+itinObject.recid+','+itinObject.iType+')" class="iconAddItin">Add To Itinerary</a></span>');
					updateItin();
				},
			   failure: handlerFailuer
			 });
		}//end ajax add function	
    });

			
/*
function ajaxAddEventItin(eventid)
{
	var thisUrl  = siteURL+'itinerary/ajax/index.cfm';
	var thisData = 'action=ajax_addItin&recid='+eventid  +'&iType=2'; 
	thisData += '&avoideCatch=' + myTimestamp(); 

	$.ajax({
	   type: "GET",
	   url: thisUrl,
	   data: thisData,
	   success: function (response){
	
			$('#itinEvent_' + itinObject.recid).html('<span class="addeditin"><a href="javascript:removeEvent('+eventid+')" class="iconRemoveItin">Remove From Itinerary</a></span>');
			updateItin();
		},
	   failure: handlerFailuer
	 });
}


function ajaxAddItin(listingid)
{
	var thisUrl  = siteURL+'itinerary/ajax/index.cfm';
	var thisData = 'action=ajax_addItin&recid='+listingid +'&iType=1';
	thisData += '&avoideCatch=' + myTimestamp(); 

	$.ajax({
	   type: "POST",
	   url: thisUrl,
	   data: thisData,
	   success: function (response){
			$('#itin_' + listingid).html('<span class="addeditin"><a class="iconRemoveItin" href="'+ imgRoot+'itinerary/'+'">View Itinerary</a></span>');
			updateItin();
		},
	   failure: handlerFailuer
	 });
}

function removeListing(listingid)
{
	var thisUrl  = siteURL+'itinerary/ajax/index.cfm';
	var thisData = 'action=ajax_update&recid='+listingid +'&itype=1'; 
	thisData += '&avoideCatch=' + myTimestamp(); 

	$.ajax({
	   type: "POST",
	   url: thisUrl,
	   data: thisData,
	   success: function (response){
			$('#itin_' + listingid).html('<a class="iconAddItin" href="javascript:ajaxAddItinJQuery('+listingid+')">Add to Itinerary</a>');
			updateItin();
		},
	   failure: handlerFailuer
	 });
}

function removeEvent(eventid)
{
	var thisUrl  = siteURL+'itinerary/ajax/index.cfm';
	var thisData = 'action=ajax_update&recid='+eventid  +'&itype=2'; 
	thisData += '&avoideCatch=' + myTimestamp(); 

	$.ajax({
	   type: "POST",
	   url: thisUrl,
	   data: thisData,
	   success: function (response){
			$('#itinEvent_' + eventid).html('<a class="iconAddItin" href="javascript:ajaxAddEventItin('+eventid+')">Add to Itinerary</a>');
			//updateItin();
		},
	   failure: handlerFailuer
	 });
}

/*

/* 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();
} 
