var session = null;
var nodeid = -1;
function Product(id, title, url, img, width, height, price, oldprice)
{
	this.id = id;
	this.title = title;
	this.url = url;
	this.img = img;
	this.width = width;
	this.height = height;
	this.price = price;
	this.oldprice = oldprice;
}

function product2Str(product){
	var str = product.id;
	str += "\n" + product.title;
	str += "\n" + product.url;
	str += "\n" + product.img;
	str += "\n" + product.width;
	str += "\n" + product.height;
	str += "\n" + product.price;
	str += "\n" + product.oldprice;
	return str;
}

function str2Product(str){
	var prevIndex = 0;	
	var index = str.indexOf("\n");
	if (index ==-1)
		return null;
	var id = str.substring(prevIndex, index);
	id = parseInt(id);
	prevIndex = index + 1;

	index = str.indexOf("\n", prevIndex);
	if (index ==-1)
		return null;
	var title = str.substring(prevIndex, index);
	prevIndex = index + 1;

	index = str.indexOf("\n", prevIndex);
	if (index ==-1)
		return null;
	var url = str.substring(prevIndex, index);
	prevIndex = index + 1;
	
	index = str.indexOf("\n", prevIndex);
	if (index ==-1)
		return null;
	var img = str.substring(prevIndex, index);
	prevIndex = index + 1;


	index = str.indexOf("\n", prevIndex);
	if (index ==-1)
		return null;
	var width = str.substring(prevIndex, index);
	width = parseInt(width);
	prevIndex = index + 1;

	index = str.indexOf("\n", prevIndex);
	if (index ==-1)
		return null;
	var height = str.substring(prevIndex, index);
	height = parseInt(height);
	prevIndex = index + 1;

	index = str.indexOf("\n", prevIndex);
	if (index ==-1)
		return null;
	var price = str.substring(prevIndex, index);
	prevIndex = index + 1;

	var oldprice = str.substring(prevIndex, str.length);
	return new Product(id, title, url, img, width, height, price, oldprice);
}

function product2Cookie(prd){
	return toCookie(product2Str(prd));
}

function cookie2Product(value){
	return str2Product(fromCookie(value));
}

function viewProduct(product, count)
{
	session = getStoredArray();
	addElement(session, product, count);
	nodeid = product.id;
}

function getStrFromArray(ar, count, isfifo){
	if (ar.length < 1)
		return "";
	var _count = count + 1;
	while(ar.length > _count){
		ar.shift();
	}
	var str="";
	for(var i=0; i< ar.length; i++){
		if (str.length>0){
			str += ",";
		}
		str += product2Cookie(ar[i]); 
	}
	return str;
}


function setStoredArray(ar, count){
	str = getStrFromArray(ar, count);
	var action = new Cookie();
	action.setCookie("recentlyCount", ""+count);
	action.setCookie("recentlyViewed", str);
}

function getStoredArray(){
	result = new Array();
	var action = new Cookie();
	var ar= action.getCookie("recentlyViewed");
	if (!ar || ar.length < 1) 
		return result;
	prevIndex = 0;
    index = ar.indexOf(",", prevIndex);
	var i=0;
	while(index != -1){
		result[i] = cookie2Product(ar.substring(prevIndex, index));
		prevIndex = index + 1;
		index = ar.indexOf(",", prevIndex);
		i++;
	}
	elem = cookie2Product(ar.substring(prevIndex, ar.length));
	if (elem){
		result[i] = elem;
	}
	return result;
}

function addElement(ar, value, count){
	var found = false;
	for(var i=0; i<ar.length; i++){
		if (ar[i].id == value.id){
			ar.splice(i, 1);		
			i--;
		} 
	}
		var _count = count + 1;
		while(ar.length > _count){
			ar.shift();
		}
		ar[ar.length] = value;
		setStoredArray(ar, count);
	return !found;
}

function shiftArray(ar){
	var result = new Array();
	for(var i=1; i< ar.length; i++){
		result[i-1]=ar[i];
	}
	return result;
}


function recentlyViewed(){
    var ctrl = document.getElementById("recentlyViewed");
	if (!ctrl){
		return;
	}
	var count= getRecentlyViewedCount();
	var ar = getStoredArray();
	var _count =0;
	if (nodeid == -1){

		_count = ar.length - count;
		if (_count < 0){
			_count =0;
		}
	}
	ctrl.innerHTML=buildRvi(count, _count, ar);
}

function getSessionStoredArray(){
	if (isAdded){
		return session;
	} else {
		return getStoredArray();
	}
}

function getRecentlyViewedCount(){
	var action = new Cookie();
	var count = action.getCookie("recentlyCount");
	if (count && count.length > 0){
		count = parseInt(count);
		return count;
	} else {
		return 0;
	}
}
