function getXmlHttp() {
        var xmlhttp;
        if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
        }
        else {
                xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
        return xmlhttp;
}


function SendGetRequest(container, url) {
        var xmlhttp = getXmlHttp();
        xmlhttp.open('GET', url, true);
        xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                        if(xmlhttp.status == 200) {
                                try {
                                        restxt = xmlhttp.responseText;
                                        document.getElementById(container).innerHTML = restxt;
                                }
                                catch(err) {
                                }
                        }
                }
        };
        xmlhttp.send(null);
}

var rated = new Array();

function UpdateRate(container, url) {
        if (rated[container] != 1) {
                SendGetRequest(container, url);
                rated[container] = 1;
        }
}

function UpdateContainerFromUrl(container, url) {
        var xmlhttp = getXmlHttp();
        xmlhttp.open('GET', url, true);
        xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                        if(xmlhttp.status == 200) {
                                try {
                                        restxt = xmlhttp.responseText;
                                        document.getElementById(container).innerHTML = document.getElementById(container).innerHTML+restxt;
                                }
                                catch(err) {
                                }
                        }
                }
        };
        xmlhttp.send(null);
}

var no_sug = 0;
var suggs = Array();
var suggs_cache = Array();
var sugg_index = -1;


function NotEmptyLine(element, index, array) {
        return (element != '');
}

function Suggest(query, container, container_2) {
        // Hack for IE
        if (!Array.prototype.filter) {
                Array.prototype.filter = function(fun /*, thisp*/) {
                        var len = this.length;
                        if (typeof fun != "function")
                                throw new TypeError();
                        var res = new Array();
                        var thisp = arguments[1];
                        for (var i = 0; i < len; i++) {
                                if (i in this) {
                                        var val = this[i];
                                        if (fun.call(thisp, val, i, this))
                                                res.push(val);
                                }
                        }
                        return res;
                };
        }
        if (no_sug == 0 && query.length > 1) {
                if (suggs_cache[query]) {
                        suggs = suggs_cache[query];
                        sugg_index = -1;
                        DisplaySuggs(container, container_2);
                        document.getElementById(container_2).style.display = '';
                }
                else {
                        var xmlhttp = getXmlHttp();
                        xmlhttp.open('GET', '/hint/?qs='+escape(query), true);
                        xmlhttp.onreadystatechange = function() {
                                if (xmlhttp.readyState == 4) {
                                        if(xmlhttp.status == 200) {
                                                try {
                                                        restxt = xmlhttp.responseText;
                                                        suggs = restxt.split("\n").filter(NotEmptyLine).sort();
                                                        suggs_cache[query] = suggs;
                                                        sugg_index = -1;
                                                        DisplaySuggs(container, container_2);
                                                        document.getElementById(container_2).style.display = '';
                                                }
                                                catch(err) {
                                                }
                                        }
                                }
                        };
                        xmlhttp.send(null);
                }
        }
        else {
                document.getElementById(container_2).style.display = 'none';
                document.getElementById(container_2).innerHTML = '';
        }
        no_sug = 0;
}
function SetQuery(query, container, container_2) {
        no_sug = 1;
        document.getElementById(container).value = query;
        document.getElementById(container_2).style.display = 'none';
        document.getElementById(container_2).innerHTML = '';
}

function HideSuggest(container) {
        document.getElementById(container).style.display = 'none';
        document.getElementById(container).innerHTML = '';
}

function SuggestKeyPress(e, query, container, container_2, searchform) {
        var evtobj = window.event? event : e;
        var unicode = evtobj.charCode? evtobj.charCode : evtobj.keyCode;
        // Up
        if (unicode == 38 && sugg_index > -1) {
                sugg_index--;
                DisplaySuggs(container, container_2);
        }
        // Down
        else if (unicode == 40 && sugg_index < suggs.length-1) {
                sugg_index++;
                DisplaySuggs(container, container_2);
        }
        // Enter
        else if (unicode == 13) {
                if (document.getElementById(container_2).style.display == '' && sugg_index > -1) {
                        SetQuery(suggs[sugg_index], container, container_2);
                }
                else if (document.getElementById(container).value != '') {
                        document.forms[searchform].submit();
                }
        }
        // ESCAPE
        else if (unicode == 27) {
                HideSuggest(container_2);
        }
        // Symbol or Backspace or DEL or Space
        else if (unicode >= 48 || unicode == 8 || unicode == 46 || unicode == 32) {
                Suggest(query, container, container_2);
        }
        else {
        }
        return false;
}
function DisplaySuggs(container, container_2) {
        if (suggs.length > 0) {
                line = '';
                for (i = 0; i < suggs.length; i++) {
                        id = 'sugg_'+i;
                        onclick = 'onclick="javascript:SetQuery(\''+suggs[i]+'\', \''+container+'\', \''+container_2+'\')"';
                        if (i == sugg_index) {
                                line += '<span '+onclick+' id="'+id+'" class="request_hint_item_active">'+suggs[i]+"</span><br>\n";
                        }
                        else {
                                line += '<span '+onclick+' id="'+id+'" class="request_hint_item">'+suggs[i]+"</span><br>\n";
                        }
                }
        }
        else {
                line = "<span style=\"font-style:italic;\">No suggestions found...</span>\n";
        }
        document.getElementById(container_2).innerHTML = line;
}

function CloseGoFrame() {
        parent.document.getElementById('fiframes').rows='0,*';
}

function ShowAdvanced() {
        e = document.getElementById('search-detail');
        if (e.style.display == 'block') {
                e.style.display = 'none';
        }
        else {
                e.style.display = 'block';
        }
}

