var loadedJS = '';

function safeLoadJS(filename) {
	if (loadedJS.indexOf(filename) == -1) {
		//alert('loading: ' + filename);
		var thescript = document.createElement('script');
		thescript.setAttribute('type','text/javascript');
		thescript.setAttribute('src',filename);
		document.getElementsByTagName('head')[0].appendChild(thescript);
		loadedJS += filename;
	} else {
		//alert(filename + ' already loaded');
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
		if (oldonload) {
			oldonload();
		}
		func();
		}
	}
}

/*
jQuery.fn.hcae = function( fn, args ) {
        return jQuery.hcae( this, fn, args );
};

$(
jQuery.hcae = function( obj, fn, args ) {
        if ( obj.length == undefined )
                for ( var i in obj )
                        fn.apply( obj[i], args || [i, obj[i]] );
        else
                for ( var i = obj.length - 1; i >= 0; --i )
                        fn.apply( obj[i], args || [i, obj[i]] );
        return obj;
};
*/

function layoutL2() {
	/*
	jQuery.fn.reverse = function() {
		return this.pushStack(this.get().reverse(), arguments);
	};
	*/

	if ($('#topNavL2 li').length <= 1) {
		return;
	}
	
	$('#topNavL2').show();
	
	total = $('#topNavL2 a.TopNavL2').length + 1;
	
	var width = 0;
	
	$('#topNavL2 li').each(function(i) {
		width += $(this).width();
	});		
	
	var first = $('#topNavL2 li:first-child');

	var delta = 40;
	
	if (width > 538) {
		$('#topNavL2 li').each(function(i) {
			if ($(this).attr("class") == "TopNavL2sel_current") {
				delta += 18;
			}
			if ($(this).attr("class") == "divider") {
				if ($(this).position().left > first.position().left + width / 2 + delta) {
					$(this).removeClass("divider");
					$(this).html('<br/>');
					return false;
				}
			}
		});		
	}
	
	$('#topNavL2').width(758);
	//$('#topNavL2').show();
}


$(function(){
	if ($("a.mediagallery").length > 0) {
		$("a.mediagallery").fancybox({
			//'zoomSpeedIn': 300, 
			//'zoomSpeedOut': 300, 
			/*'overlayShow': false */
			'frameWidth': 1000,
			'frameHeight': 655,
			'overlayOpacity': .5,
			'centerOnScroll': false,
			'hideOnContentClick': false
			/*,
			'callbackOnShow': function() {
				$('div#fancy_inner').get(0).style.removeAttribute('filter');
			}*/
		});
	}
});

function stripHTML(string) { 
    return string.replace(/<(.|\n)*?>/g, ''); 
}

//fl20090529 - These one fix the !ClearType in IE(7)
jQuery.fn.fadeIn = function(speed, callback) { 
	return this.animate({opacity: 'show'}, speed, function() { 
		if (jQuery.browser.msie)  
			this.style.removeAttribute('filter');  
		if (jQuery.isFunction(callback)) 
			callback();  
	}); 
}; 
	
jQuery.fn.fadeOut = function(speed, callback) { 
	return this.animate({opacity: 'hide'}, speed, function() { 
		if (jQuery.browser.msie)  
			this.style.removeAttribute('filter');  
		if (jQuery.isFunction(callback)) 
			callback();  
	}); 
}; 
	
jQuery.fn.fadeTo = function(speed,to,callback) { 
	return this.animate({opacity: to}, speed, function() { 
		if (to == 1 && jQuery.browser.msie)  
			this.style.removeAttribute('filter');  
		if (jQuery.isFunction(callback)) 
			callback();  
	}); 
};

function DisableClientSideValidation() {
	if(window.ValidatorEnable) {
		//alert("disabling client side validation");
		$('[controltovalidate]').each(function(i) {
			ValidatorEnable(eval($(this).attr("id")), false);
		});		
	}
}

function getQueryParam(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}