var eyecatcher = false;
var click_sub = false;
var ie = false;
var random_sponsors;

/*---------------------------------------------------------------------
CUSTOM CODE VAN JOHAN 
---------------------------------------------------------------------*/
function expandXPBlock() {
	var fullURL = top.location.href;
	var indx = fullURL.indexOf('xp=');
	if (indx>0) {
		var xp = fullURL.substring(indx+3,indx+5);
		var obj = document.getElementById('xp'+xp);
		if (obj) {
		    obj.style.display='block';
		}
	}
}



/*---------------------------------------------------------------------
BROWSERCHECK (SAFARI == IE)
---------------------------------------------------------------------*/
function browserCheck(){
	ie = false;
	var browser = navigator.userAgent.toLowerCase();
	if (browser.indexOf("msie 6") != -1) ie = true;
}



/*---------------------------------------------------------------------
SIFR
---------------------------------------------------------------------*/
var sifrTitle = {
 	src: '/flash/sifr.swf'
};
function initSifr(){
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({sSelector:"body #sitemap h1", sFlashSrc:"flash/sifr.swf", sColor:"#343434,", sWmode:"transparent"}));
		sIFR.replaceElement(named({sSelector:"body h1", sFlashSrc:"flash/sifr.swf", sColor:"#ffffff,", sWmode:"transparent"}));		
	}
}



/*---------------------------------------------------------------------
HISTORY
---------------------------------------------------------------------*/
var StateManager = EXANIMO.managers.StateManager;
StateManager.onstatechange = function(e){
    StateManager.setTitle('MobileHCI 2008 : 10th edition');
    getContent(e.id, false);
}
function stripTags(input){
	var output = input.replace(/<\/?[^>]+(>|$)/g, "");
	return output;
}
function go_this(linktopass){	
	StateManager.setState(linktopass);
}
function getContent(hash, flash){
	var pars = stripTags(hash);
	if (pars === null) pars = 'sub1=1';
	new Ajax.Request('php/getcontent.php', { 
		method:'get', 
		evalScripts:true,
		parameters:pars, 
		onComplete:function(transport) {
			$('center').innerHTML = transport.responseText;
			initSifr();
			window.scrollTo(0,0);
			checkForProgram();
			checkForGoogleMaps();
			checkForFeedback();
			checkForOverview();
			checkForSearch();
			Behaviour.apply();
			new PeriodicalExecuter(function(pe) {
				Behaviour.apply();
				pe.stop();
			}, 0.5);
		}
	});
	if(!flash && pars != 'sub1=1'){
		eyecatcher_close();
	}
	setMenuActive(pars);
	setSitemapActive(pars);
}




/*---------------------------------------------------------------------
BEHAVOUR RULES
---------------------------------------------------------------------*/
var mainRules = {	
	'.searchForm' :
		function(element){
			element.onsubmit = function(){
				searchForm(this);
				return false;
			}
		} 
	};

var functionaliteitRules = {
	'.functionaliteit a.send' : 
		function(element){
			element.onclick = function(){
				loadSendFriend();
				return false;
			}
		},
	'.functionaliteit a.print' : 
		function(element){
			element.onclick = function(){
				window.print();
				return false;
			}
		}
	};

var speakerRules = {
	'div.speaker a.speaker' : 
		function(element){		
			element.onclick = function(){							
				var linktopass = this.href;
				linktopass = linktopass.split("?");
				linktopass = linktopass[1];			
				StateManager.setState(linktopass);									
				return false;
			}
		},
	'div.speaker_appearances a' :
		function(element){
			element.onclick = function(){
				var linktopass = this.href;
				linktopass = linktopass.split("?");
				linktopass = linktopass[1];			
				StateManager.setState(linktopass);
				return false;
			}
		}
	};

var rightNavRules = {
	'#rightNav #highlight a' :
		function(element){
			element.onclick = function(){
				var linktopass = this.href;
				if (this.target != "_blank"){
					linktopass = linktopass.split("?");
					linktopass = linktopass[1];			
					StateManager.setState(linktopass);
					return false;
				}
			}
		}		
	};
	
