//Copy Rights @ RK JS factory

RK_JS = {
	getMouseXY : function(e){ //for onmouse image
		var IE = document.all?true:false;
		if (!IE) document.captureEvents(Event.MOUSEMOVE)
		var newDiv = document.getElementById("dynamicImage"), body = document.getElementsByTagName("body").item(0), mX=0, mY=0, diffW = 0, diffT = 0;
        if (IE){
			mX = event.clientX + document.documentElement.scrollLeft;
            mY = event.clientY + document.documentElement.scrollTop;
        }else{
            mX = e.pageX;
            mY = e.pageY;
        }
        if (mX < 0){ mX = 0; }
        if (mY < 0){ mY = 0; }
		if(newDiv){ 
			newDiv.style.left = mX+13+"px";
			newDiv.style.top = mY+20+"px";
			diffW = newDiv.offsetLeft+newDiv.offsetWidth-body.offsetWidth;
			diffT = (newDiv.offsetTop+newDiv.offsetHeight)-(document.documentElement.scrollTop+body.offsetHeight);
			if( diffW > 0 ){
				newDiv.style.left = mX-newDiv.offsetWidth-7+"px";
			}
			if(diffT > 0){
				newDiv.style.top = mY+20-diffT+"px";
			}
		}
        return true;
	},
	onMouseImage : function (t,si){ //onmouseover image display at all places
        var newDiv = document.createElement("div");
        var body  = document.getElementsByTagName("body").item(0);
        var nimg = t.src.replace("/"+si+"/",'/400/');
        newDiv.id = "dynamicImage";
        newDiv.style.position = "absolute";
		newDiv.style.zIndex = 1000;
        newDiv.innerHTML = "<img src=\""+nimg+"\" style=\"max-height:"+(body.offsetHeight-2)+"px\" />";
		if(!document.getElementById("dynamicImage")){ 
        	body.appendChild(newDiv);
			t.onmousemove = RK_JS.getMouseXY;
			t.onmouseout = function(){
			 if(document.getElementById("dynamicImage")){
					body.removeChild(document.getElementById("dynamicImage")); 
				}
			};
		}
	},
	showData:function(d){ //for menu in product Information page
		var mi, mid, di, did, i;
		for(i=1; i<=4; i++) {
			mi = "m"+i; di = "d"+i;
			mid = document.getElementById(mi);
			if(mid != null){
				did = document.getElementById(di); 
				if( i == d) {
					mid.className = "mact"; did.style.display = "block"; 
				} else {
					mid.className = "minact"; did.style.display = "none"; 
				} 
			}
		}
	},
	bigImage : function(imgStr){ //onclick image in productinforamtion page
		var newImg = new Image(), fadeDiv = document.createElement("div"), bodytr = document.getElementsByTagName("body").item(0);
		var  posX, posY, diff, ndiff, iWidth, iTop = 0, timeout = 0, imgDiv = document.createElement("div");
		newImg.src = "userfiles/gallery/"+imgStr;
		if(parseInt(newImg.width) == 0){
			timeout=500;
			fadeDiv.id = "fadeDiv"; 
			bodytr.appendChild(fadeDiv);
		}else{
			timeout=0;
		}
		setTimeout(function(){
			if(parseInt(newImg.width) > 800) { iWidth = 800; } else { iWidth = newImg.width; }
			iTop = (parseInt(bodytr.offsetHeight/2)-parseInt(newImg.height/2)-26) ;
			if(iTop > 0) { posY = iTop; } else { posY = 5; }
			fadeDiv.id = "fadeDiv"; 
			imgDiv.id = "imgDiv";
			if(navigator.appName != "Microsoft Internet Explorer" || parseInt(navigator.appVersion) >= 4){
			fadeDiv.style.position = "fixed"; }
			imgDiv.innerHTML="<div>Click on screen To close Image</div><br /><img src=userfiles/gallery/"+imgStr+" width="+iWidth+"/>"
			imgDiv.style.left = "0px"; 
			imgDiv.style.top = posY+"px";
			bodytr.appendChild(fadeDiv);
			bodytr.appendChild(imgDiv);
			diff = parseInt(bodytr.offsetWidth/2);
			ndiff = parseInt(imgDiv.offsetWidth/2);
			posX = diff-ndiff;
			imgDiv.style.left = posX+"px";
			fadeDiv.onclick = imgDiv.onclick = function(){ 
				while(document.getElementById("fadeDiv")){
					bodytr.removeChild(document.getElementById("fadeDiv")); 
					}
				bodytr.removeChild(document.getElementById("imgDiv")); 
			}; 
		},timeout);
	}, 
	// auto search starts...
	XMLObject:"",auto_id: -1,autoText:"*", 
	getHttpObject : function(){ //for AJAX in auto search
		if(window.XMLHttpRequest){
			return new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			return null;
		}
	},
	autoSearch : function(ev, box, k){ //search starts here
		var autoId=1;
		if(ev.keyCode == 13){
			if(document.getElementById("aup"+RK_JS.auto_id+"")){ 
				window.location=DOCUMENT_ROOT+"products.php?pid="+document.getElementById("aup"+RK_JS.auto_id+"").value;
			}
			else{
				window.location=DOCUMENT_ROOT+"advsearch.php?keys="+box.value;
			}
		}
		else if(ev.keyCode == 27){
			RK_JS.autoClose();
		}
		else if(ev.keyCode == 40){
			// Modified for IE double jumping on 04-02-2012			
			if(navigator.appVersion.indexOf("MSIE") != -1){ }
			else{
				if(k == 0){ exit(); }
			}
			if(RK_JS.auto_id >= 0 && document.getElementById("au"+(RK_JS.auto_id+1)+"")){
				RK_JS.auto_id++;
				while(document.getElementById("au"+autoId+"")){
					if(autoId == RK_JS.auto_id){
						document.getElementById("au"+autoId+"").style.background = "#CCCCCC";
						box.value = document.getElementById("auv"+autoId+"").value;
						box.focus();
					}
					else{
						document.getElementById("au"+autoId+"").style.background = "#FFFFFF";
					}
					autoId++;
				}
			}
		}else if (ev.keyCode == 38){
			// Modified for IE double jumping on 04-02-2012			
			if(navigator.appVersion.indexOf("MSIE") != -1){ }
			else{
				if(k == 0){ exit(); }
			}
			if(RK_JS.auto_id >=1 && document.getElementById("au"+(RK_JS.auto_id-1)+"")){
			RK_JS.auto_id--;
			while(document.getElementById("au"+autoId+"")){
					if(autoId == RK_JS.auto_id){
						document.getElementById("au"+autoId+"").style.background = "#CCCCCC";
						box.value = document.getElementById("auv"+autoId+"").value;
						box.focus();
					}
					else{
						document.getElementById("au"+autoId+"").style.background = "#FFFFFF";
					}
					autoId++;
				}
			}
		}
		else
		{
			if(box.value != "" ){
				if(RK_JS.autotext == "*" || RK_JS.autoText != box.value){
					RK_JS.autoOpen();
				}
			}else{	
				RK_JS.autoClose();
			}
		}
	},
	autoOpen : function(){ //main action for auto search
		var posX = 0, posY = 0, autoUrl, t1 = t = document.getElementById("auto_box");
		RK_JS.autoText = t.value;
		RK_JS.auto_id = 0;
		if(!document.getElementById("resultDiv")){
			var resultDiv = document.createElement("div"), conDiv = document.createElement("div"), bodyt = document.getElementsByTagName("body").item(0);
			resultDiv.id = "resultDiv";
			posY += t1.offsetHeight;
			do{
				posX += parseInt(t1.offsetLeft);
				posY += parseInt(t1.offsetTop);
				t1 = t1.offsetParent;
			} while(t1.offsetParent);
			resultDiv.style.width = (t.offsetWidth-7)+"px";
			resultDiv.style.left = posX+"px";
			resultDiv.style.top = posY+"px";
			bodyt.appendChild(resultDiv);
			bodyt.onclick = function() { RK_JS.autoClose(); };
		}
	RK_JS.XMLObject = RK_JS.getHttpObject();
	if(RK_JS.XMLObject != null){
		autoUrl = DOCUMENT_ROOT+"autoSearch.php?key="+t.value;
		RK_JS.XMLObject.open("GET",autoUrl,true);
		RK_JS.XMLObject.send(null);
		RK_JS.XMLObject.onreadystatechange = RK_JS.searchOutput;
		}
	},
	searchOutput : function(){ //displaying the search results
		if(RK_JS.XMLObject.readyState == 4){
			if(document.getElementById("resultDiv")){
				document.getElementById("resultDiv").innerHTML = RK_JS.XMLObject.responseText;
			}
		}	
	},
	autoClose : function(){ //close the search result
		RK_JS.auto_id = -1;
		RK_JS.autoText = "*";
		while(document.getElementById("resultDiv")){
			document.getElementsByTagName("body").item(0).removeChild(document.getElementById("resultDiv"));
		}
	},
	autoSearchClick: function(){
		window.location="advsearch.php?keys="+document.getElementById("auto_box").value;
	}
	// auto search 
};

