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 = '<a class="itineraryAdded" href="#" title="Currently Added in your Itinerary"></a>';
			itinCountElement.innerHTML = itinCount++;
			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 Itinerary</span>';
			return true;
		},
		'onFailure' : function(response){
			document.body.innerHTML = response.responseText;
			return false;
		}
	});*/
	var itinCountElement = document.getElementById('numItinItems');
	var thisUrl  = imgroot+'/includes/itinerary/index.cfm';
	var thisData = 'action=ajax_addItin&listingid=' + listingid; 
	
	$.ajax({
	   type: "POST",
	   url: thisUrl,
	   data: thisData,
	   success: function (response){
				$('#itin_' + listingid).removeClass('itineraryAdd');
				$('#itin_' + listingid).addClass('itineraryAdded');
				$('#itin_' + listingid).html('');
				itinCount++;
				itinCountElement.innerHTML = itinCount;
				//try{
				//	document.getElementById('itinerary').innerHTML = response;
				//}catch(e){}
				return true;
				
		},
	   error: function(response){	
			  document.getElementById('itin_' + listingid).innerHTML = response.responseText;
	   }
	 });
}

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 Itinerary</span>';
			return true;
		},
		'onFailure' : function(response){
			document.body.innerHTML = response.responseText;
			return false;
		}
	});*/
	
	
	var itinCountElement = document.getElementById('numItinItems');
	var thisUrl  = imgroot+'/includes/itinerary/index.cfm?action=ajax_addEventItin';
	var thisData = 'action=ajax_addEventItin&eventid='+eventid; 
	
	$.ajax({
	   type: "POST",
	   url: thisUrl,
	   data: thisData,
	   success: function (response){
				$('#itinEvent_' + eventid).removeClass('itineraryAdd');
				$('#itinEvent_' + eventid).addClass('itineraryAdded');
				$('#itinEvent_' + eventid).html('');
				itinCount++;
				itinCountElement.innerHTML = itinCount;
				//try{
				//	document.getElementById('itinerary').innerHTML = response;
				//}catch(e){}
				return true;
				
		},
	   error: function(response){	
			  document.getElementById('itinEvent_' + eventid).innerHTML = response.responseText;
	   }
	 });
}

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 Itinerary</b>';
			itinAnimateToCart(img);
		},
		'onFailure' : function(response){
			alert("Item failed to add: " + response.responseText);
		}
	});
}
