///////////////////////////////////////////////////////// Menu (submenu animation)

var SubMenuOpen = false;
	
function menuNavigation(url){

	var o = $('submenu_container');
	var o2 = $('mainmenu_container');

	if (!SubMenuOpen){

		SubMenuOpen	= true;

		var openSubMenu = new Fx.Morph('submenu_container', {duration: 150, transition: Fx.Transitions.Sine.easeOut});
		openSubMenu.start({ 'left': 0 });
		
		var closeMainMenu = new Fx.Morph('mainmenu_container', {duration: 150, transition: Fx.Transitions.Sine.easeOut});
		closeMainMenu.start({ 'left': -135 });
		
		closeMainMenu.onComplete = function (){
		
			if (url){

				window.open(url,'_self');
				
			}
			
		}

	} else {

		SubMenuOpen = false;
		
		var closeSubMenu = new Fx.Morph('submenu_container', {duration: 150, transition: Fx.Transitions.Sine.easeOut});
		closeSubMenu.start({ 'left': 135 });
		
		var openMainMenu = new Fx.Morph('mainmenu_container', {duration: 150, transition: Fx.Transitions.Sine.easeOut});
		openMainMenu.start({ 'left': 0 });

		closeSubMenu.onComplete = function (){

			if (url){
			
				window.open(url,'_self');	
				
			}

		}

	}

}

window.addEvent('domready', function() {
		
		var backbutton				= $('backButton');
		backbutton.onclick			= function() { menuNavigation('/news/'); return false; }
		
		var projectMenuButton		= $('projectMenuButton');
		projectMenuButton.onclick	= function() { menuNavigation('/projects/'); return false; }

});

///////////////////////////////////////////////////////// video

var VideoOpen = false;

function showHideVideo(){

	var o = $('videoContainer');

	if (!VideoOpen){

		o.style.display	= 'block';
		GoogleMapOpen	= true;

		var openVideoPopup = new Fx.Morph('videoContainer', {duration: 100, transition: Fx.Transitions.Sine.easeOut});
		openVideoPopup.start({ 'height': 340 });

	} else {

		VideoOpen = false;
		
		var closeVideoPopup = new Fx.Morph('videoContainer', {duration: 100, transition: Fx.Transitions.Sine.easeOut});
		closeVideoPopup.start({ 'height': 0 });

		closeVideoPopup.onComplete = function (){

			o.style.display	= 'none';

		}

	}

}

window.addEvent('domready', function() {

	// Google Maps display
	if ($('videoContainer')){
		
		// preload loader image
		pic1			= new Image(16,16); 
 		pic1.src		= '/images/layout/search-close-over.gif';

		// assign content to the Google Maps div
		var o			= $('videoContainer');
		o.innerHTML		= '<div><a href="/" id="close-video-link"><img src="/images/layout/search-close.gif" width="15" height="15" alt="" id="close-video-img" /></a><div><video></video>';

		// assign functions to the Google Maps link
		var l			= $('videoLink');
		l.onclick		= function() { showHideGoogleMap(); return false; }
		
		var l			= $('close-video-link');
		l.onmouseover	= function () { $('close-video-img').src = '/images/layout/search-close-over.gif'; }
		l.onmouseout	= function () { $('close-video-img').src = '/images/layout/search-close.gif'; }
		l.onclick		= function() { showHideVideo(); return false; }
	
	}

});

///////////////////////////////////////////////////////// Google Maps

var GoogleMapOpen = false;

function showHideGoogleMap(){

	var o = $('googleMapsDiv');

	if (!GoogleMapOpen){

		o.style.display	= 'block';
		GoogleMapOpen	= true;

		var openGoogleMapsPopup = new Fx.Morph('googleMapsDiv', {duration: 100, transition: Fx.Transitions.Sine.easeOut});
		openGoogleMapsPopup.start({ 'height': 340 });

	} else {

		GoogleMapOpen = false;
		
		var closeGoogleMapsPopup = new Fx.Morph('googleMapsDiv', {duration: 100, transition: Fx.Transitions.Sine.easeOut});
		closeGoogleMapsPopup.start({ 'height': 0 });

		closeGoogleMapsPopup.onComplete = function (){

			o.style.display	= 'none';

		}

	}

}