function ShowTab(tabnum, tabcount, tab_prefix, tab_head_prefix) {
        for (i = 1; i <= tabcount; i++) {
                if (i == tabnum) {
                        document.getElementById(tab_prefix + i).style.display = '';
                        document.getElementById(tab_head_prefix + i).className = 'active';
                }
                else {
                        document.getElementById(tab_prefix + i).style.display = 'none';
                        document.getElementById(tab_head_prefix + i).className = '';
                }
        }
}


//var page_current = 'download';

function InitEventTracker(){
        var page = '';
        if(typeof(page_current) == 'undefined'){
                page = location.pathname.split('\/')[1];
                if(page == ''){
                        page = '/';
                }
        }
        else{
                page = page_current;
        }
        var pageEvents = {
                '/': [
                        {id: 'newcomers', action: 'newcomers'},
                        {id: 'blogposts-last', action: 'blogposts-last'},
                        {id: 'downloads-top', action: 'downloads-top'},
                        {id: 'trends_files_1', action: 'downloads-top'},
                        {id: 'trends_files_2', action: 'downloads-top'},
                        {id: 'cloud', action: 'search-last'},
                        {id: 'downloads-my', action: 'downloads-my'},
                        //{id: 'filters', action: 'input-filters', child: 'input'},
                        {id: 'searchform', action: 'select-hosting', child: 'select'},
                        {id: 'searchform', action: 'search-form', child: 'input'},
                        {id: 'toolbar', action: 'toolbar-download'}
                ],
                'download': [
                        //{id: 'ads', action: 'ads'},
                        {id: 'search-result', action: 'serp'},
                        {id: 'search-result-exact', action: 'search-result-exact'},
                        {id: 'search-similar', action: 'similar'},
                        {id: 'tab_1', action: 'tags'},
                        {id: 'search-latest', action: 'latest'},
                        {id: 'search-tags', action: 'tags'},
                        {id: 'tab_2', action: 'tags'},
                        {id: 'search-pager', action: 'pager'},
                        //{id: 'filters', action: 'input-filters', child: 'input'},
                        {id: 'searchform', action: 'select-hosting', child: 'select'},
                        {id: 'searchform', action: 'search-form', child: 'input'},
                        {id: 'link-more-result', action: 'link-more-result'},
                        {id: 'search-orderby', action: 'search-orderby'}
                ],
                'fileinfo': [
                        //{id: 'ads', action: 'ads'},
                        //{id: 'download-button', action: 'download-button'},
                        {id: 'search-latest', action: 'latest'},
                        {id: 'search-tags', action: 'tags'},
                        {id: 'search-related', action: 'relate'},
                        {id: 'tab_1', action: 'relate'},
                        {id: 'search-download', action: 'last-download'},
                        {id: 'tab_2', action: 'relate'},
                        //{id: 'filters', action: 'input-filters', child: 'input'},
                        {id: 'searchform', action: 'select-hosting', child: 'select'},
                        {id: 'searchform', action: 'search-form', child: 'input'}
                ]
        };
        var o;
        if(pageEvents[page] !== undefined){
                o = pageEvents[page];
        }
        else{
                return false;
        }
        for(i in o){
                if(o[i].child === undefined){
                        o[i].child = 'a';
                }
                var tObj = document.getElementById(o[i].id);
                if(tObj !== null){
                        var category = page;
                        var action = o[i].action;
                        var childs = tObj.getElementsByTagName(o[i].child);
                        for(y in childs){
                                if(childs[y].nodeType != 1){
                                        continue;
                                }
                                if(childs[y].className !== undefined){
                                        if(childs[y].className.match('trackEvent-none')){
                                                continue;
                                        }
                                        var match = [];
                                        if((match = childs[y].className.match('trackEventAction-(.+)')) !== null){
                                                action = match[1];
                                        }
                                }
                                var node = childs[y].nodeName.toLowerCase();
                                if(node == 'a'){
                                        (function(y, c, a){
                                                addHandler(childs[y], 'click',
                                                        function(){
                                                                pageTracker._trackEvent(c, a, this.href);
                                                        }
                                                );
                                        })(y, category, action);
                                }
                                else if(node == 'input'){
                                        (function(y, c, a){
                                                addHandler(childs[y], 'click',
                                                        function(){
                                                                pageTracker._trackEvent(c, a, this.name);
                                                        }
                                                );
                                        })(y, category, action);
                                }
                                else if(node == 'select'){
                                        (function(y, c, a){
                                                addHandler(childs[y], 'change',
                                                        function(){
                                                                pageTracker._trackEvent(c, a, this.value);
                                                        }
                                                );
                                        })(y, category, action);
                                }
                        }
                }
        }
}


setGlobalOnLoad(InitEventTracker);


function addHandler(object, event, handler){
        if (typeof object.addEventListener != 'undefined')
                object.addEventListener(event, handler, false);
        else if (typeof object.attachEvent != 'undefined')
                object.attachEvent('on' + event, handler);
}
function removeHandler(object, event, handler){
        if (typeof object.removeEventListener != 'undefined')
                object.removeEventListener(event, handler, false);
        else if (typeof object.detachEvent != 'undefined')
                object.detachEvent('on' + event, handler);
}
function setGlobalOnLoad(f) {
        var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
        if (root){
                if(root.addEventListener) root.addEventListener("load", f, false);
                else if(root.attachEvent) root.attachEvent("onload", f);
        } else {
                if(typeof window.onload == 'function') {
                        var existing = window.onload;
                        window.onload = function() {
                                existing();
                                f();
                        };
                } else {
                        window.onload = f;
                }
        }
}

