// JavaScript Document
var is_IE = 0;
var is_firefox = 0;
var loaded = 0;
nav  = navigator.userAgent

re = /MSIE/gi
if(nav.match(re)){
	is_IE = 1;
}else{
	re = /Firefox/gi
	if(nav.match(re)){
		is_firefox = 1;
	}
	
}



var speed = 10;
var inc = 15;
var inc_x = 40;

var menus = "";

var last_menu_obj = null;
var menu_in_obj = null;
var menu_out_obj = null;

var products = Array(); //category_1,category_2,product_name,images_path,menu_image,id

var menu_width=200;


function checkSearch(){
	
	closeCatalogueMenus();closeAllMenus();
	document.getElementById('shadow').style.display = "none";
		
	fm = document.search_form;
	
	if((fm.search_txt.value  == "")||(fm.search_txt.value  == "enter product code or keyword")||(fm.search_txt.value  == "searching...")){
	
		fm.search_txt.value = "enter product code or keyword";
		fm.search_txt.focus();
		fm.search_txt.select();
	
	}else{
		
		searchProduct(fm.search_txt.value);
		
	}
	
	
}


function closeSearch(clear){
	
	fm = document.search_form;
	fm.search_txt.value  = (clear)?"":last_search;
	fm.search_txt.disabled = false;
	fm.search_btn.value  = "find"
	fm.search_btn.disabled = false;
	document.getElementById('search_div').style.display = "none";
	
}

var last_search = "";
function searchProduct(keyword){
	
	last_search = keyword;
	
	fm = document.search_form;
	fm.search_txt.value  = "searching..."
	fm.search_txt.disabled = true;
	fm.search_btn.value  = "..."
	fm.search_btn.disabled = true;
	
	document.getElementById('search_status_txt').innerHTML="searching products please wait...";
	document.getElementById('search_status_txt').style.display = "block";
	document.getElementById('search_results_frame').style.display = "none";
	document.getElementById('search_div').style.display = "block";
	
	document.getElementById('search_results_frame').src = "scripts/search_products.php?"+encode64(keyword);
	
}


function showSearchResults(){
	
	fm = document.search_form;
	fm.search_txt.value  = last_search;
	fm.search_txt.disabled = false;
	fm.search_btn.value  = "find"
	fm.search_btn.disabled = false;
	document.getElementById('search_status_txt').style.display = "none";
	document.getElementById('search_results_frame').style.display = "block";
	
}


function checkScroll(){
	
		h = Math.abs(document.body.clientHeight);
	
		h1 = Math.abs(document.getElementById('main_div').clientHeight);
	

	if(h1 > h){
		//alert("scroll");
		document.body.scroll='yes';
		document.body.style.overflowY="scroll";
		
	}else{
		//alert("noscroll");
		document.body.scroll='no';
		document.body.style.overflowY="hidden";
		
	}
	
	
}


var submenu_left = menu_width;
var submenu_width = menu_width;

var menu_image_height = 200;
var submenu_height = menu_image_height;
var submenu_top = 0;

var item_menu_left = menu_width * 2;
var item_menu_width = 400;
var item_menu_height = menu_image_height;
var disable_item_out = 0;
var disable_sub_out = 0;


var last_goto = 0;

function gotoItem(id){
	
		closeSearch();
		closeCatalogueMenus();
		hideShadow();
		showLoading();
		document.getElementById('main_frame').src = "scripts/catalogue.php?"+id;
	
	
	
}

function loadSubMenu(category, top){
	
	
	disable_item_out = 1;
	itemmenuOut();
	
	
	submenu_left = menu_width;
	submenu_width = menu_width;
	
	
	document.getElementById('sub_menu').src = "scripts/menu.php?sub_menu|"+menu_width+"|"+ encode64(category);
	

	document.getElementById('sub_menu').style.left = (submenu_left + 1) + "px";
	document.getElementById('sub_menu').style.top =document.getElementById('item_menu').style.top = top +"px";
	
}

var submenu_in = null;
var submenu_out = null;


function checksubmenuIn(){
	
	if(document.getElementById('menu_1').style.display=="block"){
		
		submenuIn();
		
	}
	
}


function checkitemmenuIn(){
	
	if(document.getElementById('sub_menu').style.display=="block"){
		itemmenuIn();
		
	}
	
}

var itemmenu_in = null;
var itemmenu_out = null;

function itemmenuIn(){

	disable_item_out = 0;
	if(itemmenu_out){
		clearTimeout(itemmenu_out);
	}
	
	
	height = item_menu_height;
	width = Math.abs(item_menu_width);
	
	
	
	this_menu = document.getElementById('item_menu');
	
	if(this_menu){
		this_menu.style.height = item_menu_height;
		
		current_width = parseInt(this_menu.style.width);
	
		if(current_width < width){
			
			this_menu .style.display = "block";
			
			document.getElementById('debug').innerHTML = "w: "+current_width+"<br>";
			
			w = ((current_width + inc_x) > width) ? width : current_width + inc_x;
			
			
			this_menu .style.width = w + "px";
			itemmenu_in = setTimeout(itemmenuIn,speed);

			
		}
	   
	}
	
	
}



