/**************************************************************************************************
 * HTML5 Diashow Loeader - programmed by Jeremy A. Prescott (jap@prescore.ch) @ 20Minuten AG (jeremy.prescott@20minuten.ch)
******************************************************************************************/

var diaHtml5Load=function(){

	var _diaHtml5Loaded=-1,
		_lastShowId=-1,
		_toLoad=[],
		_isHashCapable= ("onhashchange" in window?true:false),
		_scriptAnchor=document.getElementsByTagName('script')[0],
		analizeURI=function(){
			var showid=window.location.href.match(/[#]{1}.*?showid=(\d+)/),
				index=window.location.href.match(/[#]{1}.*?index=([\d-]+)/);
				
			if (showid!==null && showid.length>=2){
				showid=showid[1];
			}
			else{
				showid=null;
			}
			
			if (index!==null && index.length>=2){
				index=index[1];
			}
			else{
				index=null;
			}
			
			return {
				"showId": showid,
				"index": index
			};
		},
		
		load=function load(){
		
			++_diaHtml5Loaded;
		
			var onload=(function(loaded){
			
				return function onLoad(e){
					if( e.type==="load" || (e.type==="readystatechange" && typeof this.readyState!=="undefined" && (this.readyState == "loaded" || this.readyState == "complete") ) ) {
						if (_toLoad[loaded]!==null){
							//check if javascript has been fully exectued
							if (window[ _toLoad[loaded].name ] ){
								_toLoad[loaded]=null;
								if (_toLoad.length-1>_diaHtml5Loaded){
									load(); //call self again
								}
								else{
									++_diaHtml5Loaded; //count plus one, so that display doesent go through till we have finished loading shizzle!
								}
							}
							else{
								//retrigger loaded
								setTimeout(function(){
									onLoad.call(this,e);
								},50);
							}
						}
					}
				}
			}(_diaHtml5Loaded));
			
			
			
			if (_diaHtml5Loaded===0){
				//insert into head
				var style=document.createElement("link");
				style.type="text/css";
				style.rel="stylesheet";
				style.href="/2010/html5/diashow/diashowHtml5.css";
				_scriptAnchor.parentNode.insertBefore(style, _scriptAnchor);
			}
			
			jQuery(_toLoad[_diaHtml5Loaded].script).
				bind("load",onload).
				bind("readystatechange",onload);
			
			_scriptAnchor.parentNode.insertBefore(_toLoad[_diaHtml5Loaded].script, _scriptAnchor);
		},
		
		display=function display(showid,index){
		/*	if (typeof Modernizr==="undefined") {
				setTimeout(display,50);
				return;
			}*/
			var data={};
			if (typeof showid!=="undefined" && typeof index!=="undefined"){
				data.showId=showid;
				data.index=index;
			}
			else{
				data=analizeURI();
			}
			
			if (data.showId>0){
			
				if (data.showId!==_lastShowId){
			
					//if (Modernizr.canvas){ //HTML5 Diashow
					//alert("loaded"+_diaHtml5Loaded);
						if (_diaHtml5Loaded < _toLoad.length ){
							if (_diaHtml5Loaded===-1){
								//start loading process
								load();
								//insert HTML
								penetratingHTMLSet();
							}
							setTimeout(display,50);
							return;
						}
						//alert("start shizzler!");
						diaHtml5.start(data.showId, data.index);
						_lastShowId=data.showId;
					/*}
					else{
						window.open("http://"+window.location.hostname+"/diashow/diashowHtml.tmpl?showid="+data.showId)
					}*/
				
				}
			}
			else{
				_lastShowId=-1;
			}
		
		},
		penetratingHTMLSet=function penetratingHTMLSet(){
			if (!document.body){
				setTimeout(penetratingHTMLSet,50);
				return;
			}
			var div=(document.body.appendChild(document.createElement("div"))).innerHTML="<div id='html5'><div class='lead'><div class='title'></div><div class='social'></div></div><div class='big'></div><div class='cover'><div class='items'></div></div><div class='prevs'></div><div class='close'></div><div class='googleWemf'></div></div><div class='html5loading'><img src='/images/ajax-loader.gif' alt='loading 20minuten html5 diashow..'/></div>";
		};
		
		
	//SETUP

	//TemporaryShizzler
	jQuery(document).ready(function(){
		var castrateAnchorLinks=function(i,elem){
			var showid;
			var newUrl="";
			var baseUrl = window.location.href.match(/(.+?)\#/);
			if (elem.href.indexOf("diashow.tmpl")!==-1){
				showid=elem.href.match(/\?showid=(\d+)/)[1];
			}
			else if ( elem.parentNode.getAttribute("id") ){
				showid=elem.parentNode.getAttribute("id").match(/bildstrecke_(\d+)/)[1];
			}
			if (typeof showid !== "undefined") {
				
				if (baseUrl === null || baseUrl.length < 2) {
					baseUrl = window.location.href;
				}
				else {
					baseUrl = baseUrl[1];
				}
				newUrl=baseUrl + "#showid=" + showid + "&index=0";
				
				elem.onclick = "return false;";
				elem.href = newUrl;
				
				jQuery(elem).parent().find(".gallerybar").click(function(newUrl){
					return function(){
						window.location.href=newUrl;
					};
				}(newUrl));
			}
		};
		
		//diashow elem, makebig link
		jQuery("div.teaser.multislider.gallery a.showbig").each(castrateAnchorLinks);
		//story elem with diashow
		jQuery("div.teaser div.teaser_media a span.icon_diashow").parent().each(castrateAnchorLinks);
		//info panel in story
		jQuery("span.info_panel.info_bildstrecken a").each(castrateAnchorLinks);
		//html elem 'neusten bildstrecken'
		jQuery("div.full .teaser.multislider a").each(castrateAnchorLinks);
		/* jQuery(".story_head .topelement.picture").prepend(" */
	});
	
	//dependencies!
	/*_toLoad[0]={
		"name":"Modernizr",
		"script":document.createElement("script")
	};
	_toLoad[0].script.src="/2010/html5/diashow/diashowHtml5_modernizr.js";
	_toLoad[0].script.type="text/javascript";
	_toLoad[0].script.language="javascript";*/
	
	_toLoad[0]={
		"name":"mat4",
		"script":document.createElement("script")
	};
	_toLoad[0].script.src="/2010/html5/diashow/glmatrix.js";
	_toLoad[0].script.type="text/javascript";
	_toLoad[0].script.language="javascript";
	
	_toLoad[1]={
		"name":"diaHtml5",
		"script":document.createElement("script")
	};
	_toLoad[1].script.src="/2010/html5/diashow/diashowHtml5.js";
	_toLoad[1].script.type="text/javascript";
	_toLoad[1].script.language="javascript";
	
	//construct
	if (_isHashCapable){
		window.onhashchange=function(){
			display(); 
		}
	}
	else{ //fake the hashChange by timeouting
		jQuery(document).bind("click",function(e){
			if (e.target.nodeName==="A"){
				if (window.location.hash=="" || window.location.hash=="#"){
					_lastShowId=-1;
				}
				setTimeout(function(){
					//attempt to display cause something could have changed to the url..
					
					display();
				},50);
			}
		});
	}
	

	//check the url for starting parameters.. (incase of facebook like and shit..)
	var data=analizeURI();
	if (data.showId!==null){
		display(data.showId,data.index);
	}
	
	return display;
};

setTimeout(function checkJQuery(){
	if (jQuery){
		//IE6 is not supported!
		if (!jQuery.browser.msie || jQuery.browser.version != "6.0") {
			diaHtml5Load = diaHtml5Load();
		}
	}
	else{
		setTimeout(checkJQuery,50);
	}
},50);

