function D(n) { return document.getElementById(n); }

function interview_go(id) {
	if (interviews == null || interviews.length == 0) return;
	var interview = interviews[id];
	if (!interview) {
		return interview_go(0);
	}
	
	interviewid = id;
	
	var html = _render_interview(interview);
	
	D("interview").innerHTML = html;
	
	html = _render_interview_nav(interviewid);
	
	D("interviewnav").innerHTML = html;
}

function _render_interview_nav(id) {
	var html = "";
	if (interviews.length <=1 ) return html;
	
	html += '<table width=100%><tr>';
	
	if (id > 0) {
		html += "<td align=" + (lang=="en" ? "left" : "right") + "><a href='javascript:void(interview_go(" + (id - 1) + "))'><img src='images/" + (lang=="en" ? "left" : "right") + "arrow.png' width=13 height=11 border=0/></a></td>";
	}
	
	if (id < interviews.length - 1) {
		html += "<td align=" + (lang=="en" ? "right" : "left") + "><a href='javascript:void(interview_go(" + (id + 1) + "))'><img src='images/" + (lang=="en" ? "right" : "left") + "arrow.png' width=13 height=11 border=0/></a></td>";
	}
	html += "</tr></table>";
	return html;
	
}

function _render_interview(obj) {
	var html = "";
	
	
	html += "<div class=interviewtitle ";
	if (obj.title.match(/&#/)) { 
	html += "align=justify>";
	 } else {
	html += "dir=ltr align=justify>";		
	 } 
	html += "<a href='index.php?id=6&aid=" + obj.id + "'>" + obj.title.replace(/BRLINE/gi, "<br>")  +  "</a></div><div class=interviewdate ";
	if (obj.title.match(/&#/)) { 
	html += "align=justify>";
	 } else {
	html += "dir=ltr align=justify>";		
	 } 	
	html += obj.date  + "</div>";
	
	html += "<div class=interview ";
	
	 if (obj.summary.match(/&#/)) { 
	html += "align=justify>";
	 } else {
	html += "dir=ltr align=justify>";		
	 } 
	if (obj.pic != "") {
		
				if (obj.spic_align != "center") {
		html += "<img src='" + obj.pic + "' align="+ obj.spic_align +" border=1 style=\"margin-"+ (lang=="en" ? "left" : "right") +":7px\"/>";	
	} else {
				html += "<div align=\"center\"><img src='" + obj.pic + "' border=1 style=\"margin-top:5px\"/></div>";	
	}
	
	}
	
	html +=  obj.summary + "...";
	
	if (lang == "en") {
		html += "<a href='index.php?id=6&aid=" + obj.id + "'>more</a>";
	} else {
		
		html += "<a href='index.php?id=6&aid=" + obj.id + "'>&#1578;&#1578;&#1605;&#1577;</a>";
	}
	
	html += "</div>";
	
	return html;
}


function indepth_go(id) {

	if (indepth == null || indepth.length == 0) return;
	var ind = indepth[id];
	if (!ind) {
		return indepth_go(0);
	}
	
	indepthid = id;
	
	var html = _render_indepth(ind, indepthid);
	
	D("indepth").innerHTML = html;
	
	html = _render_indepth_nav(indepthid);
	
	D("indepthnav").innerHTML = html;
}

function _render_indepth_nav(id) {
	var html = "";
	if (indepth.length <=1 ) return html;
	
	html += '<table width=100%><tr>';
	
	if (id > 0) {
		html += "<td align=" + (lang=="en" ? "left" : "right") + "><a href='javascript:void(indepth_go(" + (id - 1) + "))'><img src='images/" + (lang=="en" ? "left" : "right") + "arrow.png' width=13 height=11 border=0/></a></td>";
	}
	
	if (id < indepth.length - 1) {
		html += "<td align=" + (lang=="en" ? "right" : "left") + "><a href='javascript:void(indepth_go(" + (id + 1) + "))'><img src='images/" + (lang=="en" ? "right" : "left") + "arrow.png' width=13 height=11 border=0/></a></td>";
	}
	html += "</tr></table>";
	return html;
	
}

function _render_indepth(obj, id) {
	var html = "";
	
	html += '<div align="center"><a href="' + obj.link + '" target="_new" onmouseover="hover_indepth(event, '+id+')" onmouseout="unhover_historical()"><img src="' + obj.pic + '" border="0"/></a></div>';	
		
	return html;
}

/*   *** */

function getMouseXY(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	
	if (e.pageX || e.pageY) 	{
		
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

  return {x:posx, y:posy};
}


function __getXY(obj) {
	var X = 0;
	var Y = 0;
	var tmp = obj;
	do {
		X += tmp.offsetX;
		Y += tmp.offsetY;
		tmp = tmp.offsetParent;		
	} while (tmp);
	
	return {x:X, y:Y};
}


/*   *** */


var curhistx = 0;
function render_historical() {
	var html = "<div id='historical_inner'><div id='historical_inner2'><table><tr>";
	for (var i=0; i < historical.length; i++) {
		html += "<td><img border=1 onmouseover='hover_historical(event, "+i+")' onmouseout='unhover_historical()' src='" + historical[i].pic + "' /></td>";
	}
	html += "</tr></table></div></div>";
	D("historical").innerHTML = html;
	
	/*
	if (lang == "en") {
		D("historical_inner").style.left = "687px";
		curhistx = 687;
	} else {
		curhistx = 0 - 687;
		D("historical_inner").style.left = curhistx + "px";
	}
	*/
	
	//animatehistorical();
}

var histid = 0;
function animatehistorical(dir) {
	histid = setInterval(function() {
		
		
		if (dir == "left") {
			curhistx += 2;
		} else {
			curhistx -= 2;
		}
		if (lang == "en") {
			if (curhistx > 0) curhistx = 0;
			else if (curhistx < 0 - D("historical_inner2").offsetWidth + 840) 
				curhistx = 0 - D("historical_inner2").offsetWidth + 840;
		} else {
			if (curhistx < 0) curhistx = 0;
			else if (curhistx > 0 + D("historical_inner2").offsetWidth - 840) 
				curhistx = 0 + D("historical_inner2").offsetWidth - 840;
		}
		D("historical_inner").style.left = curhistx + "px";
		
		
		
		}, 10);
}

function histstop() {
	clearInterval(histid);
}

function histleft() {
	animatehistorical("left");
}

function histright() {
	animatehistorical("right");
}

function unhover_historical() {
	//animatehistorical();
	D("historical_tag").style.display = "none";
	D("other_tag").style.display = "none";
	document.onmousemove = null;
	
}

function hover_historical(e, id) {
	clearInterval(histid);
	document.onmousemove = function (ev) {
		var obj = historical[id];
		_hover_historical(ev, obj.text, obj.bpic, obj.title, obj.pwidth, obj.pheight);
	}
	
}

function _hover_historical(e, text, bpic, title, pwidth, pheight) {
	
	var o = getMouseXY(e);
	// align="+ (lang=="en" ? "left" : "right")
	D("historical_tag").innerHTML = "<table cellpadding=4 cellspacing=1><tr><td align=center><img src="+bpic+" width="+pwidth+" height="+pheight+"></td></tr><tr><td dir="+ (lang=="en" ? "LTR" : "RTL")+"><div class=stitle>"+title+"</div><div align=justify>"+text+"</div></td></tr></table>";
	D("historical_tag").style.display = "block";
	D("historical_tag").style.position = "absolute";
	
	D("historical_tag").style.top = (o.y - D("historical_tag").offsetHeight - 10) + "px";
	D("historical_tag").style.left = (o.x - D("historical_tag").offsetWidth/2)  + "px";
	
}


function _hover_other(e, text) {
	
	var o = getMouseXY(e);
	
	D("other_tag").innerHTML = "<div style=padding:2px>"+text+"</div>";
	D("other_tag").style.display = "block";
	D("other_tag").style.position = "absolute";
	D("other_tag").style.width = "200px";
	D("other_tag").style.top = (o.y - D("other_tag").offsetHeight - 10) + "px";
	D("other_tag").style.left = (o.x - D("other_tag").offsetWidth/2)  + "px";
	
}


function hover_indepth(e, id) {
	
	clearInterval(histid);
	document.onmousemove = function (ev) {
		var obj = indepth[id];
		_hover_other(ev, obj.description);
	}
	
}


function _addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function sendtofriend() {
	D("sendform").style.display = D("sendform").style.display == "block" ? "none" : "block";
}

function commentbox() {
	D("commentbox").style.display = D("commentbox").style.display == "block" ? "none" : "block";
}


function mokh_go(id) {
	if (mokh == null || mokh.length == 0) return;
	var mok = mokh[id];
	if (!mok) {
		return;
	}
	
	mokhid = id;
	
	if (mokhid > mokh.length - 5) {
		mokhid = mokh.length - 5;
	}
	if (mokhid < 0) mokhid = 0;
	
	var html = "";
	for (var i = mokhid; i < mokhid + 5 && i < mokh.length; i++) {
		html += _render_mokh(mokh[i]);
	}
	
	D("mokh").innerHTML = html;
	
	html = _render_mokh_nav(mokhid);
	
	D("mokhnav").innerHTML = html;
}

function _render_mokh_nav(id) {
	var html = "";
	if (mokh.length <=1 ) return html;
	
	html += '<table width=100%><tr>';
	
	if (id > 0) {
		html += "<td align=" + (lang=="en" ? "left" : "right") + "><a href='javascript:void(mokh_go(" + (id - 5) + "))'><img src='images/" + (lang=="en" ? "left" : "right") + "arrow.png' width=13 height=11 border=0/></a></td>";
	}
	
	if (id < mokh.length - 5) {
		html += "<td align=" + (lang=="en" ? "right" : "left") + "><a href='javascript:void(mokh_go(" + (id + 5) + "))'><img src='images/" + (lang=="en" ? "right" : "left") + "arrow.png' width=13 height=11 border=0/></a></td>";
	}
	html += "</tr></table>";
	return html;
	
}

function _render_mokh(obj) {
	var html = "";
	html += '<table width=100% cellspacing=1 cellpadding=1><tr>';
	html += '<td valign="top" width="16" style="padding-top:10px"><img src="images/logo_s.png" width="18" height="16"></td><td> <div class="mokhtarat"><a href="index.php?id=7&aid=' + obj.id + '">' + obj.title + '</a></div></td>';
	html += '</tr></table>';
	return html;
}