var aRules = {
	'div#menu ul li ul li a' : 
		function(element){		
			element.onclick = function(){							
				var linktopass = this.href;
				linktopass = linktopass.split("?");
				linktopass = linktopass[1];			
				StateManager.setState(linktopass);									
				return false;
			}
		},
	'div#menu ul li a' :
		function(element){
			element.onclick = function(){
				if(!click_sub){
					var linktopass = this.href;
					linktopass = linktopass.split("?");
					linktopass = linktopass[1];					
					StateManager.setState(linktopass);
				}
				return false;
			},
			element.onmouseover = function(){
				if(ie){
					var descendants = Element.descendants(this);				
					if(this.getElementsByTagName('table')){
						if(this.getElementsByTagName('ul').length > 0){
							this.getElementsByTagName('ul')[0].show();
						}
					}					
				}
			},
			element.onmouseout = function(){
				if(ie){
					var descendants = Element.descendants(this);				
					if(this.getElementsByTagName('table')){
						if(this.getElementsByTagName('ul').length > 0){
							this.getElementsByTagName('ul')[0].hide();
						}
					}					
				}
			}
		},
	'div#menu ul li a table tr td ul li a' :
		function(element){
			element.onclick = function(){							
				var linktopass = this.href;
				linktopass = linktopass.split("?");
				linktopass = linktopass[1];				
				StateManager.setState(linktopass);						
				click_sub = true
				new PeriodicalExecuter(function(pe) {
					click_sub = false;
					pe.stop();
				}, 1);
				return false;
			}
		}		
	,
	'div#menu ul li a table tr td ul' :
		function(element){
			element.onmouseout = function(){
				Element.hide(this);
			}
		},
	'div#sitemap a' : 
		function(element){
			if (element.id != "backtotop"){
				element.onclick = function(){	
					var linktopass = this.href;
					linktopass = linktopass.split("?");
					linktopass = linktopass[1];								
					StateManager.setState(linktopass);					
					return false;
				}
			}
		},
	'div.backtotop a' :
		function(element){
			element.onclick = function(){
				new Effect.ScrollTo($('top'), {duration: 0.6});
				return false;
			}
		},
	'div#additional div a':
		function(element){
			element.onclick = function(){
				var linktopass = this.href;
				linktopass = linktopass.split("?");
				linktopass = linktopass[1];	
				StateManager.setState(linktopass);
				return false;
			}
		},
	'div#contact div a':
		function(element){
			element.onclick = function(){
				var linktopass = this.href;
				linktopass = linktopass.split("?");
				linktopass = linktopass[1];	
				StateManager.setState(linktopass);
				return false;
			}
		}
	};

var googleTags = {
		'div#directions ul li a':
		function(element){
			element.onclick = function(){
				moveto(this.id);
				return false;
			}
		}
	};

var sendFriendRules = {
		'#sendFriend #sendFriendForm' :
		function(element){
			element.onsubmit = function(){
				sendFriend(this);
				return false;
			}
		}
	};

var sendFeedbackRules = {
	'#sendFeedback #sendFeedbackForm' :
		function(element){
			element.onsubmit = function(){
				sendFeedback(this);
				return false;
			}
		}
	};

var overviewRules = {
	'#overview .read a' :
		function(element){
			element.onclick = function(){
				var linktopass = this.href;
				linktopass = linktopass.split("?");
				linktopass = linktopass[1];	
				StateManager.setState(linktopass);
				return false;
			}
		}
	};
	
var searchRules = {
	'#searchresults a' :
		function(element){
			element.onclick = function(){
				var linktopass = this.href;
				linktopass = linktopass.split("?");
				linktopass = linktopass[1];	
				StateManager.setState(linktopass);
				return false;
			}
		}
	};


