/*

	@version: 2.1

	@last updated: 9:46 AM 5/6/2009

	@autho: Emile Khattar <ekhattar@kyube.com>

	@copyright: KYUBE inc

*/

	if (!KY_URIFixerObject.arrInst){

	KY_URIFixerObject.arrInst = new Array();

	KY_URIFixerObject.count=0;

	}

	

	function KY_URIFixerObject()

	{

		this.iIndex = KY_URIFixerObject.count;

		KY_URIFixerObject.arrInst[this.iIndex] = this;	

		this.bDebug = 0;

		this.bTextChange = 0;

		this.bListing = 0;

		KY_URIFixerObject.count++;

		this.strWait = 'oading';

		this.doc = document;

	    this.URL = "http://cms.herbalgram.org/";

		this.URL2 = 0;

		this.pageNameToAdd = 0;

		this.pageNameToAdd2 = 0;//support hg dep + ...

		this.divList = new Array('');

		this.affiliationURL = "http://cms.herbalgram.org/herbstream/";

	}

	

KY_URIFixerObject.prototype.setAffiliateName = function(){



	var x = window.location.href.split(this.affiliationURL);

	if (x[1])

	   var y = x[1].split("/"); 

	

	if (y){

		this.affName = y[0];	

	    this.contentSectionName = y[1];

	}

}



KY_URIFixerObject.prototype.execute = function(){	

		

	this.setAffiliateName();

	for (var j=0; j < this.divList.length; j++)

	{	

		

		 if (!this.divList[j].done)

		 {

			this.divList[j].done = 1;

			this['ky_OverRideSearchTimer_'+this.divList[j]]  = 

			setInterval('KY_URIFixerObject.arrInst['+this.iIndex+'].postExecute("'+this.divList[j]+'")' , 780);

		 }

	

	}

}



KY_URIFixerObject.prototype.postExecute = function(dId){



	if (this.doc.getElementById('topFilter'))

		    this.doc.getElementById('topFilter').style.display = "none";



	if (this.bDebug)

	  this.doc.title = 'postExecute'+dId;

	  

	if (this.doc.getElementById(dId) && this.doc.getElementById(dId).innerHTML.indexOf(this.strWait) == -1)

	{	 

			clearInterval (this['ky_OverRideSearchTimer_'+dId]);

			this.doc.getElementById(dId).style.display="none";

			this.KY_fixSearchResultsURI(dId);

	}

	else

	{

			if (this.bDebug)

			{

				x = new Date();

				this.doc.title = 'Still waiting the results ..['+ x.getTime()+']';     

			}

	}	

}