window.addEvent('domready', function() {

	// Google Maps display
	if ($('googleMapsDiv')){
		
		// preload loader image
		pic0			= new Image(16,16); 
 		pic0.src		= '/images/layout/search-close-over.gif';
	
		// assign content to the Google Maps div
		var o			= $('googleMapsDiv');
		o.innerHTML		= '<div><a href="/" id="close-googleMaps-link"><img src="/images/layout/search-close.gif" width="15" height="15" alt="" id="close-googleMaps-img" /></a><div><iframe src="/js/google_maps.php?t=' + t + '&id=' + id + '" width="580" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>';

		// assign functions to the Google Maps link
		var l			= $('googleMapsLink');
		l.onclick		= function() { showHideGoogleMap(); return false; }
		
		var l			= $('close-googleMaps-link');
		l.onmouseover	= function () { $('close-googleMaps-img').src = '/images/layout/search-close-over.gif'; }
		l.onmouseout	= function () { $('close-googleMaps-img').src = '/images/layout/search-close.gif'; }
		l.onclick		= function() { showHideGoogleMap(); return false; }
	
	}

});

///////////////////////////////////////////////////////// SEARCH

// start XML request
if (window.XMLHttpRequest){ var searchXML = new XMLHttpRequest(); } else { if (window.ActiveXObject){ var searchXML = new ActiveXObject("Microsoft.XMLHTTP"); } }
if (!searchXML && typeof XMLHttpRequest != 'undefined') { searchXML = new XMLHttpRequest(); }

// do a query
function doSearch() {

	var s = encodeURIComponent(document.getElementById('searchField').value);

	if (s.length > 1){

		var url = "/xml/search.php?s=" + s;
		searchXML.open("GET", url, true);
		searchXML.onreadystatechange = updateSearchResults;
		searchXML.send(null);

	} else {

		hideResults();

	}

}

// get results from XML
function updateSearchResults() {

	if (searchXML.readyState == 4) {

		var r = searchXML.responseXML;
		var c = '';
		
		// count the number of search results per category
		var c1 = r.getElementsByTagName('project').length;
		var c2 = r.getElementsByTagName('publication').length;
		var c3 = r.getElementsByTagName('news').length;
		var c4 = r.getElementsByTagName('static').length;
		var cT = c1 + c2 + c3 + c4;

		var s = r.getElementsByTagName('search')[0].firstChild.data;

		// output HTML for the popup window
		if (cT > 0){
		
			c += '<p class="close"><a href="javascript:resetSearchField();">close window</a><p>';
		
			if (c1 > 0){
				
				c += '<h1>projects</h1>';
				c += '<ul>';
				
				// get the information to fill up the divs
				for (i=0;i<c1;i++){
					
					titl	= r.getElementsByTagName('projecttitle')[i].firstChild.data;
					upd		= r.getElementsByTagName('projectupdate')[i].firstChild.data;
					url		= r.getElementsByTagName('projecturl')[i].firstChild.data;
					
					c		+= '<li><a href="/projects/' + url + '/?s=' + s + '">' + titl + '</a>';
					if (upd == 1){ c += ' <span>recently updated</span>'; }
					c 		+= '</li>';

				}
				
				c += '</ul>';
				
			}
			
			if (c2 > 0){
				
				c += '<h1>publications</h1>';
				c += '<ul>';
				
				// get the information to fill up the divs
				for (i=0;i<c2;i++){
					
					titl	= r.getElementsByTagName('publicationtitle')[i].firstChild.data;
					url		= r.getElementsByTagName('publicationurl')[i].firstChild.data;
					
					c		+= '<li><a href="/publications/' + url + '/?s=' + s + '">' + titl + '</a></li>';
	
				}
				
				c += '</ul>';
				
			}
			
			if (c3 > 0){
				
				c += '<h1>news items</h1>';
				c += '<ul>';
				
				// get the information to fill up the divs
				for (i=0;i<c3;i++){
					
					titl	= r.getElementsByTagName('newstitle')[i].firstChild.data;
					url		= r.getElementsByTagName('newsurl')[i].firstChild.data;
					
					c		+= '<li><a href="/news/' + url + '/?s=' + s + '">' + titl + '</a></li>';
	
				}
				
				c += '</ul>';
				
			}
			
			if (c4 > 0){
				
				c += '<h1>static pages</h1>';
				c += '<ul>';
				
				// get the information to fill up the divs
				for (i=0;i<c4;i++){
					
					titl	= r.getElementsByTagName('statictitle')[i].firstChild.data;
					url		= r.getElementsByTagName('staticurl')[i].firstChild.data;
					
					c		+= '<li><a href="/' + url + '/?s=' + s + '">' + titl + '</a></li>';
	
				}
				
				c += '</ul>';
				
			}
			
			c += '<span class="sub">click <a href="javascript:submitSearchForm();">here</a> for more results</span>';
			
			document.getElementById('searchdiv').innerHTML = c;
			
			showResults();
		
		} else {
			
			hideResults();
			
		}
		
	}
	
}

