var animArray = new Array();
var animLock = false;

function animation(startx,starty,iendx,iendy,sw,sh,d,pos){
	this.startx = startx;
	this.starty = starty;
	this.iendx = iendx;
	this.iendy = iendy;
	this.sw = sw;
	this.sh = sh;
	this.d = d;
	this.pos = pos;
	this.theta = 2* Math.PI / 2;
	this.step = 0.01;
	this.ovalModifier = (this.starty - this.iendy) / (this.startx - this.iendx);
	var x1 = this.startx;
	var x2 = this.iendx;
	var y1 = this.starty;
	var y2 = this.iendy;
	this.step = 10 / Math.sqrt(Math.pow(x2 - x1,2) + Math.pow(y2-y1,2));
}

function itinAnimateToCart(img){
	var d = document.createElement("IMG");
	d.src = img.src;
	document.body.appendChild(d);
	d.style.position = 'absolute';
	Position.clone(img,d);

	var starty = parseInt(d.style.top);
	var startx = parseInt(d.style.left);
	var iendx = Position.cumulativeOffset($('itinImage'))[0];
	var iendy = Position.cumulativeOffset($('itinImage'))[1];

	var sw = img.width;
	var sh = img.height;
	var a = new animation(startx,starty,iendx,iendy,sw,sh,d,1);
	//new Effect.Opacity(d,{duration:1.0,from:0.4,to: 0.4});
	d.style.zIndex = 4;

	while (animLock){	
	}
	var l = animArray.length;
	animArray.push(a);
	var intid = setInterval("toCartHelper(" + l + ")",2);
	a.intid = intid;
	animLock = false;
}


function toCartHelper(index){
	var a = animArray[index];

	a.d.style.top = (a.starty - Math.round(Math.sin(a.theta) * (a.starty - a.iendy)))  + "px";
	a.d.style.left = (a.iendx - Math.round(Math.cos(a.theta) * (a.startx - a.iendx))) + "px";
	a.d.style.width = (a.sw + Math.abs(Math.round(Math.sin(a.theta * 2)* 25))) + "px";
	a.d.style.height = (a.sw + Math.abs(Math.round(Math.sin(a.theta * 2) * 25))) + "px";


    if (a.theta < Math.PI / 2){
		clearInterval(a.intid);
		document.body.removeChild(a.d);
		updateItin();		
	}
	else
		a.theta -= a.step;
}

function ajaxAddItin(listingid){
	var a = new Ajax.Request(imgroot+'/itinerary/index.cfm?action=ajax_addItin&listingid=' + listingid,
	{
		'onSuccess' : function (response){
		/*	$('itin_' + listingid).innerHTML = '<span class="itineraryAdded">Added to Itinerary</span>'; */
			$('itin_' + listingid).innerHTML = '<a class="itineraryAdded" href="#" title="Currently Added in your Itinerary"></a>';
			return true;
		},
		'onFailure' : function(response){
			document.body.innerHTML = response.responseText;
			return false;
		}
	});
}

function ajaxAddItinTxt(listingid){
	var a = new Ajax.Request(imgroot+'/itinerary/index.cfm?action=ajax_addItin&listingid=' + listingid,
	{
		'onSuccess' : function (response){
		$('itin_' + listingid).innerHTML = '<span class="itineraryAdded">Added to My Trip Planner</span>';
			return true;
		},
		'onFailure' : function(response){
			document.body.innerHTML = response.responseText;
			return false;
		}
	});
}

function ajaxAddEventItin(eventid){
	var a = new Ajax.Request(imgroot+'/itinerary/index.cfm?action=ajax_addEventItin&eventid=' + eventid,
	{
		'onSuccess' : function (response){
			$('itinEvent_' + eventid).innerHTML = '<span class="itineraryAdded">Added to My Trip Planner</span>';
			return true;
		},
		'onFailure' : function(response){
			document.body.innerHTML = response.responseText;
			return false;
		}
	});
}

