/*
 * Copyright 2012 1fb.net Financial Services.
 *
 * This document may not be reproduced, distributed or used in any manner whatsoever without the expressed written
 * permission of 1st Financial Bank USA.
 */

/**
 * @fileOverview This file contains common code to process article pages.
 * @author John Yang, Vincent Cai, Garrett Li, Ray Chen
 * @version 1.12
 * @since 4.60, 2012-01-03
 */

//reset the header infor and article id in cookie when the user access the article from url or portal page.
if(!needRemember()){
	clearValueInCookie("articleHeaders")	
    clearValueInCookie("openedArticleIds")  
}

DomLoaded.load(ArticleControl);
DomLoaded.load(RefreshReferrer);

function RefreshReferrer(){
	if(!needRemember()){
		if(navigator.userAgent.indexOf('MSIE') >= 0){
			hiddenLink = document.createElement("A")
			hiddenLink.href = window.location.href
			hiddenLink.style.visibility = "hidden"
			$ele("panelboxtitle").appendChild(hiddenLink)
			hiddenLink.click()				
		}else{
			window.location = window.location.href
		}
	}
}

function ArticleControl(){
	InitArticleBox.call($ele("articlelist"));	

	//get the article id from cookie and open it	
	var openedArticleIds = getCookie("openedArticleIds").split(",");
	for(var i = 0; i < openedArticleIds.length; i++){
		if(openedArticleIds[i] == null || openedArticleIds[i] == "")
			continue;
		try{
			if(openedArticleIds[i]) $ele(openedArticleIds[i]).Open();
		}catch(e){}
	}
}

function InitArticleBox(){
	DelEmptyTag.call(this);
	BatchRunFun.call(this.childNodes, RelateHeadAndList);
}

function RelateHeadAndList(){
	DelEmptyTag.call(this);
	if(this.firstChild && this.tagName == "LI" && this.firstChild.tagName == "H4"){
		this.firstChild.nextSibling.Open = function(){
				this.previousSibling.className = "opened";
				this.style.display = "block";
		}

		
		InitBindHead.call(this.firstChild);
		//get the header infor from cookie and open it when user stay in article page.
		if(needRemember()){
			var articleHeaders = getCookie("articleHeaders")
			if(articleHeaders && articleHeaders != "" && articleHeaders.indexOf(this.firstChild.innerHTML) != -1){
				this.firstChild.nextSibling.Open()
			}
		}
		//make the subsection be opened when user access the article from url or portal page.
		//and add the header to cookie
		else if(this.firstChild.id && this.firstChild.id == "mainsection"){	
			this.firstChild.SetStyle("opened","block");
			addValueInCookie("articleHeaders", this.firstChild.innerHTML);
		}
		BatchRunFun.call(this.firstChild.nextSibling.childNodes, RelateHeadAndList);
	}
}

//need remember the article tree's status
function needRemember(){
	refer = document.referrer
	return (refer.indexOf("content/content_choosearticle_tmpl.jhtml") != -1
				||refer.indexOf("content/content_getinarticle_tmpl.jhtml") != -1
				||refer.indexOf("content/content_payarticle_tmpl.jhtml") != -1
				||refer.indexOf("content/content_magarticle_tmpl.jhtml") != -1
				||refer.indexOf("content_removearticle_tmpl.jhtml")!=-1
				)
}

function getchildren(){
	var children = []
	DelEmptyTag.call(this);
	for(var i=0, j=this.childNodes.length; i<j; i++){    
        if(this.childNodes[i] && this.childNodes[i].tagName=="LI"){
         if(this.childNodes[i].id == ""){         	
			retval = getchildren.call(this.childNodes[i].firstChild.nextSibling)		
			for(var x=0; x<retval.length; x++){
			 children.push(retval[x])
			}
	     }else{
	        children.push(this.childNodes[i].id)
	     }
        }
    }
    return children
}

function removeHeadersFromCookie(){	
	removeValueInCookie("articleHeaders", this.innerHTML);
	nextSibling = this.nextSibling
	DelEmptyTag.call(nextSibling);
	for(var i=0, j=nextSibling.childNodes.length; i<j; i++){  
        if(nextSibling.childNodes[i] && nextSibling.childNodes[i].tagName=="LI" && nextSibling.childNodes[i].firstChild.tagName == "H4"){
        	removeValueInCookie("articleHeaders", nextSibling.childNodes[i].firstChild.innerHTML);
        }
    }
}

function InitBindHead(){
	this.onclick = function(){
		if(this.nextSibling.style.display == "block") {
			// user close the header
			//remove related articles of this header
			children = getchildren.call(this.nextSibling)					
			for(var i=0; i<children.length; i++){
				removeValueInCookie("openedArticleIds", children[i])
			}
			//remove the head info and the children of the header from cookie
			removeHeadersFromCookie.call(this)
			this.SetStyle("","none");
		}else{
			// user open the header
			//store the head info to cookie
			addValueInCookie("articleHeaders", this.innerHTML);
			this.SetStyle("opened","block");
		}
	}
	this.SetStyle = function(CssClass,sName){
			this.className = CssClass;
			this.nextSibling.style.display = sName;
	}
	if(this.nextSibling.firstChild.tagName != "H4")
		BatchRunFun.call(this.nextSibling.childNodes, InitSubLiEvent);
}

function InitSubLiEvent(){
	this.Open = function(){
		this.parentNode.Open();
		this.parentNode.parentNode.parentNode.Open();
	}	
}
function addValueInCookie(c_name,value)
{	
	var nowValue = getCookie(c_name);
	if(nowValue == null || nowValue == "") {
		document.cookie=c_name+ "=" +escape(value) + ";path=/";
	}else {
		if(nowValue.indexOf(value)==-1)
			document.cookie=c_name+ "=" +nowValue + "," + escape(value) + ";path=/";
	}
	
}
function removeValueInCookie(c_name,value) {
	var nowValue = getCookie(c_name);
	if(value == null || value == "") {
		return;	
	}else {
		var newValue = nowValue.replace(","+value, "");
		document.cookie=c_name+ "=" + newValue + ";path=/";
	}
}


function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=")
	  if (c_start!=-1)
	    { 
	    c_start=c_start + c_name.length+1 
	    c_end=document.cookie.indexOf(";",c_start)
	    if (c_end==-1) c_end=document.cookie.length
	    return unescape(document.cookie.substring(c_start,c_end))
	    } 
	  }
	return ""
}
function clearValueInCookie(c_name) {
	document.cookie=c_name+ "=;path=/";
}
