function oneLevelmenuHorizontalFlash(){
	if($('#menu').length != 0){
		function resizeNavigation() { return false; }
		function calculateClipping() { return false; }
		$('#menu').prepend('<div id="flash-navigation"></div>');
		var so_lf = new SWFObject("swf/navigation.swf", "flash-nav", "100%", "42px", "8", "#ffffff");
		so_lf.addParam("wmode", "transparent");
		so_lf.addParam("scale", "noscale");
		so_lf.addParam("salign", "tl");
		so_lf.addVariable("containerID", "flash-navigation");
		if (typeof PAGE_ID == "undefined") {
			PAGE_ID = "protocol";
		}
		so_lf.addVariable("activeEntryID", PAGE_ID);
		so_lf.addVariable("navXML", "xml/navXML.xml");
		so_lf.write("flash-navigation");
	}
}

var ZINDEX_ACTIVE = 1000;
var ZINDEX_INACTIVE = 100;

if (document.attachEvent) {
	if($('#menu.generated').length > 0) {
		/*functions for internet explorer*/
		function resizeNavigation(divid, options){
			if ($('#menu.generated').length > 0) {
				var $flyoutnav = $("#" + divid);
				var $flyoutnav_parent = $flyoutnav.parents(".flyoutnav_positioner:first");
				var newZIndex = ZINDEX_ACTIVE;
				var newHeight;
				if (options.height) {
					newHeight = options.height;
					if ($("#stage-zone").length && newHeight == 42) 
						newHeight = 41; // hide bottom border
					$flyoutnav.height(newHeight);
				}
			}
		}
	}
} else {
	/*functions for all browsers except IE*/
	resizeNavigation = function(divid, options){
		var $flyoutnav = $("#"+divid);
		var $flyoutnav_parent = $flyoutnav.parents(".flyoutnav_positioner:first");
		if(options){
			if($('#menu.generated').length > 0) {
				calculateClipping($flyoutnav, $flyoutnav_parent, options);
			}
		}
	}
}

function calculateClipping($flyoutnav, $flyoutnav_parent, options){
	if($('#menu.generated').length > 0) {
		var newWidth = $flyoutnav_parent.width();
		var newHeight = $flyoutnav_parent.height();
		var newOffsetTop = 0;
		var newOffsetLeft = 0;
		if(options){
			newWidth = options.width ? options.width : $flyoutnav_parent.width();
			newHeight = options.height ? options.height : $flyoutnav_parent.height();
			if(options.valign && options.valign == "bottom" && $flyoutnav.hasClass("open")){
				newOffsetTop = $flyoutnav.height() - $flyoutnav_parent.height();
				newHeight= $flyoutnav_parent.height();
			}

			if(options.align && options.align == "right" && $flyoutnav.hasClass("open")){
				newOffsetLeft = $flyoutnav.width() - $flyoutnav_parent.width();
				newWidth= $flyoutnav_parent.width();
			}
		}

		$flyoutnav.css({clip:"rect("+newOffsetTop+"px, "+newWidth+"px, "+newHeight+"px, "+newOffsetLeft+"px)"});
		if($("#stage-zone").length && newHeight == 42) newHeight = 41; // hide bottom border
		$flyoutnav.height(newHeight);
	}
}

// ----------------------------------------------------------------------------------------------------------------------

