//bind each
$(document).ready(function() {
	bindCtrls();
});

if(typeof window.itinerary == 'undefined'){
	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,iType:iType};
		window.itineary.ajaxAdd(itinObject);
		if (window.itineary.find(itinObject) == -1) {
			window.itineary.collection.push(itinObject);
		}
		this.executeCallBack(recid,iType,fn);
	}
	
	window.itineary.remove = function(recid,iType,fn) {
		var index = window.itineary.find(recid,iType);
		if(index > 0){
			itineary.collection.splice(index);
		}
		window.itineary.ajaxRemove({recid:recid,iType:iType});
		this.executeCallBack(recid,iType,fn);
	};
	
	window.itineary.find = function(recid,type){
		//return the index of the object in the collection
		for(var i =0; i < window.itineary.collection.length;i++ ){
			var o = window.itineary.collection[i];
			if(o.iType == type && o.recid==recid){
				return i;
			}
		}
	return -1;
	}

/* NEW-----------------------------		*/		
	window.itineary.executeCallBack = function(recid,itype,fn){
		if(typeof fn == 'undefined')
			return;
		else
			return fn(recid,itype);	 	
	}
/* NEW-----------------------------		*/			
	
	window.itineary.size = function(){
		return window.itineary.collection.length;
	};

/* NEW-----------------------------		*/		
	function bindCtrls(){
		$('a.iconAddItin, a.textAddItin').each(function(){
			$(this).bind("click", function(){
				var urlParams = window.itineary.getUrlParams($(this));
				window.itineary.add(urlParams.recid,urlParams.iType);
				return false;
			});
		});
		
		$('a.iconRemoveItin, a.textRemoveItin').each(function(){
			$(this).bind("click", function(){
				var urlParams = window.itineary.getUrlParams($(this));
				window.itineary.remove(urlParams.recid, urlParams.iType);
				return false;
			});
		});
/* NEW-----------------------------		*/				
		
		window.itineary.typeToClass = function(iType){
			switch (parseInt(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){
					//eval('var rtn = ' + response);
					window.itineary.updateLink(itinObject,"ADDED");
					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){
					//eval('var rtn = ' + response);
					window.itineary.updateLink(itinObject,"REMOVED");
					updateItin();
				},
				failure: handlerFailuer
			});
		}
/* NEW-----------------------------		*/		
		window.itineary.updateLink = function(itinObject, action){
			var c = window.itineary.typeToClass(itinObject.iType) + '_' + itinObject.recid;
			var linkText = '';
			var linkclass = $("#"+c).children("a").attr("class");
			if (linkclass == "textAddItin") { 
				linkclass = 'textRemoveItin';
				linkText = 'remove from pack';
			} else if (linkclass == "iconAddItin") {
				linkclass = 'iconRemoveItin';
				linkText = 'remove from pack';
			} else if (linkclass == "textRemoveItin") {
				linkclass = 'textAddItin';
				linkText = 'add To pack';
			} else if (linkclass == "iconRemoveItin") {
				linkclass = 'iconAddItin';
				linkText = 'add To pack';
			}
			if (action == "ADDED") 
				$('#'+c).html('<a href="javascript:window.itineary.remove(' + itinObject.recid + ',' + itinObject.iType + ')" class="'+linkclass+'">'+linkText+'</a>');
			else 
				$('#'+c).html('<a href="javascript:window.itineary.add(' + itinObject.recid + ',' + itinObject.iType + ')" class="'+linkclass+'">'+linkText+'</a>');
		}
/* NEW-----------------------------		*/				
		window.itineary.getUrlParams = function(link){
			//get the link
			var url = ($(link).attr('href').split("?", 2)[1] || "").split("#")[0].split("&") || [];
			var urlParam = new Object();
			//extract out the url paramaters
			//duplicates are overwritten       
			for (var i = 0; i < url.length; i++) {
				var arg = url[i].split("=");
				urlParam[arg[0]] = unescape(arg[1]);
			}
			return urlParam;
		}
	}

/* 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(rtn){
	if ($('#couponHolder').length != 0) {
		$.get(siteURL + '/includes/cftags/itinerary/itineraryDisplay.cfc', {
			method: "updateItin",
			fuse_root: siteURL,
			avoideCache: myTimestamp()
		}, function(data){
			$('#couponHolder').fadeOut().empty();
			$('#couponHolder').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);
	return false;
}

function myTimestamp(){
    tstmp = new Date();    
    return tstmp.getTime();
} 

