var permaLinkCounter = false;

//////////////
// Functions//
//////////////

function BodyLoadMain() {
	this.permaLink(); 
	this.SetFocusField();
	this.Autosuggest_dec();
}

// This function opens a search request with a given GET method link index.html?q=test&e=mp3&hl=de

function permaLink() {
 
	suffix = decodeURI(window.location.search);		//suffix = ?s=test&e=download&l=en	//Component
	breaker = "&";									//suffix break symbol

	if (window.location.search != "") {
		
		this.permaLinkCounter = true;
		
		s = this.getGetValue(/q=/, suffix, breaker);	//search query
		l = this.getGetValue(/hl=/, suffix, breaker);	//language
		label = this.getGetValue(/label=/, suffix, breaker);	//label hoster
  		
		//set default language value
		if (l != "de" && l != "es" && l !="ru" && l != "tr") { l = "en"; }
		
  		document.menuform.language.value = l;
  		document.menuform.langEngine.value = l;
  		document.menuform.label.value = label;	
  			
  		//sets the textfield value
  		//calls onsubmit and submit of the form
  		this.searchQuery(s.replace(/\+/gi, " ")) ; //urlencode
 	}
 	
}

function removeLabel(query) {
	var queryIndex = query.search("more:");
	if (queryIndex != -1) {
		query = query.substring(0, queryIndex - 1);
	}
	return query;
}

function addPlugin() {
	
	lang = document.menuform.language.value;
    searchplugin = page_url+"resources/xml/browser_search_plugin_ff_" + lang + ".xml";

	//check browser version ie does not support suggest
 	switch (lang) {
  		case "de": checkText = "Sie möchten unser deutchsprachiges\nFirefox download Suchplugin installieren ?"; break;	
  		case "es": checkText = "Que desea instalar nuestro\ncomo descargar plugin Firefox español ?"; break; 				
  		default: checkText = "You want to install our english (default)\nFirefox download searchplugin ?"; break;
 	}
	
	check = confirm(checkText);
	if (check == true) {
		try {
			window.external.AddSearchProvider(searchplugin);	
		} 
		catch (a) {
			alert("This feature needs Firefox 2+ or Internet Explorer 7+."); 
		}
	}
	
}


function getCookies() {
	if (navigator.cookieEnabled == true ) 
	{
		var l = WertHolen ("language");
		var cookieCounter = WertHolen ("cookieCounter");
		if (cookieCounter != "off" 
			&& this.permaLinkCounter == false 
			&&  (l == "es" || l == "de" || l == "light")) 
		{
			WertSetzen("cookieCounter","off",0,0,0,0,2);
			self.location.href = page_url + "lang/" + l + '/index_' + l + '.html';
		}

	}
}




///////////////////////////////////////////////////////////////////////
//@divVisibility = 	will turn a given container on or off
//					additional with an fade effect
//@param objectId = Id of the Object
//@param from = start percentage of opacity fade effect [0,100] 
//@param to = end percentage of opacity fade effect [0,100]
//@param time = time in millisecond for the fade effect
///////////////////////////////////////////////////////////////////////


function divVisibility (objectId, from, to, time) 
{ 
	if(document.getElementById(objectId).style.opacity == 0) 
        setOpacity(objectId, 0, 100, time);
    else 
    	setOpacity(objectId, 100, 0, time);
}

//////////////////////////////////////////////////
//@setOpacity = sets timer for changeOpacity()
//@param objectId = Id of the Object
//@param opacity = opacity of div [0,100]
//////////////////////////////////////////////////

function setOpacity(objectId, from, to, time) {

    //speed for each frame
    var speed = Math.round(time / 100);
    var timer = 0;
	var opacity = document.getElementById(objectId).style.opacity;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(from > to) {
        for(i = from; i >= to; i--) {
            setTimeout("changeOpacity(" + i + ",'" + objectId + "')",(timer * speed));
            timer++;
        }
    } else if(from < to) {
        for(i = from; i <= to; i++)
            {
            setTimeout("changeOpacity(" + i + ",'" + objectId + "')",(timer * speed));
            timer++;
        }
    }
}

//////////////////////////////////////////////////
//changeOpacity = sets the opacity of a given div 
//@param objectId = Object Id
//@param opacity = opacity of div [0,100]
//////////////////////////////////////////////////

function changeOpacity(opacity, objectId) {
    object = document.getElementById(objectId).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 



//////////////////////////////////////////////////
//will get the current style attribute of an
//object, detects firefox or internet explorer
//because both use differen style parameters
//@param objectId = id of the object
//@param style = selected style
//////////////////////////////////////////////////

function getStyle(objectId,style) {

	var object = document.getElementById(objectId);
	
	if (object.currentStyle) {		//Internet Explorer
		style = this.mozToIE_Style(style);
		var value = object.currentStyle[style];	
	}
	else if (window.getComputedStyle) {		//Firefox
		var value = document.defaultView.getComputedStyle(object,null).getPropertyValue(style);
	}
		
	return value;
}

//////////////////////////////////////////////////
//converts any mozilla style attribute to 
//internet explorer style attribute
//@param style = selected style
//////////////////////////////////////////////////

function mozToIE_Style(style) {

	var styleArray = style.split("-");
	var style = styleArray[0];
	var styleArrayLength = styleArray.length;
	
	if (styleArrayLength > 0) {
		for (i = 1; i < styleArrayLength; i++) {
			style += styleArray[i].charAt(0).toUpperCase() + styleArray[i].substr(1);
		}
	}

	return style;
	
}

