var is_ie6 = document.all && (navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1);

function toggleElements(tagname, display){
	var tags = document.getElementsByTagName(tagname);
	for(var i=0;i<tags.length;i++){
		tags[i].style.display=display;
	}
}

function showLogin(aclose){
	if ( fadeBusy == false ) {
		var login_popup = document.getElementById('login_popup');
		var login_popup_back = document.getElementById('login_popup_back');

		if ( aclose == true ) {
			loopOpacity('login_popup', 'login_popup_back', 100, 'down');
			fadeBusy = true;
		} else {
			if ( login_popup.style.display != 'block' ) {
				login_popup.style.display = 'block';
				login_popup_back.style.display = 'block';
				fadeBusy = true;
				loopOpacity('login_popup', 'login_popup_back', 0, 'up');
			} else {
				//login_popup.style.display = 'none';
				//login_popup_back.style.display = 'none';
				loopOpacity('login_popup', 'login_popup_back', 100, 'down');
				fadeBusy = true;
			}
		}
	}
}
var fadeSpeed = 5;
var fadeTime = 20;
var fadeBusy = false;
function loopOpacity(id, id2, cur, dir){

	var elm = document.getElementById(id);
	if ( id2 != '0' ) {
		var elm2 = document.getElementById(id2);
	}
	if ( dir == 'up' ) {
		if ( cur < 100 ) {
			setOpacity(elm, cur);
			if ( id2 != '0' ) {
				setOpacity(elm2, Math.round(cur/2));
			}
			setTimeout("loopOpacity('"+id+"', '"+id2+"', "+parseInt(cur+fadeSpeed)+", '"+dir+"');", fadeTime);
		} else {
			fadeBusy = false;
		}
	} else if ( dir == 'down' ) {
		if ( cur > 0 ) {
			setOpacity(elm, cur);
			if ( id2 != '0' ) {
				setOpacity(elm2, Math.round(cur/2));
			}
			setTimeout("loopOpacity('"+id+"', '"+id2+"', "+parseInt(cur-fadeSpeed)+", '"+dir+"');", fadeTime);
		} else {
			elm.style.display='none';
			if ( id2 != '0' ) {
				elm2.style.display='none';
			}
			fadeBusy = false;
		}
	}
}

function setOpacity(elm, opacity){
	if ( elm == null ) {return false;}
	elm.style.opacity = (opacity/100);
	elm.style.MozOpacity = (opacity/100);
	elm.style.filter = "alpha(opacity="+opacity+")";
}

var scrollSpeed = 2;
var scrollTimeout = 20;
var scrollEnabled = false;
var scrollBusy = false;

function portfolioScroll(dir){
	if ( scrollBusy == false ) {
		var home_portfolio_thumbs = document.getElementById('home_portfolio_thumbs');
		scrollEnabled = true;
		scrollLoop(dir);
	}
}

function portfolioScrollStop(){
	scrollEnabled = false;
}

function scrollLoop(dir){
	var home_portfolio_thumbs = document.getElementById('home_portfolio_thumbs');
	if ( dir == 'l' ) {
		home_portfolio_thumbs.scrollLeft = home_portfolio_thumbs.scrollLeft-scrollSpeed;
	} else if ( dir == 'r' ) {
		home_portfolio_thumbs.scrollLeft = home_portfolio_thumbs.scrollLeft+scrollSpeed;
	}
	if ( scrollEnabled == true ) {
		scrollBusy = true;
		setTimeout("scrollLoop('"+dir+"');", scrollTimeout);
	} else {
		scrollBusy = false;
	}
}

var canHideBubble = true;

function showPortfolioBubble(txt){
	document.getElementById('home_portfolio_thumbs_bubble').style.display='block';
	document.getElementById('home_portfolio_thumbs_bubble').innerHTML = txt;
	canHideBubble = false;
}

function hidePortfolioBubble(){
	canHideBubble = true;
	setTimeout("hidePortfolioBubble2();", 5);
}

function hidePortfolioBubble2(){
	if ( canHideBubble ) {
		document.getElementById('home_portfolio_thumbs_bubble').style.display='none';
	}
}

var isIE = document.all;
var mouseX = 0;
var mouseY = 0;

function getMouseXY(e)
{ 
	if (!e) e = window.event;
	if (e)
	{
		mouseX = isIE ? (e.clientX + document.body.scrollLeft) : e.pageX;
		mouseY = isIE ? (e.clientY + document.body.scrollTop) : e.pageY;
	}

	if ( mouseX < 250 ) {
		portfolioScroll('l');
		scrollSpeed = 1;
		if ( mouseX < 200 ) {
			scrollSpeed = 3;
		}
		if ( mouseX < 160 ) {
			scrollSpeed = 5;
		}
	} else if ( mouseX > 460 ) {
		portfolioScroll('r');
		scrollSpeed = 1;
		if ( mouseX > 510 ) {
			scrollSpeed = 3;
		}
		if ( mouseX > 550 ) {
			scrollSpeed = 5;
		}
	} else {
		portfolioScrollStop();
	}
	if ( document.getElementById('home_portfolio_thumbs_bubble') ) {
		document.getElementById('home_portfolio_thumbs_bubble').style.left = mouseX+'px';
	}
	/*window.status = mouseX + ' / ' + mouseY;*/

}

var ajax = new sack();

var ajax_busy = false;

var ajax_txt = '';
var ajax_home_update_img = '';

