jQuery(document).ready(/*This function gets loaded when all the HTML, not including the portlets, is loaded.*/
	function() {
	jQuery('div.text strong:contains(Newsmeldung)').remove();
	jQuery('img.icon[title=Web Content überarbeiten]').remove(); //remove Icon to edit articles
	loadInfo(); 
	var load = window.setInterval("loadInfo()", 60000); 
	var scroll = window.setInterval(function() {
		scrollInfo('#' + jQuery("#info_wrapper :first-child").attr('id'));
	}, 10500); 
	
	jQuery("#infobox a#tab_infos").click(function(){ 
		jQuery("#infobox a#tab_webcams").removeClass("active"); 
		jQuery(this).addClass("active"); 
		jQuery("#infobox #webcams").hide(); 
		jQuery("#infobox #infos").show(); 
		jQuery("#infobox .footer").html('<a style="margin-top: 8px; display: inline-block; float: left" href="/verkehrsinformationen">&raquo;&nbsp;weitere Verkehrsinfos</a><a title="Ö3-Verkehrsservice" target="_blank" style="margin-top: 5px; display: inline-block; float: right; margin-right: 9px" href="http://oe3.orf.at/verkehr"><img alt="Ö3 Icon" src="/stat/images/oe3.png" /></a>'); 
		window.clearInterval(scroll); 
		scroll = window.setInterval(function() {
			scrollInfo('#infos');
		}, 10500); 
		window.clearInterval(load); 
		load = window.setInterval("loadInfo()", 60000); 
		return false; 
	}); 
	
	jQuery("#infobox a#tab_webcams").click(function(){ 
		jQuery("#infobox a#tab_infos").removeClass("active"); 
		jQuery(this).addClass("active"); 
		jQuery("#infobox #infos").hide(); 
		jQuery("#infobox #webcams").show(); 
		jQuery("#infobox .footer").html('<a href="/webcams" style="margin-top: 8px; display: inline-block; float: left">&raquo; weitere Webcams</a>'); 
		window.clearInterval(scroll); 
		scroll = window.setInterval(function() {
			scrollInfo('#webcams');
		}, 10500); 
		window.clearInterval(load); 
		/* prepare lightbox */ 
		
	
		jQuery('li.webcam a').colorbox({
			iframe:true, 
			innerWidth: '410px', 
			innerHeight: '420px',
			scrolling:false,
			opacity:0.7, 
			onCleanup:function(){clearInterval(timer);}, 
			title:'Webcam'
		}); 
		//jQuery('li.webcam a').unbind('click.myEvents'); 
		/* remove old click events */ 
		//jQuery('li.webcam a').bind('click.myEvents', function() {
		//	reloadCam(this);
		//}); 
		return false; 
	}); 


	/* display the searchfield in IE6 correctly - have to replace dirty browser detection! */
	if(jQuery.browser.msie && !window.XMLHttpRequest) {
		if((jQuery('#portlet-wrapper-77 input[type=text]')).length > 0) {
			jQuery('div#portlet-wrapper-77 input[type=text]').css({
				'height': '16px',
				'border': 'none',
				'padding': '4px 0px 4px 4px',
				'background': 'none'
			});
			jQuery('#portlet-wrapper-77 form > table:first-child tr td:first-child').css({
				'width': '241px',
				'background': 'url(/asfinag-theme/images/site/searchfield_new_300px.gif) no-repeat scroll 0px 1px transparent'
			});
		}
		if((jQuery('#portlet-wrapper-77 input[type=image]')).length > 0) {
			jQuery('#portlet-wrapper-77 input[type=image]').css({
				'position': 'relative',
				'right': '0px',
				'top': '-2px',
				'top': '0px'
			});
		}
	}
}); 

