﻿/* british orthopaedic association *
 * -------------------------------
 * common functions
 *
 * jared at type3digital.com ----- */

// init page
$(document).ready(function() {
    if ($("#btnSearch").length) { setupSearch(); }
    if ($("#ctl00_txtPassword").length) { setupLogin(); }
});

setupLogin = function() {
    $("#ctl00_txtPassword").bind("keypress", function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            location.href = $("#ctl00_btnLogin").attr("href");
        }
    });
    return false;
}

// global search
setupSearch = function() {
    $("#btnSearch").click(function() { runSearch(); });
    $("#txtSearch").bind("keypress", function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            runSearch();
        }
    });
    return false;
}
runSearch = function() {
    var search = $("#txtSearch").val();
    if (search.length > 0)
        location.href = "/site/showsearch.aspx?query=" + escape(search);
    return false;
}

// Google search
loadSearch = function(id, search) {
    var searchControl = new google.search.SearchControl();

    var options = new google.search.SearcherOptions();
    options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

    // Add in a full set of searchers
    var boaSearch = new google.search.WebSearch();
    boaSearch.setSiteRestriction("boa.ac.uk");
    searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
    searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
    
    searchControl.addSearcher(boaSearch, options);

    // Tell the searcher to draw itself and tell it where to attach
    searchControl.draw(document.getElementById(id));

    // Execute an inital search
    searchControl.execute(search);
}

// embed flash
loadFlash = function(id, swf, height, vars) {

    var width = $("#" + id).width();
    var params = {};
    var attributes = {};

    params.menu = "false";
    params.quality = "high";
    params.wmode = "transparent";
    params.bgcolor = "#000000";
    params.allowFullScreen = "true";
    params.base = "."; // using a slash '/' here breaks ie6
    params.salign = "tl";
    params.scale = "default";
    attributes.allowFullScreen = "true";

    attributes.id = id;
    attributes.name = id;

    swfobject.embedSWF(swf, id, width, height,
	    "9.0.0", false, vars, params, attributes);
}

setdiv = function(name) {
	var elem = document.getElementById(name);

		if (elem.style.display == 'block')
			elem.style.display = 'none';
		else
			elem.style.display = 'block';
			
		if (elem.style.visibility == 'visible')
			elem.style.visibility = 'hidden';
		else
			elem.style.visibility = 'visible';
	
}


// Required for ie6
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

openWin = function(link) {
    dummy = window.open(link, "alert", "toolbar=0,status=no,menuBar=0,scrollbars=no,resizable=no,width=400,height=350,left=50,top=50");
}

if (document.images) {
    sb_home_on = new Image(); sb_home_on.src = "/images/sb_home_on.gif";
    sb_profile_on = new Image(); sb_profile_on.src = "/images/sb_profile_on.gif";
    sb_login_on = new Image(); sb_login_on.src = "/images/sb_login_on.gif";
    sb_logout_on = new Image(); sb_logout_on.src = "/images/sb_logout_on.gif";
    sb_search_on = new Image(); sb_search_on.src = "/images/sb_search_on.gif";
    sb_sitemap_on = new Image(); sb_sitemap_on.src = "/images/sb_sitemap_on.gif";
    sb_legal_on = new Image(); sb_legal_on.src = "/images/sb_legal_on.gif";

    sb_home_off = new Image(); sb_home_off.src = "/images/sb_home_off.gif";
    sb_profile_off = new Image(); sb_profile_off.src = "/images/sb_profile_off.gif";
    sb_login_off = new Image(); sb_login_off.src = "/images/sb_login_off.gif";
    sb_logout_off = new Image(); sb_logout_off.src = "/images/sb_logout_off.gif";
    sb_search_off = new Image(); sb_search_off.src = "/images/sb_search_off.gif";
    sb_sitemap_off = new Image(); sb_sitemap_off.src = "/images/sb_sitemap_off.gif";
    sb_legal_off = new Image(); sb_legal_off.src = "/images/sb_legal_off.gif";
}

//Service Box On
sbOn = function(imgName) {
    if (document.images) {
        document[imgName].src = eval(imgName + "_on.src");
    }
}

