var parent = 'parent';
var child = 'child';
var cookie_name='product_list_history';

function showProductList(){
	if(this.prod_list == undefined){
		//document.write('Could not find Config File...');
		return;
	}
	else{
		var result = '<div width="100%" border="0" cellspacing="0" cellpadding="0">';
		for(var i = 0; i < prod_list.length; i++){
			result += getTableRow(prod_list[i][0], prod_list[i][1], prod_list[i][2], prod_list[i][0]+i);
		}
		result += '</div>';
		document.write(result);
		
		restoreHistory();
	}
}

function getTableRow(type, title, url, id){
	var result = '<div class="'+type+'" id="'+id+'"';
	if(type == parent){
		result += ' onclick="manager(this.id);" >' +
				  '<div class="left_img">&nbsp;</div>' +
				  '<div class="right_img">&nbsp;</div>';
	}
	else{
		result += ' onmouseout="m2(this);" onmouseover="m1(this);" >' +
				  '<div class="left_img">&nbsp;</div>';

	} 
	result += ' <div class="title"><a href="'+url+'" title="'+title+'">'+title+'</a></div>'+
			  '</div>';
	return result;
}

function m1(obj){
	obj.style.backgroundColor='#f0f5fa';
}

function m2(obj){
	obj.style.backgroundColor='#FFFFFF';
}

function isChild(index){
	var result = false
	if(prod_list[index] != undefined && prod_list[index][0] != undefined){
		if(prod_list[index][0] == child){
			result = true;
		}
	}

	return result;
}

function manager(row_id, flagg){
	var row_index = parseInt(row_id.substring(6, row_id.length))+1;
	var child_row = document.getElementById(child + row_index);
	
	if(child_row != undefined){
	var vis = child_row.style.visibility;
		while(isChild(row_index)){
			
			if(vis == 'visible'){
				child_row.style.visibility = 'hidden';
				child_row.style.position = 'absolute';
			}
			else{
				child_row.style.visibility = 'visible';
				child_row.style.position = 'relative';	
			}
		row_index = row_index+1; 
		child_row = document.getElementById(child + row_index);
		}
		plHistory(row_id, flagg);
	}
}

function plHistory(row, flagg){
	if(getPLCookie(cookie_name) == undefined){
		setPLCookie(cookie_name, '', null);
	}
	var cookie_value = getPLCookie(cookie_name);
	if(cookie_value.indexOf(row) == -1){
		setPLCookie(cookie_name, cookie_value+ ',' + row, null);
	}
	else if (flagg == undefined){
		cookie_value = cookie_value.replace(row, '');	
		cookie_value = cookie_value.replace(',,', '');
		setPLCookie(cookie_name, cookie_value, null);
	}
	
	//alert('HISTORY: '+getPLCookie(cookie_name));
}

function setPLCookie(sName, sValue, sExpire){
	var sCookie = sName + "=" + escape(sValue) +"; path=/";
		if (sExpire){
			sCookie += "; expires=" + sExpire.toGMTString();
		}
	document.cookie = sCookie;
	return null;
}

function getPLCookie(sName){
	var sCookiecrumbs = document.cookie.split("; ");
	var sNextcrumb;
	for (var i=0; i < sCookiecrumbs.length; i++) {
		sNextcrumb = sCookiecrumbs[i].split("=");
		if (sNextcrumb[0] == sName){
			 return unescape(sNextcrumb[1]);
		}
	}
	return null;
}

function restoreHistory(){
	if(getPLCookie(cookie_name) == undefined){
		return;
	}
	var cookie_values = getPLCookie(cookie_name).split(',');
	
	for (var i=0; i < cookie_values.length; i++) {
		if(cookie_values[i] != ''){
			manager(cookie_values[i], 'something');	
		}
	}
	//alert('RESTORE: '+getPLCookie(cookie_name));
}