/**
 * will be used for shopping cart functions
 * @author jochen vandendriessche
 */

function Basket(sid){
	this.sid		=	sid;
	this.jah		=	new joinkJAH();		// private jah object
}

Basket.prototype = {
	
	addColor : function(id){
		// do ajax request, check what colors we have, show confirmbox with ticked colors and unmarked colors
		this.jah.setHtmlObj('');
		this.jah.setFunction(bind(this, this.addColorBox));
		this.jah.setParams('id=' + id);
		this.jah.doRequest('/javascript/ajax/getRemainingColors.php?PHPSESSID=' + this.sid, 'POST');
	},
	
	addColorBox: function(){
		var response = this.jah.response;
		var id = 0;
		var form = response;
		new confirmBox('Select colors', 'Please select the colors you want to add:<br />' + form, this, id, this.add2Basket, 'OkCancel');
	},
	
	add : function(id){
			// store it in the basket array;
			// first, let's see what the user wants
			this.addBuildBox(id);
			/*
			this.jah.setHtmlObj('');
			this.jah.setFunction(bind(this, this.addStatus));
			this.jah.setParams('id=' + id);
			this.jah.doRequest('/javascript/ajax/add2basket.php?PHPSESSID=' + this.sid, 'POST');
			*/
	},
	
	
	addBuildBox : function(id){
			this.jah.setHtmlObj('');
			this.jah.setFunction(bind(this, this.addShowBox));
			this.jah.setParams('id=' + id);
			this.jah.doRequest('/javascript/ajax/getColors.php?PHPSESSID=' + this.sid, 'POST');		
	},
	
	addShowBox : function(){
			var inh = this.jah.response;
			var txt = inh.split('|');
			var id = txt[0];
			var clr = txt[1].split('\n');
			var form = '<form id="colors" onsubmit="return false;" method="post" action="">';
			form += '<input type="hidden" value="' + txt[0] + '" name="parentid" id="parentid" />';
			for (var i = 0;i<clr.length;i++){
				var val = clr[i].split(",");
				form += '<label for="' + val[0] + '">' + val[1] + '</label>';
				form += '<input type="text" id="' + val[0] + '" name="basket[' + val[0] + ']" />';
				form += '<br />';
			}
			form += '</form>';
			var conf = new confirmBox('Select type', 'Please select the amount you want:<br />' + form, this, id, this.add2Basket, 'OkCancel');
	},
	
	add2Basket : function(){
		this.jah.setHtmlObj('');
		this.jah.setParams($('colors'));
		this.jah.setFunction(bind(this, this.addStatus));
		this.jah.doRequest('/javascript/ajax/add2basket.php?PHPSESSID=' + this.sid, 'POST');
				
	},
	
	addStatus : function(){
		var response = this.jah.response.split(',');
		if (response[0] == 'true'){
			//window.alert('added to cart\n' + response[2]);
			if (response[1] != 0){
				new Ghost(response[1]);
			}
		}else{
			window.alert('error');
			//window.alert(response);
		}
	},
	
	editQty : function(id){
		var inp = $('qty' + id);
		if (inp.disabled){
			inp.disabled = false;
			inp.className = 'qtyac';
			inp.focus();
			var img = $('edit' + id);
			img.src = '/assets/shop/save.png';
		}else{
			inp.disabled = true;
			inp.className = 'qty';
			var img = $('edit' + id);
			img.src = '/assets/shop/edit.png';
			this.saveQty(id);
		}
	},
	
	saveQty	: function(id){
		this.jah.setHtmlObj('');
		$('qty'+id).value = Math.abs(parseInt($('qty'+id).value));
		if ($('qty' + id).value == 'NaN'){
			$('qty'+id).value = '0';
		}
		this.jah.setParams('id=' + id + '&val=' + Math.abs(parseInt($('qty' + id).value)));
		this.jah.setFunction(bind(this, this.editPrice));
		this.jah.doRequest('/javascript/ajax/editValue.php?PHPSESSID=' + this.sid, 'POST');
	},
	
	editPrice : function(){
		var ret = this.jah.response;
		var eit = ret.split(',');
		if( eit[0] == 'true'){
			$('total' + eit[1]).value = Math.abs(parseInt($('price' + eit[1]).value * $('qty' + eit[1]).value));
		}
				
	},
	
	deleteItem : function(al, id, title, type){
			// al is the array level: parent or child
			var form = '<form method="post" action="" id="deleteForm">';
				form+= '<input type="hidden" value="' + al + '" name="al" id="al" />';
				form+= '<input type="hidden" value="' + id + '" name="id" id="id" />';
				form+= '</form>';
			var conf = new confirmBox('Remove cart item', 'Are you sure you want to remove <i>the ' + title + '</i> from your cart?' + form, this, id, this.doDelete, 'YesNo');
	},

	doDelete : function(){
			this.jah.setHtmlObj('');
			this.jah.setFunction(bind(this, this.deleteStatus));
			this.jah.setParams($('deleteForm'));
			this.jah.doRequest('/javascript/ajax/deleteFromBasket.php?PHPSESSID=' + this.sid, 'POST');	
	},
	
	deleteStatus : function(){
			var ret = this.jah.response;
			var eit = ret.split(',');
			if (eit[0] == 'true'){
				// is eit[2] a parent or a child
				// call proper function with the eit[1] (id)
				this.deleteNode(eit[1], eit[2]);
			}
	},
	
	deleteNode : function(id, left){
		var row = $('article' + id);
		var par = row.parentNode;
		if (row){
			// fade out and lower height, sounds like a job for joinkFX / Fader
			row.style.overflow = 'hidden';
			row.style.backgroundColor = '#fcc';
			var ran = new joinkFX(row, {time:30,animation:'outQuintic',style:'height',start:'' + row.offsetHeight,distance:'' + (0 - row.offsetHeight)});
			var rop = new Fader(row, 100, 0, 10);
			rop.fadeOut();
			ran.move();
		}
		if (left == 0){
			var txt = document.createTextNode('No items in cart.');
			par.appendChild(txt);
		}
		
	},
	
	jahFeedback : function(){
		
		window.alert(this.jah.response);
		
	},
	
	showRawSession : function(){
		this.jah.setFunction('');
		this.jah.setHtmlObj('basketDebug');
		this.jah.doRequest('/javascript/ajax/showBasket.ph?' + this.sid, 'GET');
	}
	
}

