﻿   function tabActiveOnClick(selTab) {
            var tabName = selTab.id;
            inactiveAllSearchTab();
            if (document.all) {
                selTab.childNodes[0].className = 'tab-act-left';
                selTab.childNodes[1].className = 'tab-act-center';
                selTab.childNodes[2].className = 'tab-act-right';
            } else {
                selTab.childNodes[1].className = 'tab-act-left';
                selTab.childNodes[3].className = 'tab-act-center';
                selTab.childNodes[5].className = 'tab-act-right';
            }
            
            showSearchPanel(tabName);
            setTextSearhButton(tabName);
            document.getElementById("selTab").value = tabName;
        }

        function inactiveAllSearchTab() {
            var object = document.getElementById('searchTabContainer');
            for (var childItem in object.childNodes) {
                if (object.childNodes[childItem].nodeType == 1) {
                    if (object.childNodes[childItem] != null) {
                        var panName = object.childNodes[childItem].id;
                        hidePanel(panName);
                    }
                    var subNodes = object.childNodes[childItem].childNodes;
                    if (document.all) {
                        subNodes[0].className = 'tab-pass-left';
                        subNodes[1].className = 'tab-pass-center';
                        subNodes[2].className = 'tab-pass-right';
                    } else {
                        subNodes[1].className = 'tab-pass-left';
                        subNodes[3].className = 'tab-pass-center';
                        subNodes[5].className = 'tab-pass-right';
                    }
                }
            }
        }

        function hidePanel(hidePanName) {
            var hidePan = document.getElementById('pan' + hidePanName);
            hidePan.style.display = 'none';
        }

        function showSearchPanel(showPanName) {
            var showPan = document.getElementById('pan' + showPanName);
            showPan.style.display = 'block';
        }
        function setTextSearhButton(tabName) {
            var whereSearch;
            switch (tabName) {
                case "Internet":
                    whereSearch = "Искать в Интернете";
                    break;
                case "Image":
                    whereSearch = "Искать в картинках";
                    break;
                case "Video":
                    whereSearch = "Искать в видео";
                    break;
                case "News":
                    whereSearch = "Искать в новостях";
                    break;
                case "Blog":
                    whereSearch = "Искать в блогах";
                    break;
                default:
                    whereSearch = "Искать";
                    break;
            }
            var btnSearch = document.getElementById("btnSearch");
            btnSearch.value = whereSearch;
        }

        function selectSearchItem(selItem) {
            var parentPan = document.getElementById(selItem.parentNode.id);
            for (var childItem in parentPan.childNodes) {
                if (parentPan.childNodes[childItem].nodeType == 1) {
                    if (parentPan.childNodes[childItem] != null) {
                        parentPan.childNodes[childItem].className = "search-search-menu-item";
                    }
                }
            }
            selItem.className = "search-search-menu-item-selected";
        }

        function hideWaterMark() {
            document.getElementById("searchwatermark").style.display = 'none';
            document.getElementById("searchTextField").focus();
        }
        function showWaterMark() {
            document.getElementById("searchwatermark").style.display = 'block';
        }

        //----------------------------------------------------------------------------------------------
        /*search helper*/
        /**
        *  URL encode / decode
        *  http://www.webtoolkit.info/
        **/

        var Url = {

            // public method for url encoding
            encode: function(string) {
                return escape(this._utf8_encode(string));
            },

            // public method for url decoding
            decode: function(string) {
                return this._utf8_decode(unescape(string));
            },

            // private method for UTF-8 encoding
            _utf8_encode: function(string) {
                string = string.replace(/\r\n/g, "\n");
                var utftext = "";

                for (var n = 0; n < string.length; n++) {

                    var c = string.charCodeAt(n);

                    if (c < 128) {
                        utftext += String.fromCharCode(c);
                    }
                    else if ((c > 127) && (c < 2048)) {
                        utftext += String.fromCharCode((c >> 6) | 192);
                        utftext += String.fromCharCode((c & 63) | 128);
                    }
                    else {
                        utftext += String.fromCharCode((c >> 12) | 224);
                        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                        utftext += String.fromCharCode((c & 63) | 128);
                    }

                }

                return utftext;
            },

            // private method for UTF-8 decoding
            _utf8_decode: function(utftext) {
                var string = "";
                var i = 0;
                var c = c1 = c2 = 0;

                while (i < utftext.length) {

                    c = utftext.charCodeAt(i);

                    if (c < 128) {
                        string += String.fromCharCode(c);
                        i++;
                    }
                    else if ((c > 191) && (c < 224)) {
                        c2 = utftext.charCodeAt(i + 1);
                        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                        i += 2;
                    }
                    else {
                        c2 = utftext.charCodeAt(i + 1);
                        c3 = utftext.charCodeAt(i + 2);
                        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                        i += 3;
                    }

                }

                return string;
            }

        }
        /*end search helper*/
        /******************************/
        /* functions for search tabs  */
        /******************************/

        function getSelectedSource(groupName) {
            var value;
            var panel = document.getElementById("pan" + groupName);
            var selectedItem = panel.childNodes;
            //for (item in selectedItem) {
			for (var i = 0; i < selectedItem.length; i++) {
				var item = selectedItem[i];
                //if (selectedItem[item].nodeType == 1) {
				if (item.nodeType == 1) {
                    //if (selectedItem[item].className == "search-search-menu-item-selected") {
					if (item.className == "search-search-menu-item-selected") {
                        //value = selectedItem[item].innerHTML;
						value = item.innerHTML;
                        value = value.replace(/^\s+|\s+$/g, '');
                    }
                }
            }
            //value = $("#pan" + groupName + " > div.search-search-menu-item-selected").html();
            return value;
        }

        function doSearch() {
            var selTab = document.getElementById("selTab").value;
            switch (selTab) {
                case "Internet":
                    doSearchInternet();
                    break;
                case "Image":
                    doSearchImage();
                    break;
                case "Video":
                    doSearchVideo();
                    break;
                case "News":
                    doSearchNews();
                    break;
                case "Blog":
                    doSearchBlog();
                    break;
                default:
                    break;
            }
        }

        function doSearchInternet() {
            var search = document.getElementById("searchTextField").value;
            
            if (search == "") {
                showWaterMark();
                return;
            }

            var value = getSelectedSource('Internet');

            var searchUrl;
            if (value == "Google") searchUrl = "http://www.google.com.ua/search?q=";
            else if (value == "Яндекс") searchUrl = "http://yandex.ru/yandsearch?text=";
            else if (value == "Мета") searchUrl = "http://meta.ua/search.asp?q=";
            else if (value == "Rambler") searchUrl = "http://www.rambler.ru/srch?words=";
            else if (value == "Live") searchUrl = "http://search.live.com/results.aspx?go=&form=QBLH&scope=web&q=";
            else if (value == "Yahoo") searchUrl = "http://search.yahoo.com/search?p=";

            var url = searchUrl + Url.encode(search);
            window.open(url);

            //else if (value == "Msn") searchUrl = "http://search.msn.com/results.aspx?q=";	
            //else if (value == "Altavista") searchUrl = "http://www.altavista.com/web/results?q=";
            //else if (value == "Aport") searchUrl = "http://sm.aport.ru/scripts/template.dll?r=";
            //else if (value == "Nigma") searchUrl = "http://nigma.ru/index.php?s=";
        }

        function doSearchImage() {
            var search = document.getElementById("searchTextField").value;
            
            if (search == "") {
                showWaterMark();
                return;
            }

            var value = getSelectedSource('Image');

            var searchUrl;
            if (value == "Google") searchUrl = "http://images.google.com/images?q=";
            else if (value == "Яндекс") searchUrl = "http://images.yandex.ru/yandsearch?text=";
            else if (value == "Rambler") searchUrl = "http://nova.rambler.ru/pictures?query=";
            else if (value == "Live") searchUrl = "http://search.live.com/images/results.aspx?go=&form=QBLH&scope=images&q=";
            else if (value == "Yahoo") searchUrl = "http://images.search.yahoo.com/search/images;_ylt=A0WTefeCjdtINykAU9aJzbkF?p=";

            var url = searchUrl + Url.encode(search);
            window.open(url);

            //else if (value == "Msn") searchUrl = "http://search.msn.com/images/results.aspx?q=";	
            //else if (value == "Altavista") searchUrl = "http://www.altavista.com/video/results?q=";
            //else if (value == "Aport") searchUrl = "http://sm.aport.ru/scripts/template.dll?That=pics&flt=f00&r=";
            //else if (value == "Nigma") searchUrl = "http://nigma.ru/index.php?t=img&s=";
        }

        function doSearchVideo() {
            var search = document.getElementById("searchTextField").value;

            if (search == "") {
                showWaterMark();
                return;
            }
            var value = getSelectedSource('Video');

            var searchUrl;
            if (value == "Google") searchUrl = "http://video.google.com/videosearch?q=";
            else if (value == "Yahoo") searchUrl = "http://video.search.yahoo.com/search/video;_ylt=A0WTefdbk9tIAhIA1ESJzbkF?&p=";
            else if (value == "Msn") searchUrl = "http://search.msn.com/video/results.aspx?q=";
            else if (value == "Яндекс") searchUrl = "http://video.yandex.ru/search.xml?text=";
            else if (value == "Youtube") searchUrl = "http://www.youtube.com/results?search_query=";
            else if (value == "Dailymotion") searchUrl = "http://www.dailymotion.com/relevance/search/";
            else if (value == "Metacafe") searchUrl = "http://www.metacafe.com/tags/";
            else if (value == "Altavista") searchUrl = "http://www.altavista.com/image/results?q=";

            var url = searchUrl + Url.encode(search);
            window.open(url);

            //else if (value == "Rambler") searchUrl = "http://www.rambler.ru/cgi-bin/pic.cgi?words=";
            //else if (value == "Aport") searchUrl = "http://sm.aport.ru/scripts/template.dll?That=pics&flt=f00&r=";
            //else if (value == "Nigma") searchUrl = "http://nigma.ru/index.php?t=img&s=";
        }

        function doSearchNews() {
            var search = document.getElementById("searchTextField").value;
            if (search == "") {
                showWaterMark();
                return;
            }
            var value = getSelectedSource('News');

            var searchUrl;
            if (value == "Google") searchUrl = "http://news.google.ru/news?q=";
            else if (value == "Яндекс") searchUrl = "http://news.yandex.ru/yandsearch?rpt=nnews&grhow=clutop&text=";
            else if (value == "Rambler") searchUrl = "http://today.rambler.ru/search?query=";

            var url = searchUrl + Url.encode(search);
            window.open(url);

            //else if (value == "Aport") searchUrl = "http://sm.aport.ru/scripts/template.dll?That=news&Rt=2&Base=news&Tn=50&r=";
            //else if (value == "Meta") searchUrl = "http://news.meta.ua/?q=";
            //else if (value == "Yahoo") searchUrl = "http://ru.news.search.yahoo.com/search/news?p=";
            //else if (value == "Msn") searchUrl = "http://search.msn.com/video/results.aspx?q=";	
            //else if (value == "Altavista") searchUrl = "http://www.altavista.com/image/results?q=";	
            //else if (value == "Nigma") searchUrl = "http://nigma.ru/index.php?t=img&s=";
        }

        function doSearchBlog() {
            var search = document.getElementById("searchTextField").value;
            if (search == "") {
                showWaterMark();
                return;
            }
            var value = getSelectedSource('Blog');

            var searchUrl;
            if (value == "Google") searchUrl = "http://www.google.ru/blogsearch?q=";
            else if (value == "Яндекс") searchUrl = "http://blogs.yandex.ru/search.xml?text=";

            var url = searchUrl + Url.encode(search);
            window.open(url);

            //else if (value == "Rambler") searchUrl = "http://today.rambler.ru/search?query=";	
            //else if (value == "Aport") searchUrl = "http://sm.aport.ru/scripts/template.dll?That=news&Rt=2&Base=news&Tn=50&r=";
            //else if (value == "Yahoo") searchUrl = "http://ru.news.search.yahoo.com/search/news?p=";
            //else if (value == "Msn") searchUrl = "http://search.msn.com/video/results.aspx?q=";
            //else if (value == "Altavista") searchUrl = "http://www.altavista.com/image/results?q=";
            //else if (value == "Nigma") searchUrl = "http://nigma.ru/index.php?t=img&s=";
        }

        function searchKeyPress(e) {
            var search = document.getElementById('searchTextField');
            if (search.value != "") {
                if (e.keyCode == 13) {
                   // $("#btnSearch").click();
                    document.getElementById("btnSearch").click();
                }
            }
           }

           //-----------------------------------------------------
           //---------- Tab event
           //-----------------------------------------------------
           function tabClick(el) {
           	if (el.className == "tab-link-selected") return;
           	var parent = el.parentNode;
           	inactiveAllTab(el.parentNode.childNodes);
           	el.className = "tab-link-selected";
           	var namePanel = el.getAttribute('pnl');
           	document.getElementById(namePanel).style.display = 'block';
           }
           //-----------------------------------------------------
           //---------- Tab event end
           //-----------------------------------------------------

           //-----------------------------------------------------
           //---------- Tab event helper
           //-----------------------------------------------------
           function inactiveAllTab(nodes) {
           	for (var item in nodes) {
           		if (nodes[item].nodeType == 1) {
           			nodes[item].className = "tab-link";
           			var namePanel = nodes[item].getAttribute('pnl');
           			document.getElementById(namePanel).style.display = 'none';
           		}
           	}
           }
		   
		   (function() {

function goTop(acceleration, time) {
	acceleration = acceleration || 0.1;
	time = time || 16;

	var dx = 0;
	var dy = 0;
	var bx = 0;
	var by = 0;
	var wx = 0;
	var wy = 0;

	if (document.documentElement) {
		dx = document.documentElement.scrollLeft || 0;
		dy = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		bx = document.body.scrollLeft || 0;
		by = document.body.scrollTop || 0;
	}
	var wx = window.scrollX || 0;
	var wy = window.scrollY || 0;

	var x = Math.max(wx, Math.max(bx, dx));
	var y = Math.max(wy, Math.max(by, dy));

	var speed = 1 + acceleration;
	window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
	if(x > 0 || y > 0) {
		var invokeFunction = "MGJS.goTop(" + acceleration + ", " + time + ")"
		window.setTimeout(invokeFunction, time);
	}
}

window['MGJS'] = {};
window['MGJS']['$'] = $;
window['MGJS']['goTop'] = goTop;

})();