/* &copy; 2009 Goofram.com All rights reserved. */

function isIE()
{
    return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function urlencode(s) {
    s = encodeURIComponent(s);
    return s.replace(/~/g,'%7E').replace(/%20/g,'+');
}

function refloatWolf()
{   
    var wolf = document.getElementById("wdiv");
    if (window.innerWidth < 1175) {
	wolf.style.right = '';
	wolf.style.left = '530px';
    } else {
	wolf.style.left = '';
	wolf.style.right = '0px';
    }
}

function selectSlot() {
    var gFrame = document.getElementsByName("googleSearchFrame");
    if (gFrame.length <= 0) {
	alert("gFrame's length is " + gFrame.length);
	return;
    }
    var wFrame = document.getElementById("wolf_window");
    gFrame = gFrame[0];
    var gfCH = gFrame.clientHeight;
    var gfH = gFrame.height;
    wFrame.height = gFrame.height;
}

function doNothing() { }

function notifyWolfLoaded() {
    var wFrame = document.getElementById("wolf_window");
    var sHeight = wFrame.scrollHeight;

    var scrollTop = wFrame.scrollTop;
    selectSlot();
}

    function getDomain (thestring) {
	//simple function that matches the beginning of a URL
	//in a string and then returns the domain.
	var urlpattern = new RegExp("(http|ftp|https)://(.*?)/.*$");
	var parsedurl = thestring.match(urlpattern);
	return parsedurl[2];
    }

    function putPermaCookie(cookname, value) {
	var expires = new Date();
	expires.setFullYear(expires.getFullYear() + 16);
	var cook = cookname + "=" + value + "; expires=" + expires + "; ";
	cook += "path=/; domain=" + getDomain(document.URL) + ";";
	document.cookie = cook;
    }

function addOpenSearch(url) {
    try {
	window.external.AddSearchProvider(url);
	putPermaCookie("hasopensearch", 1);
	/* $("#addquicksearch").fadeOut("fast"); */
	document.getElementById("addquicksearch").style.display = 'none';
    } catch (e) {
	alert("You need to be using IE7, Firefox2 or a newer version of your browser to add Goofram to your quick searches.\r\nYou can also install the quick search plugins by using the drop down menu to the right of the search box.");
	return;
    }
}

function calcHeight(elem)
{
    //find the height of the internal page
    var the_height=500;
    var padding=30;//to avoid the scroll bar
    /* try
	{
	    if (elem.contentWindow && elem.contentWindow.document)
		{
		    the_height=elem.contentWindow.document.body.scrollHeight;
		}
	}
    catch(e){} */
    // change the height of the iframe
    elem.height = the_height + padding;
}

function valert(obj) {
    alert("this is " + obj);
}
function onLoadHandler(){};
function changeHandler(){
    onLoadHandler = function(){
	alert("added successfully");
    };
}

function repositionFooter(len) {
    if (len < document.body.clientHeight) {
	var diff = document.body.clientHeight - len;
	diff -= 330;
	if (diff <= 0)
	    return;
	var footer = document.getElementById("footer");
	footer.style.paddingTop = diff + "px";
    }
    return;
}

function resizeWolfWindow(tWolf) {
    //return;
    var wFrame = null;
    if (tWolf == null) {
	if (isIE())
	    wFrame = window.frames['wolf_window'];
	else
	    wFrame = document.getElementById("wolf_window");
    } else {
	wFrame = tWolf;
    }
    var gh = 0;
    if (wFrame == null)
	return;
    var gres = document.getElementById('gresults');
    gh = gres.clientHeight;
    if (gh < 505) {
	gh = 505;
    }
    if (isIE()) {
	wFrame.style.height = gh + "px";
    } else {
	wFrame.height = gh;
    }
    repositionFooter(gh);
}

function fadeOutWaiting() {
	//var curDate = new Date();
	//var curTime = curDate.getTime();
	//var elapsed = curTime - time;
	
	$("#waiting").fadeOut("fast");    
    }

//window.onload = function() { resizeWolfWindow(null); };

$(document).ready(function() {

    resizeWolfWindow(null);
    $('#outimg').mouseover(function() {
	$('#message').show("slow");
    });
    $(".breakout").mouseout(function() {
	$('#message').hide("fast");
    });
    
    var aqs = document.getElementById("addquicksearch");
    $("#addquicksearch").mouseover(function() {
	aqs.style.backgroundColor = "#D60202";
    }).mouseout(function() {
	aqs.style.backgroundColor = "#DF5162";
    });
    refloatWolf();
    window.onresize = refloatWolf
    });

/*
  

window.onload = function() {
    alert("page laoded");
    //selectSlot();
    //changeHandler();
    
    wFrame = frames["wolf_window"];
    wFrame.onload = function() {
	alert("Hey!");
	notifyWolfLoaded();
    };
    
    window.onblur = function() {
	
	
	var beenBlurred = false;

	//selectSlot();
	if (!beenBlurred) {
	    beenBlurred = true;
	    setTimeout("doNothing();", 1000);
	}
	window.focus();

    };
};
*/

function testGFrame() {
    var gFrame = document.getElementsByName('googleSearchFrame');
    //var gFrame = window.frames['googleSearchFrame'];
    if (!gFrame) {
	alert('gFrame is null');
	setTimeout('testGFrame();', 10000);
    } else {
	alert('gFrame is ' + gFrame);
    }
}
