function HistoryStack (){
    this.stack = new Array();
    this.current = -1;
    this.stack_limit = 1000;
    this.exc = null;
}

HistoryStack.prototype.addResource = function(resource){
    if (this.stack.length > 0) {
        this.stack = this.stack.slice(0, this.current + 1);
    }
    this.stack.push(resource);
    while (this.stack.length > this.stack_limit){
        this.stack.shift();
    }
    this.current = this.stack.length - 1;
    this.save();
};

HistoryStack.prototype.getCurrent = function (){
    return this.stack[this.current];
};


HistoryStack.prototype.hasPrev = function(){
    return (this.current > 0);
};


HistoryStack.prototype.hasNext = function(){
    return (this.current < this.stack.length - 1 && this.current > -1);
};
HistoryStack.prototype.go = function(increment){
    // Go back...
    if (increment < 0) {
        this.current = Math.max(0, this.current + increment);
    
    // Go forward...
    } else if (increment > 0) {
        this.current = Math.min(this.stack.length - 1, this.current + increment);
    
    // Reload...
    } else {
        location.reload();
    }
    
    this.save();
};
HistoryStack.prototype.setCookie = function(name, value){
    var cookie_str = name + "=" + escape(value);
    document.cookie = cookie_str;
};


HistoryStack.prototype.getCookie = function(name){
    if (!name) return '';
    
    var raw_cookies, tmp, i;
    var cookies = new Array();
    
    raw_cookies = document.cookie.split('; ');
    for (i=0; i < raw_cookies.length; i++) {
        tmp = raw_cookies[i].split('=');
        cookies[tmp[0]] = unescape(tmp[1]);
    }

    if (cookies[name] != null) {
        return cookies[name];
    } else {
        return '';
    }
};
HistoryStack.prototype.save = function(){
    this.setCookie('CHStack', this.stack.toString());
    this.setCookie('CHCurrent', this.current);
};

HistoryStack.prototype.load = function(){
    var tmp_stack = this.getCookie('CHStack');
	//alert(tmp_stack);
    if (tmp_stack != ''){
        new_array = tmp_stack.split('|,');
		this.stack = replace_all (new_array, '|');
		//alert(this.stack);
    }
	
   
    var tmp_current = parseInt(this.getCookie('CHCurrent'));
    if (tmp_current >= -1){
        this.current = tmp_current;
    }
};

var myHistory = new HistoryStack();
myHistory.load();

function do_add(num){
		//alert(num);
//    var num = Math.round(Math.random() * 1000);
	myHistory.addResource(num);
    display();
    return false;
}

function do_back(){
    myHistory.go(-1);	
	myHistory.exc = 'E';
	//alert(myHistory.stack[myHistory.current]);
	if (myHistory.stack[myHistory.current]){
		execute(myHistory.stack[myHistory.current].replace("|",""));
	}
	myHistory.exc = null;
	
    display();
}

function do_forward(){
    myHistory.go(1);
	myHistory.exc = 'E';
	//alert(myHistory.stack[myHistory.current]);
	if (myHistory.stack[myHistory.current]){
		execute(myHistory.stack[myHistory.current].replace("|",""));
	}
	myHistory.exc = null;
    display();
}

function execute(method){
	//alert(method);
	return eval(method);
}

function do_reload(){
    myHistory.go(0);
}

function addLink(link){
//	alert(link);
//	open_add();
//	alert(myHistory.exc);
	if (myHistory.exc == null){
		do_add(link+"|");
	}	
}

function load_page(){
	//alert(myHistory.stack[myHistory.current]);
	if(myHistory.stack.length > 1 && myHistory.stack[myHistory.current]){
		myHistory.exc = 'E';
		execute(myHistory.stack[myHistory.current].replace("|",""));
		myHistory.exc = null;
	}
	
}
function display(){
    // Display history buttons
    var str = '';
	
    if (myHistory.hasPrev()) {
        str += '<a href="javascript:do_back();void(0);">' + '<img src="images/back1.gif" alt="Back" border=0/></a> ';
    } else {
        str += '<img src="images/back1.gif" alt="" /> ';
    }
    if (myHistory.hasNext()) {
        str += '<a href="javascript:do_forward();void(0);">' + '<img src="images/forword1.gif" alt="Forward"  border=0/>' + '</a> ';
    } else {
        str += '<img src="images/forword1.gif" alt="" /> ';
    }
   // str += '<a href="#" onclick="do_reload(); return false;">' + '<img src="images/reload.gif" alt="Reload"/></a>';
  
	historybuttons_cookie = 'historybuttons=' + escape(str);	
	document.cookie = historybuttons_cookie;	
	document.getElementById("historybuttons").innerHTML = str;
	//document.getElementById("butomm_backforword").innerHTML = str;

    // Display the current history stack, highlighting the current
    // position.
	if (myHistory.stack.length == 0)
	{
		addLink('user_backtohome();');
	}
    var str = '';//'<div>History:</div>';	
    for (i=0; i < myHistory.stack.length; i++) {
        if (i == myHistory.current) {
			str += "<div><b>" + myHistory.stack[i] + "</b></div>";
        } else {
            str += "<div>" + myHistory.stack[i] + "</div>";
        }
    }
	
    document.getElementById("output").innerHTML = str;
}

function replace_all(new_array, by) {
	//new_array = string.split(text);
	var newstring = new Array(new_array.length);
	for (h=0; h<new_array.length; h++){
		//alert("onmethod before"+new_array[h]);
		if (new_array[h].indexOf(by) == -1){
			newstring[h] = new_array[h]+by;
		}else{
			newstring[h] = new_array[h];
		}
		
		//alert("onmethod after"+newstring[h]);
	}
   return newstring;
}
