/*
* Search Object
*/
function SearchObject(id, url, url_images, url_videos, url_news, url_maps)
{
    this.id = id;//identifier of iframe holding the searching site
    this.url = url;//searching URL such as "http://google.com/search?q"
    this.url_images = url_images;//searching URL for images such as "http://images.google.com/images?q="
    this.url_videos = url_videos;//searching URL for videos
    this.url_news = url_news;//searching URL for news such as http://news.google.com/news?q=
    this.url_maps = url_maps;//searching URL for maps
}

searchObjects = new Array(
    new SearchObject("google", "http://google.com/search?q=", 
                                                        "http://images.google.com/images?q=", 
                                                        "http://video.google.com/videosearch?q=", 
                                                        "http://news.google.com/news?q=", 
                                                        "http://maps.google.com/maps?q="), 
    new SearchObject("msn", "http://search.live.com/results.aspx?q=", 
                                                        "http://search.live.com/images/results.aspx?q=", 
                                                        "http://search.live.com/video/results.aspx?q=", 
                                                        "http://search.live.com/news/results.aspx?q=", 
                                                        "http://maps.live.com/?q="),
    new SearchObject("yahoo", "http://search.yahoo.com/search?p=", 
                                                        "http://images.search.yahoo.com/search/images?p=", 
                                                        "http://video.search.yahoo.com/search/video?p=", 
                                                        "http://news.search.yahoo.com/news/search?p=", 
                                                        "http://maps.yahoo.com/?q1="),
    new SearchObject("ask", "http://www.ask.com/web?search=search&q=", 
                                                        "http://images.ask.com/pictures?q=", 
                                                        "http://www.ask.com/video?q=", 
                                                        "http://news.ask.com/news?p=", 
                                                        "http://maps.ask.com/maps?p="),
    new SearchObject("baidu", "http://www.baidu.com/s?word=", 
                                                        "http://images.baidu.com/i?word=", 
                                                        "http://video.baidu.com/v?word=", 
                                                        "http://news.baidu.com/ns?word=", 
                                                        "http://maps.baidu.com/maps?word="),
    new SearchObject("sogou", "http://www.sogou.com/web?query=", 
                                                        "http://pic.sogou.com/pics?query=", 
                                                        "http://v.sogou.com/v?query=", 
                                                        "http://news.sogou.com/news?query=", 
                                                        "http://www.sogou.com/web?query="), // no maps?
    new SearchObject("yahoocn", "http://one.cn.yahoo.com/s?p=", 
                                                        "http://one.cn.yahoo.com/s?v=image&p=", 
                                                        "http://one.cn.yahoo.com/s?v=video&p=", // maybe just v=image again
                                                        //"http://one.cn.yahoo.com/s?v=ks&p=", // this is for Q&A
                                                        "http://one.cn.yahoo.com/s?v=news&p=", // this is for news
                                                        "http://one.cn.yahoo.com/s?p="), // no maps?
    new SearchObject("googlecn", "http://google.cn/search?q=", 
                                                        "http://images.google.cn/images?q=", 
                                                        "http://video.google.cn/videosearch?q=", 
                                                        "http://news.google.cn/news?q=", 
                                                        "http://maps.google.cn/maps?q="), 
    new SearchObject("soso", "http://www.soso.com/q?sc=web&w=", 
                                                        "http://image.soso.com/image.cgi?sc=img&w=", 
                                                        "http://video.soso.com/q?sc=vid&w=",
                                                        "http://news.soso.com/n.q?sc=news&ty=c&w=",
                                                        "http://www.soso.com/q?sc=web&w="), // no maps?
    new SearchObject("youdao", "http://www.youdao.com/search?q=", 
                                                        "http://image.youdao.com/search?q=", 
                                                        "http://video.youdao.com/search?q=",
                                                        "http://news.youdao.com/search?q=",
                                                        "http://ditu.youdao.com/search?q="),
    new SearchObject("ads", "search.cgi?type=search&log=1&q=", 
                                                        "search.cgi?type=images&q=", 
                                                        "search.cgi?type=videos&q=", 
                                                        "search.cgi?type=news&q=", 
                                                        "search.cgi?type=maps&q=")
);

var selectedDomain = "web";//Selected domain of searching
var firstPage = true;//The very search page with just search form

/*
* Get the searching url according to specified domain
*/
function getSearchUrlByTarget(domain, searchObject)
{
    var url;
    if (domain == "web") { url = searchObject.url; }
    else if (domain == "images") { url = searchObject.url_images; }
    else if (domain == "videos") { url = searchObject.url_videos; }
    else if (domain == "news")   { url = searchObject.url_news; }
    else if (domain == "maps")   { url = searchObject.url_maps; }
    return url;
}

/*
* Main search function
* @param domain      Target domain for searching such as web, images or news
*/
function searchText(domain)
{
    var text = document.getElementById("search").value;
    text = encodeURIComponent(text);
    
    //Go through each search object, build search URL and send it to target iframe
    for (var i = 0; i < searchObjects.length; i++)
    {
        var searchObject = searchObjects[i];
        var tab = document.getElementById("tab_" + searchObject.id);
        if (tab)
        {
            var iframe = document.getElementById(searchObject.id);
            if (iframe)
            {
                //Get the search URL according to given domain such as images, news etc.
                if (!domain)
                {
                    firstPage = false;
                }
                else
                {
                    document.getElementById(selectedDomain).style.display = "inline";
                    document.getElementById("label_" + selectedDomain).style.display = "none";
                    
                    document.getElementById(domain).style.display = "none";
                    document.getElementById("label_" + domain).style.display = "inline";
                    
                    selectedDomain = domain;
                }
                
                if (!firstPage || !domain)
                {
                    var url = getSearchUrlByTarget(selectedDomain, searchObject);
                    iframe.src = url + text;
                }
            }
        }
    }
    
    if (!firstPage || !domain)//Don't display the content if user just clicked a link (domain) from very first page
    {
        //Move up search form and display the tabs content
        document.getElementById("tabbed_content").style.display = "block";
        document.getElementById("search_content").removeAttribute("style");
    }
}

/*
* Change the tab and tab content
*/
function changeTab(obj)
{
    for (var i = 0; i < searchObjects.length; i++)
    {
        var searchObject = searchObjects[i];
        var next = document.getElementById("tab_" + searchObject.id);
        if (next)
        {
            next.className = "";
            document.getElementById(searchObject.id).style.display = "none";
        }
    }
    obj.className = "selected";
    var iframeId = obj.id.substring("tab_".length);
    document.getElementById(iframeId).style.display = "block";
}

/*
* Automatically resize tab area to the size of the browser window
*/
function updateSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth -20;
    myHeight = window.innerHeight - 115;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth - 15;
    myHeight = document.documentElement.clientHeight - 230;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth - 15;
    myHeight = document.body.clientHeight - 230;
  }
  
  var body_content = document.getElementById("body_content");
  body_content.style.width = myWidth + "px";
  body_content.style.height = myHeight + "px";
}