/*---------------------------------------------------------------------
SUBMENU EN SITEMAP ACTIVE ZETTEN
---------------------------------------------------------------------*/
function setMenuActive(hash){
	if(hash){
		if(!ie){
			var active_menu = $$('#menu .menuCurrent');
			for(var i=0; i<active_menu.length; i++){
				active_menu[i].removeClassName('menuCurrent');
			}
			var active_submenu = $$('#menu .subCurrent');
			for(var i=0; i<active_submenu.length; i++){
				active_submenu[i].removeClassName('subCurrent');
			}
			var menu = $('menu').getElementsByTagName('a');
			for(var i=0; i<menu.length; i++){
				var linktopass = menu[i].href;
				linktopass = linktopass.split("?");
				linktopass = linktopass[1];
				if(linktopass == hash){		
					var ancestors = menu[i].ancestors();
					if(ancestors.length == 7){
						ancestors[1].addClassName('menuCurrent');
					}
					if(ancestors.length == 9){
						ancestors[3].addClassName('menuCurrent');
						ancestors[0].addClassName('subCurrent');
					}
				}
			}
		}
	}
}
function setSitemapActive(hash){
	if(hash){
		if(!ie){
			var active_menu = $$('#sitemap_container .sitemapCurrent');
			for(var i=0; i<active_menu.length; i++){
				active_menu[i].removeClassName('sitemapCurrent');
			}
			var menu = $('sitemap_container').getElementsByTagName('a');
			for(var i=0; i<menu.length; i++){
				var linktopass = menu[i].href;
				linktopass = linktopass.split("?");
				linktopass = linktopass[1];
				if(linktopass == hash){
					menu[i].addClassName('sitemapCurrent');
				}
			}
		}
	}
}



/*---------------------------------------------------------------------
SEND TO FRIEND & FEEDBACK
---------------------------------------------------------------------*/
function loadSendFriend(){
	if(!$('sendFriend')){
 		new Ajax.Request('ajax/sendtofriend.ajax.php', {
			method: 'get',
			onComplete: function(transport){
				new Insertion.Bottom($('content'),transport.responseText);
				$('url').value = location.href;
				Behaviour.register(sendFriendRules);
				Behaviour.apply();
			}
		});
	}
}
function sendFriend(el){
	if (checkForm(el)){
		var tmpFormSerialized = el.serialize(false);
		new Ajax.Request('php/send.php', { 
				method: 'post',
				postBody: tmpFormSerialized,
				onComplete:	function(transport){
					if(transport.responseText == 1){
						$('sendFriend').innerHTML = '<h2>Send to Friend</h2><p id="introSend">Thanks for spreading the word, the link has been send by email.</p>';
					}
				}
			}
		);
	}
}
function sendFeedback(el){
	if (checkForm(el)){
		var tmpFormSerialized = el.serialize(false);
		new Ajax.Request('php/send.php', { 
				method:'post',
				postBody: tmpFormSerialized,
				onComplete:	function(transport){
					if(transport.responseText == 1){
						$('sendFeedback').innerHTML = '<h2>Your information</h2><p id="introSend">Thanks for the feedback</p>';
					}
				}
			}
		);
	}
}
function checkForm(el){
	if(el){
		valid = true;
		var x = el.getElementsByTagName('input');
		for (var i=0;i<x.length;i++) {
			if($(x[i].id+"_error")) $(x[i].id+"_error").innerHTML = "";
			if($(x[i].id+"_error")) $(x[i].id).setStyle({border:'1px solid #333333'});
		}
		for (var i=0;i<x.length;i++) {
			if (!x[i].value){
				if($(x[i].id+"_error")) $(x[i].id+"_error").innerHTML = "This field is required";
				if($(x[i].id+"_error")) $(x[i].id).setStyle({border:'1px solid #ff0000'});
				valid = false;
			}
		}
		
		if(x['fromMail'] && x['fromMail'].value.indexOf('@') == -1){
			if($(x['fromMail'].id+"_error")) $(x['fromMail'].id+"_error").innerHTML = "Not a valid email address";
			if($(x['fromMail'].id+"_error")) $(x['fromMail'].id).setStyle({border:'1px solid #ff0000'});
			valid = false;
		}
		if(x['toMail'] && x['toMail'].value.indexOf('@') == -1){
			if($(x['toMail'].id+"_error")) $(x['toMail'].id+"_error").innerHTML = "Not a valid email address";
			if($(x['toMail'].id+"_error")) $(x['toMail'].id).setStyle({border:'1px solid #ff0000'});
			valid = false;
		}
		
		if(x['fromMail1'] && x['fromMail1'].value.indexOf('@') == -1){
			if($(x['fromMail1'].id+"_error")) $(x['fromMail1'].id+"_error").innerHTML = "Not a valid email address";
			if($(x['fromMail1'].id+"_error")) $(x['fromMail1'].id).setStyle({border:'1px solid #ff0000'});
			valid = false;
		}
		if(x['toMail1'] && x['toMail1'].value.indexOf('@') == -1){
			if($(x['toMail1'].id+"_error")) $(x['toMail1'].id+"_error").innerHTML = "Not a valid email address";
			if($(x['toMail1'].id+"_error")) $(x['toMail1'].id).setStyle({border:'1px solid #ff0000'});
			valid = false;
		}
		if (valid){
			return true
		}else{
			return false;
		}
	}else{
		return false;
	}
}