function itemmenuOut(){

	if(itemmenu_in){
		clearTimeout(itemmenu_in);
	}
	
	
	height = item_menu_height;
	width = item_menu_width;
	
	this_menu = document.getElementById('item_menu');
	
	if(this_menu){
		this_menu.style.height =item_menu_height;
		current_width = parseInt(this_menu.style.width);
	
		if(current_width > 0){
			
			
			
			document.getElementById('debug').innerHTML = "w: "+current_width+"<br>";
			
			w = ((current_width - inc_x) <= 0) ? 0 : current_width - inc_x;
			
			
			this_menu .style.width = w + "px";
			itemmenu_out = setTimeout(itemmenuOut,speed);

			
		}else{
			
			
			if(disable_item_out == 0){
				if(closeAll == 1){submenuOut()}
			}else{
				disable_item_out = 0;
			}
			this_menu.style.display = "none";
			
		}
	   
	}
	
	
}


function submenuIn(){


	disable_sub_out = 0;
	
	if(submenu_out){
		clearTimeout(submenu_out);
	}
	
	
	height = submenu_height;
	width = submenu_width + 2;
	
	this_menu = document.getElementById('sub_menu');
	
	if(this_menu){
		this_menu.style.height = submenu_height;
		
		current_width = parseInt(this_menu.style.width);
	
		if(current_width < width){
			
			this_menu .style.display = "block";
			
			document.getElementById('debug').innerHTML = "w: "+current_width+"<br>";
			
			w = ((current_width + inc_x) > width) ? width : current_width + inc_x;
			
			
			this_menu .style.width = w + "px";
			submenu_in = setTimeout(submenuIn,speed);

			
		}
	   
	}
	
	
}




function submenuOut(){

	if(submenu_in){
		clearTimeout(submenu_in);
	}
	
	
	height = submenu_height;
	width = submenu_width;
	
	this_menu = document.getElementById('sub_menu');
	
	if(parseInt(document.getElementById('item_menu').style.width) > 0){
					if(submenu_out){
						clearTimeout(submenu_out);
					}
					itemmenuOut();
					
	}else{
	
				if(this_menu){
					this_menu.style.height = submenu_height;
					current_width = parseInt(this_menu.style.width);
				
					if(current_width > 0){
						
						
						
						document.getElementById('debug').innerHTML = "SUB MENU w: "+current_width+"<br>";
						this_menu.style.display = "block";
						w = ((current_width - inc_x) <= 0) ? 0 : current_width - inc_x;
						
						
						this_menu.style.width = w + "px";
						submenu_out = setTimeout(submenuOut,speed);
			
						
					}else{
						if(disable_sub_out == 0){
							if(closeAll == 1){menuOut('menu_1');}
						}else{
							disable_sub_out = 0;
						}
						
						this_menu.style.display = "none";
						
					}
				   
				}
	
	}
	
	
}

function loadItemMenu(category_1,category_2){
	
	disable_item_out = 1;
	itemmenuOut();
	item_menu_left = (menu_width * 2);
	
	
	
	document.getElementById('item_menu').src = "scripts/menu.php?item_menu|"+menu_width+"|"+ encode64(category_1)+"|"+ encode64(category_2);
	

	document.getElementById('item_menu').style.left = (item_menu_left + 3) + "px";
	
	
}



function start(){
	createMenus();
	//hideLoading();
	
	
}

function hideShadow(){
	
	document.getElementById('shadow').style.display = 'none';
}

function createMenus(){
	document.getElementById('main_page').innerHTML ='<div><iframe name="main_frame" id="main_frame" style="width:1000px;height:520px;position:absolute;left:0px;top:0px;border-left:2px solid #cccccc;border-right:2px solid #cccccc;border-bottom:2px solid #cccccc;" allowtransparency="true" frameborder="0" src="scripts/pages.php"></iframe></div>';
	
	
	document.getElementById('main_page').innerHTML +='<div id="shadow" style="width:1000px;height:550px;position:absolute;top:0px;left:2px;text-align:center;display:none"><div style="width:1000px;height:520px;position:relative;top:0px;;text-align:center;background-color:#000000;color:#000000;font-size:16px;filter: Alpha(opacity=60);-moz-opacity:.60;opacity:.60;" onClick=\"javascript:closeCatalogueMenus();closeAllMenus();hideShadow();\">&nbsp;</div></div>';
	
	
	
	
	
	document.getElementById('main_page').innerHTML +='<iframe name="sub_menu" id="sub_menu" style="width:1px;height:100px;position:absolute;left:200px;top:0px;display:none;" allowtransparency="true" frameborder="0" scrolling="no" src=""></iframe>';
	document.getElementById('main_page').innerHTML +='<iframe name="item_menu" id="item_menu" style="width:1px;height:200px;position:absolute;left:400px;top:0px;display:none;" allowtransparency="true" frameborder="0" scrolling="no" src=""></iframe>';
	document.getElementById('main_page').innerHTML += menus;
	
	//document.getElementById('menus_page').innerHTML += menus;
	
}

function showLoading(){
	
	document.getElementById('loading').style.display = "block";
}

