//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(rtn);
				},
				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(rtn);
				},
				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 Trip Builder';
			} else if (linkclass == "iconAddItin") {
				linkclass = 'iconRemoveItin';
			} else if (linkclass == "textRemoveItin") {
				linkclass = 'textAddItin';
				linkText = 'Add To Trip Builder';
			} else if (linkclass == "iconRemoveItin") {
				linkclass = 'iconAddItin';
			}
			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;
		}
	}
			
/*
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 Trip Builder</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 Trip Builder</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 Trip Builder</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 Trip Builder</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(rtn){
	var items = rtn.itincount;
	$('#itin_count').html(items);
	if (items == 1)
		$('#itin_descriptor').html('Item');
	else
		$('#itin_descriptor').html('Items');
	/*
	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);
	return false;
}

function myTimestamp(){
    tstmp = new Date();    
    return tstmp.getTime();
} 

