// JavaScript Document
// Copyright Emil Refn 2008

// Funktion til at skjule/vise indholdet af et element på siden
function showhideRow(node) {
	var elementNode = document.getElementById(node);
	if(elementNode.style.display == 'none') {
		elementNode.style.display = 'block';
	} else {
		elementNode.style.display = 'none';
	}
}

// Funktion til at skifte mellem to billeder
function changeImage(id) {
	var image = document.getElementById(id);
	if(id.src == "http://www.gentoftestars.dk/images/knap.gif" || id.src == "http://gentoftestars.dk/images/knap.gif") {
		id.src = 'http://gentoftestars.dk/images/knap2.gif';
	} else {
		id.src = 'http://gentoftestars.dk/images/knap.gif';
	}
}
//Our requires
dojo.require("dojo.back");

ApplicationState = function(stateData, outputDivId, backForwardOutputDivId, bookmarkValue){
	this.stateData = stateData;
	this.outputDivId = outputDivId;
	this.backForwardOutputDivId = backForwardOutputDivId;
	this.changeUrl = bookmarkValue || false;
}
dojo.extend(ApplicationState, {
	back: function(){
		this.showBackForwardMessage("BACK for State Data: " + this.stateData);
		this.showStateData();
		loc = window.location.hash.substring(1);
		if(loc == '') {
			loc = 'calendar.php';
		}
		var contentNode = dojo.byId("content");
		dojo.xhrGet({
			url: urldecode(loc),
			handleAs: "text",
			load: function(data,args){
				// fade out the node we're modifying
				dojo.fadeOut({
					//delay:1000,
					node: contentNode,
					onEnd: function(){
						// set the data, fade it back in
						contentNode.innerHTML = data;
						dojo.fadeIn({node: contentNode}).play();    
					}
				}).play();
			},
			// if any error occurs, it goes here:
			error: function(error,args){
				console.warn("error!",error);
			}
		});
	},
	forward: function(){
		this.showBackForwardMessage("FORWARD for State Data: " + this.stateData);
		this.showStateData();
	},
	showStateData: function(){
		//dojo.byId(this.outputDivId).innerHTML += this.stateData + '<br />';
	},
	showBackForwardMessage: function(message){
		//dojo.byId(this.backForwardOutputDivId).innerHTML += message + '<br />';
	}
});

//Functions
function urldecode(str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
	var ret = str.toString();
    var histogram = {
	};
	var histogram_r = {
	};
	var code = 0;
	var str_tmp = [];
    
    // The histogram is identical to the one in urlencode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
 
    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        tmp_arr = ret.split(search); // Custom replace
        ret = tmp_arr.join(replace);   
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
 
    return ret;
}
function changePage(page) {
	//window.location.hash = '#'+urldecode(page);
	var contentNode = dojo.byId("content");
	dojo.xhrGet({
		url: urldecode(page),
		handleAs: "text",
		load: function(data,args){
			// fade out the node we're modifying
			
			dojo.fadeOut({
				//delay:1000,
				node: contentNode,
				onEnd: function(){
				  // set the data, fade it back in
				   contentNode.innerHTML = data;
				  dojo.fadeIn({node: contentNode}).play();    
				}
			}).play();
		},
		// if any error occurs, it goes here:
		error: function(error,args){
			console.warn("error!",error);
		}
	});
}
function goNav(id){
	var titel = document.title;
	var appState = new ApplicationState(id, "output", "dataOutput", id);
	appState.showStateData();
	dojo.back.addToHistory(appState);
	changePage(id);
}
