﻿var Search = function(objId,strText)
{
    this.obj = $(objId);
    this.IsInMenu = false;
    this.IsOpen = false;
    this.Text = strText ? strText : "";    
};
Search.prototype.onload = function()
{
    this.input = this.input ? this.input : getTags(this.obj,"input")[0];
    var ImgObjs = getTags(this.obj,"img");
    this.allObj = this.allObj ? this.allObj : ImgObjs[0];
    this.selObj = this.selObj ? this.selObj : ImgObjs[1];
    this.selMenu = this.selMenu ? this.selMenu : getTags(this.obj,"div")[0];    
    var me = this;
    this.input.value = this.Text;
    this.input.onblur = function(){ if(this.value.trim().length == 0){this.value = me.Text; this.className = "";}}
    this.input.onmousemove = function(){ if(this.value == me.Text){this.value = ""; this.className = "on";this.focus();}}
    this.input.onkeypress = function(){
        var keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode);
        if(keyCode == 13){me.submit(me.allObj);}
    }
    this.input.onnkeydown = function(){
        var keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode);
        if(keyCode == 13){me.submit(me.allObj);}
    }
    this.allObj.onclick = function(){me.submit(this);}
    this.allObj.style.cursor = "pointer";
    if(this.selMenu) {
        this.selMenu.style.display = "none";        
        this.selMenu.onmouseover = function(){me.IsInMenu = true;};
        this.selMenu.onmouseout = function(){me.IsInMenu = false;};
        var menuObjs = getTags(this.selMenu,"li");
        for(var i = 0; i < menuObjs.length; i++)
        {
            menuObjs[i].onclick = function(){
                me.submit(this);
                me.selMenu.style.display = "none";
                me.selObj.src = me.selObj.getAttribute("closeSrc") ? me.selObj.getAttribute("closeSrc") : me.selObj.src;
                me.IsInMenu = false;
            }
            menuObjs[i].onmouseover = function(){this.className = "on";};
            menuObjs[i].onmouseout = function(){this.className = "";};
        }
    }
    if(this.selObj)
    {
        this.selObj.style.cursor = "pointer";
        this.selObj.onclick = function(){
            if(me.selMenu.style.display == "none")
            {
                 me.selMenu.style.display = "block";
                 this.focus();
                 me.IsOpen = true;
                 this.src = this.getAttribute("openSrc") ? this.getAttribute("openSrc") : this.src;
            }
            else
            {
                me.selMenu.style.display = "none";
                this.src = this.getAttribute("closeSrc") ? this.getAttribute("closeSrc") : this.src;
                me.IsInMenu = false;
            }
        };
        this.selObj.onblur = function(){me.IsOpen = false;me.selMenu.style.display = "none";}
    }
};
Search.prototype.submit = function(e)
{
    var url = e.getAttribute("url") ? e.getAttribute("url") : "Search.aspx";
    var keyWord =  this.input.value.toString().trim();
    if(keyWord.length > 0 && keyWord != this.Text){
        linkPage("/" + url + "?keyword=" + escape(keyWord));
    }
};
function $(id){
  if (typeof(id) != "string" || id == "") return null;
  if (document.all) return document.all(id);
  if (document.getElementById) return document.getElementById(id);
  try {return eval(id);} catch(e){ return null;}
}
function getIdTags(id,tagName)
{ 
	return $(id).getElementsByTagName(tagName);
}
function getTags(obj,tagName)
{ 
	return obj.getElementsByTagName(tagName);
}
String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
function linkPage(url)
{
	if(window.location.href)
		window.location.href  = url;
	else
		window.location = url;
}