/**************************************************************************************************************************/

function Ghost(id){
	this.id = id;
	this.orig = document.getElementById('product' + id);
	this.ghost = new Image();
	this.ghost.id = 'ghost' + id;
	this.ghost.src = this.orig.src;
	this.ghost.style.width = '99px';
	this.ghost.style.height = '99px';
	this.ghost.style.position = 'absolute';
	var pos = findPos(this.orig);
	this.ghost.style.top = pos[1] + 'px';
	this.ghost.style.left = pos[0] + 'px';
	this.ghost.style.MozOpacity = '0.7';
	this.ghost.style.border = '1px solid #000';
	document.body.appendChild(this.ghost); 

	var posto = findPos(document.getElementById('basket'));
	var difx = posto[1] - pos[1];
	var dify = posto[0] - pos[0];
		
	this.anim = new joinkFX('ghost' + id, {time:30,animation:'outQuintic',style:'top,left,width,height',start:pos[1] + ',' + pos[0] + ',99,99',distance:difx + ',' + dify + ',-88,-88'});
	this.anim.setFunction(bind(this, this.removeGhost));
	this.anim.move();
}

Ghost.prototype = {
	
	removeGhost : function(){
		document.body.removeChild(this.ghost);
	}
	
}

/**************************************************************************************************************************/

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function $(id){
	if (document.getElementById(id)){
		return document.getElementById(id);
	}else{
		return false;
	}
}

/**************************************************************************************************************************/

function confirmBox(title, msg, obj, id, func, type){
	this.id	= id;
	this.title = title;
	this.msg = msg;
	this.caller = obj;
	this.func = func;
	this.type = type;
	this.createBox();
}

confirmBox.prototype = {
	
	createBox : function(){
		this.box = document.createElement('div');
		this.box.id = 'confirmBox';
		var boxText = document.createTextNode(this.title);
		var strong	=	document.createElement('strong');
		strong.appendChild(boxText);
		this.box.appendChild(strong);
		var msgdiv = document.createElement('div');
		msgdiv.style.padding = '5px';
		msgdiv.style.fontSize = '10px';
		msgdiv.style.fontFamily = 'verdana';
		msgdiv.style.color = '#fff';
		msgdiv.innerHTML = this.msg;
		this.box.appendChild(msgdiv);		
		document.body.appendChild(this.box);
		
		var center = document.createElement('center');
		
		switch (this.type){
			case "YesNo":
				var yes = document.createElement('button');
				var yesTxt = document.createTextNode('Yes');
				yes.id = 'btnYes';
				yes.appendChild(yesTxt);
				yes.onclick = bind(this, this.doYes);
				
				var no = document.createElement('button');
				var noTxt = document.createTextNode('No');
				no.appendChild(noTxt);
				no.id = 'btnNo';
				no.onclick = bind(this, this.doNo);
				
				center.appendChild(yes);
				center.appendChild(no);
				break;
			case "OkCancel":
				var yes = document.createElement('button');
				var yesTxt = document.createTextNode('Ok');
				yes.id = 'btnYes';
				yes.appendChild(yesTxt);
				yes.onclick = bind(this, this.doYes);
				
				var no = document.createElement('button');
				var noTxt = document.createTextNode('Cancel');
				no.appendChild(noTxt);
				no.id = 'btnCancel';
				no.onclick = bind(this, this.doNo);
				
				center.appendChild(yes);
				center.appendChild(no);			
				break;
			default:
				var ok = document.createElement('button');
				var okTxt = document.createTextNode('Ok');
				ok.appendChild(okTxt);
				ok.id = 'btnOk';
				ok.onclick = bind(this, this.doYes);				
				center.appendChild(ok);
		}
		
		this.box.appendChild(center);
		
	},
	
	doYes : function(){
		// do an ajax submit to the script selected
		bind(this.caller, this.func).call();
		this.removeBox();
	},
	
	doNo : function(){
		this.removeBox();
	},
	
	removeBox : function(){
		document.body.removeChild(this.box);
	}
	
}

var toggleArr = [];

function toggleItem(id, i){
	var th = 17;
	var o = $(id);
	var h = o.offsetHeight;
	
	if (h == th){
		i.src = '/assets/Web/min.gif';
		i.title = 'collapse';
	}else{
		i.src = '/assets/Web/plus.gif';
		i.title = 'expand'
	}
	
	if (!toggleArr[id]){
		toggleArr[id] = new joinkFX(o, {time:30,animation:'outQuintic',style:'height',start:'' + h,distance:'' + (th - h),bi:'true'});
		toggleArr[id].move();
	}else{
		toggleArr[id].move();
	}	
}

function checkObjHeight(id, arrId){
	
}