/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function getBrowser() {
  var sBrowser = navigator.userAgent;
  if (sBrowser.toLowerCase().indexOf('msie') > -1) return 'ie';
  else if (sBrowser.toLowerCase().indexOf('firefox') > -1) return 'firefox';
  else return 'mozilla';
}
function sh(o, h) {
    o.style.height = parseInt(h) + "px";
}
function sw(o, h) {
    o.style.width = parseInt(h) + "px";
}

function setHeight(sender) {
    var height = window.innerHeight;
    if (document.body) {
        if (document.body.clientHeight) {
            height = document.body.clientHeight;
//alert('1');
        }
    }

    if (sender == null) {
        sender = document.getElementById('google');
        //alert('found element google');
//alert('2');
    }
    if (sender == null || document.body.className == 'b') {
        sender = document.getElementById('bing');
        //alert('found element bing');
//alert('3');
    }

    if (sender != null) {
        if (document.body.className == 'h') {
            height = (height - sender.offsetTop - 8) / 2;
//alert('4');
        }
        else {
            height = height - sender.offsetTop - 4;
//alert('5 height:' + height + ' sender.offsetTop: ' + sender.offsetTop);
        }
    }
    sh(document.getElementById('bing'), height);
    sh(document.getElementById('google'), height);
}

function resizeFrames(sender) {
    try {
        //alert('resizeFrames call');
        //begin h-split
        //alert ('body class: ' + document.body.className);
        if (document.body.className == 'h') {
            sh(document.getElementById('bing'), "0px");
            sh(document.getElementById('google'), "0px");
            sw(document.getElementById('google'), "0px");
            sw(document.getElementById('bing'), "0px");
            var width = window.innerWidth;
            if (document.getElementById('search-results-container')) {
                if (document.getElementById('search-results-container').clientWidth) { //only works with FF
                    //width = document.getElementById('search-results-container').clientWidth - 5;
                    width = document.body.clientWidth - 17;
                } else { //for IE
                    width = document.body.clientWidth - 17;
                }
            } else {
                width = document.body.clientWidth;
            }
            //alert('h width:' + width);
            sw(document.getElementById('google'), width);
            sw(document.getElementById('bing'), width);
            setHeight(sender);
        }
        //end h-split

        //begin v-split - adjust height to 0 first to get rid of wrapping caused scroll bar then width then do height properly
        if (document.body.className == 'v') {
            sh(document.getElementById('bing'), "0px");
            sh(document.getElementById('google'), "0px");
            //alert('r7: ' + document.getElementById('search-results-container').clientWidth);
            if (document.getElementById('search-results-container').clientWidth > 33)
                width = (document.getElementById('search-results-container').clientWidth * 0.63) - 33;
            else
                width = (document.body.clientWidth * 0.7) - 33; //IE
            //alert('r8: ' + width);
            if (width >= 820)
                width = 820;
            //alert('r8.5: ' + width);
            sw(document.getElementById('google'), width);
            width = document.body.clientWidth - document.getElementById('google').clientWidth - 31;
            //if (isFirstResize)
            //    width = width -30;
            sw(document.getElementById('bing'), width);
            //alert('v width:' + width);
            setHeight(sender);
        }
        //end v-split
        
        if (document.body.className == 'b') {
            sw(document.getElementById('bing'), "0px");
            setHeight(sender);
            width = document.body.clientWidth - 17;
            sw(document.getElementById('bing'), width);
            
        }
        
        if (document.body.className == 'g') {
            setHeight(document.getElementById('google'));
            width = document.body.clientWidth - 17;
            sw(document.getElementById('google'), width);
            
        }
    } catch(err) {}
}

function f() {
    document.getElementById('query-box').focus();
}
function s(sl) {
    document.body.className = sl;
    //cc('s', sl, 30);
    resizeFrames();
    return false;
}

function setCookieDays(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function get_cookie (cookie_name)
{
  var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
  if (results)
    return (unescape (results[2]));
  else
    return null;
}
function delete_cookie (cookie_name)
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function set_cookie (name, value, exp_y, exp_m, exp_d, path, domain, secure)
{
  var cookie_string = name + "=" + escape (value);

  if (exp_y)
  {
    var expires = new Date (exp_y, exp_m, exp_d);
    cookie_string += "; expires=" + expires.toGMTString();
  }
  if (path)
        cookie_string += "; path=" + escape ( path );
  if (domain)
        cookie_string += "; domain=" + escape ( domain );
  if (secure)
        cookie_string += "; secure";
  document.cookie = cookie_string;
}


function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'none')
        e.style.display = 'block';
    else
        e.style.display = 'none';
}

