window.onload = function() {
	contactpopup();
	contactlink();
	noframes();
	language_navigation();
	blog_language_navigation();
	add_xml_request();
	add_windows_close();
}

function language_navigation() {
	if(document.getElementById('langform')) {
		var language_options = document.getElementById('langform').language;
		YAHOO.util.Event.addListener(language_options,'change',change_my_language());
		
		var go_button = document.getElementById('langgo');
		go_button.parentNode.removeChild(go_button);
	}

}

function blog_language_navigation() {
	if(document.getElementById('bloglangform')) {
		var language_options = document.getElementById('bloglangform').language;
		YAHOO.util.Event.addListener(language_options,'change',change_my_blog_language());
		
		var go_button = document.getElementById('langgo');
		go_button.parentNode.removeChild(go_button);
	}

}

function change_language() {
	var old_link = document.getElementById('langform').loc.value;
/*	if(old_link.match(/^\/(de|fr|en)/)) { */
		var new_link = old_link.replace(/^\/(de|fr|en)?/,"/" + document.getElementById('langform').language.value);
	/*}
	else {
		var new_link = document.langform.language.value + '/' + old_link;
	}*/
	var new_location = "http://christianflury.com/" + new_link + "/";
	top.location = new_location;
}

function change_blog_language() {
	document.getElementById('bloglangform').submit();
}


function change_my_language() {
	return function() { change_language() };
}

function change_my_blog_language() {
	return function() { change_blog_language() };
}


function contactpopup() {
	var v = document.getElementById("contactpopup");
	if (v) {
		var contactlink = v.getAttribute('href');
		YAHOO.util.Event.addListener(v,'click',contact_popup_wrapper(v));
		YAHOO.util.Event.addListener(v,'keypress',contact_popup_wrapper(v));
	}
}

function contactlink() {
	var v = document.getElementById("contactlink");
	if (v) {
		var contactlink = v.getAttribute('href');
		YAHOO.util.Event.addListener(v,'click',contact_popup_wrapper(v));
		YAHOO.util.Event.addListener(v,'keypress',contact_popup_wrapper(v));
	}
}


function contact_popup_wrapper(v) {
	var url = v.getAttribute('href');
	v.removeAttribute('href');
	return function() { FF60(url); }	
}

function add_windows_close() {
	var alllinks = document.getElementsByTagName('a');
	for(var i=0;i<alllinks.length;i++) {
		if ((alllinks[i].getAttribute('class') == 'close_details')||(alllinks[i].getAttribute('className') == 'close_details')) {
			alllinks[i].removeAttribute('href');
			YAHOO.util.Event.addListener(alllinks[i],'click',close_details());
			YAHOO.util.Event.addListener(alllinks[i],'click',close_details());
		}
	}
}

function noframes() {
  if(top != self)
  top.location=self.location;
}

function FFF60(url) {
 FFFen60 =
 window.open(url, "Details", "width=800,height=600,scrollbars");
 FFFen60.focus();
}

function FF60(url) {
	
 	FFen60 = window.open(url, "Details", "width=670,height=600,scrollbars");
 	FFen60.focus();

}


function add_xml_request() {
	var divs = document.getElementsByTagName('div');
	for (var i=0;i<divs.length;i++) {
		if ((divs[i].getAttribute("class") == "detailswindow")||(divs[i].getAttribute("className") == "detailswindow")) { /*Fuck Internet Explorer */
			divs[i];
			divs[i].childNodes[0].removeAttribute('href');
			YAHOO.util.Event.addListener(divs[i],'click',details_wrapper());
			YAHOO.util.Event.addListener(divs[i],'keypress',details_wrapper());
		}
	}
}

function details_test() {
	return function() {
	alert("Works");
	alert("current target: "+ this);
	try {
	YAHOO.util.Event.purgeElement(this);
}
catch (e) {
	alert("keine chance");
}
}
}