//Service Box Off
sbOff = function(imgName) {
    if (document.images) {
        document[imgName].src = eval(imgName + "_off.src");
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

addLoadEvent(function() { add_print_link('nav') });

function add_print_link(id) {
    if (!document.getElementById ||
      !document.getElementById(id)) return;

    // add extra functions to <b style="color:black;background-color:#a0ffff">page</b> tools list
    var print_page = document.getElementById(id);

    // create <b style="color:black;background-color:#ffff66">print</b> link
    var print_function = document.createElement('p');
    print_function.className = '<b style="color:black;background-color:#ffff66">print</b>-link';
    print_function.onclick = function() { print_preview(); return false; };
    print_function.appendChild(document.createTextNode('<b style="color:black;background-color:#ffff66">Print</b> the <b style="color:black;background-color:#a0ffff">Page</b>'));

}

function print_preview() {
    // Switch the stylesheet
    setActiveStyleSheet('<b style="color:black;background-color:#ffff66">Print</b> <b style="color:black;background-color:#99ff99">Preview</b>');

    // Create <b style="color:black;background-color:#99ff99">preview</b> message
    add_preview_message();

    // <b style="color:black;background-color:#ffff66">Print</b> the <b style="color:black;background-color:#a0ffff">page</b>
    //	window.<b style="color:black;background-color:#ffff66">print</b>();
}

function add_preview_message() {
    var main_content = document.getElementById('content');
    var main_body = main_content.parentNode;

    if (document.getElementById) {

        var preview_message = document.createElement('div');
        preview_message.id = '<b style="color:black;background-color:#99ff99">preview</b>-message';

        // Create Heading
        var preview_header = document.createElement('h3');
        var preview_header_text = document.createTextNode('This is a <b style="color:black;background-color:#ffff66">print</b> <b style="color:black;background-color:#99ff99">preview</b> of this <b style="color:black;background-color:#a0ffff">page</b>');
        preview_header.appendChild(preview_header_text);

        // Create paragraph
        var preview_para = document.createElement('p');
        var preview_para_text = document.createTextNode('Without this message of course. ');

        var cancel_function_link = document.createElement('a');
        cancel_function_link.onclick = function() { cancel_print_preview(); return false; };
        cancel_function_link.setAttribute('href', '#');
        var cancel_function_link_text = document.createTextNode('Return to the existing <b style="color:black;background-color:#a0ffff">page</b>.');
        cancel_function_link.appendChild(cancel_function_link_text);
        preview_para.appendChild(preview_para_text); //
        preview_para.appendChild(cancel_function_link);

        // Put it all toegether
        preview_message.appendChild(preview_header);
        preview_message.appendChild(preview_para);
        main_body.insertBefore(preview_message, main_content);

    }
}

function cancel_print_preview() {
    // Destroy the <b style="color:black;background-color:#99ff99">preview</b> message
    var print_preview = document.getElementById('<b style="color:black;background-color:#99ff99">preview</b>-message');
    var main_body = print_preview.parentNode;
    main_body.removeChild(print_preview);

    // Switch back stylesheet
    setActiveStyleSheet('default');
}

function setActiveStyleSheet(title) {
    var i, a, main;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
            a.disabled = true;
            if (a.getAttribute("title") == title) a.disabled = false;
        }
    }
}

function printPage() {
    if (window.print) {
        window.print();
        window.location.href = window.location.href.replace("&print=true", "").replace("?print=true","");
    }
    else {
        var WebBrowser = "<object id=\"WebBrowser1\" width=\"0\" height=\"0\" classid=\"CLSID:8856F961-340A-11D0-A96B-00C04FD705A2\"></object>";
        document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
        WebBrowser1.ExecWB(6, 2); //Use a 1 vs. a 2 for a prompting dialog box WebBrowser1.outerHTML = "";  
    }

}
function doPrint(tagName) {
    if (document.getElementById != null) {
        var html = '<html>\n<head>\n';

        html += '<link title="default" media="screen" href="/css/screen.css" type="text/css" rel="stylesheet">';
        html += '\n</head>\n<body>\n';

        html += $("#" + tagName).html();
        html += '\n</body>\n</html>';

        var printWin = window.open("", "printSpecial", "toolbar=yes,location=no,status=no,menubar=no,scrollbars=yes");
        printWin.document.open();
        printWin.document.write(html);
        printWin.document.close();
        printWin.print();
    }
    else {
        alert("Sorry, the print ready feature is only available in modern browsers.");
    }
}
