var oldObj = null;
var oldListObj = null;
var effects = new Array();
var exendGallery = null;
var opened = false;
var hideMenu = null;
var ended = false;

function extLink(){
	if(!document.getElementsByTagName) return;
	l=document.getElementsByTagName("a");
	for(i=0;i<l.length;i++){
		if(l[i].className.indexOf("ext")!=-1){
			img = document.createElement("img");
			img.src = "../images/ext.gif";
			l[i].title="External link";
			l[i].onclick=function(){window.open(this.href);return(false)};
			l[i].appendChild(img);
		}
	}
}

function applySelect(obj){
	li = obj.parentNode;
	a = li.firstChild;
	if(a.className != "lnksSelect") a.className = "lnksSelect";
	else a.className = "";
}

function expand(){
	boxid = this.href.substr(this.href.lastIndexOf("#")).replace("#","");
	if(oldListObj == null){
		oldListObj = boxid;
	}else if(oldListObj == boxid){
		oldListObj = null
	}else if(oldListObj != null){
		effects[oldListObj].toggle();
		applySelect(document.getElementById(oldListObj));
		oldListObj = boxid;
	}
	effects[boxid].toggle();
	applySelect(this);
}

function initExpandList(){
	uls = document.getElementsByTagName("ul");
	for(i=0;i<uls.length;i++){
		if(uls[i].className != "expandible") continue;
		n = uls[i].childNodes.length;
		for(k=0;k<n;k++){			
			if(uls[i].childNodes[k].nodeType == 1){
				li = uls[i].childNodes[k];
				if(li.firstChild.nodeType !=1) continue;
				div = li.childNodes[1];
				effects[div.id] =  new fx.Combo(div.id, {height: true, opacity: true, duration: 1300});
				effects[div.id].toggle();
				li.firstChild.onclick = expand;
			}else continue
		}
	}
}

function showcontent(){
	boxid = this.href.substr(this.href.lastIndexOf("#")).replace("#","");
	if(oldObj != null)
		document.getElementById(oldObj).style.display = 'none';	
	document.getElementById(boxid).style.display = 'block';	
	document.getElementById("selected").id ="";
	this.id = "selected";
	oldObj = boxid;
	if(location.href.indexOf("gallery.php") > -1) initGalleryNav();
}

function swapimage(){
	path = this.style.backgroundImage;
	if(path.indexOf("_h") > -1)
		this.style.backgroundImage = path.replace("_h","");
	else
		this.style.backgroundImage = path.replace(".jpg","_h.jpg");
}

function showMenu(){
	obj = document.getElementById("menu");
	if(opened){
		obj.style.display = "block";
		opened = false;
	}else{	
		obj.style.display = "none";
		opened = true;
	}
}

function reInitLightBox(){
	var anchors = document.getElementsByTagName('a');
	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
	var anchor = anchors[i];
	var relAttribute = String(anchor.getAttribute('rel'));
	// use the string.match() method to catch 'lightbox' references in the rel attribute
	if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
			anchor.onclick = function () {myLightbox.start(this); return false;}
		}
	}
}

function checkEffect(content){
	if(ended){
		effects[oldObj].toggle();
		ended = false;
	}
}

function overwrite(content){
	document.getElementById(oldObj).innerHTML = content;	
	initGalleryNav();
	reInitLightBox();
}

function getdata(){
	if(effects.length > 0) effects[oldObj].toggle();
	sendGetReq(this.href);
	ended = true;
	return false;
}

function initGalleryNav(){
	div =  document.getElementsByClassName("paging");	
	for(j=0;j<div.length;j++){
		for(i=0;i<div[j].childNodes.length;i++){
			el = div[j].childNodes[i];
			if(el.nodeType != 1) continue;
			el.onclick = getdata;
		}
	}
}

function showHideMenu(){
	if(opened){
		exendGallery.custom(765,387);
		document.getElementById("lnkg").innerHTML = "Hide Menu";
		hideMenu.toggle();
		opened = false;
	}else{	
		hideMenu.toggle();
		document.getElementById("lnkg").innerHTML = "Show Menu";
		exendGallery.custom(387,765);
		opened = true;
	}
}

function initGallery(){
	document.getElementById("lnkg").onclick = showHideMenu;
	exendGallery = new fx.Width("content",{duration: 1500});
	hideMenu = new fx.Combo("menu", {height: false, width: true, opacity: true, duration: 1500});
	showHideMenu();
	div =  document.getElementById("cbox");	
	for(i=0;i<div.childNodes.length;i++){
		el = div.childNodes[i];
		if(el.nodeType != 1) continue;
		effects[el.id] = new fx.Opacity(el.id, {duration: 1500, onComplete: checkEffect});
	}
	initGalleryNav();
}

function initSubmenu(){
	anchors = document.getElementsByTagName("a");
	c = 0;
	for(i=0;i<anchors.length;i++){
		if(anchors[i].className != "sub")	continue;
		boxid = anchors[i].href.substr(anchors[i].href.lastIndexOf("#")).replace("#","");
		anchors[i].onclick = showcontent;
		if(c > 0)
			document.getElementById(boxid).style.display = 'none';
		else{ anchors[i].id = "selected";
			oldObj = boxid;
			c++;}
	}
}
function initEventsLink(){
	anchors = document.getElementsByTagName("a");
	c = 0;
	for(i=0;i<anchors.length;i++){
		if(anchors[i].className != "eventlnk")	continue;
		anchors[i].onclick = function(){
			boxid = 'gallery';
			if(oldObj != null) document.getElementById(oldObj).style.display = 'none';	
			document.getElementById(boxid).style.display = 'block';	
			oldObj = boxid;	
			sendGetReq(this.href);
			return false;
		};
	}
}	

function preloadHoverImg(path){
	var img = new Image();
	img.src = path;
}

function initMenu(){
	lang = document.getElementById("activelang").innerHTML.toLowerCase();
	ul = document.getElementById("menu");
	ul.firstChild;
	li = ul.firstChild;
	page = location.href;
	for(i=0;i<ul.childNodes.length;i++){
		if(li.firstChild.nodeType != 1) continue;
		span = document.createElement("span");
		a = li.firstChild;		
		img = a.href.substr(a.href.lastIndexOf("/")+1).replace(".php",".jpg");
		preloadHoverImg(img.replace(".jpg","_h.jpg"));
		if(page.indexOf(a.href) > -1)
			img = img.replace(".jpg","_h.jpg");
		else{
			span.onmouseover = swapimage;
			span.onmouseout = swapimage;
		}	
		span.style.backgroundImage = "url(../images/"+lang+"/"+img+")";
		a.appendChild(span);
		li = li.nextSibling;
	}
}
		function checkHash(){
			var found = false;
			toggles.each(function(h3, i){
				if (window.location.href.indexOf(h3.title) > 0) {
					myAccordion.showThisHideOpen(stretchers[i]);
					found = true;
				}
			});
			return found;
		}

		window.onload=function(){
									initMenu();
									initSubmenu();
									initLightbox();
									if(navigator.userAgent.indexOf("Safari/312.6") <0)
										initExpandList();
									if(location.href.indexOf("gallery.php") > -1) {
										initGallery();										
									}
									if(location.href.indexOf("events.php") > -1) {
										initEventsLink();										
									}
									extLink();
								}
