var animArray = new Array();
var animLock = false;
var itinCount = 0;

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(imgroot,listingid){
	var a = new Ajax.Request(imgroot+'/itinerary/index.cfm?action=ajax_addItin&listingid=' + listingid,
	{
		'onSuccess' : function (response){
			itinCount++;
			$('itin_' + listingid).innerHTML = '<span class="itineraryAdded">ADDED</span>';
			$('itinCountDisplay').innerHTML = "(" + itinCount + ")";
			return true;
		},
		'onFailure' : function(response){
			document.body.innerHTML = response.responseText;
			return false;
		}
	});
}

function ajaxAddItinJQuery(imgroot,listingid)
{
	var thisUrl  = imgroot+'/itinerary/index.cfm';
	var thisData = 'action=ajax_addItin&listingid='+listingid; 

$.ajax({
   type: "POST",
   url: thisUrl,
   data: thisData,
   success: function (response){
	//		itinCount++;
	//		$('#itin_' + listingid).removeClass('itineraryAdd');
	//		$('#itin_' + listingid).addClass('itineraryAdded');
			$('#itin_' + listingid).html('<a href="'+imgroot+'/itinerary/" class="l-link">View Itinerary</a>');
	//		$('itinCountDisplay').innerHTML = "(" + itinCount + ")";
			return true;
		},
   failure: handlerFailuer
 });
}

function handleSuccess(response)
{
	document.body.innerHTML = response.responseText;
			return false;
}

function handlerFailuer(response)
{
	alert(response);
}


function ajaxAddItin2(imgroot,listingid){
	var a = new Ajax.Request(imgroot+'/itinerary/index.cfm?action=ajax_addItin&listingid=' + listingid,
	{
		'onSuccess' : function (response){
			itinCount++;
			$('itin_' + listingid).removeClass('itineraryAdd');
			$('itin_' + listingid).addClass('itineraryAdded');
			$('itin_' + listingid).innerHTML = 'Added to Itinerary';
			$('itinCountDisplay').innerHTML = "(" + itinCount + ")";
			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){
			$('#itin_' + eventid).removeClass('itineraryAdd');
			$('#itin_' + eventid).addClass('itineraryAdded');
			$('#itin_' + eventid).html('<span>Added to Itinerary</span>');
			return true;
		},
		'onFailure' : function(response){
			document.body.innerHTML = response.responseText;
			return false;
		}
	});
}



function ajaxAddEventItinJQuery(imgroot,eventid)
{
	var thisUrl  = imgroot+'/itinerary/index.cfm';
	var thisData = 'action=ajax_addEventItin&eventid='+eventid; 

$.ajax({
   type: "POST",
   url: thisUrl,
   data: thisData,
   success: function (response){
		//	itinCount++;
			//$('#itinEvent_' + eventid).removeClass('itineraryAdd');
			//$('#itinEvent_' + eventid).addClass('itineraryEventAdded');
			$('#itinEvent_' + eventid).html('<a style="font-weight:normal;" href="'+imgroot+'/itinerary/">View Itinerary</a>');
			//$('#itinEvent_' + eventid).innerHTML = '<span class="itineraryEventAdded">Added to Itinerary</span>';
		//	$('itinCountDisplay').innerHTML = "(" + itinCount + ")";
			return true;
		},
   failure: function (response) {
		alert(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);
		}
	});
}
