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() {
		bindCtrls();
	});


	function bindCtrls(){
		$('a.iconAddItin').each(function(){
			$(this).bind("click", function(){
				var urlParams = window.itineary.getUrlParams($(this));
				
				window.itineary.add(urlParams.recid,urlParams.iType);
				if (urlParams.iType == 2) {
					//for events we need to check and see if the same eventid are on the page such as another day and mark these as added
						window.itineary.updateLink(urlParams,"ADDED");
				}
					return false;
			});
		});
		
		$('a.iconRemoveItin').each(function(){
			$(this).bind("click", function(){
				var urlParams = window.itineary.getUrlParams($(this));
				window.itineary.remove(urlParams.recid, urlParams.iType);
				if (urlParams.iType == 2) {
					//for events we need to check and see if the same eventid are on the page such as another day and mark these as added
						window.itineary.updateLink(urlParams,"REMOVED");
				}
					return false;
			});
		});
		
		
		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){
					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){
				
					window.itineary.updateLink(itinObject,"REMOVED");
					updateItin();
				},
				failure: handlerFailuer
			});
		}
		
		window.itineary.updateLink = function(itinObject, action){
			var c = window.itineary.typeToClass(itinObject.iType) + '_' + itinObject.recid;
			var container = itinObject.iType == 3 ? 'Coupon Pack' : 'Itinerary';
			if (action == "ADDED") 
				$('.'+c).html('<a href="javascript:window.itineary.remove(' + itinObject.recid + ',' + itinObject.iType + ')" class="iconRemoveItin"><span class="img-replace"></span>Remove From ' + container + '</a>');
			else 
				$('.'+c).html('<a href="javascript:window.itineary.add(' + itinObject.recid + ',' + itinObject.iType + ')" class="iconAddItin"><span class="img-replace"></span>Add To ' + container + '</a>');
			
		}
		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 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/cfcs/itinerary/itineraryDisplay.cfc', {
			method: "updateItin",
			fuse_root: siteURL,
			avoideCache: myTimestamp()
		}, function(data){
			$('#itinListHolder').fadeOut().empty();
			$('#itinListHolder').html(data).fadeIn();
		});
		$.get(siteURL + '/includes/cfcs/itinerary/itineraryDisplay.cfc', {
			method: "countItin",
			avoideCache: myTimestamp()
		}, function(data){
			$('#itinCount').empty();
			$('#itinCount').html(data);
			
		});
	}
	// update pack count
	var $pack = $('.pack-count');
	if ( $pack.length > 0 ) {
		$.get(siteURL + '/includes/cfcs/itinerary/itinerary.cfc', {
			method: "countItin",
			itype: '3',
			avoideCache: myTimestamp()
		}, function(data){
			$pack.html(data);
		});
	}
	// update coupon pack
	var $detailList = $('.coupon-detail-list');
}

function handleSuccess(response)
{
	document.body.innerHTML = response.responseText;
			return false;
}

function handlerFailuer(response)
{
	alert(response);
}

function myTimestamp(){
    tstmp = new Date();    
    return tstmp.getTime();
} 

$(document).ready(function() {
	// bind refresh to shadowbox close
	$('a.pack-link').bind('SHADOWBOX_CLOSE', function(event) {
		var itinUpdated = $(document).data('itinUpdated');
		if ( typeof itinUpdated == 'undefined'
				|| myTimestamp() - itinUpdated < 1000 ) {
			updateItin();
		}
	});

	$('itinCoupon').bind('SHADOWBOX_CLOSE', function(event) {
		$(this).find('a.iconAddItin').each(function() {
			var itinUpdated = $(document).data('itinUpdated');
			var itinArr = $(document).data('itinArr');
			if ( typeof itinUpdated == 'undefined'
					|| typeof itinArr == 'undefined'
					|| myTimestamp() - itinUpdated < 1000 ) {
				$.ajax({
					url: siteURL + '/includes/cfcs/itinerary/itinerary.cfc'
					,async: false
					,cache: false
					,data: {
						method: "getItin"
					}
					,success: function(data){
						$(document).data('itinArr',data);
						$(document).data('itinUpdated',myTimestamp());
					}
					,type: 'GET'
					,dataType: 'json'
				});
				itinArr = $(document).data('itinArr');
			}
			var $this = $(this);
			var itinData = $this.data('itinData');
			if (typeof itinData == 'undefined') {
				itinData = window.itineary.getUrlParams($this);
				$this.data('itinData',itinData);
			}
			var inPack = false;
			for ( var ii = 0 ; ii < itinArr[itinData.iType - 1].length ; ii++ ) {
				if ( itinArr[itinData.iType - 1][ii] == itinData.recid ) {
					inPack = true;
				}
			}
			if ( inPack ) {
				//alert($this.attr('id') + ' IN PACK!');
				window.itineary.updateLink(itinData,"ADDED");
			} else {
				//alert($this.attr('id') + ' NOT IN PACK!');
				window.itineary.updateLink(itinData,"REMOVED");
			}
		});

		$(this).find('a.iconRemoveItin').each(function(event) {
			var itinUpdated = $(document).data('itinUpdated');
			var itinArr = $(document).data('itinArr');
			if ( typeof itinUpdated == 'undefined'
					|| typeof itinArr == 'undefined'
					|| myTimestamp() - itinUpdated < 1000 ) {
				$.ajax({
					url: siteURL + '/includes/cfcs/itinerary/itinerary.cfc'
					,async: false
					,cache: false
					,data: {
						method: "getItin"
					}
					,success: function(data){
						$(document).data('itinArr',data);
						$(document).data('itinUpdated',myTimestamp());
					}
					,type: 'GET'
					,dataType: 'json'
				});
				itinArr = $(document).data('itinArr');
			}
			var $this = $(this);
			var itinData = $this.data('itinData');
			if (typeof itinData == 'undefined') {
				itinData = window.itineary.getUrlParams($this);
				$this.data('itinData',itinData);
			}
			var inPack = false;
			for ( var ii = 0 ; ii < itinArr[itinData.iType - 1].length ; ii++ ) {
				if ( itinArr[itinData.iType - 1][ii] == itinData.recid ) {
					inPack = true;
				}
			}
			if ( !inPack ) {
				//alert($this.attr('id') + ' NOT IN PACK!');
				window.itineary.updateLink(itinData,"REMOVED");
			} else {
				//alert($this.attr('id') + ' IN PACK!');
				window.itineary.updateLink(itinData,"ADDED");
			}
		});
	});
	});

