var iCurIdx = 0;
var oAnmTmr;
var bInAnm = false;

function animateHomePanels()
{
	if (!bInAnm)
	{
		bInAnm = true;
		var iNewIdx = (iCurIdx + 1 > 3 ? 0 : iCurIdx + 1);

		// Fade-swap large image from iCurIdx to iNewIdx
		$("#hpPhotoBox div:eq("+iCurIdx+")").fadeOut('fast',function(){
			$("#hpPhotoBox div:eq("+iNewIdx+")").fadeIn('fast',function(){});
		});

		// Animate left position of slider over to new position
		var oNewPos = $(".hpnav-block:eq("+iNewIdx+")").position();
		var iOffSet = (oNewPos.left - $(".hpnav-block:eq("+iCurIdx+")").position().left);				
		$(".nav-control .nav-slider").animate({	left: '+='+iOffSet },100, function(){});

		$("#tabset-panels div.content-box:eq("+iCurIdx+")").fadeOut('fast', function(){
			$("#tabset-panels div.content-box:eq("+iNewIdx+")").fadeIn('fast', function(){
				iCurIdx = iNewIdx;
				bInAnm = false;
				oAnmTmr = setTimeout("animateHomePanels()", 8000);
			});
		});				
	}
}

$(document).ready( function()
{
	function initPage()
	{
		clearFormFields({
			clearInputs: true,
			clearTextareas: true,
			passwordFieldText: false,
			addClassFocus: "focus",
			filterClass: "default"
		});
	}
	function clearFormFields(o)
	{
		if (o.clearInputs == null) o.clearInputs = true;
		if (o.clearTextareas == null) o.clearTextareas = true;
		if (o.passwordFieldText == null) o.passwordFieldText = false;
		if (o.addClassFocus == null) o.addClassFocus = false;
		if (!o.filterClass) o.filterClass = "default";
		if(o.clearInputs) {
			var inputs = document.getElementsByTagName("input");
			for (var i = 0; i < inputs.length; i++ ) {
				if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
					inputs[i].valueHtml = inputs[i].value;
					inputs[i].onfocus = function ()	{
						if(this.valueHtml == this.value) this.value = "";
						if(this.fake) {
							inputsSwap(this, this.previousSibling);
							this.previousSibling.focus();
						}
						if(o.addClassFocus && !this.fake) {
							this.className += " " + o.addClassFocus;
							this.parentNode.className += " parent-" + o.addClassFocus;
						}
					}
					inputs[i].onblur = function () {
						if(this.value == "") {
							this.value = this.valueHtml;
							if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
						}
						if(o.addClassFocus) {
							this.className = this.className.replace(o.addClassFocus, "");
							this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
						}
					}
					if(o.passwordFieldText && inputs[i].type == "password") {
						var fakeInput = document.createElement("input");
						fakeInput.type = "text";
						fakeInput.value = inputs[i].value;
						fakeInput.className = inputs[i].className;
						fakeInput.fake = true;
						inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
						inputsSwap(inputs[i], null);
					}
				}
			}
		}
		if(o.clearTextareas) {
			var textareas = document.getElementsByTagName("textarea");
			for(var i=0; i<textareas.length; i++) {
				if(textareas[i].className.indexOf(o.filterClass) == -1) {
					textareas[i].valueHtml = textareas[i].value;
					textareas[i].onfocus = function() {
						if(this.value == this.valueHtml) this.value = "";
						if(o.addClassFocus) {
							this.className += " " + o.addClassFocus;
							this.parentNode.className += " parent-" + o.addClassFocus;
						}
					}
					textareas[i].onblur = function() {
						if(this.value == "") this.value = this.valueHtml;
						if(o.addClassFocus) {
							this.className = this.className.replace(o.addClassFocus, "");
							this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
						}
					}
				}
			}
		}
		function inputsSwap(el, el2) {
			if(el) el.style.display = "none";
			if(el2) el2.style.display = "inline";
		}
	}

	// IMM060911 Setup main nav hover events >>>
	$("#nav > li").hover( function()
	{
		$(this).siblings('.persist-hover').removeClass('persist-hover');
		$(this).addClass("hover").addClass("persist-hover")
	}, function()
	{
		$(this).removeClass("hover"); 
		var that = this;
		setTimeout(function(){
			turnLiOff(that);
		},500)
	});
	$("#nav .drop ul li").mouseover( function()
	{
		$("ul",this).addClass("leftshow");
	});
	$("#nav .drop ul li").mouseout( function()
	{
		$("ul",this).removeClass("leftshow");
	});
	// <<< IMM060911

	// IMM061311>>>
	$("#sharetools ul li.print").click( function() 
	{
		 window.print();
	});
	// <<<IMM061311

	//SMc061311>>>>
	function turnLiOff(elem){
		var that = elem;
		//console.log('Function');
		if(!$(that).hasClass("hover")){
			//console.log($('strong',that).html());
			$(that).removeClass("persist-hover");
		}
		
	}
	//<<<SMc061311
	

	// IMM060411 Setup homepage banner navigation >>>
	if ($(".tabset-box .description .nav-tabset").length > 0)
	{
		oAnmTmr = setTimeout("animateHomePanels()", 5000);

		function switchHomePanels( oHdl, iNewIdx )
		{
			if ((iNewIdx != iCurIdx) && (!bInAnm))
			{
				bInAnm = true;

				// Fade-swap large image from iCurIdx to iNewIdx
				$("#hpPhotoBox div:eq("+iCurIdx+")").fadeOut('fast',function(){
					$("#hpPhotoBox div:eq("+iNewIdx+")").fadeIn('fast',function(){});
				});

				// Animate left position of slider over to new position
				var oNewPos = $(".hpnav-block:eq("+iNewIdx+")").position();
				var iOffSet = (oNewPos.left - $(".hpnav-block:eq("+iCurIdx+")").position().left);				
				$(".nav-control .nav-slider").animate({	left: '+='+iOffSet },100, function(){});

				$("#tabset-panels div.content-box:eq("+iCurIdx+")").fadeOut('fast', function(){
					$("#tabset-panels div.content-box:eq("+iNewIdx+")").fadeIn('fast', function(){
						iCurIdx = iNewIdx;
						bInAnm = false;
					});
				});				
			}
		}

		var spanClick = $(".hpnav-block").click( function()
		{
			clearTimeout(oAnmTmr);
			switchHomePanels( $(this), spanClick.index(this) );
		});
	}
	// <<< IMM060411 Setup homepage banner navigation
	
	
	
	// BIOCLINICA 09092011 Adding jquery UI >>>
	
	// Accordion
	$("#uiAccordion").accordion({ header: "h3" });

	// Tabs
	$('#tabs').tabs();


	// Dialog			
	$('#uiDialog').dialog({
		autoOpen: false,
		width: 600,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close"); 
			}, 
			"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
	
	// Dialog Link
	$('#uiDialog_link').click(function(){
		$('#dialog').dialog('open');
		return false;
	});

	// Datepicker
	$('#uiDatepicker').datepicker({
		inline: true
	});
	
	// Slider
	$('#uiSlider').slider({
		range: true,
		values: [17, 67]
	});
	
	// Progressbar
	$("#uiProgressbar").progressbar({
		value: 20 
	});
	
	//hover states on the static widgets
	$('#uiDialog_link, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
	
	// <<< BIOCLINICA 09092011 Adding jquery UI
	
	
	

	
});






// BIOCLINICA 11042011 Adding image cycle >>>
(function($) {

	$(document).ready(function(){
			$('#myslides').cycle({
				fx: 'fade',
				speed: 5000,
				timeout: 2000
			});
		}); 
			
 
})(jQuery); 

// <<< BIOCLINICA 11042011 Adding image cycle 