function setHomeUpdate(){
	var e = document.getElementById('home_div_update');
	var ajax_response = ajax.response;
	
	var ajax_response_split = ajax_response.split('<split>');
	
	cur_home_update_id = ajax_response_split[0];
	
	loopOpacity('home_div_update', '0', 100, 'down');
	loopOpacity('home_update_img', '0', 100, 'down');

	ajax_txt = ajax_response_split[1];
	ajax_home_update_img = ajax_response_split[2];

	setTimeout("delayedHomeUpdate('home_div_update');", 1000);
	setTimeout("delayedHomeUpdate('home_update_img');", 1000);
	
}

function delayedHomeUpdate(id){
	var e = document.getElementById(id);
	e.style.display='block';
	loopOpacity(id, '0', 0, 'up');
	if ( id == 'home_div_update' ) {
		e.innerHTML = ajax_txt;
	}
	if ( ajax_home_update_img ) {
	document.getElementById('home_update_img').src=ajax_home_update_img.replace('&amp;', '&');
	}
	ajax_busy = false;
}

function getHomeUpdate(dir){
	if ( ajax_busy == false ) {
		ajax_busy = true;
		ajax.requestFile = '/ajax.php?a=getHomeUpdate&d='+dir+'&i='+cur_home_update_id;
		ajax.onCompletion = setHomeUpdate;
		ajax.runAJAX();
	}
}


function setPortfolioPage(){
	var e = document.getElementById('home_div_update');
	var ajax_response = ajax.response;
	document.getElementById('portfolio_content').innerHTML = ajax_response;
	ajax_busy = false;
}

function getPortfolioPage(pagenum, cat){
	if ( ajax_busy == false ) {
		ajax_busy = true;
		ajax.requestFile = '/ajax.php?a=getPortfolioOverview&p='+pagenum+'&c='+cat;
		ajax.onCompletion = setPortfolioPage;
		ajax.runAJAX();
	}
}

var curPortfolioItem;

var scrollPortfolioBusy = false;

function scrollPortfolioPopupRight(newimgid, curpos){
	document.getElementById('portfolio_popup_foto').style.backgroundPosition=parseInt(curpos-20)+'px 0';
	if ( curpos == 20 ) {
		scrollPortfolioBusy = false;
		showPortfolioPopup(newimgid);
		return true;
	}
	scrollPortfolioBusy = true;
	if ( curpos == -460 ) {
		document.getElementById('portfolio_popup_foto').style.backgroundImage='url("'+portfolioitems[newimgid]['image'].replace('&amp;', '&')+'")';
		setTimeout("scrollPortfolioPopupRight("+newimgid+", 460);", 20);
	} else {
		if ( curpos > -461 ) {
			setTimeout("scrollPortfolioPopupRight("+newimgid+", "+parseInt(curpos-20)+");", 20);
		}
	}
}

function scrollPortfolioPopupLeft(newimgid, curpos){
	document.getElementById('portfolio_popup_foto').style.backgroundPosition=parseInt(curpos+20)+'px 0';
	if ( curpos == -20 ) {
		scrollPortfolioBusy = false;
		showPortfolioPopup(newimgid);
		return true;
	}
	scrollPortfolioBusy = true;
	if ( curpos == 460 ) {
		document.getElementById('portfolio_popup_foto').style.backgroundImage='url("'+portfolioitems[newimgid]['image'].replace('&amp;', '&')+'")';
		setTimeout("scrollPortfolioPopupLeft("+newimgid+", -460);", 20);
	} else {
		if ( curpos < 461 ) {
			setTimeout("scrollPortfolioPopupLeft("+newimgid+", "+parseInt(curpos+20)+");", 20);
		}
	}
}

function portfolioNext(){
	if ( scrollPortfolioBusy == false ) {
		if ( curPortfolioItem +1 == portfolioitems.length ) {
			if ( is_ie6 ) {
				showPortfolioPopup(0);
			} else {
				scrollPortfolioPopupRight(0, 0);
			}
		} else {
			if ( is_ie6 ) {
				showPortfolioPopup(curPortfolioItem +1);
			} else {
				scrollPortfolioPopupRight(curPortfolioItem +1, 0);
			}
		}
	}
}

function portfolioPrev(){
	if ( scrollPortfolioBusy == false ) {
		if ( curPortfolioItem == 0 ) {
			if ( is_ie6 ) {
				showPortfolioPopup(portfolioitems.length-1);
			} else {
				scrollPortfolioPopupLeft(portfolioitems.length-1, 0);
			}
		} else {
			if ( is_ie6 ) {
				showPortfolioPopup(curPortfolioItem-1);
			} else {
				scrollPortfolioPopupLeft(curPortfolioItem-1, 0);
			}
		}
	}
}

function showPortfolioPopup(pid){
	
	curPortfolioItem = pid;
	document.getElementById('portfolio_popup_container').style.display='block';
	document.getElementById('portfolio_popup_shadow').style.display='block';
	
	document.getElementById('portfolio_popup_back').style.display='block';
	
	document.getElementById('portfolio_popup_foto').style.backgroundImage='url("'+portfolioitems[pid]['image'].replace('&amp;', '&')+'")';
	
	document.getElementById('portfolio_popup_info').innerHTML=portfolioitems[pid]['text'];
	if ( is_ie6 ) {
		toggleElements('SELECT', 'none');
	}
	
}

function hidePortfolioPopup(){
	document.getElementById('portfolio_popup_container').style.display='none';
	document.getElementById('portfolio_popup_shadow').style.display='none';
	document.getElementById('portfolio_popup_back').style.display='none';
	if ( is_ie6 ) {
		toggleElements('SELECT', 'inline');
	}
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;
