	var __VideoSearchFactory;
	var _vInterval;

	function VideoSearchFactory(useDefaultParams)
	{	
		this.initialized  = false;
		this.page         = 1;
		this.gameTypes    = new Array();
		this.contentTypes = new Array();
		this.searchParams = new VideoSearchParams(useDefaultParams);
		this.form         = new VideoSearchForm();
		this.form.init();
			
		__VideoSearchFactory = this;
	}
	
	
	VideoSearchFactory.prototype.loadData = function(url)
	{
		$.ajax(
		{
			url: url,
			dataType: "json",
			success: function(result) 
			{
				__VideoSearchFactory.onLoadComplete(result);
				
				$("#loadCustomSearch").css("height", $("#search-wrapper").height()-16);
				$("#saveCustomSearch").css("height", $("#search-wrapper").height()-16);
			}
		});
	}
	
	
	VideoSearchFactory.prototype.onLoadComplete = function(result)
	{		
		this.setGametypes(result[0]);
		this.setContenttypes(result[1]);
		this.init();
	}
	
	
	VideoSearchFactory.prototype.init = function()
	{
		var hash = window.location.hash.replace("#","");

		if (hash == "")
		{			
			this.setParamsByPathname();
		
			hash = this.generateHashByParams();
			
			this.form.updateGUI(this.searchParams);
			this.initialized = true;
			
			this.search();
		}
		else
		{
			this.searchParams.refreshByHash(hash);
			
			hash = this.generateHashByParams();

			this.form.updateGUI(this.searchParams);
			this.initialized = true;
			this.onPageChange(hash);
		}
		
		this.refreshPopularlinks(hash);
	}
	
	
	VideoSearchFactory.prototype.setParamsByPathname = function()
	{
		var pathname = window.location.pathname;
								
		if (pathname.indexOf("/",pathname.length-1) == -1)
		{
			pathname += "/";
		}
	
		switch (pathname)
		{
			case "/video/fixed-limit/":
			this.searchParams.contentType = 17;
			this.searchParams.gameType = 1;
			break;
		
			case "/video/no-limit/":
			this.searchParams.contentType = 17;
			this.searchParams.gameType = 5;
			break;
			
			case "/video/sng/":
			this.searchParams.contentType = 17;
			this.searchParams.gameType = 7;
			break;
			
			case "/video/omaha/":
			this.searchParams.contentType = 17;
			this.searchParams.gameType = 4;
			break;
			
			case "/video/stud/":
			this.searchParams.contentType = 17;
			this.searchParams.gameType = 9;
			break;
			
			case "/video/guide/":
			this.searchParams.contentType = 13;
			break;
			
			case "/video/fun/":
			this.searchParams.contentType = 14;
			break;
			
			case "/video/event/":
			this.searchParams.contentType = 12;
			break;
			
			case "/video/psychology/":
			this.searchParams.contentType = 15;
			break;
		}
		
		return;
	}
	
	
	VideoSearchFactory.prototype.getLimitsByGameTypeId = function(gameTypeId) 
	{ 
		var i; 
		var l = this.gameTypes.length; 
		var gameType;
		
		for (i=0; i<l; i++)
		{
			gameType = this.gameTypes[i];
			
			if (gameType.id == gameTypeId)
			{
				return gameType.levels;
			}
		}
		
		return new Array();
	}
	
	VideoSearchFactory.prototype.setRPP = function(rpp)
	{
		this.searchParams.rpp = rpp;
		this.searchNoCopy();
	}
	
	
	VideoSearchFactory.prototype.setContenttypes = function(contentTypes)
	{
		var contentType;
		var i;
		var j;
		var l = contentTypes.length;
				
		for (i = 0; i < l; i++)
		{
			contentType              = new ContentType();
			contentType.id           = contentTypes[i]['idx'];
			contentType.useGametype  = (contentTypes[i]['useGametype']  == "1");
			contentType.useLimit     = (contentTypes[i]['useLimit']     == "1");
			contentType.useTablesize = (contentTypes[i]['useTablesize'] == "1");
						
			this.addContentType(contentType);
		}
	}
	
	
	VideoSearchFactory.prototype.setGametypes = function(gametypes)
	{
		var v   = parseInt(jQuery.browser.version);
		var ie7 = (jQuery.browser.msie && v > 6 && v < 8);
	
		var gameType;
		var i;
		var j;
		var l1 = gametypes.length;
		var l2;
		var limits;
		var limit;
				
		for (i = 0; i < l1; i++)
		{
			gameType        = new GameType();
			gameType.id     = gametypes[i]['gametype_id'];
			gameType.name   = gametypes[i]['gametype_name'];
				
			limits = new Array();
			l2     = gametypes[i]['limits'].length;
			
			for (j=0; j<l2; j++)
			{
				limit           = new GameTypeLimit();
				limit.name      = gametypes[i]['limits'][j]['name'];
				limit.value     = gametypes[i]['limits'][j]['value'];
				limit.id        = gametypes[i]['limits'][j]['id'];
				limit.pctStart  = gametypes[i]['limits'][j]['pctStart'];
				limit.pctStop   = gametypes[i]['limits'][j]['pctStop'];
				
				limits.push(limit);
			}
							
			gameType.limits = limits;
						
			this.addGameType(gameType);
			
			if (gameType.limits.length == 0)
			{
				$("#container_" + gameType.id).hide();
				gameType.useLimits = false;
			}
			else
			{
				$("#container_" + gameType.id).show();
				gameType.useLimits = true;
			}
			
			if (ie7)
			{
				$("#gametype_arrow_" + gameType.id).css("background-position","0 1px");
			}
		}
	}
	
	
	VideoSearchFactory.prototype.addContentType = function(contentType)
	{
		this.contentTypes.push(contentType);
	}
	
	
	VideoSearchFactory.prototype.addGameType = function(gameType)
	{
		this.gameTypes.push(gameType);
	}
	
	
	VideoSearchFactory.prototype.setParamsByHash = function(hash)
	{
		hash += "&page=1&rpp=" + this.searchParams.rpp;  
		window.location.href = hash;
	}
	
	VideoSearchFactory.prototype.setSearch = function(searchText,searchKey)
	{
		this.searchParams.reset();
		this.searchParams.page = 1;
		this.searchParams.searchText = searchText;
		this.searchParams.searchKey  = searchKey;
		this.searchNoCopy();
	}
	
	
	VideoSearchFactory.prototype.setTag = function(searchText)
	{
		this.setSearch(searchText,"tag");
	}
	
	
	VideoSearchFactory.prototype.setAuthor = function(searchText)
	{
		this.setSearch(searchText,"author");
	}
	
	
	
	VideoSearchFactory.prototype.setPage = function(page)
	{
		this.searchParams.page = page;
		this.searchNoCopy();
	}
	
	
	VideoSearchFactory.prototype.setOrder = function(orderBy,orderDir)
	{
		this.searchParams.orderBy  = orderBy;
		this.searchParams.orderDir = orderDir;
		this.searchNoCopy();
	}
	
	
	VideoSearchFactory.prototype.onPageChange = function(hash)
	{
		hideErrorPanel();
		
		if (!this.initialized)
		{
			return;
		}

		this.searchParams.refreshByHash(hash);	

		this.getResults(hash);
		this.form.updateGUI(this.searchParams);
		this.refreshPopularlinks(hash);
	}
	
	
	VideoSearchFactory.prototype.refreshPopularlinks = function(hash)
	{
		hash = "#" + hash;
		
		$('#popularsearches a').each( function()
		{
			var linkParams = String($(this).attr('href').replace(/&amp;/g, '&')).replace("javascript\:vsf\.setParamsByHash\(\'","").replace("');", "");
												
			if(hash.match(linkParams))
			{
				$(this).addClass('currently-represented');					
			}
			else
			{
				$(this).removeClass('currently-represented');
			}
		});
	}
	
	
	VideoSearchFactory.prototype.newSearch = function()
	{
		this.searchParams.page     = 1;
		this.searchParams.orderBy  = "date";
		this.searchParams.orderDir = "desc";
		this.search();
	}
	
	
	VideoSearchFactory.prototype.generateHashByParams = function()
	{
		var hash = "";
		
		this.searchParams.searchText = this.searchParams.searchText.replace(/[&]/g,"%26").replace(/[?]/g,"%3F");
		this.searchParams.searchText = this.searchParams.searchText.replace(/[+]/g,"%26#43").replace(/[\-]/g,"%2D");

		if (this.searchParams.searchText.trim() != "")
		{
			hash += "searchtext="  + this.searchParams.searchText.trim() + "&";
			hash += "key="         + this.searchParams.searchKey + "&";
		}
		
		hash += "contenttype=" + this.searchParams.contentType + "&";

		hash += "gametype="    + this.searchParams.gameType + "&";
		hash += "tablesize="   + this.searchParams.tableSize + "&";
		hash += "languages="   + this.searchParams.languages + "&";
		hash += "levels="      + this.searchParams.levels + "&";
		hash += "lowerlimit="  + this.searchParams.lowerLimit + "&";
		hash += "upperlimit="  + this.searchParams.upperLimit + "&";
		hash += "ob="          + this.searchParams.orderBy + "&";
		hash += "od="          + this.searchParams.orderDir + "&";
		hash += "page="        + this.searchParams.page + "&";
		hash += "rpp="         + this.searchParams.rpp;

		return hash;
	}
	
	/**
	 * Fire the search
	 * 
	 * @returns null
	 */
	VideoSearchFactory.prototype.search = function()
	{
		this.form.copyValuesToParams(this.searchParams);
		
		hash = this.generateHashByParams();
		hash = encodeURI(hash);		

		window.location.href = "/video/#" + hash;
	}
	
	/**
	 * Fire the search without using the current values 
	 * 
	 * @returns null
	 */
	VideoSearchFactory.prototype.searchNoCopy = function()
	{		
		hash = this.generateHashByParams();
		hash = encodeURI(hash);		
								
		window.location.href = "/video/#" + hash;
	}
	

	/**
	 * Ajax call to get results
	 * 
	 * @param String hash
	 * @return null
	 */
	VideoSearchFactory.prototype.getResults = function(hash)
	{
		$('#spinner').css('display','block');
		$.ajax(
		{
			type: "POST",	
			url: "/page/video/search/result.ajax.php",
			data: hash,
			success: function(result) 
			{
				$("#searchresult").html(result);
			}
		});
	}
	
	
	/**
	 * Get Content type by id
	 * 
	 * @param int contentTypeId
	 * @returns ContentType
	 */
	VideoSearchFactory.prototype.getContentTypeById = function(contentTypeId)
	{
		var i;
		var l = this.contentTypes.length;
		var contentType;
		
		for (i=0; i<l; i++)
		{
			contentType = this.contentTypes[i];
			
			if (contentType.id == contentTypeId)
			{
				return contentType;
			}
		}
	}
	
	
	/**
	 * Save the current search
	 * 
	 * @returns null
	 */
	VideoSearchFactory.prototype.saveSearch = function()
	{
		var searchname = $("#savesearch-name").val();
		
		if (searchname.trim() == "")
		{
			showErrorPanel("emptyName");
			return;
		}
		
		var hashArray = window.location.href.split("#");
		var hash = "";
		
		if (hashArray.length > 1)
		{
			hash = hashArray[1];
		}
		
		if (hash == "")
		{
			showErrorPanel("emptyHash");
			return;
		}
				
		var postData = new Object();
		postData.name = searchname;
		
		var hashToArray = hash.split("&");
		var i;
		var entry;
		
		for (i=0; i<hashToArray.length; i++)
		{
			entry = hashToArray[i].split("=");
			if (entry[0] && entry[1])
			{
				eval("postData." + entry[0] + "='" + entry[1]+ "'");
			}
		}
		
		$.ajax(
		{
			type: "POST",	
			url: "/page/video/search/save.ajax.php",
			dataType: "json",
			data: postData,
			success: __VideoSearchFactory.onSave
		});
	}
	
	
	/**
	 * Cancel the save
	 * 
	 * @returns null
	 */
	VideoSearchFactory.prototype.cancelSaveSearch = function()
	{
		togglePanel('saveCustomSearch', 'loadCustomSearch');
		hideErrorPanel();
	}
	
	
		
	VideoSearchFactory.prototype.onSave = function(result)
	{	
		if (result['success'])
		{
			$("#searcheslist").removeOption(/./);
			
			var i;
			var l = result['message'].length;
			var html = "";
			
			for (i=0; i<l; i++)
			{
				html += "<option value='" + result['message'][i]['idx'] + "'>" + result['message'][i]['name'] + "</option>";
			}
			
			$("#searcheslist").html(html);
			$("#savesearch-name").val("");
			
			togglePanel('saveCustomSearch', 'loadCustomSearch');
			hideErrorPanel();
		}
		else
		{
			showErrorPanel(result['message']);
		}
	}
	

	VideoSearchFactory.prototype.deleteSearch = function()
	{
		var search_id = $("#searcheslist option:selected").val();
		
		if (!search_id)
		{
			return;
		}
		
		$.ajax(
		{
			type: "POST",	
			url: "/page/video/search/delete.ajax.php",
			dataType: "json",
			data: "search_id=" + search_id,
			success: __VideoSearchFactory.onDelete,
			
			error: function(msg)
			{
				showErrorPanel(msg);
			}
		});
	}
	
	
	
	VideoSearchFactory.prototype.onDelete = function(result)
	{
		if (result['success'])
		{			
			var i;
			var l = result['message'].length;
			var html = "";
			
			for (i=0; i<l; i++)
			{
				html += "<option value='" + result['message'][i]['idx'] + "'>" + result['message'][i]['name'] + "</option>";
			}
			
			$("#searcheslist").html(html);
		}
		else
		{
			showErrorPanel(result['message']);
		}
	}
	
	
	

	VideoSearchFactory.prototype.loadSearch = function()
	{
		var search_id = $("#searcheslist option:selected").val();
					
		if (!search_id)
		{
			return;
		}
		
		$.ajax(
		{
			type: "POST",	
			url: "/page/video/search/load.ajax.php",
			dataType: "json",
			data: "search_id=" + search_id,
			success: __VideoSearchFactory.onLoadSearchComplete,
			
			error: function(msg)
			{
			}
		});
	}
	
	
	VideoSearchFactory.prototype.onLoadSearchComplete = function(result)
	{							
		if (result['success'])
		{			
			__VideoSearchFactory.createHashFromObj(result['message']);
		
		}
		else
		{
			showErrorPanel(result['message']);
		}
	}
	

	VideoSearchFactory.prototype.createHashFromObj = function(obj)
	{
		
		this.searchParams.reset();
				
		var key;
		
		for (key in obj)
		{
			this.searchParams.setByKey(key,obj[key]);
		}


		this.form.updateGUI(this.searchParams);
		this.searchNoCopy();
	}

	VideoSearchFactory.prototype.submitSearch = function()
	{
		this.searchParams.searchKey = "all";
		this.newSearch();
	}






	function VideoSearchForm()
	{
		this.hash;
		this.lowerLimit = 0;
		this.upperLimit = 100;
	}

	VideoSearchForm.prototype.init = function()
	{	
		$("#gametypes").keyup(function(result)
		{
			__VideoSearchFactory.form.filterLimitsRanges();		
		});
		
		
		$("#gametypes").change(function(result)
		{
			__VideoSearchFactory.form.filterLimitsRanges();		
		});
		
		
		$("#contenttypes").keyup(function(result)
		{
			__VideoSearchFactory.searchParams.contentType = $(this).val();
			__VideoSearchFactory.form.filterByContentType(__VideoSearchFactory.searchParams.contentType);		
		});
		
		
		$("#contenttypes").change(function(result)
		{
			__VideoSearchFactory.searchParams.contentType = $(this).val();
			__VideoSearchFactory.form.filterByContentType(__VideoSearchFactory.searchParams.contentType);		
		});
		
		
		$("#searchterm").focus(function()
		{
			if ($(this).val() == $("#system-InitialText").val())
			{
				$(this).val("");
			}
		});
		
		
		$("#startsearch").click(function()
		{
			__VideoSearchFactory.submitSearch();
		});
		
		
		$("#searchterm").keydown(function(event)
		{
			$(this).removeClass("sys-initialtext");
		
			
			if (event.keyCode == 13)
			{
				$("a.lib-gfx-btn-sml").css("background-position","left -27px");
				$("a.lib-gfx-btn-sml *").css("background-position","right -27px");
			}
		});
		

		$("#searchterm").keyup(function(event)
		{
			if (event.keyCode == 13)
			{
				_vInterval = window.setTimeout("unhoverSubmit()",100);

				__VideoSearchFactory.submitSearch();
			}
		});
		
		
		$("#savesearch").click(function()
		{
			__VideoSearchFactory.saveSearch();
		});
		
		$("#cancelsavesearch").click(function()
		{
			__VideoSearchFactory.cancelSaveSearch();
		});
		
		
		$("#deletesearch").click(function()
		{
			__VideoSearchFactory.deleteSearch();
		});
		
		
		$("#loadsearch").click(function()
		{
			__VideoSearchFactory.loadSearch();
		});
		
		
		$("#paginator").change(function()
		{
			__VideoSearchFactory.setRPP($(this).val());
		});
		
		
		$("#paginator").keyup(function()
		{
			__VideoSearchFactory.setRPP($(this).val());
		});
		

/*
 * --- wird eh per CSS gemacht --- $("a.lib-gfx-btn-sml ").mouseover(function() {
 * $("a.lib-gfx-btn-sml ").css("background-position","left -27px"); $("a.lib-gfx-btn-sml
 * *").css("background-position","right -27px"); }).mouseout(function() { $("a.lib-gfx-btn-sml
 * ").css("background-position","left 0"); $("a.lib-gfx-btn-sml *").css("background-position","right
 * 0"); }); $("a.lib-gfx-btn-sml *").mouseover(function() { $("a.lib-gfx-btn-sml
 * ").css("background-position","left -27px"); $("a.lib-gfx-btn-sml
 * *").css("background-position","right -27px"); }).mouseout(function() { $("a.lib-gfx-btn-sml
 * ").css("background-position","left 0"); $("a.lib-gfx-btn-sml *").css("background-position","right
 * 0"); });
 */


    $('#searchterm').blur(function(event)
		{
			if (($.trim($(this).val()) === $('#system-InitialText').val()) || ($.trim($(this).val()) === ''))
			{
				$(this).val($('#system-InitialText').val());
				$(this).addClass('sys-initialtext');
			} 
		});
	}
	
	function unhoverSubmit()
	{
		window.clearTimeout(_vInterval);
		
		$("a.lib-gfx-btn-sml ").css("background-position","left 0");
		$("a.lib-gfx-btn-sml *").css("background-position","right 0"); 
	}
	
	
	VideoSearchForm.prototype.updateGUI = function(searchParams)
	{				
		this.setSearchText(searchParams.searchText,searchParams.searchKey);
		this.setLimitSliderByValues(searchParams.lowerLimit,searchParams.upperLimit);
	
		this.setContentType(searchParams.contentType);
		this.setGametype(searchParams.gameType);
		this.setTablesize(searchParams.tableSize);
		
		this.setLanguages(searchParams.languages);
		this.setLevels(searchParams.levels);

		this.setOrder(searchParams.ob,searchParams.od);
		
		this.filterByContentType(searchParams.contentType);
		this.filterLimitsRanges();
	}
	
	VideoSearchForm.prototype.setContentType = function(contenttype)
	{
		$("#contenttypes").val(contenttype);
	}
	
	VideoSearchForm.prototype.setTablesize = function(tablesize)
	{
		$("#tablesizes").val(tablesize);
	}
	
	VideoSearchForm.prototype.setGametype = function(gametype)
	{
		$("#gametypes").val(gametype);
	}
	
	VideoSearchForm.prototype.setOrder = function(ob,od)
	{
		$("#ob").val(ob);
		$("#od").val(od);
	}
	
	
	VideoSearchForm.prototype.setSearchText = function(text,key)
	{		
		if (text == "" || key != "all")
		{
			text = $("#system-InitialText").val();
			$("#searchterm").addClass("sys-initialtext");
		}
		else
		{
			$("#searchterm").removeClass("sys-initialtext");
		}
		
		var t = text.replace(/%26/g,"&").replace(/%3F/g,"?").replace(/&#43/g,"+").replace(/%2D/g,"-");

		
		if (key == "all")
		{
			$("#searchterm").val(t);
		}
		else
		{
			t = $("#system-InitialText").val();
			$("#searchterm").val(t);
		}
	}
	

	VideoSearchForm.prototype.filterLimitsRanges = function()
	{
		var val = $("#gametypes").val();		

		var l = __VideoSearchFactory.gameTypes.length;
		var i;
		var matchId = "container_" + val;
		var currentId;
		var gameType;
		
		var fadeLimits = false;
				
		for (i=0; i<l; i++)
		{
			gameType  = __VideoSearchFactory.gameTypes[i];
			currentId = "container_" + gameType.id;

			if (currentId == matchId)
			{
				if (gameType.useLimits)
				{					
					$("#"+ currentId).fadeTo("def",1);
				}
				else
				{
					fadeLimits = true;
				}				
			}
			else if (val == 0)
			{				
				$("#"+ currentId).fadeTo("def",1);
			}
			else
			{
				$("#"+ currentId).fadeTo("def",0.2);
			}
		}
		
		
		
		this.refreshLimitDisplay();
	}

	VideoSearchForm.prototype.setLanguages = function(languages)
	{
		var checkBoxes = $("#searchlanguages input");
		var checkBox;
		var checked;
		var i;
		var l = checkBoxes.length;
				
		for (i=0; i<l; i++)
		{
			checkBox = $(checkBoxes[i]);
			checked  = (languages.in_array(checkBox.val()));
									
			checkBox.attr("checked",checked);
		}	
	}

	VideoSearchForm.prototype.setLevels = function(levels)
	{
		var checkBoxes = $("#playerranks input");
		var checkBox;
		var checked;
		var i;
		var l = checkBoxes.length;
								
		for (i=0; i<l; i++)
		{
			checkBox = $(checkBoxes[i]);
			checked = (levels.in_array(checkBox.val()));
									
			checkBox.attr("checked",checked);
		}	
	}

	VideoSearchForm.prototype.filterByContentType = function(contentTypeId)
	{		
		if (contentTypeId == 0)
		{
			this.setGametypeField(true);
			this.setLimitsField(true);
			this.setTablesizeField(true);
		}
		else
		{
			var contentType = __VideoSearchFactory.getContentTypeById(contentTypeId);
		
			this.setGametypeField(contentType.useGametype);
			this.setLimitsField(contentType.useLimit);
			this.setTablesizeField(contentType.useTablesize);
		}
	}

	VideoSearchForm.prototype.setGametypeField = function(enabled)
	{
		$("#gametypes").attr("disabled",!enabled);
		
		if (enabled)
		{
			$("#gametypes").fadeTo("def",1);
		}
		else
		{
			$("#gametypes").fadeTo("def",0.2);
		}
	}
	
	VideoSearchForm.prototype.setLimitsField = function(enabled)
	{
		if (enabled)
		{
			$("#slider-limit-range").slider('enable');
			$("#rightrow").fadeTo("def",1);
		}
		else
		{
			$("#slider-limit-range").slider('disable');
			$("#rightrow").fadeTo("def",0.2);
		}
	}

	VideoSearchForm.prototype.setTablesizeField = function(enabled)
	{
		$("#tablesizes").attr("disabled",!enabled);
		
		if (enabled)
		{
			$("#tablesizes").fadeTo("def",1);
		}
		else
		{
			$("#tablesizes").fadeTo("def",0.2);
		}
	}

	VideoSearchForm.prototype.refreshLimitDisplay = function()
	{
		
		var v = parseInt(jQuery.browser.version);
		var ie7 = (jQuery.browser.msie && v >= 6 && v < 8);
	
		var spanLeft;
		var spanRight;
		var upperLimit;
		var lowerLimit;
		var arrowDiv;
		var gameTypeId;

		var i;
		var gameTypes =  __VideoSearchFactory.gameTypes;
		var l = gameTypes.length;
		var w;
		
		for (i=0; i<l; i++)
		{
			gameTypeId = gameTypes[i]['id'];
			lowerLimit = gameTypes[i].getLimitByPct(this.getLowerLimit());
			upperLimit = gameTypes[i].getLimitByPct(this.getUpperLimit());
						
			lowerLimit = (!lowerLimit) ? 0 : lowerLimit;
			upperLimit = (!upperLimit) ? 0 : upperLimit;
			
				
			spanLeft = document.getElementById("gametype_lowerlimit_" + gameTypeId);
			
			if (spanLeft)
			{
				spanLeft.firstChild.nodeValue = lowerLimit.name;	
			}
				
			spanRight = document.getElementById("gametype_upperlimit_" + gameTypeId);
			if (spanRight)
			{
				spanRight.firstChild.nodeValue = upperLimit.name;
			}
				
			
			arrowDiv = document.getElementById("gametype_arrow_" + gameTypeId);
			
			if (arrowDiv.style && spanRight.offsetLeft)
			{
				if (ie7)
				{
					arrowDiv.style.position = "absolute";
					spanRight.style.position = "absolute";

					w = spanRight.offsetLeft - arrowDiv.offsetLeft - spanRight.offsetWidth - 12;

					arrowDiv.style.position = "relative";
					spanRight.style.position = "relative";
				}
				else
				{
					w = spanRight.offsetLeft - arrowDiv.offsetLeft - 12;
				}
		
				// dirty hide fix
				w = parseInt(w) - 2;
				
				if (w < 1)
				{
					w = 0;
				}
				
				w = new String(w);
				arrowDiv.style.width = w + "px";
			}
		}
	}

	VideoSearchForm.prototype.setLimitSliderByValues = function(lowerLimit,upperLimit)
	{
		$("#sliderlimitleft").val(lowerLimit);
		$("#sliderlimitright").val(upperLimit);
		
		$("#slider-limit-range").slider('destroy');
		$("#slider-limit-range").slider(
		{
			range: true,
			min: 0,
			max: 100,
			values: [lowerLimit, upperLimit],
			animate: true,
			slide: function(event, ui)
			{
				var left  = ui.values[0];
				var right = ui.values[1];
				
				$("#sliderlimitleft").val(left);
				$("#sliderlimitright").val(right);
				
				__VideoSearchFactory.form.refreshLimitDisplay();
			}
		});
	}
	
	
	VideoSearchForm.prototype.copyValuesToParams = function(searchParams)
	{
		if (searchParams.searchKey   == "all")
		{
			searchParams.searchText  = this.getSearchText();
		}

		searchParams.contentType = this.getContentType();
		searchParams.gameType    = this.getGameType();
		searchParams.tableSize   = this.getTableSize();
		searchParams.languages   = this.getLanguages();
		searchParams.levels      = this.getLevels();
		searchParams.lowerLimit  = this.getLowerLimit();
		searchParams.upperLimit  = this.getUpperLimit();
		searchParams.page        = this.getPage();
		searchParams.rpp         = this.getRPP();
	}


	
	
	
	VideoSearchForm.prototype.getRPP = function()
	{
		return $("#paginator").val();
	}
	
	VideoSearchForm.prototype.getPage = function()
	{
		return $("#page").val();
	}
	
	VideoSearchForm.prototype.getOrderBy = function()
	{
		return $("#order_by").val();
	}
	
	VideoSearchForm.prototype.getOrderDir = function()
	{
		return $("#order_dir").val();
	}
	
	VideoSearchForm.prototype.getSearchText = function()
	{
		return ($("#searchterm").val() != $("#system-InitialText").val()) ? $("#searchterm").val() : "";
	}
	
	
	
	VideoSearchForm.prototype.getContentType = function()
	{
		return ($("#contenttypes option:selected").val());
	}
	
	VideoSearchForm.prototype.getGameType = function()
	{
		return ($("#gametypes option:selected").val());
	}
	
	VideoSearchForm.prototype.getTableSize = function()
	{
		return ($("#tablesizes option:selected").val());
	}

	VideoSearchForm.prototype.getLanguages = function()
	{
		var languages = $("#searchlanguages input:checked");
		var i;
		var l = languages.length;
		var result = new Array();
		
		for (i=0; i<l; i++)
		{
			result.push($(languages[i]).val());
		}
		
		return result;
	}

	VideoSearchForm.prototype.getLevels = function()
	{
		var levels = $("#playerranks input:checked");
		var i;
		var l = levels.length;
		var result = new Array();
		
		for (i=0; i<l; i++)
		{
			result.push($(levels[i]).val());
		}
		
		return result;
	}
	
	VideoSearchForm.prototype.getLowerLimit = function()
	{
		return ($("#sliderlimitleft").val());
	}

	VideoSearchForm.prototype.getUpperLimit = function()
	{
		return ($("#sliderlimitright").val());
	}




	
	function VideoSearchParams(useDefaultParams)
	{
		this.useDefaultParams = useDefaultParams;
		this.searchText;
		this.searchKey;
		this.contentType;
		this.gameType;
		this.tableSize;
		
		this.lowerLimit;
		this.upperLimit;
		
		this.languages;
		this.levels;
		this.page;
		this.orderBy;
		this.orderDir;
		
		this.reset();
	}
	
	
	VideoSearchParams.prototype.reset = function()
	{		
		if (this.useDefaultParams)
		{
			this.resetToDefault();
		}
		else
		{
			this.resetToLoggedOut();
		}
	}
	
	VideoSearchParams.prototype.resetToDefault = function()
	{
		this.searchText  = "";
		this.searchKey  = "all";
				
		this.contentType = 0;
		this.gameType    = 0;
		this.tableSize   = 0;
		
		this.lowerLimit  = 0;
		this.upperLimit  = 100;
		
		this.page        = 1;
		this.rpp         = $("#paginator").val();
		
		this.languages   = new Array();
		
		this.orderBy     = "date";
		this.orderDir    = "desc";
		
		var langforms = $("#searchlanguages input");

		this.languages.push($(langforms[0]).val());
		
		if (langforms[1])
		{
			this.languages.push($(langforms[1]).val());
		}
		
		
		this.levels      = new Array();
		var levels = $("#playerranks input");
		
		for (var i=0; i<levels.length; i++)
		{
			this.levels.push($(levels[i]).val());
		}		
	}
	
	
	VideoSearchParams.prototype.resetToLoggedOut = function()
	{
		this.searchText  = "";
		this.searchKey  = "all";
				
		this.contentType = 17;
		this.gameType    = 0;
		this.tableSize   = 0;
		
		this.lowerLimit  = 0;
		this.upperLimit  = 100;
		
		this.page        = 1;
		this.rpp         = $("#paginator").val();
		
		this.languages   = new Array();
		
		this.orderBy     = "views";
		this.orderDir    = "desc";
		
		var langforms = $("#searchlanguages input");

		this.languages.push($(langforms[0]).val());
		
		if (langforms[1])
		{
			this.languages.push($(langforms[1]).val());
		}
		
		
		this.levels      = new Array();
		this.levels.push('basic');	
	}
	
	
	VideoSearchParams.prototype.refreshByHash = function(hash)
	{
		this.reset();
		
		var params = hash.split("&");
		var i;
		var l = params.length;
		var param;
		var key;
		var value;
				
		for (i=0; i<l; i++)
		{
			param = params[i].split("=");
			
			if (param[1] != undefined && param[1].length > 0)
			{
				key   = decodeURI(param[0]);

				value = decodeURI(param[1]);
				
				this.setByKey(key,value);
			}
		}
	}
	
	
	VideoSearchParams.prototype.setByKey = function(key,value)
	{		
		key = key.trim();
		value = value.trim();
		
		switch (key)
		{
			case "search":
			this.searchText = value;
			break;

			case "searchtext":
			this.searchText = value;
			break;
			
			case "key":
			this.searchKey = value;
			break;
					
			case "contenttype":
			this.contentType  = parseInt(value);
			break;
					
			case "gametype":
			this.gameType  = parseInt(value);
			break;
					
			case "tablesize":
			this.tableSize  = parseInt(value);
			break;
					
			case "lowerlimit":
			this.lowerLimit = parseInt(value);

			break;
					
			case "upperlimit":
			this.upperLimit = parseInt(value);

			break;
			
			case "ob":
			this.orderBy = value;
			break;
			
			case "od":
			this.orderDir = value;
			break;
			
			case "languages":
			this.languages = value.split(",");
			break;
					
			case "levels":
			this.levels = value.split(",");
			break;
			
			case "page":
			this.page = value;
			break;
			
			case "rpp":
			this.rpp = value;
			break;
		}
	}
	



	function GameType()
	{
		this.id;
		this.name;
		this.limits = new Array();
		this.useLimits;
	}
	
	
	
	GameType.prototype.getLimitByPct = function(pct)
	{
		var limit;
		var l = this.limits.length;
		var i;
		
		for (i=0; i<l; i++)
		{
			limit = this.limits[i];

			if (limit.pctStart <= pct && limit.pctStop >= pct)
			{
				return limit;
			}
		}	
	}
	


	function GameTypeLimit()
	{
		this.id;
		this.value;
		this.name;
		this.pctStart;
		this.pctStop;
	}
	
	
	
	function ContentType()
	{
		this.id;
		this.useGametype;
		this.useLimit;
		this.useTablesize;
	}
	
	
	function trace(text)
	{
		debug(text);
	}
	

	function debug(text)
	{
		try
		{
			console.debug(text);
		}
		catch (e)
		{
		}
	}
	
	function togglePanel(idHide, idShow)
	{
		$('#'+idHide).css('display','none');
		$('#'+idShow).css('display','block');
	}

	
	
	Array.prototype.in_array = function(obj)
	{
     var i;
     var l = this.length;
     
     for (i = 0; i < l; i++) 
     {
       if (this[i] === obj)
       {
         return true;
       }
     }
     return false;
   };
   
   
  String.prototype.trim = function ()
  {
  	return this.replace (/^\s+/, '').replace (/\s+$/, '');
	}


 
   
	function showErrorPanel(field)
	{
		var content = $("#error_" + field).val();
		
		$("#error ul li").html(content);
		$("#error").slideDown(200);
	}
	
	function hideErrorPanel()
	{
		$("#error").slideUp(100);
	}

