var update_history;
var topmenu_timeover;
var topmenu_timeout;
var top_timeover;
var top_timeout;
var top_currentout = 0;
var top_currentover = 0;
var topmenu_currentout;
var topmenu_currentover;
var hover = true;
var searchnum=0;

function autoslide(){
	if(fade==1){
		if(document.getElementById('sub_twitter'+fade_name)){
			if (fade_cycle<6){//fade out
				document.getElementById('sub_twitter'+fade_name).style.height="40px";
				opacityAsInt =100 - (20*fade_cycle);
				opacityAsDecimal = opacityAsInt / 100;
				document.getElementById('sub_twitter'+fade_name).style.filter  = "alpha(opacity=" + opacityAsInt + ")";
				document.getElementById('sub_twitter'+fade_name).style.opacity = (opacityAsDecimal);
				fade_cycle=fade_cycle+1;
			}else if(fade_cycle<11){ //fade in
				opacityAsInt = 20*(fade_cycle-5);
				opacityAsDecimal = opacityAsInt / 100;
				document.getElementById('sub_twitter'+fade_name).style.height="0px";
				if (fade_name==5){
					fadename=1;
				}else{
					fadename = fade_name+1;
				}
				
				document.getElementById('sub_twitter'+fadename).style.filter  = "alpha(opacity=" + opacityAsInt + ")";
				document.getElementById('sub_twitter'+fadename).style.opacity = (opacityAsDecimal);
				document.getElementById('sub_twitter'+fadename).style.height="40px";
				fade_cycle=fade_cycle+1;
			}else{ //settling everything
				fade=0;
				fade_cycle=1;
				if (fade_name==5){
					fade_name=1;
				}else{
					fade_name=fade_name+1;
				} 
			}
		}
	}		
}

function fadeNews(){
	fade = 1;
}

function update_search(showing){		
	categshow = showing;
	var searchstring=document.getElementById('search_input_id').value;
	mainsearch_exec(searchstring);
}

function update_prodsearch(showing){		
	prodshow = showing;
	var searchstring=document.getElementById('search_input_id').value;
	mainsearch_exec(searchstring);
}

function search_updatehistory(searchstring){
	var inChar = " ";
	var outChar = "|";
  	var newString = searchstring.split(inChar);
  	newString = newString.join(outChar);
  	searchstring = newString;
	
	if(searchstring.length !=0 ){
		dhtmlHistory.add("catid=search:searchstring="+searchstring, null);
	}else{	
		var r = window.location.href;
		var i = r.indexOf("?");
		if (i>=0){
			window.location.href="/index.php";
		}
		dhtmlHistory.add("home", "Home Page");
	}
}

function mainsearch_exec(searchstring, option){
	var keycount = searchstring.length;
	if (keycount !=0 ){
		clearTimeout(update_history);
		pgnumber=1;
		sndReq(searchstring, option);
	}else{
		clearTimeout(timeout);
		curSel="0";
		catSel="0";
		catid = 0;
		if(current_file == "/index.php"){
			var clear = new Ajax.Updater('ProductListing',current_file,
					{method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid='+catid+'&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&show='+showy,
						onComplete:function(){
							search_updatehistory(searchstring);
							get_twit();
						}
					} );
		}
	}	
}

function sndReq(searchstring, option){
	if(current_file == "/index.php"){
		pageTracker._trackPageview('search/searchstring/'+searchstring);
		document.getElementById('WaitMessage').style.visibility="visible";
		curSel="0";
		catid='home';
		searchnum++;
		var ajax_num = 'ajax'+searchnum;
		window[ajax_num] = new Ajax.Updater('ProductListing',current_file,
						{method: 'get', parameters:'sort=search&imgsize='+imgsize+'&catid=search&option='+option+'&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&show='+showy+'&searchstring='+searchstring+'&categshow='+categshow+'&prodshow='+prodshow,
						onComplete: function(){
								if(document.getElementById('mainsearch').value==document.getElementById('current_search').innerHTML){
									search_updatehistory(searchstring);
								}
								document.getElementById('mainsearch_optionbox').innerHTML = document.getElementById('mainsearch_optionbox_copy').innerHTML;
								document.getElementById('mainsearch_optionbox').style.height="auto";
								allow_search=1;
								document.getElementById('WaitMessage').style.visibility="hidden";
								//resize_topban();
						}//close oncomplete
		} );	//close productlisting
		
		if(!document.getElementById('left_accordion')){
			var myAjax = new Ajax.Updater('CategoryList',current_file,
							{method: 'get', parameters:'action=draw_leftcatmenu&supercatid='+curSel, 
								onComplete:function(){
									var leftAccordion = new accordion('#left_accordion', {
										classNames : {
											toggle : 'left_menu_sub',
											toggleActive : 'left_menu_sub_selected',
											content : 'left_menu_sub_content'
											},
										defaultSize : {
											width : 175,
											height: 220
											},
										direction : 'vertical',
										onEvent   : 'mouseover'
									});
								} //close oncomplete
							} );//close left menu
		}
	}else{
		window.location.href='/index.php#catid=search:searchstring='+searchstring;
	}
}

function searchCatClick(cat_id, name){
	pgnumber=1;
	document.getElementById('WaitMessage').style.visibility="visible";
	catid=cat_id;
	name_adj=name.replace(/&/g,'-AND-',name);
	var myAjax = new Ajax.Updater('ProductListing','index.php', 
				{method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid='+catid+'&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&catname='+name_adj+'&show='+showy, 
				onComplete:function(){
										//document.getElementById('SuperCatInfo').innerHTML = "This is the selection of products in the Category chosen";
										document.getElementById('CatSelected').style.marginTop="0";
										catSel=cat_id;
										//document.getElementById('Cat_Cat').style.visibility="visible";
										//document.getElementById('Cat_Cat').innerHTML = name;
										document.getElementById('WaitMessage').style.visibility="hidden";
										}
			});	
}