// hide the popup window 
function hideResults(){
	
	document.getElementById('searchdiv').innerHTML 		= '';
	document.getElementById('searchdiv').className 		= 'searchhidden';
	document.getElementById('sidebar').style.opacity	= 1;
	document.getElementById('sidebar').style.filter		= "alpha(opacity=100)";
	document.getElementById('content').style.opacity	= 1;
	document.getElementById('content').style.filter		= "alpha(opacity=100)";
	
}

// show the popup window
function showResults(){
	
	document.getElementById('searchdiv').className 		= 'searchvisible';
	document.getElementById('sidebar').style.opacity	= .3;
	document.getElementById('sidebar').style.filter		= "alpha(opacity=30)";
	document.getElementById('content').style.opacity	= .3;
	document.getElementById('content').style.filter		= "alpha(opacity=30)";

}

// focus on the search field -> empty it
function searchFieldFocus(){
	
	var o = document.getElementById('searchField');
	
	if (o.value == 'search'){
	
		o.value = '';
	
	}
	
	o.className = 'searchField_focus';

}

// click away from the search field -> hide results
function searchFieldBlur(){

	var o = document.getElementById('searchField');

	if (o.value == ''){

		o.value = 'search';
		hideResults();

	}

	o.className = 'searchField_blur';

}

// reset the search field and hide results
function resetSearchField(){
		
	document.getElementById('searchField').value = 'search';
	hideResults();

}

// go and get search results -> form submit
function submitSearchForm(){
	
	 document.getElementById('searchForm').submit();

}

///////////////////////////////////////////////////////// Slideshow and thumbnails

// popup openen
function openImage(i,w,h){

	var intLeft		= (screen.availWidth / 2) - (w / 2 );
	var intTop		= (screen.availHeight / 2) - (h / 2 );
	var strAttrib	= 'toolbar=0,location=0,resizable=0,scrollbars=0,statusbar=0,'+ 'width=' + w + ',height=' + h + ',top=' + intTop + ',left=' + intLeft;
	var objWin		= window.open ('/js/image.php?i=' + i + '', 'popup', strAttrib);

}

// scroller
function scrollDiv(e,o,c) {
	
	var obj		= document.getElementById(o);
	var cont	= document.getElementById(c);

	var posy	= 0;

	var objh	= obj.offsetHeight;
	var conth	= cont.offsetHeight;
	var conty	= cont.offsetTop;

	cont.style.overflow = 'hidden';

	if (objh > conth){
	
		if (!e) var e = window.event;
		
		if (e.pageY) {
		
			posy	= e.pageY;
			conty	= findPosY(cont);
		
		} else if (e.clientY) 	{
		
			var de	= document.documentElement;
			var b	= document.body;
			posy	= e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
			conty	= findPosY(cont);
		}
	
		var relposy	= posy - conty;
		var relh	= objh - conth;
		var newpos 	= Math.ceil((relposy / conth) * relh);
	
		obj.style.marginTop = '-' + newpos + 'px';
		
	}

}

