// JavaScript Document

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}



function DefValuedTextInput(inputObj, defValue)
{
	this.focussed = false;
	this.defValue = defValue;
	this.focusColor = "#000000";
	this.blurColor = "#999999";
	var obj = this;
	inputObj.value = this.defValue;
	if(inputObj.style.color=="") {
		inputObj.style.color = this.blurColor;
	}
	inputObj.defaultValue = this.defValue;
	inputObj.onfocus = function() {
		if(!obj.focussed) {
			this.value = "";
			this.style.color = obj.focusColor;
			obj.focussed = true;
		}
	}
	inputObj.onblur = function() {
		if(trim(this.value)=="") {
			this.value = obj.defValue;
			this.style.color = obj.blurColor;
			obj.focussed = false;
		}
	}
}

function SpaceReplacer(defaultDiv)
{
	defaultDiv = defaultDiv || "";
	this.currentDiv = null;
	this.menu = new Array();
	
	if(defaultDiv!="" && document.getElementById(defaultDiv)) {
		this.currentDiv = $("#"+defaultDiv);
	}
	
	this.display = function(divId)
	{
		if(document.getElementById(divId)) {
			if(this.currentDiv) {
				this.currentDiv.slideUp();
			}
			this.currentDiv = $("#"+divId);
			this.currentDiv.slideDown();
			return true;
		}
		return false;
	}
	
	this.SetMenu = function(itemId, displayDivId)
	{
		var thisObj = this;
		if(document.getElementById(itemId) && document.getElementById(displayDivId)) {
			$("#"+itemId).bind("click", function() { thisObj.display(displayDivId); });
			if($("#"+itemId).attr("nodeName")!="INPUT" && $("#"+itemId).attr("nodeName")!="BUTTON") {
				$("#"+itemId).css("cursor", "pointer");
			}
			return true;
		}
		return false;
	}
}

var MyDisplayer =
{
	SetSwitch : function(switchId, spaceId, callback)
	{
		callback = callback || null;
		if(document.getElementById(switchId) && document.getElementById(spaceId)) {
			if($("#"+switchId).attr("nodeName")!="INPUT" && $("#"+switchId).attr("nodeName")!="BUTTON") {
				$("#"+switchId).css("cursor", "pointer");
			}
			$("#"+switchId).bind("click", function(){ $("#"+spaceId).slideToggle("normal", callback) });
		}
	}
}



function SetCustomButtonBorder(className)
{
	className = className || "";
	var inputs = className=="" ? "input[type=\"button\"],input[type=\"submit\"]" : "input."+className;
	var buttons = className=="" ? "button" : "button."+className;
	var inset = function() { this.style.borderStyle="inset"; }
	var outset = function() { this.style.borderStyle="outset"; this.blur(); }
	
	var myItems = $(inputs).add(buttons);
	myItems.bind("mousedown", inset);
	myItems.bind("mouseup", outset);
	myItems.bind("mouseout", outset);
	myItems.bind("mousemove", outset);
	
	myItems.each(function(){
		//$(this).css("backgroundColor", "#FF0000");
	});
}


function searchProduct() {
	with(document.topSearch) {
		q.value = trim(q.value);
		if(q.defaultValue!=q.value && q.value.length > 0) {
			submit();
		}
		else {
			window.alert("Lütfen aranacak sözcükleri giriniz.");
			q.focus();
		}
	}
}

function advancedSearch(formObj) {
	formObj.q.value = trim(formObj.q.value);
	/*if(formObj.q.value.length == 0) {
		window.alert("Lütfen aranacak sözcükleri giriniz.");
		formObj.q.focus();
		return;
	}*/
	var catId = 0;
	var exQS = '';
	if(formObj.cat.value < 0) {
		catId = 0-formObj.cat.value;
	}
	else {
		catId = formObj.cat.value;
		exQS = '&sc=1';
	}
	
	window.location.href = "search.php?q="+formObj.q.value+"&cat="+catId+"&mark="+formObj.mark.value+exQS;
}

function listProducts(catId, markId, other)
{
	other = other || 0;
	var exQS = other==1 ? '&sc=1' : '';
	if(catId >= 0) {
		window.location.href = "products.php?cat="+catId+"&mark="+markId+exQS;
	}
	else if(catId < 0) {
		window.location.href = "products.php?cat="+(0-catId)+"&mark="+markId;
	}
}

function showProductDetails(listId)
{
	var rowId = "itemDetail"+listId;
	var bRowId = "itemDetail"+listId+"_blank"
	var imgPlusId = "detailButtonPlus"+listId;
	var imgMinusId = "detailButtonMinus"+listId;
	if(document.getElementById(rowId)) {
		if($("#"+rowId).is(":hidden")) {
			$("#"+rowId).slideDown();
			document.getElementById(imgPlusId).style.display = "none";
			document.getElementById(imgMinusId).style.display = "inline";
		}
		else {
			$("#"+rowId).slideUp();
			document.getElementById(imgMinusId).style.display = "none";
			document.getElementById(imgPlusId).style.display = "inline";
		}
		/*
		var itemRow = document.getElementById(rowId);
		if(itemRow.style.display!="") {
			itemRow.style.display="";
			document.getElementById(imgPlusId).style.display = "none";
			document.getElementById(imgMinusId).style.display = "inline";
		}
		else {
			itemRow.style.display="none";
			document.getElementById(imgMinusId).style.display = "none";
			document.getElementById(imgPlusId).style.display = "inline";
		}
		*/
	}
	
	
}







function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


