var axaxloaderGif = '<span style="display:block;width:150px;margin-left:auto;margin-right:auto;"><br /><br />Loading...<br /><br /><img src="fileadmin/templates/scripts_m1.2/ebv-logo-animiert.gif" alt="" title="" /></span>';
var requestFailed = '<br /><br />The request failed.';
var url='';
var selectedRadio=$get('sorttype');
var selectedSupplier=$get('supplier');
var selectedMarket =$get('market');
var selectedCategory =$get('category');
var selectedVideotype = $get('videotype');
if(selectedVideotype == '') {
	selectedVideotype = (location.href.substr(33,2) == 'un' ? 2 : 1);
}
var browse=0;
var page=1;
var changedSelect = false;

var browse_back = function(){browseList(-1);}
var browse_forward = function(){browseList(1);}

var listreq = new Request.HTML({
	url: url,
	method: 'get',
	evalScripts :false,
	evalResponse : false,
	noCache : true,
	update: $('videolist'),
	onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
		
		$('videolist').set('text', '');
		$('videolist').set('html',responseHTML);
		
		$exec(responseJavaScript);
	},
	onRequest:function(){
		$('videolist').set('html', axaxloaderGif );
	},
	onFailure: function(){
		$('videolist').set('html', requestFailed );
	}
});

window.addEvent("domready", function(){
if ($('formvideosearch')) {
    
	$('videosearchinput').value = decodeURI($get('searchstr'));
	$('vsearch_supplier').setProperty('value', selectedSupplier);
	$('vsearch_market').setProperty('value', selectedMarket);
	$('vsearch_category').setProperty('value', selectedCategory);

    //mod ML 17:00
    //var vsearchsupplier = new elSelect( {container : "vsearch_supplier", selection : "sel_supplier",maxoptions:5, breite:110} );
    //var vsearchmarkets = new elSelect( {container : "vsearch_market", selection : "sel_markets",maxoptions:5, breite:110} );
    //var vsearchcat = new elSelect( {container : "vsearch_category", selection : "sel_cat",maxoptions:5, breite:110} );

	$('formvideosearch').addEvent('submit', function(){e.stop(); return false;});
	$('pagenumber').addEvent('keypress', function(event){return enterKey(event,'');});
	$('videosearchinput').addEvent('keypress', function(event){return enterKey(event,1);});
		
	$$('#vsearch_supplier .option').addEvent('click', function(){
		$('videosearchinput').value = '';
		selectedSupplier = this.value;
		changedSelect = true;
		renewProductlist('', 1, '');
	});
	$$('#vsearch_market .option').addEvent('click', function(){
		$('videosearchinput').value = '';
		selectedMarket = this.value;
		changedSelect = true;
		renewProductlist('', 1, '');
	});
	$$('#vsearch_category .option').addEvent('click', function(){
		$('videosearchinput').value = '';
		selectedCategory = this.value;
		changedSelect = true;
		renewProductlist('', 1, '');
	});

	$$('#videosearch .radio').addEvent('click', function(){
		if (this.nextSibling.checked==true) {
			selectedRadio = this.nextSibling.value;
			renewProductlist(this.nextSibling.value, 1, '');
		}
	});

	//var tagtips = new Tips($$('#videodetails .tags .tag'), {
	//	maxOpacity: 0.9,
	//	maxTitleChars: 25,
	//	fixed: false,
	//	hideDelay: 50,
	//	showDelay: 50,
	//	className:'tipcontainer'
	//});
}

	if ($('videosearchinput')) {
		var inputWord = $('videosearchinput');
		var autocmpleterUrl = 'index.php?eID=ebvvideolistautocompleter';
		new Autocompleter.Request.HTML(inputWord, autocmpleterUrl, {
			'minLength': 3,
			'width' : 250,
			'delay' : 300,
			'autoSubmit' : true,
			'onComplete' : function()	{
				$('autocompleter-choices').addEvent('click', function(){renewProductlist('',1,'');});
			}
			
		});
	}
}); //addEvent Ende

