$(document).ready(main);

function main() {
    $('iframe').attr('src', getIframeUrl());
    registerEvents();
    resizeIframe();
}

function getIframeUrl() {
    var url = window.location.href;
    var iframe_url = 'http://www.searchers.co.uk';
    var param_start = url.indexOf("url=");
    if (param_start != -1)
        iframe_url = url.substr(param_start + 4, url.length - param_start - 4);
    if (iframe_url.indexOf("http://") == -1)
        iframe_url = "http://" + iframe_url;

    return iframe_url;
}

function registerEvents() {
    $(window).resize(function() { resizeIframe(); });
    $("#back").bind("click", function() { window.history.back(); });
    $("#feedback").bind("click", showFeedback);
    $("#enquiry").bind("click", showEnquiry);   
    $("#friend").bind("click", showFriend);
    $("#hide").bind("click", onArrow);
    $("#searchBtn").bind("click", onSearch);
    $("#home").bind("click", onHome);
    $("#close").bind("click", function() { window.location.href = $("iframe").attr("src"); });
    $("#twitter").bind("click", function() { window.location.href = "http://twitter.com/?status=http://www.linku2.co.uk - "; });
    //$("#twitter").bind("click", function() { window.location.href = "http://twitter.com/?status=" + getIframeUrl() + " - "; });
    $("#search").bind("keypress", function(e) {
        e = e || window.event;

        if (e.keyCode == 13) {
            onSearch();
        }
    });
}

var arrawState = "up";

function onArrow() {
    if (arrawState == "up") onDownArrow();
    else onUpArrow();
}

function onDownArrow() {
    $("#toolbar").animate(
		{
		    height: 450
		}, 1000, "swing", function() {
		    //$("#arrow").css("background-position", "-32px 0");
		    resizeIframe();
		    arrawState = "down";
		});
}

function onUpArrow() {
    $("#toolbar").animate(
		{
		    height: 38
		}, 1000, "swing", function() {
		    //$("#arrow").css("background-position", "-16px 0");
		    resizeIframe();
		    arrawState = "up";
		});
}

function onSearch() {
    var qs = $("#qs").val();
    $('iframe').attr('src', 'http://www.google.com/search?q=' + qs);
}


function onHome() {
    $('iframe').attr('src', 'http://www.linku2.co.uk');

}


function resizeIframe() {
    $("#iframe").height(WindowHeight() - getObjHeight(document.getElementById("toolbar")));
}

function WindowHeight() {
    var de = document.documentElement;
    return self.innerHeight ||
		(de && de.clientHeight) ||
		document.body.clientHeight;
}

function getObjHeight(obj) {
    if (obj.offsetWidth) {
        return obj.offsetHeight;
    }
    return obj.clientHeight;
}

function addBookmark(url, title) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (document.all) {
        window.external.AddFavorite(url, title);
    } else if (window.opera && window.print) {
        // opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else {
        return true;
    }
}
function showFeedback() {
    $("div#tab1").show();
    $("div#tab2").hide();
    $("div#tab3").hide();
    onArrow();
}
function showEnquiry() {
    $("div#tab1").hide();
    $("div#tab2").show();
    $("div#tab3").hide();
    onArrow();
}

function showFriend() {
    $("div#tab1").hide();
    $("div#tab2").hide();
    $("div#tab3").show();
    onArrow();
}

