$(document).ready(function() {
	// homepage rotating banner images					   
	$('#herobanner').before('<div id="nav">').cycle({
			fx:'fade',
			speed:900,
			timeout:6000,
			pager: '#nav'
		});

	// MERMA news					   

	$('.VC-tabModule').tabs({
		load: function(event, ui) {
			$('a', ui.panel).click(function() {
				$(ui.panel).load(this.href);
				return false;
			});
		}
	});

	var pageURL = window.location.href;
	
	var parameter = null;
	var parameterExists = false;
	
	if(pageURL.indexOf("?") != -1){
		//get the location of the ?
		//The parameter is in the form 20100216_0 
		var parameterIndex = pageURL.indexOf("?") +4;
		//Returns the parameter
		var parameter = pageURL.substring(parameterIndex, pageURL.length);

		//get the year from the parameter (the first four characters)
		var year = pageURL.substring(parameterIndex, parameterIndex+ 4);		
		//check that the parameter is in the right form
		var re = new RegExp("\\b\\d{1,8}_");
 
		if (parameter.match(re)) {
			parameterExists = true;
		} else{
		//	nothing
		}
	
		if (parameterExists)
		{
			var iframeSrc =  $("iframe").attr('src');
			var newsSrc = iframeSrc.substring(0, (iframeSrc.length - 8)) + parameter + ".htm";
			// get the iframe with the src that contains the year
			// set the src attribute to the press release from the parameter
			// select it and add the class "selected"
			$("iframe[src*=" + year + "]").attr('src', newsSrc).tabs( { selected: 4 } ).addClass("selected");	
		} else {
		//	nothing
		}
	}

//  add icons to links
	// Add pdf icons to pdf links
	$("a[href$='.pdf']").addClass("pdf");
	// Add Word icons to word links (doc, rtf, txt)
	$("a[href$='.doc'], a[href$='.docx']").addClass("doc");
	// Add Excel icons to Excel links (xls, xlsx)
	$("a[href$='.xls'], a[href$='.xlsx']").addClass("excel");
	// Add txt icons to document links (doc, rtf, txt)
	$("a[href$='.txt'], a[href$='.rtf']").addClass("txt");
	// Add zip icons to Zip file links (zip, rar)
	$("a[href$='.zip'], a[href$='.rar']").addClass("zip"); 
	// Add email icons to email links
	$("a[href^='mailto:']").addClass("email");
	//Add external link icon to external links - 
	$('a').filter(function() {
		//Compare the anchor tag's host name with location's host name
	    return this.hostname && this.hostname !== location.hostname;
	  }).addClass("external");
	//You might also want to set the _target attribute to blank
	//$('a').filter(function() {
		//Compare the anchor tag's host name with location's host name
	//   return this.hostname && this.hostname !== location.hostname;
	// }).addClass("external").attr("target", "_blank");

	$('a.popup').bind('click',function(e){
		popwindow(e)
		});

	function popwindow(e){
		e.preventDefault();
		var href = $(e.target).attr('href');
		window.open(href,'','width=800,height=700,scrollbars=yes,toolbar=no,status=no,directories=no');
	}

});