function hide_element(id) {
    try {
        document.getElementById(id).style.display = 'none';
    } catch (err) {}
}

function show_element(id) {
    try {
        document.getElementById(id).style.display = 'block';
    } catch (err) {}
}

function changeClassByID(id,newClass) {
    document.getElementById(id).className= newClass;
}
function changeID(oldID,newID) {
    var e = document.getElementById(oldID);
    if (e != null) {
        try {
            e.id = newID;
        } catch(err) {}
    }
}

function show_b() {
    changeID('bing-search-results-trimmed','bing-search-results');
    show_element('bing-search-results');
    hide_element('google-search-results');
    show_element('google-adblock1');
    changeClassByID('google-tab','tab-inactive-80');
    changeClassByID('bing-tab','tab-active-80');
    changeClassByID('v-split-tab','tab-inactive-90');
    changeClassByID('h-split-tab','tab-inactive-90');
    set_cookie ("displayPref", "bing", 2020, 01, 15);
    return s('b');
}

function show_g() {
    changeID('bing-search-results-trimmed','bing-search-results');
    hide_element('bing-search-results');
    show_element('google-search-results');
    show_element('google-adblock1');
    changeClassByID('bing-tab','tab-inactive-80');
    changeClassByID('google-tab','tab-active-80');
    changeClassByID('v-split-tab','tab-inactive-90');
    changeClassByID('h-split-tab','tab-inactive-90');
    set_cookie ("displayPref", "google", 2020, 01, 15);
    return s('g');
}

function show_v() {
    changeID('bing-search-results','bing-search-results-trimmed');
    show_element('bing-search-results-trimmed');
    show_element('google-search-results');
    show_element('google-adblock1');
    changeClassByID('bing-tab','tab-inactive-80');
    changeClassByID('google-tab','tab-inactive-80');
    changeClassByID('v-split-tab','tab-active-90');
    changeClassByID('h-split-tab','tab-inactive-90');
    set_cookie ("displayPref", "v-split", 2020, 01, 15);
    return s('v');
}


function show_h() {
    changeID('bing-search-results-trimmed','bing-search-results');
    show_element('bing-search-results');
    show_element('google-search-results');
    hide_element('google-adblock1');
    changeClassByID('bing-tab','tab-inactive-80');
    changeClassByID('google-tab','tab-inactive-80');
    changeClassByID('v-split-tab','tab-inactive-90');
    changeClassByID('h-split-tab','tab-active-90');
    set_cookie ("displayPref", "h-split", 2020, 01, 15);
    //alert('kkk');
    //document.getElementById('nulltarget').window.location.href = 'log.php?action=show-h&src=local';
    //window.frames['nulltarget'].window.location.replace('log.php?action=show-h&src=local');
    //window.frames[2].
    //window.frames['nulltarget'].location.replace(regexp_or_substr, newSubStr_or_function, flags)
    return s('h');
    
}

function addToSearchBar() {
    setCookieDays ("addSearchBar", "true", 30);
    //window.open('http://searchInstruction.php','Boogle Search Bar','width=400,height=200');
    alert('Make sure you tick the check box if you want to make this your default search engine.\n\nOtherwise you can select it from the drop down menu in your search bar');
    window.external.AddSearchProvider('http://booglesearch.com/plugin.xml');
}

function welcomeToSearchBar() {
    setCookieDays ("addSearchBar", "true", 30);
    //window.open('http://searchInstruction.php','Boogle Search Bar','width=400,height=200');
    alert('Would you like to add Boogle to your search bar?  \n\nIf so make sure you tick the check box if you want to make Boogle your default search engine.\n\nOtherwise you can select it later from the drop down menu in your search bar');
    window.external.AddSearchProvider('http://booglesearch.com/plugin.xml');
}

function setHomePage() {
    this.setHomePage('http://booglesearch.com');
}

function getURLVar(urlVarName) {
    //divide the URL in half at the '?'
    var urlHalves = String(document.location).split('?');
    var urlVarValue = '';
    if(urlHalves[1]){
        //load all the name/value pairs into an array
        var urlVars = urlHalves[1].split('&');
        //loop over the list, and find the specified url variable
        for(i=0; i<=(urlVars.length); i++){
            if(urlVars[i]){
                //load the name/value pair into an array
                var urlVarPair = urlVars[i].split('=');
                if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
                    //I found a variable that matches, load it's value into the return variable
                    urlVarValue = urlVarPair[1];
                }
            }
        }
    }
    return urlVarValue;
}

window.onresize = function() { if (document.body.className == 'v' && getBrowser() == 'ie') {show_v();} else {resizeFrames(null);} }; //hack to make sure resize doesn't clip in IE
