function setClip() {
	var IE7 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 7.")!=-1)) ? 1 : 0;
	var IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? 1 : 0;
	
	/* IE7 */
	if(IE7){
	var element1 = document.getElementById("searchLeftSelect");
	var element2 = document.getElementById("selectedtag");
	var height1 = element1.clientHeight+3;
	var width1 = element1.clientWidth+21;
	
	var height2 = element2.clientHeight+3;
	var width2 = element2.clientWidth+3;
	element1.style.position = "absolute";
	element1.style.clip ="rect("+3+"px "+width1+"px "+height1+"px "+3+"px)" ;
	element2.style.position = "absolute";
	element2.style.clip ="rect("+3+"px "+width2+"px "+height2+"px "+3+"px)" ;
	}
	/* IE 6 */
	else if(IE6){
	var element1 = document.getElementById("searchLeftSelect");
	var element2 = document.getElementById("selectedtag");
	var height1 = element1.clientHeight-6;
	var width1 = element1.clientWidth-2;
	
	var height2 = element2.clientHeight-14;
	var width2 = element2.clientWidth-3;
	element1.style.position = "absolute";
	element1.style.clip ="rect("+3+"px "+width1+"px "+height1+"px "+3+"px)" ;
	element2.style.position = "absolute";
	element2.style.clip ="rect("+3+"px "+width2+"px "+height2+"px "+3+"px)" ;
	
	}
}

function redirectToInbox()
{
   window.location="search.php";
}

function buildConditionsVariable()
{
	var selectBox = $("[@id=selectedtag]");
	var optionGroupElements = selectBox.children();
	var conditions = "";
	
	for(i = 0; i < optionGroupElements.length; i++)
	{
		options = optionGroupElements[i].childNodes;
	
		for(j = 0; j < options.length; j++)
		{
			if (typeof options[j].value != "undefined")
			{
				conditions += options[j].value+"+";
			}
		}
	}
	
	if (conditions.length > 0)
	{
		conditions = conditions.substr(0, conditions.length - 1);
	}
	
	$("input#conditions").attr("value", conditions);
}

function hideSaveSearchPage(a)
{
	$("[@id=saveSearchDiv]").slideUp("fast");
	return false;
}

function goSaveSearchPage()
{
	$("[@id=saveSearchDiv]").slideDown("fast");
	$("input#savingSearchCancel1").click(hideSaveSearchPage);
	$("input#saveSearchDivButton").click(buildConditionsVariable);
	$("input#saveSearchDivInput").keyup(switchQuickSaveButtonState);
	//switchQuickSaveButtonState();
	return false;
}

function getSelectedItem(selectList)
{
	for(i=0; i < selectList.options.length; i++)
	{
		if (selectList.options[i].selected == true)
		{
			return selectList.options[i];
		}
	}
}

function getSelectedGroup(selectList)
{
	for(i=0; i < selectList.options.length; i++)
	{	
		if (selectList.options[i].selected == true)
		{
			return selectList.options[i].parentNode;
		}
	}
}

function appendSearchItem(optGroup, selectItem)
{
	var selectBox = $("[@id=selectedtag]");
	var groupElementId = optGroup.id.replace("available", "chosen");
	var enableAddEvent = true;
	var optionGroupElements = selectBox.children();

	for(i = 0; i < optionGroupElements.length; i++)
	{
		id = optionGroupElements[i].id;

		if (id == groupElementId)
		{
			enableAddEvent = false;
		}
	}
	
	if (enableAddEvent)
	{
		$('<optgroup label="'+optGroup.label+'" id="'+optGroup.id.replace("available", "chosen")+'"><option id="chosen_'+selectItem.id+'" value="'+selectItem.value+'">'+selectItem.innerHTML+'</option></optgroup>').appendTo("[@id=selectedtag]");
	}
	else
	{
		$('<option id="chosen_'+selectItem.id+'" value="'+selectItem.value+'">'+selectItem.innerHTML+'</option>').appendTo("[@id="+groupElementId+"]");
	}
	
	$("option").mouseover(rolloverEffect);
	$("option").mouseout(rolloverOut);
}

