// Logs the input to a small window
function log(message) {
    if (!log.window_ || log.window_.closed) {
        var win = window.open("", null, "width=400,height=200," +
                              "scrollbars=yes,resizable=yes,status=no," +
                              "location=no,menubar=no,toolbar=no");
        if (!win) return null;
        var doc = win.document;
        doc.write("<html><head><title>Debug Log</title></head>" +
                  "<body></body></html>");
        doc.close();
        log.window_ = win;
    }
    var logLine = log.window_.document.createElement("div");
    logLine.appendChild(log.window_.document.createTextNode(message));
    log.window_.document.body.appendChild(logLine);
}
//Display overdiv
function lightBox(showDiv, showDivBG){
	
	showDivBG = "div#"+showDivBG;
	var newsDiv = document.getElementById(showDiv);
	
	var widthMinusOverDiv = $(document).width() - $(newsDiv).width();
	var divLeft = widthMinusOverDiv/2;
	$(newsDiv).css("left", divLeft);			
	var divTop = $(document.body).offset().top + 50;
	$(newsDiv).css("top", divTop);
	
	$(showDivBG).width($(document).width());	
	$(showDivBG).height($(document).height());
	
	$(newsDiv).show();	
	$(showDivBG).show();
}
//Close overdiv
function closeLightBox(divId, bgDiv){
	var lightBox = document.getElementById(divId); 
	$(lightBox).hide();
	$("div#"+bgDiv).hide();
}

// Measures de width of de scrollbar of current internet browser 
function scrollbarWidth() { 
    var div = $("<div style=\"width:50px; height:50px; overflow:hidden; position:absolute; top:-200px; left:-200px;\"><div style=\"height: 100px;\"></div>"); 
    // Append our div, do our calculation and then remove it 
    $("body").append(div); 
    var w1 = $("div", div).innerWidth(); 
    div.css("overflow-y", "scroll"); 
    var w2 = $("div", div).innerWidth(); 
    $(div).remove(); 
    return (w1 - w2); 
}

// Handles de margin to be set for the content to align correctly when scrollbar appears
function measure() { 
	//if (document.documentElement.scrollHeight > document.documentElement.clientHeight) { 
	//	document.body.style.paddingRight = scrollbarWidth()+"px";
	//} else { 
	//	document.body.style.paddingRight = "0px";
	//} 
}

// Redirect the window to given url
function openLink(url){
	setTimeout("window.location='"+url+"'", 350);	
}
 
// Onmouseover change image
function hoverImage(myObject, imgSrc) {
	$(myObject)[0].src = imgSrc;
}
 
// Ajax Submit
function submitAjaxRefresh(theObject){
	$.ajax({
		type : "post",
		data: $(theObject).serialize(),
		success: location.reload(true)
	})
}
 
 function formSubmit(formID){
	 $("form#"+formID).submit();
 }

 function afronden(getal,dec){
	getal = getal * Math.pow(10,dec);
	getal = Math.round(getal);
	getal = getal / Math.pow(10,dec);
	return getal;
}
 
// Sound Object class
function soundObject(){
	this.soundObj = null;
	this.soundID = null;
	this.soundFile = null;
	this.playImage = null;
	this.pauseImage = null;
	this.progressBar = null;
	
	this.createSound = function(soundID, soundFile, playImage, pauseImage, progressBar){
		if(this.soundObj != null && this.soundObj.paused == true){
			this.soundObj.resume();
			this.changeImage();
		} else {
			this.soundID = soundID;
			this.soundFile = soundFile;
			this.playImage = playImage;
			this.pauseImage = pauseImage;
			this.progressBar = progressBar;
			
			this.soundObj = soundManager.createSound({ 
				id: soundID, 
				url: soundFile, 
				whileplaying:function() {
					if(this.loaded == true){
						var currentPos  = (this.position/this.duration)*100;
						$(dyzleSound.progressBar).css("width", (afronden(currentPos, 1)+"%"));
				    }					
				},
				onfinish: function(){
					dyzleSound.destroySound();
					dyzleSound.resetProgress();					
				}
			});
			this.soundObj.play({volume:100});
			this.changeImage();
		}		
	};
	this.destroySound = function(){
		soundManager.destroySound(this.soundID);
		this.changeImage();
		this.soundObj = null;
		this.soundID = null;
		this.soundFile = null;
		this.playImage = null;
		this.pauseImage = null;
	};
	this.changeImage = function(){
		if($(this.playImage).css("display") != "none"){
			$(this.playImage).css("display", "none");
			$(this.pauseImage).css("display", "inline");
		} else if($(this.pauseImage).css("display") != "none"){
			$(this.pauseImage).css("display", "none");
			$(this.playImage).css("display", "inline");
		}
	};
	this.pauseSound = function(){
		soundManager.pause(this.soundID);
		this.changeImage();
	};
	this.resetProgress = 	function (){
		$(this.progressBar).css("width", "0%");
	};
}
var dyzleSound = new soundObject();
 
 // Executes function when page is loaded
$(document).ready(
	function() {
		menuOpenItems();
		measure();
		$("span.nuxError").wTooltip();
	}
);