/*---------------------------------------------------------------------
CHECKS
---------------------------------------------------------------------*/
function checkForFeedback(){
	if($('sendFeedback')){
		Behaviour.register(sendFeedbackRules);
	}
}
function checkForGoogleMaps(){
	if($('wagt_map')){
		Behaviour.register(googleTags);
		$('routeInfo_1').setStyle({display:'none'});
		$('routeInfo_2').setStyle({display:'none'});
		$('routeInfo_3').setStyle({display:'none'});
		$('routeInfo_4').setStyle({display:'none'});
		wagt_map();
	}	
}
function checkForOverview(){
	if($('overview')){
		Behaviour.register(overviewRules);
	}
}
function checkForSearch(){
	if($('searchresults')){
		Behaviour.register(searchRules);
	}
}



/*---------------------------------------------------------------------
ZOEK FUNCTIE
---------------------------------------------------------------------*/
function searchForm(el){	
	if(el){
		var tmpFormSerialized = el.serialize(false);
		StateManager.setState(tmpFormSerialized);
	}
}



/*---------------------------------------------------------------------
SPONSORS
---------------------------------------------------------------------*/
function randomSponsors(){
	if(random_sponsors != null)random_sponsors.stop();
	random_sponsors = new Ajax.PeriodicalUpdater('partnersInner', 'ajax/sponsors_random.ajax.php', {
 		method: 'get', 
		frequency: 20, 
		decay: 1
	});
}



/*---------------------------------------------------------------------
EYECATCHER FUNCTIES
---------------------------------------------------------------------*/ 
function eyecatcher_open(){
	if (eyecatcher) $('flash').maximize();
}
function eyecatcher_close(){
	if (eyecatcher) $('flash').minimize();
}
function eyecatcher_update(height){
	if (height) $('eyecatcher').setStyle({height:height+'px'});
}
function eyecatcher_loaded(){
	eyecatcher = true;
}


/*---------------------------------------------------------------------
WINDOW.LOAD MET TIMER VOOR FLASH LOAD INIT
---------------------------------------------------------------------*/
Event.observe(window, 'load', function(){
	new PeriodicalExecuter(function(pe){
		init();
		pe.stop();
	}, 0.5);
});
function init(){
	browserCheck();
	initSifr();
	GUnload();
	Behaviour.register(mainRules);
	Behaviour.register(aRules);
	Behaviour.register(rightNavRules);
	Behaviour.register(functionaliteitRules);
	Behaviour.register(speakerRules);
	checkForProgram();
	checkForGoogleMaps();
	checkForFeedback();
	checkForOverview();
	checkForSearch();
	randomSponsors();
	Behaviour.apply();
	StateManager.defaultStateID = 'sub1=1';
	StateManager.initialize();
}