// get the Y position of an object
function findPosY(obj){

	var curtop = 0;
	
	if(obj.offsetParent){
	
		while(1){
		
			curtop += obj.offsetTop;
			
			if(!obj.offsetParent){
			
				break;
	
			}
	
			obj = obj.offsetParent;
	
		}
	
	} else if(obj.y){
	
		curtop += obj.y;
	
	}
	
	return curtop;

}

/*
Author:
	luistar15, <leo020588 [at] gmail.com>
License:
	MIT License

Class
	viewer v0.9 (rev.08-12-08)

Arguments:
	items: dom collection | required
	parameters - see Parameters below

Parameters:
	sizes: obj | item sizes (px) | default: {w:480,h:240}
	mode: string OR array | 'rand','top','right','bottom','left','alpha' | default: 'rand'
	modes: array | default: ['top','right','bottom','left','alpha']
	fxOptions: object | Fx.Tween options | default: {duration:500}
	interval: int | for periodical | default: 5000

Methods:
	previous(manual): walk to previous item
		manual: bolean | default:false
	next(manual): walk to next item
		manual: bolean | default:false
	play(wait): auto walk items
		wait: boolean | required
	stop(): stop auto walk
	walk(item,manual): walk to item
		item: int | required
		manual: bolean | default:false

Requires:
	mootools 1.2 core
*/

var viewer = new Class({

	mode: 'alpha',
	modes: ['top','right','bottom','left','alpha'],
	sizes: {w:640,h:300},
	fxOptions: {duration:500},
	interval: 4000,

	initialize: function(items,options){
		if(options) for(var o in options) this[o]=options[o];
		//
		if(this.buttons){
			this.buttons.previous.addEvent('click',this.previous.bind(this,[true]));
			this.buttons.next.addEvent('click',this.next.bind(this,[true]));
		}
		this._current = 0;
		this._previous = null;
		this.items = items.setStyle('display','none');
		this.items[this._current].setStyle('display','block');
		this.disabled = false;
		this.attrs = {
			left: ['left',-this.sizes.w,0,'px'],
			top: ['top',-this.sizes.h,0,'px'],
			right: ['left',this.sizes.w,0,'px'],
			bottom: ['top',this.sizes.h,0,'px'],
			alpha: ['opacity',0,1,'']
		};
		this.rand = this.mode=='rand';
		this.sequence = typeof(this.mode)=='object' ? this.mode : false;
		this.curseq = 0;
		this.timer = null;
	},

	walk: function(n,manual){
		if(this._current!==n && !this.disabled){
			this.disabled = true;
			if(manual){
				this.stop();
			}
			if(this.rand){
				this.mode = this.modes.getRandom();
			}else if(this.sequence){
				this.mode = this.sequence[this.curseq];
				this.curseq += this.curseq+1<this.sequence.length ? 1 : -this.curseq;
			}
			this._previous = this._current;
			this._current = n;
			var a = this.attrs[this.mode].associate(['p','f','t','u']);
			for(var i=0;i<this.items.length;i++){
				if(this._current===i){
					this.items[i].setStyles($extend({'display':'block','z-index':'2'},JSON.decode('{"'+a.p+'":"'+a.f+a.u+'"}')));
				}else if(this._previous===i){
					this.items[i].setStyles({'z-index':'1'});
				}else{
					this.items[i].setStyles({'display':'none','z-index':'0'});
				}
			}
			this.items[n].set('tween',$merge(this.fxOptions,{onComplete:this.onComplete.bind(this)})).tween(a.p,a.f,a.t);
		}
	},

	play: function(wait){
		this.stop();
		if(!wait){
			this.next();
		}
		this.timer = this.next.periodical(this.interval,this,[false]);
	},

	stop: function(){
		$clear(this.timer);
	},

	next: function(manual){
		this.walk(this._current+1<this.items.length ? this._current+1 : 0,manual);
	},

	previous: function(manual){
		this.walk(this._current>0 ? this._current-1 : this.items.length-1,manual);
	},

	onComplete: function(){
		this.disabled = false;
		this.items[this._previous].setStyle('display','none');
		if(this.onWalk) this.onWalk(this._current);
	}
});