/* AJAX Functionality */

function MW_AJAX_update_objects(data, targets) {
	var objID;
	var dom_data = $("<div>").html(data);

	if (typeof(targets) == "string") targets = new Array(targets); 
	
	if ($(dom_data).find("*#messagebox").length > 0) { // check to see if there's a new status message
		if ($("#messagebox").length == 0) { // no messagebox already exists on the page
			$("#content").prepend("<div id=\"messagebox\"></div>");
		}
		targets[targets.length] = "messagebox" // just add messagebox as another object that needs updating
	}

	for ( var i = 0; i < targets.length; i++ ) {
		objID = targets[i];
		if ($("#" + objID).length > 0) {
			var oldobj = $("#" + objID);
			$(oldobj).attr("id","old_" + objID);
			var newobj = $(dom_data).find("*#" + objID);
			$(oldobj).before(newobj);
			$(oldobj).remove();
		}
	}
	MW_reestablish_UI();
}

function MW_reestablish_UI() {
	MW_establish_link_types();
}

function MW_establish_link_types() {
	// rel="view_external" is for off-site links
	$("a[@rel=view_external]").click( function() {
		window.open(this.href,'_blank');
		return false;
	});

	// rel="file" is for links directly to other types of files (PDF, Word, text ...)
	$("a[@rel=file]").click( function() {
		window.open(this.href,'_blank');
		return false;
	});

	// rel="view_image" is for direct links to an image
	$("a[@rel=view_image]").click( function() {
		TB_open(this);
		return false;
	});
	
	$("a[@rel*=update_object]").click( function() {
		var targets = "";
		if (($(this).attr("rel").indexOf("[") > -1) && ($(this).attr("rel").indexOf("]") > -1)) {
			var objIDlist = $(this).attr("rel").substring($(this).attr("rel").indexOf("[")+1,$(this).attr("rel").indexOf("]"));
			targets = objIDlist.split(",");
		} else { // find closest parent with an ID
			for (var i = 0; i < $(this).parents().length; i++) {
				if ($($(this).parents().get(i)).attr("id")) {
					targets = $($(this).parents().get(i)).attr("id");
					break;
				}
			}
		}
		if (targets == "") { targets = "content"; } // if all else fails, update the entire #content area
		$.get(this.href, {hijax: true}, function(data) { MW_AJAX_update_objects(data, targets); } )
		$(this).before("<img src=\"/_Images/icons/iconLoading.gif\" alt=\"Loading...\" class=\"treeupdate\"/>");
		$(this).remove();
		return false;
	});
	
}

//-----------------------------------------------------------
// These functions handle the sponsor logos on the home page
//-----------------------------------------------------------
var sponsors = new Array();
//sponsors[x] = Array( <alt attribute> , <img src> );
//sponsors[x] = Array("Hospira","/_Images/sponsors/Hospira.gif");
sponsors[0] = Array("Apotex","/_Images/sponsors/Apotex.gif");
sponsors[1] = Array("Pharmaceutical Partners of Canada Inc.","/_Images/sponsors/PPC.gif");
sponsors[2] = Array("Teva","/_Images/sponsors/Teva.gif");
sponsors[3] = Array("Sandoz","/_Images/sponsors/Sandoz.gif");
sponsors[4] = Array("GlaxoSmithKline","/_Images/sponsors/Glaxo.gif");
sponsors[5] = Array("Amgen","/_Images/sponsors/Amgen.gif");
sponsors[6] = Array("AstraZeneca","/_Images/sponsors/AstraZeneca.gif");
//sponsors[x] = Array("Wyeth","/_Images/sponsors/Wyeth.gif");
//sponsors[x] = Array("Eli Lilly","/_Images/sponsors/Lilly.gif");
//sponsors[x] = Array("Servier","/_Images/sponsors/Servier.gif");
//sponsors[x] = Array("Pfizer","/_Images/sponsors/Pfizer.gif");
//sponsors[x] = Array("Novartis","/_Images/sponsors/Novartis.gif");
//sponsors[x] = Array("Sanofi Aventis","/_Images/sponsors/Sanofi.gif");
//sponsors[x] = Array("Schering-Plough","/_Images/sponsors/Schering.gif");
//sponsors[x] = Array("Abbott","/_Images/sponsors/Abbott.gif");
//sponsors[x] = Array("GlaxoSmithKline","/_Images/sponsors/Glaxo.gif");


 // * files in _Images/sponsors/ folder should match array *

function MW_sponsor_swap(pickrandom) {
	if ($("#sponsors").length > 0) { // make sure the sponsor area exists on the page
		var newsponsor, newsrc, newalt;
		
		// choose the next sponsor to show either randomly or sequentially
		if (pickrandom) {

			do { // pick a new sponsor logo
				newsponsor = Math.floor(Math.random() * sponsors.length);
				newalt = sponsors[newsponsor][0];
				newsrc = sponsors[newsponsor][1];
			} while ($("#sponsors #logo").attr("src") == newsrc) // make sure it's not the same as the last one

		} else {
			if ($("#sponsors #logo[sponsornum]").length == 0) { // we need to find the current number
				testnum = 0;
				while (($("#sponsors #logo").attr("src") != sponsors[testnum][1]) && (testnum < sponsors.length)) {
					testnum++;
				}
				$("#sponsors #logo").attr("sponsornum",testnum);
			}
			
			cursponsor = $("#sponsors #logo").attr("sponsornum");
			
			newsponsor = (parseInt(cursponsor) + 1) % sponsors.length;
			$("#sponsors #logo").attr("sponsornum",newsponsor);
			
		}
		
		newalt = sponsors[newsponsor][0];
		newsrc = sponsors[newsponsor][1];

		$("#sponsors #logo").attr("src",newsrc).attr("alt",newalt); // update the image
	}
}

function MW_establish_sponsor_spinner() {
	if ($("#sponsors").length > 0) { // make sure the sponsor area exists on the page
		setInterval('MW_sponsor_swap(false)', 4000); // set the interval to swap the logos
	}
}

/* Get flyouts working in IE */
sfHover = function() {
	var sfEls = document.getElementById("mainnav").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);
/* End flyout code */

$(document).ready( function() {
	MW_establish_link_types();
	MW_establish_sponsor_spinner();
});
