
includeJsFile("/lib/ajax/util.js");
includeJsFile("/lib/ajax/sarissa/sarissa.js");

var headHeight;
var newsHeight;
var nHeight;
var infoDivTop = 235;
var bodyHeight = 228;


function getWinSize(){
	var iWidth = 0, iHeight = 0;

	if (document.documentElement && document.documentElement.clientHeight){
		iWidth = parseInt(document.documentElement.offsetWidth,10);
		iHeight = parseInt(document.documentElement.offsetHeight,10);
	}
	else if (document.body){
		iWidth = parseInt(document.body.offsetWidth,10);
		iHeight = parseInt(document.body.offsetHeight,10);
	}

	return {width:iWidth, height:iHeight};
}

function includeJsFile(file){
	var head = document.getElementsByTagName('head')[0];
	var script = document.createElement('script');
	script.setAttribute('src', file);
	script.setAttribute('language', 'javascript');
	script.setAttribute('type', 'text/javascript');
	head.appendChild(script);
	
	return false;
}

/*
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
    		}
  	}
}

addLoadEvent(initAjax); 
*/
function initAjax(){
	/* try to istanziate XMLHttprequest */
	if( getXMLhttp() == null ){
		return;
	}
	
	if(!Sarissa){
		alert("There're some problem with Sarissa library");
		return;
	}
	
	/* FAQ Ajax process */
	initFaqLink();
	initNewsLink();
	initHighLightsLink();
	initGalleryLink();
	
	
	
}

function initFaqLink(){
	var faqLink = document.getElementsByName("faqlink");
	for(var i=0; i<faqLink.length; i++){
		var l = faqLink[i].href;
		var qs = l.substring(l.indexOf('#')+1);
		qs = qs.split("-");
		id = qs[0];
		lang = qs[1];
		faqLink[i].href = "javascript:appFaqCtrl('"+id+"', '"+lang+"')"; 
	}
}

function appFaqCtrl(id, lang){
	var url = "/lib/ajax/getFaqAnswer.php";
	url += "?id="+id+"&lang="+lang;
	
	var ul = document.getElementById("faq");
	for(var i=0; i<ul.childNodes.length; i++){
		var lastChild = ul.childNodes[i].lastChild 
		if( lastChild.tagName == "UL"){
			var cls = ul.childNodes[i].lastChild.getAttribute("class");
			if(cls == "show"){
				ul.childNodes[i].lastChild.setAttribute("class", "hide");
				ul.childNodes[i].lastChild.setAttribute("className", "hide");
			}
		}
	}
	
	var faqHttp = new ajaxRequest("GET", url, "", false, null);
	if(faqHttp.readyState == 4 && faqHttp.status == 200){
		faqHttpResult = faqHttp.responseText;
	}
	
	var faqAnUl = document.getElementById("answer"+id);
	faqAnUl.setAttribute("class", "show");
	faqAnUl.setAttribute("className", "show");
	var li = faqAnUl.firstChild;
	li.innerHTML = faqHttpResult;		
}

function initNewsLink(){
	var newsOpen = document.getElementById("newsLinkOpen");
	
	if(newsOpen != null){
		var qs = newsOpen.href.substring(newsOpen.href.indexOf('#')+1);
		qs = qs.split("-");
		id = qs[0];
		lang = qs[1];
		color = qs[2];
		newsOpen.href = "javascript:appNewsCtrlShow('"+id+"', '"+lang+"', '"+color+"')";
	}
}

