// JavaScript Document
(function($){
    var origContent = "";
		var selectedMenuItem=$("#menu a.selected");
		var thisId="";
		var thisName="";
		
		var backImages=new Array();
		var imageCounter=1;
		var firstBackgroundImage=1;
		
		var jsReadyForFlash=false;
		
		var mp3ListHeight=0;
		
		var checkEnabled=true;
		var counter=0;
		var zindexUpCounter=100;
		
		function setRealContentLinks(){
			$('#realcontent a.insite').click(function(e){
				e.preventDefault();
				var deId="";
				var deLink=$(this).attr('href').split("?")[1];
				if(deLink.split('&')[1]!= undefined){
					var checkSmid=deLink.split('smid=')[1];//.split('=')[0];
					var checkSmid=deLink.split('&')[1].split('=')[0];
					if(checkSmid=='smid')deId=deLink.split('&')[1].split('=')[1];
				}else	if(deLink.split('&')[0]!= undefined){
					var checkMid=deLink.split('&')[0].split('=')[0];
					if(checkMid=='mid')deId=deLink.split('&')[0].split('=')[1];
				}
			if(deId!=""){
					//$(this).attr('href','#'+menuIdToName[deId]);
					$(this).attr('href','#'+deId);
					var url = $(this).attr('href');
					url = url.replace(/^.*#/, '');
					check("url = "+url);
					$.history.load(url);
				}
				return false;
			});
				$('#realcontent a[href*=".mp3"]').unbind('click');
				$('#realcontent a[href*=".mp3"]').bind('click',function(e) {
						e.preventDefault();
						var deMp3=$(this).attr('href');
						$('#mp3list a.strong').removeClass('strong');
						$('#mp3list a[href="'+deMp3+'"]').addClass('strong');					
						//mp3Obj('gdmp3').playMp3(deMp3);
						if ( $('#klikhiervoormuziek').is(':visible') ) { 
							$('#klikhiervoormuziek').unbind('click');
        			$('#klikhiervoormuziek').hide();
							$('#mp3muziek').text('muziek :');
						} 
						playMp3(deMp3);
						return false;
				}); 	
				
		}
    function loadContent(hash) {
				//check("hash = "+hash);
				var deId="";
				
        if(hash != "") {
					if(startHash!=""){									
						document.location.href="/#"+hash;	
						//return;					
					}
					deId=menuNameToId[hash];
            if(origContent == "") {
                origContent = $('#realcontent').html();
            }
											
						$.ajax({
							url: "inc/getalineas.php?name="+hash,
							
							success: function(data){
								$("#realcontent").html(data);
								//
								loadBackgroundImage(deId);
								$(selectedMenuItem).removeClass('selected');
								selectedMenuItem=$("#"+deId);
								$(selectedMenuItem).addClass('selected');
								setRealContentLinks();
								scroll(0,0);
								
							}
						});
            //$('#realcontent').load("invoegen/getalineas.php?id="+hash);
        } else if(origContent != "") {
						//alert("startHash "+startHash);
            $('#realcontent').html(origContent);
						//
						deId=menuNameToId[startHash];
						
						loadBackgroundImage(deId);
						$(selectedMenuItem).removeClass('selected');
						selectedMenuItem=$("#"+deId);
						$(selectedMenuItem).addClass('selected');
						setRealContentLinks();	
						scroll(0,0);				
        }else{									
					loadBackgroundImage(firstBackgroundImage);
					origContent = $('#realcontent').html();					
				}
				setTimeout(scrollToTheTop,100);
				
				
    }
		
		function scrollToTheTop(){
			$('html').scrollTop(0);
		}
		
		
		var lastImageBackgroundId="";
		
		function placeImage(pageID){
			
			var imageID="im_"+pageID;
			var $_img=$("#"+imageID);
			//$_img.css({'opacity':0});
			$_img.hide();
			zindexUpCounter++;
			if($("#bgimage img").length>1){
				$("#bgimage img").each(function(el){
					$(this).stop();
				});
			}
			$('#bgimage').append($_img);
			
			$_img.css({'z-index':zindexUpCounter});
			setImageWidthHeight(imageID);
			$_img.stop(true,false).fadeIn('slow',imageToContainer);
		}
		
		function imageToContainer(){
			var tId=$(this).attr('id');
			$(this).css({'opacity':1,'display':'block'});
			check("child length = "+$("#bgimage img").length);
			$("#bgimage img").each(function(el){
				
				id=$(this).attr('id');
				if(tId!=id)$('#backimageContainer').append($(this));
				
			});
			//console.log("lastImageBackgroundId "+lastImageBackgroundId);
			//$('#backimageContainer').append($('#'+lastImageBackgroundId));
		}
		
		function resizing(){
			resizeBackground();
			var playerLeft=$('#header').offset().left;
			$('#topmenu').css({'left':playerLeft});
			$('#undermenu').css({'left':playerLeft});
			
		}
		
		function resizeBackground(){
			$("#bgimage img").each(function(el){
				id=$(this).attr('id');
				setImageWidthHeight(id)
			});
			
		}
		
		function setImageWidthHeight(imId){
			
			var w=$(window).width();
			var h=$(window).height();
			var iw=0;//imObj.width;
			var ih=0;//imObj.height;
			var r=0;
			var top=0;
			var pId=imId.split('_')[1];	
			iw=backgrounds[pId].orgWidth;
			check("backgrounds[pId].orgWidth = "+iw);
			ih=backgrounds[pId].orgHeight;
			r=Math.max(w/iw,h/ih);		
			top=0;//h-(ih*r);
			$("#"+imId).css({'width':Math.ceil(iw*r),'height':Math.ceil(ih*r),'top':top});
		}
				
		function loadBackgroundImage(pageID){

			var imageID="im_"+pageID;
			var im=backgrounds[pageID].im;
			if(backgrounds[pageID].loaded || backgrounds[pageID].loading){
				firstBackgroundImage=pageID;
				placeImage(pageID);		
			}else{
						
				$('#backimageContainer').append('<img id="'+imageID+'"/>');
				//$('#'+imId).css({'z-index':imageCounter});
				backgrounds[pageID].loading=true;
				// wrap our new image in jQuery, then:
				$('#'+ imageID)
					// once the image has loaded, execute this code
					.load(function () {
						// set the image hidden by default    
						/*$(this).hide();*/
						var thisId=$(this).attr('id');
						var bgId=thisId.split('_')[1];
						//firstBackgroundImage=bgId;
						backgrounds[bgId].loaded=true;
						backgrounds[bgId].loading=false;
						backgrounds[bgId].orgWidth=$(this).width();
						backgrounds[bgId].orgHeight=$(this).height();
						$(this).css({'z-index':bgId});
						
						placeImage(bgId);					
						imageCounter++;															
						
					})
					
					// show error
					.error(function () {
						// notify the user that the image could not be loaded
					})
					
					// *finally*, set the src attribute of the new image to our image
					.attr('src', im);	
					//resizeBackground();
			}
		}
		
		function thisMovie(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
         }
     }
     function playMp3(value) {
			 try{
        thisMovie('gdmp3').playMp3(value);
				if($('#mp3muziek').text()!='muziek :')$('#klikhiervoormuziek').one();
				//check("mp3 is http://members.home.nl/gdruiven"+value);
			 }catch(err){
			 	check("err = "+err);
			 }
     }
		 
		
		 
     function isReady() {
         return jsReadyForFlash;
     }
		
    $(document).ready(function() {
				var attributes = {
					id: "gdmp3",
					name: "gdmp3"
				};
				var params={
					wmode: "transparent",
					allowNetworking: "all"
				};	
				swfobject.embedSWF("flash/mp3.swf", "gdmp3", "200", "15", "10.0.0",null,null,params,attributes,mp3Geladen);
				
				var mid=$("#menu a.selected").attr('id');
				//$('body').append("<div id=\"backimageContainer\"></div>");
				
				selectedMenuItem=$("#"+mid);
				if(!(mid=='undefined' || mid==null)){
					firstBackgroundImage=mid;
					
				}else firstBackgroundImage=1;
				//firstBackgroundImage="im_0";//$("#"+mid).attr('id');
				
				$.history.init(loadContent,{ unescape: ",/ " });
				
				$(window).resize(resizing);
				$('#menu a.insite').bind('click',function(e) {
						e.preventDefault();
						$(this).attr('href','#'+menuIdToName[$(this).attr('id')]);
						var url = $(this).attr('href');
						url = url.replace(/^.*#/, '');
						$.history.load(url);
						return false;
				}); 
				var playerLeft=$('#header').offset().left;// + 656;			
				$('#topmenu').css({'left':playerLeft});
				$('#undermenu').css({'left':playerLeft});
				$('#muziek-shadowunder').css({'height':($('#topmenuitem-muziek').outerHeight()+15)});
				$("#topmenu-muziek .downup").bind('click',function(){
					if($(this).hasClass('close')){
							
							$('#topmenu-muziek').animate({'top':(-$('#mp3list').outerHeight(true))},'slow');
							$('#undermenu-muziekunder').animate({'top':(-$('#mp3list').outerHeight(true))},'slow');
							$(this).removeClass('close');
							
						}else{
							
							$(this).addClass('close');
							$('#topmenu-muziek').animate({'top':0},'slow');
							$('#undermenu-muziekunder').animate({'top':0},'slow');
							
						}
						if ( $('#klikhiervoormuziek').is(':visible') ) { 
							$('#klikhiervoormuziek').unbind('click');
        			$('#klikhiervoormuziek').hide();
							$('#mp3muziek').text('muziek :');
						} 
							
				})
				$('#topmenu-muziek').animate({'top':(-$('#mp3list').outerHeight(true))},'slow');
				$('#undermenu-muziekunder').animate({'top':(-$('#mp3list').outerHeight(true))},'slow');	
				$('#topmenu-contact').css({'top':0});
				$('a[href*=".mp3"]').bind('click',function(e) {
					if(jsReadyForFlash){
						e.preventDefault();
						var deMp3=$(this).attr('href');
						$('#mp3list a.strong').removeClass('strong');
						$('#mp3list a[href="'+deMp3+'"]').addClass('strong');					
						//mp3Obj('gdmp3').playMp3(deMp3);
						playMp3(deMp3);
						return false;
					}
				});
				
				$('#klikhiervoormuziek').one('click',function(e){
					$(this).hide();
					$('#mp3muziek').text('muziek :');
					$("#topmenu-muziek .downup").trigger('click');
				});
				
			
		});
		
		function check(txt){
			if(checkEnabled){
				try{
					console.log(txt);
				}catch(err){
					//
				}
			}
		}
		var mp3Obj;
		function mp3Geladen(){
			jsReadyForFlash=true;	
			mp3Obj=swfobject.getObjectById('gdmp3');
			check("mp3Obj = "+mp3Obj);
		}
})(jQuery);