KY_URIFixerObject.prototype.KY_fixSearchResultsURI = function (dId) {

	if (this.bDebug)

		this.doc.title = 'Fixing Links'; 

		

	lklis = this.doc.getElementById(dId).getElementsByTagName("a");

	for ( var i=0; i < lklis.length; i++)

	{		

		var c=lklis[i];// get all links loop and fix based on the case and on the .js setting

		var dCaseLink= ""; //debug trick to know the case of the fixed link

		x = new Date();

		

		//general rules don't touch outside url's that are not poiting to cms.herbalgram.org(case listing and search result - item list)

		if ( (c.href.indexOf(document.domain) > -1  || c.href.indexOf('http://cms.herbalgr') > -1) &&  ( c.href.indexOf('#') == -1 || this.comme) )

		{

			if (this.PageNameS && !this.comme)//HI and herbal medici

			{ 

				var wlh = window.location.href;
var lc = wlh.charAt(wlh.length-1);
if (lc=='/')
        wlh = wlh + 'index.html';


				wlhN =  wlh.split(this.PageNameS);

				//this.doc.title = wlhN;

				if (wlh.indexOf(this.PageNameS) > -1 && c.href!="")

				{

					var y = wlhN[0];

					if (this.bDebug)

					   dCaseLink = '?hiOrhmediciOrComme='+x.getTime();

					   

					c.href = this.pageNameToAdd+c.href.replace(y , "")+dCaseLink;

				}

			}	



			if (this.comme)//commession special cases

			{ 

				var wlhN =  c.href.split(this.affName);

				var x = "";

		

				if (wlhN[1].indexOf (this.contentSectionName) > -1 )

				   var x = this.contentSectionName+"/";

				

				var ufgp = wlhN[1].replace(x, "");

				

				var nav = 0;// case ../ in the left menu when it is in the sub levels

				if (c.href.indexOf(this.contentSectionName) == -1)//case ../

					nav = 1;

				

				

				if (c.href.indexOf('/view.html?ufgp=') > -1)// internal anchors case

					ufgp  = ufgp.replace('/view.html?ufgp=',"");

				else

				    ufgp = ufgp.substring(1);

				

				

				if (!nav)//handle the relative links inside the sub folders of comme

				{

					if (ufgp.indexOf("/") == -1)

					{

						var fns = GetParam('ufgp');

						if (fns.indexOf("/") > -1)

							ufgp =  fns.substr ( 0 , fns.indexOf("/")+1)+ufgp;

					}

				}



				//alert (this.pageNameToAdd+ufgp);

				c.href = this.pageNameToAdd+ufgp;

			}	

			else if (c.href.indexOf(this.affName+"/issue") > -1)

			// case when having a link inside the article that point to the issue page (HG, HC)

			{

				 var r = c.href.split("/");

				 if (this.bDebug)

					   dCaseLink = '&issueID'+x.getTime();

				 c.href =r[r.length-1]+dCaseLink;

			}

			else if (!this.PageNameS &&  c.href.indexOf(".pdf")==-1)// listing and search and article and all normal cases

			{

		

				if (c.id != 'kY_noFlip')

				{

					var arrT = c.href.split("/");

					c.href=c.href.replace(this.URL,"");

					

					if (this.bDebug)

					   dCaseLink = '&normal'+x.getTime();

					   

					var k= "";

					if (!this.bListing)

						k = arrT[arrT.length-2]+"/";//support lisitng widget

						

					if (c.href.indexOf("#")==-1 && c.name != c.innerHTML)

					   c.href = this.pageNameToAdd+k+arrT[arrT.length-1]+dCaseLink;

					   

					   

					if (this.bDebug)

					   dCaseLink = '&searchCaseListing'+x.getTime();

					   

					if (this.bTextChange == 1 && c.href.indexOf("#")==-1 && c.name != c.innerHTML)//search anchors support

						c.innerHTML = arrT[arrT.length-1]+dCaseLink;

					

					if (c.href.indexOf('article') > -1 &&  window.location.href.indexOf('article.html') > -1 && !this.bListing)

					// article linking to an article

					{

						var artName   = c.href.substr (c.href.lastIndexOf('/')+1);

						

						

						var issueName =GetParam('ufgp');

						issueName = issueName.substring ( issueName.indexOf("=") , issueName.indexOf("/")+1);

						

						

						c.href = this.pageNameToAdd+issueName+artName;

						

					}

				}

			

				else

				{

				 if (this.bDebug)

				   dCaseLink = '&searchCaseListing'+x.getTime();	

				   

				 if (this.URL2)

				  c.href=c.href.replace(this.URL2,this.pageNameToAdd2)+dCaseLink;//support link that dont need to be flipped

				}

			}

			

			else if (this.bPdf)// HC pdf links support

			{

				var result_local = window.location.href.split("=");

				var isId = result_local[1].split("/");

				var arrH = c.href.split("/");

				

				var issueName = 'herbclip/'+isId[0]+'/pdfs/';//inhence here

				

				if (this.bDebug)

					   dCaseLink = '&searchCaseListing'+x.getTime();

					   

				if (isId[0] < 354 )

					issueName = "herbclip/pdfs/";//pdf link add support

				

				c.href = this.pdfPath + issueName  +  arrH[arrH.length-1]+dCaseLink;

			}

		}//end of general rules, don't touch external links

		

	}//end links for loop

	

	if (this.imagesPath)// fixing images add support to HG images after issue 79 to take from the project itself not the master

	{

		imgLInks = this.doc.getElementById(dId).getElementsByTagName("img");

		for ( var i=0; i < imgLInks.length; i++)

		{

			if (imgLInks[i].name != 'imageToBeReplaced')// do not take the images of the content swaper widget

			{

				var imgNames =  imgLInks[i].src.split("images");

				var imgName  = imgNames[imgNames.length-1];
				
				//REMARK: will try to add the issueName before imagse path.
				//this support was added for HG articles where Issue<79
				var issueName ='';
				issueName=GetParam('ufgp');
				if (issueName!=''){
					issueName = issueName.substring (0 , issueName.indexOf("/"));
					var issue = issueName.replace('issue','');
					if (issue<79)
						issueName = '';
					else
						issueName += "/";
				}
				if (this.comme)
					issueName = '';
				imgLInks[i].src = this.imagesPath+issueName+'images'+imgName;

			}

		}	

	}

	

	this.doc.getElementById(dId).style.display="block"; //Fixing links is done re show the content

	

	if (this.bDebug)

		this.doc.title = 'Fixing Links Done'; 

}