function appNewsCtrlShow(id, lang, color){
	var url = "/lib/ajax/getNews.php";
	url += "?id="+id+"&lang="+lang;
	
	var newsHttp = new ajaxRequest("GET", url, "", false, null);
	if(newsHttp.readyState == 4 && newsHttp.status == 200){
		var newsHttpResult = newsHttp.responseText;
	}
	
	var newsDiv = document.getElementById("content_full");
	newsDiv.setAttribute("id", "content_full_show");
	//newsDiv.setAttribute("className", "show");
	var news = newsHttpResult;
	newsDiv.innerHTML = news;
	
	
	newsHeight = newsDiv.offsetHeight+10;
	
	var headDiv = document.getElementById("home_header");
	headHeight = headDiv.offsetHeight;
	
	var h1 = headHeight+newsHeight;
	headDiv.style.height = h1+'px';
	
	var nDiv = document.getElementById("home_news");
	nHeight = nDiv.offsetHeight;
	h2 = nHeight + newsHeight;
	if (!window.innerWidth){
		//var oWinSize = getWinSize();
		nHeight = 200;
		h2 = nHeight + newsHeight + 21;
	}
	
	
		
	nDiv.style.height = h2+'px';
	var infoDiv = document.getElementById("home_info");
	var newTop = infoDivTop + newsHeight;
	infoDiv.style.top = newTop+'px';
	
	var fillDiv = document.getElementById("home_header_fillin");
	fillDiv.setAttribute("id", "home_header_fillin_show");
	
	fillHeight = newsHeight+10;
	
	fillDiv.style.height = fillHeight+'px';
	
	var newsOpen = document.getElementById("newsLinkOpen");
	newsOpen.setAttribute("class", "hide");
	newsOpen.setAttribute("className", "hide");

	var ul = document.getElementById("home_navbar");
	var lis = ul.childNodes;
	for(var i=0; i<lis.length; i++){
		var a = lis[i].firstChild;
		if(a.tagName == "A"){
			a.setAttribute("class", "linkNavbarHomeExp");
			a.setAttribute("className", "linkNavbarHomeExp");
			id = a.getAttribute("id");
			a.setAttribute("onMouseOver", "rollOver('"+id+"', '/img/arrow/arrow-up_hover.gif', 'home')");
		}
		var img = a.lastChild;
		if(img.tagName == "IMG"){
			img.setAttribute("src", "../img/arrow/arrow-up.gif");
		}
		var ulhide = lis[i].lastChild;
		if(ulhide.tagName == "UL"){
			ulhide.setAttribute("class", "hide");
			ulhide.setAttribute("className", "hide");
			var subli = ulhide.firstChild;
			var subdiv = subli.childNodes;
			for(var k=0; k<subdiv.length; k++){
				if(subdiv[k].tagName == "DIV"){
					subdiv[k].setAttribute("style", "border-top: 1px solid white");
				}
			}
		}
	}

	var body = document.getElementById("home_body");
	body.style.height = '10px';
			
}

function appNewsCtrlShowClose(){
	var headDiv = document.getElementById("home_header");
	headDiv.style.height = headHeight+'px';
	
	var nDiv = document.getElementById("home_news");
	h2 = nHeight;
	if (!window.innerWidth){
		//var oWinSize = getWinSize();
		h2 = nHeight + 36;
	}
	nDiv.style.height = h2+'px';
	
	var infoDiv = document.getElementById("home_info");
	infoDiv.style.top = infoDivTop+'px';
	
	var fillDiv = document.getElementById("home_header_fillin_show");
	fillDiv.setAttribute("id", "home_header_fillin");
	
	var newsOpen = document.getElementById("newsLinkOpen");
	newsOpen.setAttribute("class", "show");
	newsOpen.setAttribute("className", "show");
			
	var newsOpen = document.getElementById("newsLinkClosed");
	newsOpen.setAttribute("class", "hide");
	newsOpen.setAttribute("className", "hide");
		
	var newsDiv = document.getElementById("content_full_show");
	newsDiv.setAttribute("id", "content_full");
	
        var ul = document.getElementById("home_navbar");
        var lis = ul.childNodes;
        for(var i=0; i<lis.length; i++){
              var a = lis[i].firstChild;
              if(a.tagName == "A"){
	              a.setAttribute("class", "linkNavbarHome");
		      a.setAttribute("className", "linkNavbarHome");
		      a.setAttribute("onMouseOver", "");								}
	      var img = a.lastChild;
               if(img.tagName == "IMG"){
		      img.setAttribute("src", "../img/arrow/arrow-bottom_hover.gif");
               }
																		      
		var ulshow = lis[i].lastChild;
                if(ulshow.tagName == "UL"){
                        ulshow.setAttribute("class", "show");
                        ulshow.setAttribute("className", "show");
			ulshow.style.top = '15px';
			var subli = ulshow.firstChild;
			subli.onmouseover = function(){}
			subli.onmouseout = function(){}
			var subdiv = subli.childNodes;
			for(var k=0; k<subdiv.length; k++){
				if(subdiv[k].tagName == "DIV"){
					subdiv[k].setAttribute("style", "");
				}
			}
                }
        }

	var body = document.getElementById("home_body");
	body.style.height = bodyHeight+'px';
}