function top_menu_overout(type, id){
    //this controls the hover of the button and main menu box
    if(type=='over'){
        if(top_currentout==0){ //if there is no current menu open, open immediately
            top_menu_over(id);
        }else{
            clearTimeout(top_timeover);
            top_timeover = setTimeout('top_menu_over('+id+')', 450);
            top_currentover = id;
            if(top_currentover == top_currentout){
                clearTimeout(top_timeout);
            }
        }
        
        //edges changing color immed.
        if(hover==true){
            if(document.getElementById('top_menu_edges_lefttop')){
                if(id==1){
                    document.getElementById('top_menu_edges_lefttop').style.backgroundImage = "url(/Assets/Webimages/top_menu_edges_over.jpg)";
                    document.getElementById('top_menu_edges_leftbottom').style.backgroundImage = "url(/Assets/Webimages/top_menu_edges_over.jpg)";
                }else if(id==5){
                    document.getElementById('top_menu_edges_righttop').style.backgroundImage = "url(/Assets/Webimages/top_menu_edges_over.jpg)";
                    document.getElementById('top_menu_edges_rightbottom').style.backgroundImage = "url(/Assets/Webimages/top_menu_edges_over.jpg)";
                }
            }
        }
    }else{
        if(top_currentout!=0 && top_currentover != top_currentout ){ //another menu is open - close it now
            top_menu_out(top_currentout);
        }
        clearTimeout(top_timeout);
        top_timeout = setTimeout('top_menu_out('+id+')', 450);
        top_currentout = id;
        
        //edges changing color immed.
        if(document.getElementById('top_menu_edges_lefttop')){
            if(id==1){
                document.getElementById('top_menu_edges_lefttop').style.backgroundImage = "url(/Assets/Webimages/top_menu_edges.jpg)";
                document.getElementById('top_menu_edges_leftbottom').style.backgroundImage = "url(/Assets/Webimages/top_menu_edges.jpg)";
            }else if(id==5){
                document.getElementById('top_menu_edges_righttop').style.backgroundImage = "url(/Assets/Webimages/top_menu_edges.jpg)";
                document.getElementById('top_menu_edges_rightbottom').style.backgroundImage = "url(/Assets/Webimages/top_menu_edges.jpg)";
            }
        }
    }
}

function top_menu_over(id){
    //top_currentover = id;
    if(document.getElementById('top_menu_'+id+'_main')){
            document.getElementById('top_menu_'+id+'_main').style.visibility = "visible";
    }
}

function top_menu_out(id){
    if(document.getElementById('top_menu_'+id+'_main')){
            document.getElementById('top_menu_'+id+'_main').style.visibility = "hidden";
            top_currentout=0;
    }
}

function top_menu_main_overout(type, mainid, id){
	if(type=='over'){
		clearTimeout(topmenu_timeover);
		topmenu_timeover = setTimeout('top_menu_main_over('+mainid+', '+id+')', 150);
		topmenu_currentover = id;
		if(topmenu_currentover == topmenu_currentout){
			clearTimeout(topmenu_timeout);
		}
	}else{
		clearTimeout(topmenu_timeout);
		topmenu_timeout = setTimeout('top_menu_main_out('+mainid+')', 150);
		topmenu_currentout = id;
	}
}

function top_menu_main_over(mainid, id){
        
	document.getElementById('top_menu_'+mainid+'_main_'+id).className = 'top_menu_mainmenu_box_over';
	if(document.getElementById('top_menu_'+mainid+'_submain_'+id)){
		document.getElementById('top_menu_'+mainid+'_submain_'+id).style.visibility = 'visible';
	}else{
		document.getElementById('top_menu_'+mainid+'_main_'+id).style.width="195px";
		document.getElementById('top_menu_'+mainid+'_main_'+id).style.borderRight="1px solid #00b6ca";
	}
}

function top_menu_main_out(mainid){
	var ar_class = document.getElementsByClassName('top_menu_mainmenu_box_over');
	if(ar_class.length>0){
		for(i=0; i<ar_class.length; i++){
			ar_class[i].className = 'top_menu_mainmenu_box';
			var new_id = '';
			for(x=ar_class[i].id.length; x > 0; x--){	
				if(ar_class[i].id.substring(x-1, x) == "_"){
					new_id = ar_class[i].id.substring(x, ar_class[i].id.length);
					var menu = ar_class[i].id.substring(9, x-6);
					if(document.getElementById('top_menu_'+menu+'_submain_'+new_id)){
						document.getElementById('top_menu_'+menu+'_submain_'+new_id).style.visibility="hidden";
					}
				}
			}
		}
	}
}

function top_menu_sub_overout(type,mainid, id){
	if(type=='over'){
		if(document.getElementById('top_menu_'+mainid+'_group_'+id)){
			document.getElementById('top_menu_'+mainid+'_group_'+id).style.visibility='visible';
		}
		document.getElementById('top_menu_2_sub_'+id).className = 'top_menu_submenu_box_over';
	}else{
		if(document.getElementById('top_menu_'+mainid+'_group_'+id)){
			document.getElementById('top_menu_'+mainid+'_group_'+id).style.visibility='hidden';
		}
		document.getElementById('top_menu_2_sub_'+id).className = 'top_menu_submenu_box';
	}
}