(function() {
	var Trials = {
		init: function() {

			$('.scroll-pane').jScrollPane({"scrollbarWidth": 5, "dragMinHeight": 15});
			sIFR.replaceElement("h1", named({sFlashSrc: "sifr/imagottmed.swf", sColor: "#333333"}));
			sIFR.replaceElement("h3.billboard-title", named({sFlashSrc: "sifr/dev_minion_bold_italic.swf", sColor: "#333333", sWmode: "transparent"}));
			sIFR.replaceElement("h3.heading-title", named({sFlashSrc: "sifr/dev_minion_bold_italic.swf", sColor: "#333333", sWmode: "transparent"}));
			sIFR.replaceElement("h3.heading-subtitle", named({sFlashSrc: "sifr/dev_minion_bold_italic.swf", sColor: "#666666", sWmode: "transparent"}));
			sIFR.replaceElement("h4", named({sFlashSrc: "sifr/dev_minion_bold_italic.swf", sColor: "#666666", sWmode: "transparent" }));
			sIFR.replaceElement(".menu-bottom li", named({sFlashSrc: "sifr/imago.swf", sColor: "#666666", sWmode: "transparent", sLinkColor:"#666666", sBgColor:"#FFFFFF", sHoverColor:"#2a7fd4"}));
			sIFR.replaceElement("h3.news-title", named({sFlashSrc: "sifr/dev_minion_bold_italic.swf", sColor: "#ffffff", sWmode: "transparent"}));

			this.initSelectMenus();
			this.initContactForm();
			this.confirmEvilReset();
			this.validateContactForm();
			this.changeFontSize();
			this.studyResultFixTable();
			oneLevelmenuHorizontalFlash();
			
			
		},
		
		studyResultFixTable: function() {
			$('body.studyResultGet #main-content div.trial-details dd table').parents('dl').addClass('table-inside');
		},
		
		initSelectMenus: function() {
			$('.select-menu li ul').hide();
			$('a.head').toggle(function() {
				$(this).addClass('active').next().slideDown('fast',
					function(){
						Trials.updateIE();
					}
				);
				return false;
			},
			function() {
				$(this).removeClass('active').next().slideUp('fast',
					function(){
						Trials.updateIE();
					}
				);
				return false;
			});
		},
		
		initContactForm: function() {
			//if($("#sendMail").length > 0) {
			//	$('.contact .button').hide(); /* hack */
			//}
			
			$('option[value="Please Select"]').attr("value", "");
			
			var activeSubject = 0;
			var trialSubjects = [];
				trialSubjects[0] = ['Enrolling a patient in a clinical trial','Becoming a site investigator','Trial results'];
				trialSubjects[1] = ['Enrolling in this clinical trial for myself','Enrolling a family member into this clinical trial','Enrolling a friend into this clinical trial','Trial results'];
			var contactSubjects = [];
				contactSubjects[0] = ['Enrolling a patient in a clinical trial','Becoming a site investigator','Trial results', 'All other inquiries'];
				contactSubjects[1] = ['Enrolling in a clinical trial for yourself or someone else', 'Trial results'];

			$('.contact-form select.you-are option:eq(0)').attr("selected", "selected");
			$('.contact-form select.you-are').change(function() {
				var optionSelected = $('option:selected', this);
				var index = $('option', this).index(optionSelected);
				
				$('dl.subject').show();
				$('select.subject').empty();

				var idx = index-1;

				$('select.subject').append('<option value="">Please Select</option>');
				for (i in contactSubjects[idx]) {
					$('select.subject').append('<option value="'+contactSubjects[idx][i]+'">'+contactSubjects[idx][i]+'</option>');
				}
				
				//$('#trials-for-contact').hide();
				//$('#protocols-for-contact').hide();
				$('dl.contact-detail').hide();
				$('#all-inquiry').hide();

				if(index != 0) {
					$('.contact .button').show();
				} else {
					$('.contact .button').hide();
				}

				if (!index) {
					$('dl.subject').hide();
				}
				activeSubject = index;
			});
			
			$('.contact-form select.subject').change(function() {
				var optionSelected = $('option:selected', this);
				var index = $('option', this).index(optionSelected);
				Trials.updateIE();
				

				/* if (activeSubject == 2) {
					if (index == 2) {
						$('#trials-for-contact').show();
						$('#protocols-for-contact').hide();
					} else if (index == 1) {
						$('#trials-for-contact').hide();
						$('#protocols-for-contact').show();	
					} else {
						$('#trials-for-contact').hide();
						$('#protocols-for-contact').hide();	
					}
					$('.contact .button').hide();
					return;
				} */

				$('#all-inquiry').hide();
				$('.contact .button').show();
				$('dl.contact-detail').show();
				//$('#trials-for-contact').hide();
				//$('#protocols-for-contact').hide();
				
				if (index == 4 && activeSubject == 1) {
					$('#all-inquiry').show();
					$('dl.contact-detail').hide();
					$('.contact .button').hide();
				}				
			});
			
			
			
			// sidebar form
			
			//IE hack
			var nastyIE = "";
			/*@cc_on
			nastyIE = "nasty-ie";  
			@*/
			
			$('.trial-contact select.you-are, .trial-contact select.subject').prepend('<option value="" selected="selected" class="'+nastyIE+'">Please Select</option>');
			
			//IE hack
			$('.trial-contact select.you-are option.nasty-ie, .trial-contact select.subject option.nasty-ie').attr('selected', 'selected');
			
			$('.trial-contact select.you-are').change(function () {
				var optionSelected = $('option:selected', this);
				var index = $('option', this).index(optionSelected);
				Trials.updateIE();
				
				if (activeSubject) {
					$('dl.subject'+activeSubject).hide();
				}
				activeSubject = index;

				$('dl.subject'+index).show();
				$('select.subject').empty();

				var idx = index-1;
				$('select.subject').append('<option value="">Please Select</option>');
				for (i in trialSubjects[idx]) {
					$('select.subject').append('<option value="'+trialSubjects[idx][i]+'">'+trialSubjects[idx][i]+'</option>');
				}
			});

			if ($.trim($('input.email').val()) != "") {
				$('dl.contact-detail, dl.subject').show();
			}


		//hack for point 3 from TRIALS-189
		/*
    $('.trial-contact select').mouseover(function() {
			$(this).hide().show();
     	$(this).data("origWidth", $(this).css("width")).css("width", "400px");
     }).hide().show().mouseout(function() {
      $(this).css("width", $(this).data("origWidth"));
     }).hide().show();
     */
		},
		
		confirmEvilReset: function() {
			$('form.contact-form input.evil, form.trial-contact input.evil').click(function() {
				var status = confirm('All data will be cleared!');
				
				if(!status) {
					return false;
				}
				
				$('form.contact-form dd span.error').remove();
				$('form.contact-form dd input, form.contact-form dd select').removeClass('error valid');
			});
		},
		validateContactForm: function() {
			$('form.validate').validate({ 
				errorElement: 'span'
				/*
				onfocusout: false,
				onkeyup: false,
				onclick: false 
				*/
			});
		},
		changeFontSize: function() {
			var sizeLevel = 1;
			var cookieLevel = $.cookie('fontSize');
			if (cookieLevel) {
				sizeLevel = (+cookieLevel != 1 && +cookieLevel != 2 && +cookieLevel != 3) ? 1 : cookieLevel;
				Trials.setFontSize(sizeLevel, 1);
			}
			$('#increase-font-size').click(function() {
				if (++sizeLevel <= 3) {
					Trials.setFontSize(sizeLevel, -1);
				} else {
					--sizeLevel;
				}
				return false;
			});
			$('#decrease-font-size').click(function() {
				if (--sizeLevel >= 1) {
					Trials.setFontSize(sizeLevel, 1);
				} else {
					++sizeLevel;
				}
				return false;
			});
		},
		setFontSize: function(level, up) {
			$('#content').removeClass('level-'+(level+up)).addClass('level-'+level);
			$('#footer').removeClass('level-'+(level+up)).addClass('level-'+level);
			
			if (+level >= 3) {
				$('#increase-font-size').addClass('plus-disabled');
				$('#decrease-font-size').removeClass('minus-disabled');
			} else if(+level <= 1) {
				$('#decrease-font-size').addClass('minus-disabled');
				$('#increase-font-size').removeClass('plus-disabled');
			}
			$('.scroll-pane').jScrollPane({"scrollbarWidth": 5, "dragMinHeight": 15});

			$.cookie('fontSize', level, { path: '/', expires: 100});
			Trials.updateIE();
		},
		updateIE: function() {
			if (document.attachEvent) {
				$('body').toggleClass('updateIE');
			}
		}
	};

	$().ready(function() {
		Trials.init();
		if($('form#sendMail')[0]) {
			Trials.updateIE();
		}
	});
})();
