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
  		
		
		document.textfieldform.textfield.value = s.replace(/\+/gi, " ");	//encodeURL
		
		//set default language value
		if (l != "de" && l != "es" && l !="ru" && l != "tr") { l = "int"; }
		//set default hoster value
		if (label != "rapidshare" && label != "megaupload" && label != "depositfiles" && label != "uploaded" &&
			label != "netload" && label != "easy-share" && label != "mediafire" && label != "zshare" &&
			label != "hotfile" && label != "none")
		{
			label = "rapidshare"
		}
		
  		document.menuform.language.value = l;
  		document.menuform.langEngine.value = l;
  		document.menuform.label.value = label;	
  			
  		document.textfieldform.submit();	//fire php
  		this.Send(); //fire highslide	
 	}
 	
}

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';
		}
		
 		var cookieanonym = WertHolen ("ano");
 		if (cookieanonym == "checked") {
 			document.menuform.anonym.checked = true;
 		}	
 		
	}
}




///////////////////////////////////////////////////////////////////////
//@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 + ")";
} 

//for multiple searches only 3 other searches are selectable

function CheckBoxMax() {
	var numOfMSE = document.menuform.msselect.length;
	var j = 0;
	
	//check and save indexes for checked options
	for (var k = 0; k < numOfMSE; k++) {
		if (document.menuform.msselect[k].checked == true) {
			j++;
		}

		if (j > 3) {
			alert("3 Search engines are maximum.");
			document.menuform.msselect[k].checked = false;
			break;
		}
	}
	

	
	
	
}

//Blur function

var ext1 = false;	//ext1 toogle counter
var ext2 = false;	//ext2 toogle counter

//////////////////////////////////////////////////
//repositions the two divs
//@param x = current status of divs
//////////////////////////////////////////////////

function setBBC_RC(x) {
	
	var divBBC = document.getElementById('Bottom_Bar_Container');
	var divRC = document.getElementById('Right_Container');
	var bbcTop = parseInt(this.getStyle('Bottom_Bar_Container','top'));	
	var rcMinHeight = parseInt(this.getStyle('Right_Container','min-height'));
	var offsetExt = 94;
		
	if (x == 0) {
		divBBC.style.top		= bbcTop		- offsetExt	+ "px";
		divRC.style.minHeight	= rcMinHeight	- offsetExt	+ "px";
	}
	else {
		divBBC.style.top		= bbcTop		+ offsetExt + "px";
		divRC.style.minHeight	= rcMinHeight	+ offsetExt + "px";
	}
	
}

//will enable or disable the extended search box

function toggleExtSearch() 
{
    if(this.ext1 == false) 
    {
    	this.enableExtension('Search_Box_Ext1');
    	this.ext1 = true;
    }
	else 
	{  
		this.disableExtension('Search_Box_Ext1');
		this.ext1 = false;
		if (this.ext2 == true) 
		{
			this.disableExtension('Search_Box_Ext2');
			this.ext2 = false;
		}
		var sEI = getSearchEngineIndex();
    	if (sEI != (0 || 1)) 
    	{	//only 1 = stream and 0 = download is in top box
    		document.menuform.selmain[0].checked = true;
    	}
    }
}

//blurs unavailable functions 

function Blur() {	  	
	searchEngine = this.getSearchEngine();
	if(searchEngine == "multi" && this.ext2 == false) {
		this.enableExtension('Search_Box_Ext2');
		this.ext2 = true;
	}
	else if (searchEngine == "mp3"){
		if (this.ext2 == true) {
			this.disableExtension('Search_Box_Ext2');
			this.ext2 = false;
		}
	}
}

//////////////////////////////////////////////////
//enables with a fade effect any given object
//@param id = id of the object
//////////////////////////////////////////////////

function enableExtension(id) {
	document.getElementById(id).style.display = 'inline';
	this.setOpacity(id, 0, 100, 500);
	this.setBBC_RC(1);
}

//////////////////////////////////////////////////
//disables with a fade effect any given object
//@param id = id of the object
//////////////////////////////////////////////////

function disableExtension(id) {
	document.getElementById(id).style.display = 'none';
	this.setOpacity(id, 100, 0, 500);
	this.setBBC_RC(0);	
}

//////////////////////////////////////////////////
//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;
	
}