/* Rapidhi willingness disabling hack */
function switchRapidhiWillingness(selectItem)
{
	if(selectedTag == 'nonnude')
	{
		if(selectItem.value.indexOf("undertakings", 0) == 0)
		{
			noUnderTakingSelected = true;
			
			inactivateGroup("available_category");
			
			$("option[@id*=chosen_undertakings]").each(function(index) {
				noUnderTakingSelected = false;
				selectedUndertaking = this.value.split('|');
				selectedUndertaking = selectedUndertaking[1];
				
				$("option[@id*=category_]").each(function(index) {
					category = this.value.split("|");
					isIn = false;
					
					for(undertaking in performer_undertakings[category[1]]["under"]) {
						if (selectedUndertaking == undertaking) {
							isIn = true;
						}
					}
					if(isIn) {
						$("option[@id=category_" + category[1] + "]").css({color:"#ffffff"}).attr({disabled:""});
					}
				});
			});
			
			if(noUnderTakingSelected){
				activateGroup("available_category");
			}
		}
	}
	
	if(
		selectItem.value == "category|c1000" ||
		selectItem.value == "category|c2000" ||
		selectItem.value == "category|c3000" ||
		selectItem.value == "category|c4000" ||
		selectItem.value == "category|c5000")
	{
		inactivateGroup("available_undertakings");
		
		var itemSelectorString = "";
		
		$("option[@id*=chosen_category]").each(function(index) {
			selectedId = this.value.split("|");
			
			for(undertaking in performer_undertakings[selectedId[1]]["under"]) {
				itemSelectorString = itemSelectorString + ", option[@id=undertakings_" + undertaking + "]";
			}
		});

		itemSelectorString = itemSelectorString.substr(2);
		if(itemSelectorString.length > 0)
		{
			$(itemSelectorString).css({color:"#ffffff"}).attr({disabled:""});
		}
		else
		{
			activateGroup("available_undertakings");
		}	
	}
}

function removeSearchItem(optGroup, selectItem)
{
	$("[@id="+selectItem.id+"]").remove();
	$("[@id="+selectItem.id.replace("chosen_", "")+"]").css({color:"#FFFFFF"}).removeAttr("disabled");

	if(selectedGroupHasMoreElements(optGroup))
	{
		$("[@id="+optGroup.id+"]").remove();
		
		if (optGroup.id == "chosen_penissize" &&
			$("select#searchInSelect option:selected").get(0).value == "videos" &&
			$("optgroup#chosen_category").length == 0)
		{
			 $("option#category_mature, option#category_girl, option#category_lesbian, option#category_fetish").css({color:"#FFFFFF"}).attr({disabled:""});	
		}
		
		if (optGroup.id == "chosen_breastsize" &&
			$("select#searchInSelect option:selected").get(0).value == "videos" &&
			$("optgroup#chosen_category").length == 0)
		{
			 $("option#category_boy, option#category_gay").css({color:"#FFFFFF"}).attr({disabled:""});
		}
	}
	
	if((selectItem.id == "chosen_category_girl" ||
		selectItem.id == "chosen_category_lesbian" ||
		selectItem.id == "chosen_category_mature" ||
		selectItem.id == "chosen_category_fetish") &&
	   ( 
	   	$("option#chosen_category_girl").length == 0 &&
		$("option#chosen_category_lesbian").length == 0 &&
		$("option#chosen_category_mature").length == 0 &&
		$("option#chosen_category_fetish").length == 0
	   ))
	{
		activateGroup("available_penissize");
		
		if ($("optgroup#chosen_breastsize").length == 1) {
			$("option#category_transgender, option#category_couple, option#category_groups").css({
				color: "#FFFFFF"
			}).attr({
				disabled: ""
			});
		}
		else
		{
			$("option#category_boy, option#category_gay, option#category_transgender, option#category_couple, option#category_groups").css({
				color: "#FFFFFF"
			}).attr({
				disabled: ""
			});
		}	
	}
	
	if((selectItem.id == "chosen_category_boy" ||
		selectItem.id == "chosen_category_gay") &&
	   ( 
	   	$("option#chosen_category_boy").length == 0 &&
		$("option#chosen_category_gay").length == 0
	   ))
	{
		activateGroup("available_breastsize");
		
		if ($("optgroup#chosen_penissize").length == 1) 
		{
			$("option#category_transgender, option#category_couple, option#category_groups").css({ color: "#FFFFFF" }).attr({ disabled: "" });
		}
		else {
			$("option#category_girl, option#category_lesbian, option#category_mature, option#category_fetish, option#category_transgender, option#category_couple, option#category_groups").css({ color: "#FFFFFF" }).attr({ disabled: "" });
		}
	}
	
	if((selectItem.id == "chosen_category_couple" ||
		selectItem.id == "chosen_category_groups") &&
	   ( 
	   	$("option#chosen_category_couple").length == 0 &&
		$("option#chosen_category_groups").length == 0
	   ))
	{
		$("option#category_girl, option#category_lesbian, option#category_mature, option#category_fetish, option#category_transgender, option#category_boy, option#category_gay").css({color:"#FFFFFF"}).attr({disabled:""});	
	}
	
	if(selectItem.id == "chosen_category_transgender")
	{
		$("option#category_girl, option#category_lesbian, option#category_mature, option#category_fetish, option#category_boy, option#category_gay, option#category_couple, option#category_groups").css({color:"#FFFFFF"}).attr({disabled:""});	
	}
	
	switchRapidhiWillingness(selectItem);
}