function details_wrapper() {

	return function() {
		var div = this;
		var detail_id = this.getAttribute('id')
		YAHOO.util.Event.purgeElement(this, true);
		var titlestring = div.firstChild.firstChild.nodeValue;
		while (div.firstChild) {
			div.removeChild(div.firstChild);
		}
		var old_loc = document.getElementById('langform').loc.value;
		document.getElementById('langform').loc.value += '/details/' + detail_id;
		/*div.setAttribute('class','details');
		if (div.getAttribute('class')!='details') {
			div.setAttribute('className','details');
		}*/
		div.className='details';
		var top_section = document.createElement('div');
		/*top_section.setAttribute('class','top');
		if(top_section.getAttribute('class')!='top') {
			top_section.setAttribute('className','top');
		}*/
		top_section.className='top';
		var close_link = document.createElement('a');
		close_link.setAttribute('title','Collapse/Einklappen/Fermer');
		YAHOO.util.Event.addListener(close_link,'click',close_details());
		YAHOO.util.Event.addListener(close_link,'keypress',close_details());
		close_span = document.createElement('span')
		var close_link_text = document.createTextNode("Collapse");
		close_span.appendChild(close_link_text);
		close_link.appendChild(close_span);
		top_section.appendChild(close_link);
		var titleelement = document.createElement('h4');
		titletext = document.createTextNode(titlestring);
		titleelement.appendChild(titletext);
		top_section.appendChild(titleelement);
		div.appendChild(top_section);
		var main_section = div.appendChild(document.createElement('div'));
		main_section.className='main';
		var loading_image = main_section.appendChild(document.createElement("img"));
		loading_image.setAttribute('src','http://christianflury.com/images/ajax-loader.gif');
		loading_image.setAttribute('alt','Loading/Wird geladen/Chargement en cours');
		loading_image.setAttribute('style','margin:20px;');
		if (loading_image.style.setAttribute) {
			loading_image.style.setAttribute('margin','20px');
		}
		var language = document.getElementsByTagName('html')[0].getAttribute('xml:lang')||document.lang();
		if(window.XMLHttpRequest) {
			var req = new XMLHttpRequest();
		}
		else {
			/* You, Mr. Browser, suck*/
			var types = [
        		'Microsoft.XMLHTTP',
        		'MSXML2.XMLHTTP.5.0',
        		'MSXML2.XMLHTTP.4.0',
        		'MSXML2.XMLHTTP.3.0',
        		'MSXML2.XMLHTTP'
       		];

      	 	for (var i = 0; i < types.length; i++) {
         		try             {
           			var req = new ActiveXObject(types[i]);
         		}
         		catch(e) {}
       		}
   		}

		req.onreadystatechange = function() {
	    	if (req.readyState == 4) {
           		main_section.removeChild(loading_image);
           		if (req.status == 200) {
	           		
	           		try {
		           		var xml_node = req.responseXML;
					var frag = document.createDocumentFragment();
		           		while(xml_node.getElementsByTagName('content')[0].firstChild) {
	               				frag.appendChild(xml_node.getElementsByTagName('content')[0].firstChild);
               				}
					main_section.appendChild(frag);	       
	           		}
	           		catch(e) {
		           		var stupid_browser = req.responseText;
		           		var inner = stupid_browser.replace(/(.*)<content>(.*)<\/content>(.*)/, "$2");
		           		inner = inner.replace(/&apos;/g,"'");
		           		main_section.innerHTML = inner;
	           		}
		           		
               	}
          	
          		else {
              		main_section.appendChild(document.createElement('p')).appendChild(document.createTextNode('Loading Error/Ladefehler/Erreur de chargement for file /xml/' + language + '/details/' + detail_id + '.xml: ('+req.status+') ' +req.statusText));
          		}
          	}
     	}
 		

		req.open("GET",'/xml/' + language + '/details/' + detail_id + '.xml');
		req.send(null);


		

/*		alert("My title: " + div.firstChild.firstChild.nodeValue + ".\nThe id is " + detail_id + ".\n");*/
	}

}		
	
function close_details() {
	return function() {
		if(this.parentNode.parentNode) {
		var details_div = this.parentNode.parentNode;
		var detid = details_div.getAttribute('id');
		var dettitle = details_div.getElementsByTagName('h4')[0].firstChild.nodeValue;
		while (details_div.hasChildNodes()) {
	  		details_div.removeChild(details_div.firstChild);
		}
		details_div.className='detailswindow';
		var oldlink = details_div.appendChild(document.createElement('a'));
		oldlink.appendChild(document.createTextNode(dettitle));
		oldlink.setAttribute('title',dettitle);
		var replace_string = '/details/' + detid ;
		var new_loc_link = document.getElementById('langform').loc.value;
		while(new_loc_link.indexOf(replace_string) != -1) {
			new_loc_link = new_loc_link.replace(replace_string,'');
		}
		new_loc_link = new_loc_link.replace(/\/+/g,'/');
		document.getElementById('langform').loc.value = new_loc_link;
		YAHOO.util.Event.addListener(details_div,'mousedown',details_wrapper());
		YAHOO.util.Event.addListener(details_div,'keypress',details_wrapper());
	}
	}
	
}


