// sIFR Replacement functions

// Home Page
function sIFRreplace() {
	if(typeof sIFR == "function") {
		sIFR.replaceElement(named({sSelector: "#block-block-1 h2,#block-views-whats_new h2,.contact-us h2,#block-block-11 h2", sFlashSrc: "/sites/all/themes/healthtech/sifr/gotham.swf", sColor: "#95a4ff", sBgColor: "#ffffff", sCase: "upper", sWmode: "opaque"}));
		sIFR.replaceElement(named({sSelector: "#block-block-2 h2,#block-views-quote h2,#block-views-tagadelic h2,.documents h2,#block-block-12 h2, .cta h2", sFlashSrc: "/sites/all/themes/healthtech/sifr/gotham.swf", sColor: "#00127b", sBgColor: "#ffffff", sCase: "upper", sWmode: "opaque"}));
		sIFR.replaceElement(named({sSelector: "#block-views-topic_top5 h2,#block-block-3 h2", sFlashSrc: "/sites/all/themes/healthtech/sifr/gotham.swf", sColor: "#0024ff", sBgColor: "#ffffff", sCase: "upper", sWmode: "opaque"}));
		sIFR.replaceElement(named({sSelector: ".topics h2,.content h2,.node h2", sFlashSrc: "/sites/all/themes/healthtech/sifr/gotham.swf", sColor: "#E80000", sBgColor: "#ffffff", sCase: "upper", sWmode: "opaque"}));
		sIFR.replaceElement(named({sSelector: "#block-search-0 h2,.search h2", sFlashSrc: "/sites/all/themes/healthtech/sifr/gotham.swf", sColor: "#ff8383", sBgColor: "#ffffff", sCase: "upper", sWmode: "opaque"}));
		sIFR.replaceElement(named({sSelector: "#block-event-0 h2,#block-event-1 h2,.event h2,.events h2, .video h2", sFlashSrc: "/sites/all/themes/healthtech/sifr/gotham.swf", sColor: "#ab4949", sBgColor: "#ffffff", sCase: "upper", sWmode: "opaque"}));
		sIFR.replaceElement(named({sSelector: ".block-user h2,.user h2,.users h2,.account h2", sFlashSrc: "/sites/all/themes/healthtech/sifr/gotham.swf", sColor: "#0024ff", sBgColor: "#ffffff", sCase: "upper", sWmode: "opaque"}));
		sIFR.replaceElement(named({sSelector: "#block-views-News h2,.news h2", sFlashSrc: "/sites/all/themes/healthtech/sifr/gotham.swf", sColor: "#a30000", sBgColor: "#ffffff", sCase: "upper", sWmode: "opaque"}));
		sIFR.replaceElement(named({sSelector: ".about h2,.admin h2,.category h2", sFlashSrc: "/sites/all/themes/healthtech/sifr/gotham.swf", sColor: "#000000", sBgColor: "#ffffff", sCase: "upper", sWmode: "opaque"}));
	};
}

// Top 5 Block voodoo
function top5_initCallback(carousel) {
	var wrapper = document.createElement("div"); // create div to populate
	$(wrapper).attr("id","top5feature"); // give it ID
	var ul = document.createElement("ol"); // create ol
	var li = document.createElement("li"); // create li
	
	$('#block-views-topic_top5 .module .mid .copy').height(245);
	$('#block-views-topic_top5 .content').append(wrapper); //append div to our block
	$('#top5feature').append(ul); //append ul to our dive
	$('#top5feature ol').append(li); //append li to our ul
	$('#mycarousel li:first').addClass("selected")
	$('#top5feature ol li').append($('#mycarousel li:first').html()); //add the right HTML text from the first li
	
	// Add click events to update HTML in the featured area
	$('.jcarousel-item-vertical').bind('click', function() {
		var txt = $(this).html();
		$('.jcarousel-item-vertical').removeClass("selected");
		$(this).addClass("selected");
		var iIndex = $(this).attr('jcarouselindex');
		$('#top5feature ol').attr("start", iIndex);
		$('#top5feature ol li').html(txt);
	});
	
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
}

function makeTop5() {
	if(typeof ($('#mycarousel').jcarousel) == "function") {
		if ($.browser.safari) {
			return;
		}
		$(document).ready(function() {		
			$('#mycarousel').jcarousel({
				scroll: 1,
				vertical: true,
				auto: 3,
				initCallback: top5_initCallback
			});
		});
	}
}


// Expandable footer action
function footer() {
	var offset = $('#footer').offset();

	var iOrigWidth = $('#footer').width();
	var iOrigY = offset.top;
	var bExpanded = false;
	var closeBtn = document.createElement("span");
	$(closeBtn).attr({
		id: "closeBtn",
		title: "Close" 
	}).text("Close").bind('click', collapseFooter);
	$(closeBtn).prependTo("#footer .copy").hide();
	
	$('#footer').css({position: "absolute", top: iOrigY, width: iOrigWidth});

	$('#footer .footerToggles li').bind('click', expandFooter).hover(
		function() {
			$(this).addClass('over');
		},
		function() {
			$(this).removeClass('over');
		}
	);

	function expandFooter() {
		if (!bExpanded) {
			$('#footer').animate({
				"top": "-=242",
				height: 290
			});
			$(closeBtn).show();
			$("#footer .footerMenus").show();
			$(this).addClass('on');
			bExpanded = true;
		}
	} 

	function collapseFooter() {
		$('#footer').animate({
			"top": "+=242",
			height: 47
		});
		$(closeBtn).hide();
		$("#footer .footerMenus").hide();
		$('#footer .footerToggles li').removeClass('on over');
		bExpanded = false;
	}
}

// Helper class for dropdowns
function dropHover() {
	$('#primaryNav li.expanded,#primaryNav li.leaf').mouseover(function() {
		$(this).children("a:first").addClass('over');
	});
	$('#primaryNav li.expanded,#primaryNav li.leaf').mouseout(function() {
		$(this).children("a:first").removeClass('over');
	});
}

// Advanced search link
if (window.location.hash == "#wrapper") {
	$(document).ready(function() {
		Drupal.toggleFieldset(".search-advanced");
	});
}

// Primary Initiation
$(document).ready(function() {
	sIFRreplace();
	dropHover();
	makeTop5();
});

$(window).load(function () {
// Use window.load in order to allow for image loading...
	footer();
});