function addConditionToSearchList(selectElement)
{
	element = (typeof selectElement == "string") ? document.getElementById(selectElement) : this;
	
	selectedListItem  = getSelectedItem(element);
	selectedListGroup = getSelectedGroup(element);

	if(typeof selectedListItem != "undefined" && selectedListItem.style.color != "#888888")
	{	
		$("option#"+selectedListItem.id).css({color:"#888888"}).attr({disabled:"disabled"});
		selectedListItem.selected = false;
		
		appendSearchItem(selectedListGroup, selectedListItem);
		
		if(selectedListItem.value == "category|girl" ||
		   selectedListItem.value == "category|mature" ||
		   selectedListItem.value == "category|fetish" ||
		   selectedListItem.value == "category|lesbian")
		{
			 inactivateGroup("available_penissize");
			 $("option#category_boy, option#category_gay, option#category_transgender, option#category_couple, option#category_groups").css({color:"#888888"}).attr({disabled:"disabled"});	
		}
		
		if(selectedListItem.value == "category|boy" ||
		   selectedListItem.value == "category|gay")
		{
			 inactivateGroup("available_breastsize");
			 $("option#category_girl, option#category_lesbian, option#category_mature, option#category_fetish, option#category_transgender, option#category_couple, option#category_groups").css({color:"#888888"}).attr({disabled:"disabled"});	
		}
		
		if(selectedListItem.value == "category|couple" ||
		   selectedListItem.value == "category|groups")
		{
			 $("option#category_girl, option#category_lesbian, option#category_mature, option#category_fetish, option#category_transgender, option#category_boy, option#category_gay").css({color:"#888888"}).attr({disabled:"disabled"});	
		}
		
		if(selectedListItem.value == "category|transgender")
		{
			 $("option#category_girl, option#category_lesbian, option#category_mature, option#category_fetish, option#category_couple, option#category_groups, option#category_boy, option#category_gay").css({color:"#888888"}).attr({disabled:"disabled"});	
		}
		
		if(selectedListGroup.id == "available_penissize" && 
		   $("select#searchInSelect option:selected").get(0).value == "videos")
		{
			$("option#category_mature, option#category_girl, option#category_lesbian, option#category_fetish").css({color:"#888888"}).attr({disabled:"disabled"});	
		}
		
		if(selectedListGroup.id == "available_breastsize" && 
		   $("select#searchInSelect option:selected").get(0).value == "videos")
		{
			 $("option#category_boy, option#category_gay").css({color:"#888888"}).attr({disabled:"disabled"});	
		}
		
		switchSaveButtonState();
		switchRapidhiWillingness(selectedListItem);
	}
	
	return false;
}

function switchSaveButtonState()
{
	if (chosenConditionsIsEmpty())
	{
		$("[@id=saveSearchParam1]").attr({disabled:"disabled"});
		hideSaveSearchPage(true);
	}
	else
	{
		$("[@id=saveSearchParam1]").removeAttr("disabled");
	}
}

function selectedGroupHasMoreElements(selectedGroup)
{
	var list = selectedGroup.childNodes;
	var wsCounter = 0;
	
	for(i=0; i < list.length; i++)
	{
		if (list[i].nodeType == "3")
		{
			wsCounter++;
		}
	}
	
	return (list.length == 0 || (list.length == wsCounter)) ? true : false;
}

