// 
(function($) {
	var MenuSwitch = window.MenuSwitch = function() {
		var LangType = {
			ENG: '0',	// English 
			ES: '1'		// Spanish
		};
		
		var PageType = {
			HOME: '0',	// Home Page 
			LIST: '1'	// List Page
		};
		
		var TabType = {
			BS: "bs",	// Business 
			PS: "ps",	// People
			MS: "ms",	// Map
			RL:	"rl",	// Reverse Lookup
			CS: "cs"	// Coupon
		};
		
		// global variables IN MapLIB
		var vars = {
		};

		// default options which is not changed by parameter passed by users
		var defs = defaults = {
			page: PageType.HOME, //0 - home page, 1 - listing or detail page
			lang: LangType.ENG,     
			curTab: TabType.BS,
			prevTab: TabType.BS,
			tabs: [],
			ie6msg: ""
		};

		var init = function(opts) {
			defs = $.extend(defaults, opts);	
			
			if (defs.lang == null || defs.lang == '') defs.lang = LangType.ENG;

			if (defs.curTab!='bs' && defs.curTab!='ps' && defs.curTab!='ms' &&
				defs.curTab!='rl' && defs.curTab!='cs') {
				defs.curTab = "bs";
			}
			
			if (defs.prevTab!='bs' && defs.prevTab!='ps' && defs.prevTab!='ms' &&
				defs.prevTab!='rl' && defs.prevTab!='cs') {
				defs.prevTab = "bs";
			}
			
			if (defs.curTab=="bs") {
				setTab(defs.curTab);  
				if (defs.page==PageType.LIST) setHistBox(defs.curTab);
			}
			else switchTab(defs.curTab);
						
			if (defs.page==PageType.HOME) {
				$(".hist_filt").mouseover(function(){      
		    		$(".hist_filt").css("height","37px");
		    		$(".hist_filt").css("background","none repeat scroll 100% 0 #E6E6E6");
		    		$('.snippet').attr("src",defs.snippetUpImg);
		    		$('.hist_box').css("overflow","visible");
		    		$('.hist_box').css("visibility","visible");
		    		$('.hist_box').css("display","block"); 
				});
						
		    	$(".hist_filt").mouseout(function(){
		    		$('.snippet').attr("src",defs.snippetDownImg);
			    	$(".hist_filt").css("height","");
			    	$(".hist_filt").css("background","");
			    	$('.hist_box').css("overflow","hidden");
			    	$('.hist_box').css("visibility","hidden");
			    	$('.hist_box').css("display","none");
				}); 	
		    }
			else {
				if ($.browser.msie && $.browser.version == '6.0') {
					$(window).bind("load", showIE6Message);
					$(window).bind("resize", showIE6Message);
				}
			
				if ($.browser.msie) {
					$(".hist_box_group").bind("mouseenter", function(e){ 
						if ((defs.curTab=="bs" && e.target.id == 'hist_filt_bs') ||
							(defs.curTab=="ps" && e.target.id == 'hist_filt_ps') ||
							(defs.curTab=="rl" && e.target.id == 'hist_filt_rl') || 
							(defs.curTab=="ms" && e.target.id == 'hist_filt_ms') ||						
							(defs.curTab=="cs" && e.target.id == 'hist_filt_cs')) { 
							$('.snippet').attr("src",defs.snippetUpImg);
			    			$(".hist_filt").css("height","37px");
			    			$(".hist_filt").css("background","none repeat scroll 100% 0 #E6E6E6");
			    			$('.hist_box').css("display","block"); 
			    		}
					});
						
			    	$(".hist_box_group").bind("mouseleave", function(e){
						if ((defs.curTab=="bs" && e.relatedTarget.id != 'hist_box_bs' && e.relatedTarget.id != 'hist_filt_bs') ||
							(defs.curTab=="ps" && e.relatedTarget.id != 'hist_box_ps' && e.relatedTarget.id != 'hist_filt_ps') ||
							(defs.curTab=="ms" && e.relatedTarget.id != 'hist_box_ms' && e.relatedTarget.id != 'hist_filt_ms') || 
							(defs.curTab=="rl" && e.relatedTarget.id != 'hist_box_rl' && e.relatedTarget.id != 'hist_filt_rl') ||
							(defs.curTab=="cs" && e.relatedTarget.id != 'hist_box_cs' && e.relatedTarget.id != 'hist_filt_cs')) {
			    			$('.snippet').attr("src",defs.snippetDownImg);
				    		$(".hist_filt").css("height","");
				    		$(".hist_filt").css("background","");
				    		$('.hist_box').css("display","none");
				    	}
					}); 
		    	}
				else {  //Fire fox, safari, chrome doesn't work in the way IE does
					$(".hist_box_group").bind("mouseenter", function(e){ 
						$('.snippet').attr("src",defs.snippetUpImg);
		    			$(".hist_filt").css("height","37px");
		    			$(".hist_filt").css("background","none repeat scroll 100% 0 #E6E6E6");
		    			$('.hist_box').css("display","block"); 
					});
						
		    		$(".hist_box_group").bind("mouseleave", function(e){
		    			$('.snippet').attr("src",defs.snippetDownImg);
			    		$(".hist_filt").css("height","");
			    		$(".hist_filt").css("background","");
			    		$('.hist_box').css("display","none");
					}); 
				}
			}
		};
		
		var setTab = function (curTab) {
			for (var i=0; i<defs.tabs.length;i++){
				$("#"+defs.tabs[i]+"Box").css("display", "none");
				$("."+defs.tabs[i]+"Li").attr("id", "");
			}

			if (curTab=="") curTab = TabType.BS;
			$("#"+curTab+"Box").css("display", "block");
			$("."+curTab+"Li").attr("id", "current");
		}
		
		var setHashToURL = function(curTab) {
			var hashSuffix = '#'+curTab;
			var hash = location.hash;			
			var hashVal = hash.substring(hash.indexOf('#') + 1);
			
			if(hashVal != "") {
				window.location.href = window.location.href.replace(/#[\S]+/,"") + hashSuffix;
	 		}
	 		else {
	 			window.location.href += hashSuffix;
	 		}	
		}
		
		var switchTab = function(curTab) {
			setTab(curTab);		
			defs.prevTab = curTab;
			defs.curTab = curTab;
			if (defs.page==PageType.HOME) {
				setHashToURL(curTab);  //Apply home page only
			}
			else {
				setHistBox(curTab);
			}						
		}
		
		var setHistBox = function(curTab) {
			var leftOffSet = 0;
			if (curTab=="bs" || curTab=="") leftOffSet = 427;
			else if (curTab=="ps") leftOffSet = 415;			
			else if (curTab=="rl") leftOffSet = 147;
			else if (curTab=="ms") leftOffSet = 232;
			else leftOffSet = 177;
			
			var histLeft = $("#nav_tabs").offset().left + leftOffSet;
			var histTop = $("#nav_tabs").offset().top + 109;
			//$("<div id='hb_bs' style='display:none'>"+$("#hist_box_bs").html()+"</div>").appendTo("#search_box");
			
			$(".hist_box").css({ position: "absolute",
            	   		     	   	  top: histTop, 
            	   			     	 left: histLeft });
		}
		
		var deleteHistory = function(curTab, histBox, cookieName, histFilt) {
			$.ajax({
		   		type: "GET",
		   		url: "/searchhist/delete.jsp",
		   		data: "p_c="+cookieName+"&hp=3&t="+(new Date()).getTime(),
		   		dataType: "html",
		   		success: function(html) {
					$('#'+histFilt).css("display","none");
					$('#'+histBox).css("display","none");
		   		},
		   		error: function(xhr, textStatus, errorThrown) {
		   			alert("Error : "+errorThrown);
		   		}
		 	});
		}
		
		var showIE6Message = function() {	
			var top, left, w, h;
			
			top = 0;
			left = 0;
			w = 100;//$(document).width(); 
			h = 20; 

			$('#IE6_infobox').remove();
			$('<div id="IE6_infobox"><div style="width:870px; margin:0 auto;">'+defs.ie6msg+'</div></div>')
			 .css({position:'absolute', 
				   top:top, 
				   left:left, 
				   width:w+"%", 
				   height:h+"px",  
				   color:"#000000",
				   'font-weight':"bold",
				   'background-color':"#FFFFFF",
				   'opacity':0.5, 
				   'margin': '0 auto',				   
				   'padding': '3px 0 3px',
				   'z-index':9})
			 .appendTo('body');
		}; 

		return {
			init:init,
			switchTab:switchTab,
			deleteHistory:deleteHistory,
			showIE6Message:showIE6Message
		};
	}();	
})(jQuery);