function updateItin(){
	$('itinImage').src = '../images/iconItineraryBlue.gif';
/*
	var posx = Position.cumulativeOffset($('cartimage'))[0] - 22 + 'px';
	var posy = Position.cumulativeOffset($('cartimage'))[1] + 4 + 'px';

	$('cartimage').style.position = 'absolute';
	$('cartimage').style.left = posx;
	$('cartimage').style.top = posy;
*/
}

function itinAddToCart(listingid,img){
	var a = new Ajax.Request(imgroot+'/itinerary/index.cfm?action=ajax_addItin&listingid=' + listingid,
	{
		'onSuccess' : function (response){
			$('itin_' + listingid).innerHTML = '<b>Added to My Trip Planner</b>';
			itinAnimateToCart(img);
		},
		'onFailure' : function(response){
			alert("Item failed to add: " + response.responseText);
		}
	});
}

function ajaxAddEventItinJQuery(imgroot,eventid)
{
	$.ajax({
		type: "POST",
		url: imgroot + "/includes/itinerary/index.cfm",
		data: {"action": "ajax_addEventItin", "eventid": eventid, "fuse_root": imgroot},
		success: function (response)
		{
			//$('#itinEvent_' + eventid).removeClass('itineraryAdd');
			$('#hideItinEvent_' + eventid).removeClass('showMe');
			$('#hideItinEventAdded_' + eventid).removeClass('hideMe');
			$('#hideItinEvent_' + eventid).addClass('hideMe');
			$('#hideItinEventAdded_' + eventid).addClass('showMe');
			//$('#itinEvent_' + eventid).addClass('itineraryAdded bold floatR add10pxRight add10pxTop underline orange small');
			//$('#itinEvent_' + eventid).html('<strong>Added to My Trip Planner</strong>');
			try{
				$("#itinerary").replaceWith(response);
			}catch(e){}
			return true;
		},
		error: function(response){$("#itinerary").html(response);}
	});
}


function removeEventFromItinAjax(imgroot,eventid)
{
	$.ajax({
		type: "POST",
		url: imgroot + "/includes/itinerary/index.cfm",
		data: {"action": "ajax_removeEventItin", "eventid": eventid, "fuse_root": imgroot},
		success: function (response){
			//$('#itinEvent_' + eventid).removeClass('itineraryAdded bold floatR add10pxRight add10pxTop underline orange small');
			//$('#itinEvent_' + eventid).addClass('itineraryAdd');
			$('#hideItinEventAdded_' + eventid).removeClass('showMe');
			$('#hideItinEventAdded_' + eventid).addClass('hideMe');				
			$('#hideItinEvent_' + eventid).removeClass('hideMe');
			$('#hideItinEvent_' + eventid).addClass('showMe');
			//$('#itinEvent_' + eventid).html('<a class="white bold floatR add10pxRight arrowBlueSmall underline" href="javascript:ajaxAddEventItinJQuery(' + imgroot + ',' + eventID + ');">Add to My Trip Planner</a>');
			try{
				$("#itinerary").replaceWith(response);
			}catch(e){}
			return true;
		},
		error: function(response){	
			document.getElementById('itinerary').innerHTML = response.responseText;
		}
	 });
}

function removeEvent(imgroot,eventid)
{
	$.ajax({
		type: "POST",
		url: imgroot + "/includes/itinerary/index.cfm",
		data: {"action": "ajax_removeMainEventItin", "eventid": eventid, "fuse_root": imgroot},
		url: thisUrl,
		data: thisData,
		success: function (response){
			$('#itinEvent_' + eventid).removeClass('itineraryAdded bold floatR add10pxRight add10pxTop underline orange small');
			$('#itinEvent_' + eventid).addClass('itineraryAdd');
			$('#itinEvent_' + eventid).html('<a class="white bold floatR add10pxRight arrowBlueSmall underline" href="javascript:ajaxAddEventItinJQuery(#request.fuse_root#,#qry.eventID[num]#,#Arguments.qry.Title[num]#);">Add to My Trip Planner</a>');
			try{
				$("#itinerary").replaceWith(response);
			}catch(e){}
			return true;
		},
		error: function(response){	
			document.getElementById('itinerary').innerHTML = response.responseText;
		}
	 });
}