function chosenConditionsIsEmpty()
{
	selectList = document.getElementById("selectedtag").childNodes;
	
	if (typeof selectList[0] != "undefined")
	{
		return (selectList.length == 1 && selectList[0].nodeType == "3") ? true : false;
	}
	else
	{
		return true;
	}
	
}

function inactivateGroup(optGroupId)
{	
	$("[@id='"+optGroupId+"'] > *").attr({disabled: "disabled"});
	$("[@id='"+optGroupId+"'] > *").css({color: "#888888"});
}

function activateGroup(optGroupId)
{	
	$("[@id='"+optGroupId+"'] > *").attr({disabled: ""});
	$("[@id='"+optGroupId+"'] > *").css({color: "#FFFFFF"});
}

function switchQuickSaveButtonState()
{
	if ($("input#saveSearchDivInput").attr("value").length == 0 || 
		$("input#saveSearchDivInput").attr("value").length > 16)
	{
		$("input#saveSearchDivButton").attr({disabled:"disabled"});
	}
	else
	{
		$("input#saveSearchDivButton").removeAttr("disabled");
	}
}

function removeConditionFromSearchList(selectElement)
{
	element = (typeof selectElement == "string") ? document.getElementById(selectElement) : this;
	
	selectedListItem  = getSelectedItem(element);
	selectedListGroup = getSelectedGroup(element);
	
	if(typeof selectedListItem != "undefined")
	{
		removeSearchItem(selectedListGroup, selectedListItem);
		switchSaveButtonState();
	}
	
	return false;
}

function rolloverEffect()
{
	if ( typeof $("option#"+this.id).attr("disabled") == "undefined")
	{
		$("option#"+this.id).css({color:"#FFCC00"});
	}
	
	return false;
}

function rolloverOut()
{
	if ( typeof $("option#"+this.id).attr("disabled") == "undefined" )
	{
		$("option#"+this.id).css({color:"#FFFFFF"});
	}
	
	return false;
}

function hideToolTip()
{
	$("div#DHTMLgoodies_formTooltipDiv").parent(0).remove();
	
	return false;
}

function locate()
{
	hideToolTip();
}

//keresesi kriteriumok alaphelyzetbe allitasa a kivalasztott option-nek megfeleloen
function searchIn() 
{
	var chooseTagSubmit = $("[@id=chooseTagJS]");
	var advancedSearchForm = $("[@id=advancedSearch]");

	chooseTagSubmit.attr("value", "submit");
	advancedSearchForm.submit();
}

function hideHint(id)
{
	$("div#"+id).css({
		display: "none"
	});
	
	return false;
}

function showHint(id)
{
	$("div.hint-style").css({
		display: "none"
	});
	$("div#"+id).css({
		display: "block"
	});
	
	return false;
}

$(document).ready(function()
{
	$("select.searchLeftSelect").dblclick(addConditionToSearchList);
	$("select.searchRightSelect").dblclick(removeConditionFromSearchList);
	$("input#saveSearchParam1").click(goSaveSearchPage);
	
	$("[@id=button_add]").click(function(){ addConditionToSearchList("searchLeftSelect"); return false;});
	$("[@id=button_rem]").click(function(){ removeConditionFromSearchList("selectedtag"); return false;});
	$("[@id=button_search]").css({display:"none"});
	$("[@id=searchInSelect]").change(function() {this.form.submit();});
	$("[@id=doSearch]").click(buildConditionsVariable);
	$("[@id=signup]").click(buildConditionsVariable);
	
	$("option").mouseover(rolloverEffect);
	$("option").mouseout(rolloverOut);

	$("a.trash_js").click(function(){return false;}).css({display:"block"});
	$("a.trash_nojs").css({display:"none"});
	
	$("#savingOverrideSearchCancel").click(function(){
		$("#saveSearchDiv").html($("#quickSaveNoActionTemp").html());
		switchQuickSaveButtonState();
		}
	)
	
	$("div#saveSearchDivButton").attr({
		disabled: ""
	});
	//$("a.trash_js").click(clearDeleteToolTips);
	
	switchSaveButtonState();
	
	setClip();
});