/*The following code gets the newest press articles and displays them in a widget */ 
var noOfMaxNews = 2; 
jQuery.get('/presse', function(data){ 
	var newsTable = data.match(/<tr class="lfr-template portlet-section-body results-row">.+<\/tr>/g); 
	if(newsTable) {
		var newsRows = newsTable[0].split('</tr>'); 
		var newsMessage = new Array(); 
		var newsDetails = new Array(); 
		var extractExpression = ''; 
		var newsDate = ''; 
		var newsEntry = ''; 
		for(var n = 1; n <= noOfMaxNews; n++) { 
			newsMessage = newsRows[n].split('</td>'); 
			extractExpression = /.+<a\shref=".+">(.+)<\/a>\s*/; 
			if(extractExpression.test(newsMessage[0])) { 
				newsDate = (extractExpression.exec(newsMessage[0]))[1]; 
			} 
			extractExpression = /.+<a\shref="(.+)">(.+)<\/a>\s*/; 
			if(extractExpression.test(newsMessage[1])) { 
				newsDetails = extractExpression.exec(newsMessage[1]); 
			} 
			newsEntry = '<li><span class="date">' + newsDate + '</span><a href="' + newsDetails[1] + '">' + newsDetails[2] + '</a></li>'; 
			jQuery('div.content > ul#presseMeldungen').append(newsEntry); 
		} 
	} } 
); 

/* The following functions are utility functions - not part of the document-ready-function! */ 
function loadInfo(){ 
	jQuery("#info_wrapper").html('<div class="content"><div class="loading">Inhalt wird geladen</div></div>'); 
	jQuery.ajax({ type: "GET", url: "/verkehrsmeldung/", success: function(msg){ 
		var newMsg = msg;
		/*var newMsg = msg.replace(/cgi-bin/g, 'bilder'); 
		newMsg = newMsg.replace(/showcam\.cgi\?cname=/g, ''); 
		newMsg = newMsg.replace(/(http:\/\/data\.vtg\.asfinag\.at\/bilder\/.+-I)/g, '$1'+'.jpg'); */
		jQuery("#info_wrapper").html(newMsg);
		jQuery('div#info_wrapper > div#infos > div.messages > ul:not(:has(li))').html('<br /><br />Derzeit sind uns keine St&ouml;rungen bekannt.');
		//jQuery('div#info_wrapper > div#infos > div.messages > ul:not(:has(li))').html('<br />Aus technischen Gr&uuml;nden k&ouml;nnen wir derzeit keine Verkehrs-meldungen anzeigen und bitten daher um Ihr Verst&auml;ndnis.');
		} 
	}); 
}

function scrollInfo(elementId){ 
	var h = jQuery(elementId+" .messages").height(); 
	var top_px = jQuery(elementId+" .messages").css("top"); 
	if(top_px != null) {
		var top = top_px.substr(0,top_px.indexOf("px")); 
		if( (top*(-1)+126) < h ) { 
			jQuery(elementId+" .messages").animate({top:"-=126px"},1500); 
		} else { 
			jQuery(elementId+" .messages").animate({top:"0"},2000); 
		} 
	}
} 

/* variables for lightbox webcam refresh */
var start_time = (new Date()).getTime();
var act_time; var duration = 30;
var timer;	
function reloadCam(cam) { 
	if(timer != undefined) { 
		clearInterval(timer); 
	} 
	timer = setInterval(function() { 
		act_time = new Date(); 
		if(((act_time.getTime()) - start_time) < (duration * 1000)) { 
			reload = new Date(); 
			reload = "?"+reload.getTime(); jQuery('#cboxPhoto').attr('src', cam.href+reload); 
		} else { 
			clearInterval(timer); 
			jQuery('#cboxPhoto').attr('src', 'http://data.vtg.asfinag.at/bilder/neustart.jpg'); 
			jQuery('#cboxPhoto').click(function(){ 
				start_time = (new Date()).getTime(); 
				reloadCam(cam); 
			}); 
		} 
	},1000);
}

Liferay.Portlet.ready(/* This function gets loaded after each and every portlet on the page. portletId: the current portlet's id; jQueryObj: the jQuery wrapped object of the current portlet */function(portletId, jQueryObj) {});

jQuery(document).last(/* This function gets loaded when everything, including the portlets, is on the page.	*/ 	function() {
	/*  jQuery('div.icon-actions span:first-child').remove(); */  /*remove Icon to edit articles*/
});