function ajaxAddItinJQuery(imgroot,listingid,name,callback)
{
	$.ajax({
		type: "POST",
		url: imgroot + "/includes/itinerary/index.cfm",
		data: {"action": "ajax_addItin", "listingid": listingid, "fuse_root": imgroot},
		success: function (response){
			//itinCount++;
			//$('#itin_' + listingid).removeClass('itineraryAdd');
			//$('#itin_' + listingid).addClass('itineraryAdded bold floatR add10pxRight add10pxTop underline orange small');
			$('#hideItin_' + listingid).removeClass('showMe');
			$('#hideItinAdded_' + listingid).removeClass('hideMe');
			$('#hideItin_' + listingid).addClass('hideMe');
			$('#hideItinAdded_' + listingid).addClass('showMe');
			//$('#itin_' + listingid).html('<strong>Added to My Trip Planner</strong>');
			//$('itinCountDisplay').innerHTML = "(" + itinCount + ")";
			try{
				$("#itinerary").replaceWith(response);
				if(updatePlacemarkItinerary) updatePlacemarkItinerary(listingid);
			}catch(e){}
			return true;
		},
		error: function(response){$('#itin_' + listingid).html(response.responseText);}
	});
}

function removeListingFromItinAjax(imgroot,listingid)
{
	$.ajax({
		type: "POST",
		url: imgroot + "/includes/itinerary/index.cfm",
		data: {"action": "ajax_removeItin", "listingid": listingid, "fuse_root": imgroot},
		success: function (response){
			//$('#itin_' + listingid).removeClass('itineraryAdded bold floatR add10pxRight add10pxTop underline orange small');
			//$('#itin_' + listingid).addClass('itineraryAdd');
			$('#hideItin_' + listingid).removeClass('hideMe');
			$('#hideItinAdded_' + listingid).removeClass('showMe');
			//$('#hideItin_' + listingid).addClass('showMe');
			$('#hideItinAdded_' + listingid).addClass('hideMe');
			//		$('#itin_' + listingid).html('<a href="javascript:ajaxAddItinJQuery(../..,listingid,#Arguments.qry.company#);" class="white bold floatR add10pxRight arrowBlueSmall underline">Add to My Trip Planner</a>');
			try{
				$("#itinerary").replaceWith(response);
				if(updatePlacemarkItinerary) updatePlacemarkItinerary(listingid);
			}catch(e){}
			return true;
		},
		error: function(response){	
			document.getElementById('itinerary').innerHTML = response.responseText;
		}
	});
}

function removeListing(imgroot,listingid)
{
	$.ajax({
		type: "POST",
		url: imgroot + "/includes/itinerary/index.cfm",
		data: {"action": "ajax_removeMainItin", "listingid": listingid, "fuse_root": imgroot},
		url: thisUrl,
		data: thisData,
		success: function (response){
			$('#itin_' + listingid).removeClass('itineraryAdded bold floatR add10pxRight add10pxTop underline orange small');
			$('#itin_' + listingid).addClass('itineraryAdd');
			$('#itin_' + listingid).html('<a href="javascript:ajaxAddItinJQuery(../..,listingid,#Arguments.qry.company#);" class="white bold floatR add10pxRight arrowBlueSmall underline">Add to My Trip Planner</a>');
			try{
				$("#itinerary").replaceWith(response);
			}catch(e){}
			return true;
		},
		error: function(response){	
			document.getElementById('itinerary').innerHTML = response.responseText;
		}
	});
}