function unescapeHTML(s) {
	/*
	var div = document.createElement('div');
	div.innerHTML = s;
	return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
	*/
	return s;
}

function browseList(direction)	{
	browse = direction;
	renewProductlist('', page, '');
}

function enterKey(e,page)	{
	if(e.key=='enter') {
		if(page=='') page = $('pagenumber').value;
		if(parseInt(page) > 0)
			renewProductlist('',parseInt(page),'')
		return false;
	} else	{
		return true;
	}
}

function renewProductlist (radioValue, newpage, tag) {
	
	url='index.php?eID=ebvvideolist';
	searchstr = $('videosearchinput').value;
	supplierparam = selectedSupplier;
	marketparam = selectedMarket;
	catparam = selectedCategory ;
	videotypeparam = selectedVideotype;
	sorttype = selectedRadio;
	browseparam = browse;
	if (selectedRadio){
		url += '&sorttype='+selectedRadio;
	}
	if (searchstr){
		url += '&searchstr='+encodeURI(searchstr);
	}
	if (supplierparam){
		url += '&supplier='+escape(supplierparam);
	}
	if (videotypeparam){
		url += '&videotype='+escape(videotypeparam);
	}	
	if (marketparam){
		url += '&market='+escape(marketparam);
	}
	if (catparam){
		url += '&category='+escape(catparam);
	}
	if (browseparam){
		url += '&browse='+ escape(browseparam);
	}
	if (changedSelect) url += '&changedSelect=1';
	url += '&page='+escape(parseInt(newpage)+parseInt(browse));
	url += '&tx_ebvvideo[uid]='+escape(videouid);
	url += '&ct_ref=' + escape('u903-v' + videouid);
	if(tag.length > 0) url += '&tag='+encodeURI(tag);
	
	browse=0;
	changedSelect = false;
	listreq.send({url:url});
}

function nullFlashLoopFunction() { __flash_savedUnloadHandler = null; }

/*
Function: $get 
        This function provides access to the "get" variable scope + the element anchor 
 
Version: 1.3 
 
Arguments: 
        key - string; optional; the parameter key to search for in the url's query string (can also be "#" for the element anchor) 
        url - url; optional; the url to check for "key" in, location.href is default 
 
Example: 
        >$get("foo","http://example.com/?foo=bar"); //returns "bar" 
        >$get("foo"); //returns the value of the "foo" variable if it's present in the current url(location.href) 
        >$get("#","http://example.com/#moo"); //returns "moo" 
        >$get("#"); //returns the element anchor if any, but from the current url (location.href) 
        >$get(,"http://example.com/?foo=bar&bar=foo"); //returns {foo:'bar',bar:'foo'} 
        >$get(,"http://example.com/?foo=bar&bar=foo#moo"); //returns {foo:'bar',bar:'foo',hash:'moo'} 
        >$get(); //returns same as above, but from the current url (location.href) 
        >$get("?"); //returns the query string (without ? and element anchor) from the current url (location.href) 
 
Returns: 
        Returns the value of the variable form the provided key, or an object with the current GET variables plus the element anchor (if any) 
        Returns "" if the variable is not present in the given query string 
 
Credits: 
                Regex from [url=http://www.netlobo.com/url_query_string_javascript.html]http://www.netlobo.com/url_query_string_javascript.html[/url] 
                Function by Jens Anders Bakke, webfreak.no 
*/  
function $get(key,url){
	if(arguments.length < 2) url = location.href;
	if(arguments.length > 0 && key != ""){
		if(key == "#"){
			var regex = new RegExp("[#]([^$]*)");
		} else if(key == "?"){
			var regex = new RegExp("[?]([^#$]*)");
		} else {
			var regex = new RegExp("[?&]"+key+"=([^&#]*)");
		}
		var results = regex.exec(url);
		return (results == null )? "" : results[1];
	} else {
		url = url.split("?");
		var results = {};
		if(url.length > 1){
			url = url[1].split("#");
		if(url.length > 1) results["hash"] = url[1];
			url[0].split("&").each(function(item,index){
				item = item.split("=");
				results[item[0]] = item[1];
			});
		}
		return results;
	}
}