function initHighLightsLink(){
	var prevLink = document.getElementById("hprev");
	if(prevLink != null){
		var qs = prevLink.href.substring(prevLink.href.indexOf('#')+1);
		qs = qs.split("-");
		id = qs[0];
		lang = qs[1]
		op = qs[2];
		prevLink.href = "javascript:appHighLightCtrl('"+id+"', '"+lang+"', '"+op+"')";
	}
	
	var nextLink = document.getElementById("hnext");
	if(nextLink != null){
		var qs = nextLink.href.substring(nextLink.href.indexOf('#')+1);
		qs = qs.split("-");
		id = qs[0];
		lang = qs[1]
		op = qs[2];
		nextLink.href = "javascript:appHighLightCtrl('"+id+"', '"+lang+"', '"+op+"')";
	}
}

function appHighLightCtrl(id, lang, op){
	var url = "/lib/ajax/getHighLights.php";
	url += "?id="+id+"&lang="+lang+"&op="+op+"&req=text";
	
	var highHttp = new ajaxRequest("GET", url, "", false, null);
	if(highHttp.readyState == 4 && highHttp.status == 200){
		var highHttpResult = highHttp.responseText;
	}
	
	var highDiv = document.getElementById("body_highlights");
	highDiv.innerHTML = highHttpResult;

	url = "/lib/ajax/getHighLights.php";
	url += "?id="+id+"&lang="+lang+"&op="+op+"&req=color";
	var highColorHttp = new ajaxRequest("GET", url, "", false, null);
	if(highColorHttp.readyState == 4 && highHttp.status == 200){
		var highColorHttpResult = highColorHttp.responseText;
	}
	
	highDiv.setAttribute("style", "background-color: #"+highColorHttpResult+";"); 
	
}

function initGalleryLink(){
	var prevLink = document.getElementById("galleryPrev");
	if(prevLink != null){
		var qs = prevLink.href.substring(prevLink.href.indexOf('#')+1);
		qs = qs.split("-");
		id = qs[0];
		op = qs[1];
		section = qs[2];
		subsection = qs[3];
		arrow = qs[4];
		prevLink.href = "javascript:appGalleryCtrl('"+id+"', '"+op+"', '"+section+"', '"+subsection+"', '"+arrow+"')";
	}
	
	var nextLink = document.getElementById("galleryNext");
	if(nextLink != null){
		time = window.setInterval("appGallerySlideShow()", 5000);
		var qs = nextLink.href.substring(nextLink.href.indexOf('#')+1);
		qs = qs.split("-");
		id = qs[0];
		op = qs[1];
		section = qs[2];
		subsection = qs[3];
		arrow = qs[4];
		nextLink.href = "javascript:appGalleryCtrl('"+id+"', '"+op+"', '"+section+"', '"+subsection+"', '"+arrow+"')";
	}
}

function appGalleryCtrl(id, op, sec, subsec, arrow){
	var url = "/lib/ajax/getGallery.php";
	url += "?index="+id+"&op="+op+"&section="+sec+"&subsection="+subsec+"&arrow="+arrow;
	
	var galleryHttp = new ajaxRequest("GET", url, "", false, null);
	if(galleryHttp.readyState == 4 && galleryHttp.status == 200){
		var galleryHttpResult = galleryHttp.responseText;
	}
	
	var galleryDiv = document.getElementById("body_header_gallery");
	galleryDiv.innerHTML = galleryHttpResult;
	
}

function appGallerySlideShow(){
	var galleryPrev = document.getElementById("galleryNext");
	var galleryHref = galleryPrev.href;
	galleryHref = galleryHref.substr(11);
	eval(galleryHref);
}
