window.itinerary = new Object();
window.itinerary.collection = new Array();

//blueprint for itin objects
var itinObject={iType:0,recid:0};
var ITINTYPE={LISTING:1,EVENT:2}

window.itinerary.add = function(recid,iType,fn)
{
	itinObject.recid = recid;
	itinObject.iType = iType;
	window.itinerary.ajaxAdd(itinObject);				   
	if(window.itinerary.find(itinObject) == 0)
		this.collection.push(itinObject);
	this.executeCallBack(fn);
	return false;
};

window.itinerary.remove = function(recid,iType,fn)
{
	var index = window.itinerary.find(recid,iType);
	var itinObj = this.collection.splice(index);
	window.itinerary.ajaxRemove({recid:recid,iType:iType});
	this.executeCallBack(fn);
	return false;
};

window.itinerary.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.itinerary.save = function()
{
	Shadowbox.open({content:siteURL + 'trip-builder/?action=save',player:'iframe',width:500,height:500});
}

window.itinerary.load = function()
{
	Shadowbox.open({content:siteURL + 'trip-builder/?action=load',player:'iframe',width:500,height:500});
}

window.itinerary.login = function()
{
	Shadowbox.open({content:siteURL + 'trip-builder/sign-in/',player:'iframe',width:500,height:500});
}

window.itinerary.executeCallBack = function(fn)
{
	call = (typeof call == 'undefined') ? function(x){return x;} : fn;
	return fn;	 	
};

window.itinerary.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.itinerary.add(recid,iType);
		   return false;
	    });//end click 
	});//end each
	
	//bind remove 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.itinerary.remove(recid,iType);
	   return false;
	}); 
	});	
	
	window.itinerary.typeToClass = function(iType){
		switch (iType){//itinListing
			case 1:
				return 'itinListing';
			break;
			case 2:
				return 'itinEvent';
			break;
			case 3:
				return 'itinCoupon';
			break;
		}
	}
	window.itinerary.ajaxAdd = function(itinObject)
	{
		var success = function(responseText, textStatus, XMLHttpRequest)
		{
			$(".itinerary-count").html($(".itinerary-count", responseText).html());
			//if(itinResize) itinResize();
		}
		$("#itineraryList").load(siteURL+'itinerary/ajax/ #itineraryList li', {action:'add',recid:itinObject.recid, iType:itinObject.iType}, success);
		//$('#' + window.itinerary.typeToClass(itinObject.iType) + '_' + itinObject.recid + ' .itinAdd').hide();
		//$('#' + window.itinerary.typeToClass(itinObject.iType) + '_' + itinObject.recid + ' .itinAdded').show();
		$('#' + window.itinerary.typeToClass(itinObject.iType) + '_' + itinObject.recid + ' .itineraryAdd').hide();
		$('#' + window.itinerary.typeToClass(itinObject.iType) + '_' + itinObject.recid + ' .itineraryAdded').show();
		return false;
	}//end ajaxAdd function	
	
	window.itinerary.ajaxRemove = function(itinObject)
	{
		var success = function(responseText, textStatus, XMLHttpRequest)
		{
			$(".itinerary-count").html($(".itinerary-count", responseText).html());
			//if(itinResize) itinResize();
		}
		$("#itineraryList").load(siteURL+'itinerary/ajax/ #itineraryList li', {action:'remove',recid:itinObject.recid, iType:itinObject.iType}, success);
		//$('#' + window.itinerary.typeToClass(itinObject.iType) + '_' + itinObject.recid + ' .itinAdd').show();
		//$('#' + window.itinerary.typeToClass(itinObject.iType) + '_' + itinObject.recid + ' .itinAdded').hide();
		$('#' + window.itinerary.typeToClass(itinObject.iType) + '_' + itinObject.recid + ' .itineraryAdd').show();
		$('#' + window.itinerary.typeToClass(itinObject.iType) + '_' + itinObject.recid + ' .itineraryAdded').hide();
		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();
} 