function hideLoading(){
	
	document.getElementById('loading').style.display = "none";
	//document.getElementById('main_frame').style.display = "block";
	
	
}

function loadPage(p){
	closeAllMenus();
	closeCatalogueMenus();
	hideShadow();
	showLoading();
	
	
	document.getElementById('main_frame').src = p;

	
}


	


var menu_max_height = 550;

var menu_height_array = Array();

function menuIn(m){
	document.getElementById('shadow').style.display = "block";
	if(m == "menu_1"){
		closeAll = 0;
	}
	
	if(window[m+'out']){
		clearTimeout(window[m+'out']);
	}
	
	//document.getElementById('debug').innerHTML = "m: "+m+"<br>";
	
	num = m.split("_")[1];
	num = Math.abs(num);
	height = menu_height_array[num];
	
	//document.getElementById('debug').innerHTML = "num: "+num+"<br>h: "+height;
	
	if(document.getElementById(m)){
		current_height = parseInt(document.getElementById(m).style.height);
	
		if(current_height < height){
			
			document.getElementById(m).style.display = "block";
			
			
			
			h = ((current_height + inc) > height) ? height : current_height + inc;
			
			if(h > menu_max_height){
				document.getElementById(m).style.height = menu_max_height + "px";
				
			}else{
				document.getElementById(m).style.height = h + "px";
				window[m+'in'] = setTimeout("menuIn('"+m+"')",speed);
			}

			
			
			
		}else{
			if(m == "menu_1"){
				closeAll = 0;
			}
			clearTimeout(window[m+'in']);
			process_menu = null;
			process_height = 0;
		
		}
	   	   
	   
	}else{
		
		//document.getElementById('debug').innerHTML += "<br>not found";
		
	}
	
	
}
 var closeAll = 0;
 
 
function closeCatalogueMenus(){
	
	closeAll = 1;
	itemmenuOut();
	
}
 
function closeAllMenus(menu){
	
	if(last_menu_obj != null){
		
		try{
			checkOut(last_menu_obj);
		}catch(e){
		}
		
	}
	
	//itemmenuOut();
	
	for(i=1;i<total_menus;i++){
		
		this_m = "menu_"+(i+1);
		
		
		if(document.getElementById(this_m)){
			
			//if(this_m != menu){
				
				
				menuOut(this_m);
			
			//}
			
			
			
		}		
		
	}
	

	
}



function menuOut(m){
	
	if(window[m+'in']){
		clearTimeout(window[m+'in']);
	}
	
	height=1;
	
	//document.getElementById('debug').innerHTML = "OUT m: "+m;
	
	if((m=="menu_1")&&((parseInt(document.getElementById('sub_menu').style.width) > 0) ||(parseInt(document.getElementById('item_menu').style.width) > 0))) {
					if(window[m+'out']){
						//clearTimeout(window[m+'out']);
					}
					itemmenuOut();
					
	}else{
	
			if(document.getElementById(m)){
				current_height = parseInt(document.getElementById(m).style.height);
			
				if(current_height > height){
					
					
					
					h = ((current_height - inc) < height) ? height : current_height - inc;
					
					
						document.getElementById(m).style.height = h + "px";
						window[m+'out'] = setTimeout("menuOut('"+m+"')",speed);
						
						
					
				}else{
					
					
					document.getElementById(m).style.display = "none";
					clearTimeout(window[m+'out']);
					process_menu = null;
					process_height = 0;
				
				}
				   
			   
			}else{
				
				//document.getElementById('debug').innerHTML += "<br>not found";
				
			}
	
	}
	
	
}



function checkOver(btn){
	
	
		
		btn.className = btn.className +"_over";
		if(parent.is_IE =='1'){
			btn.style.cursor='hand';
		}else{
			
			btn.style.cursor='pointer';
			
		}

	
}





function checkOut(btn){
	
	
	re = /_over/gi;
	
	if(btn.className.match(re)){
		
		newClass = btn.className.replace(re,"");
		
		btn.className = newClass;
	}
	if(parent.is_IE =='1'){
		btn.style.cursor='default';
	}
	
	
}





var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}


function loadSWF(f,fs,width,height,path,scale,salign){
	//alert(f);
	//alert(fs);
	
	if((!path)||(path == "")){
		path = global_url+'flash/';
	}
	if(!width){
		width = "100%";
	}
	if(!height){
		height = "100%";
	}
	if(!scale){
		scale = "noscale";
	}
	if(!salign){ //lt rt t lb b rb
		salign = "";
	}
	f_file = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+width+'" height="'+height+'" id="'+f+'" align="middle"><param name="salign" value="'+salign+'" /><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="'+path+fs+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="high" /><param name="scale" value="'+scale+'" /><param name="wmode" value="transparent" /><embed src="'+path+fs+'" loop="false" menu="false" quality="high"  scale="'+scale+'" wmode="transparent"  width="'+width+'" height="'+height+'" salign="'+salign+'" name="'+f+'"  align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	return f_file;
}

function drawFlash(f,fs,w,h,path,scale){
	//alert("draw flash");
	//alert(fs);
	document.write(loadSWF(f,fs,w,h,path,scale));
}
