$(document).ready(function() 
{
	// 1st level navigation
	// 2nd level navigation
	var openTimer1 = 0;
	var closeTimer1 = 500;
	var menuItem1 = 0;
		
	function openSub1() 
	{
		canceltimer1();
		closeSub1();
		menuItem1 = $(this).find('ul.sub1');
		if(menuItem1) {
			menuItem1.show();
		}
		if($(this).attr("class").indexOf("current") == -1) 
		{
			$(this).mouseover(function()  {			
				$(this).addClass('current');
			});
			$(this).mouseout(function()  {
				$(this).removeClass('current');
			});
		}
		
	}
	function closeSub1() 
	{
		if(menuItem1) {
			menuItem1.hide();
		}
	}
	function timer1() {  
		closeTimer1 = window.setTimeout(closeSub1, openTimer1);
	}
	function canceltimer1()
	{  
		if(closeTimer1)
		{  
			window.clearTimeout(closeTimer1);
			closeTimer1 = null;
		}
	}	
	
	// 3rd level navigation
	var openTimer2 = 0;
	var closeTimer2 = 500;	
	var menuItem2 = 0;
	
	function openSub2() 
	{
		canceltimer2();
		closeSub2();		
		menuItem2 = $(this).find('ul.sub2');
		if(menuItem2) {
			menuItem2.show();
		}
	}
	function closeSub2() 
	{
		if(menuItem2) {
			menuItem2.hide();
		}
	}
	function timer2() 
	{  
		closeTimer2 = window.setTimeout(closeSub2, openTimer2);
	}
	function canceltimer2()
	{  
		if(closeTimer2)
		{  
			window.clearTimeout(closeTimer2);
			closeTimer2 = null;
		}
	}	
	
	// init 1st level navigation
	// init 2nd level navigation
	$('#navigationMain02 > ul.top > li').bind('mouseover', openSub1);
	$('#navigationMain02 > ul.top > li').bind('mouseout',  timer1);
	
	// init 3rd level navigation
	$('#navigationMain02 > ul.top > li > ul.sub1 > li').bind('mouseover', openSub2);
	$('#navigationMain02 > ul.top > li > ul.sub1 > li').bind('mouseout',  timer2);	
	

	
	// schaltet alles aus wenn der browser nicht ie ist
	if(!$.browser.msie) {
		$('.showOnlyIE').css('display','none');
	}
	
	
	
	// headerSearchField
	$("#headerSearchField").autocomplete(locationsAutocomplete, {
		width:243,
		selectFirst: false,
		formatItem: function(item) {
			if(typeof item.province != 'undefined' && item.province != '') {
				return item.name+' ('+item.province+')';
			} else {
				return item.name+' ('+item.countryName+')';
			}
		}
	}).result(function(event, item) {
		if(item.md5) {
			document.location.href = '/wetter/vorhersage/aktuell/'+item.countryNameSeo+'/'+item.nameSeo+'/md5/'+item.md5;		
		}
	}).click(function() {
		if($('#headerSearchField').val() == 'Ort, PLZ oder Land') {
			$('#headerSearchField').val('');
		}
	}).blur(function() {
		if($('#headerSearchField').val() == '') {
			$('#headerSearchField').val('Ort, PLZ oder Land');
		}
	});
	$("#headerSearchButton").click(function(){
		$("#headerSearchForm").submit();
	});
	
	
	
	// homeSearchField
	$("#homeSearchField").autocomplete(locationsAutocomplete, {
		width:290,
		formatItem: function(item) {
			if(typeof item.province != 'undefined' && item.province != '') {
				return item.name+' ('+item.province+')';
			} else {
				return item.name+' ('+item.countryName+')';
			}
		}
	}).result(function(event, item) {
		if(item.md5) {
			document.location.href = '/wetter/vorhersage/aktuell/'+item.countryNameSeo+'/'+item.nameSeo+'/md5/'+item.md5;		
		}
	}).click(function() {
		if($('#homeSearchField').val() == 'Ort, PLZ oder Land') {
			$('#homeSearchField').val('');
		}
	}).blur(function() {
		if($('#homeSearchField').val() == '') {
			$('#homeSearchField').val('Ort, PLZ oder Land');
		}
	});
	
	
	
	// premium auswahl
	$('.premiumConfigElement').each(function(i) {
		var current = this;
		
		$(current).mouseover(function()  {			
			$(current).addClass('premiumConfigElementOver');
		});
		$(current).mouseout(function()  {
			$(current).removeClass('premiumConfigElementOver');
		});
	});
	
	
	
	// wegen ie min-height problemen
	// setze die höhe des der article Seiten auf min height
	if($("div.pageArticle").length > 0) {
		if($("div.pageArticle").height() < 666){
			$("div.pageArticle").height(666);
		}
	}
});

/**
 * agof2sktg
 */
function agof2sktg(agof) {
	var skgt = '';
	var agofFirstLiterals = agof.substring(0,3);
	switch(agof.substring(0,1)) {
	case 'r':
        skgt = 'RedCont';
        if (agofFirstLiterals == 'ruk' || agof == 'ren_tvprog') {
            sktg = 'Service';
        } else if (agofFirstLiterals == 'rer') {
            sktg = 'UnterhaltungGames';
        } else if (agof === 'ren_wwm') {
            sktg = 'Infotainment';
        }
        break;
	case 'u':
        sktg = 'Community';
        break;
	case 'k':
		if (agofFirstLiterals == 'kco') {
			sktg = 'Community';
    	} else {
    		sktg = 'Service';
    	}	
    	break;
	case 's':
		sktk = 'Service';
		break;
	case 'g':
		sktg = 'UnterhaltungGames';
		break;
	case 'e':
		sktg = 'Ecommerce';
		break;
	case 'd':
		sktg = 'Diverses';
		break;
	}
	return sktg;
}

/**
 * reloadIVW
 */
function reloadIVW () {
		if (_location == 'at') {
			var pixel = document.getElementById("oewapixel");
			if (pixel != null) {
		       	var ref = document.referrer;
		        if ((reloadIVW.arguments) && (reloadIVW.arguments.length == 2)) {
		        	var _agof = reloadIVW.arguments[0]; 
		        	var imgurl = 'http://rtl.oewabox.at/cgi-bin/ivw/CP/' + agof2sktg(_agof) + '?r=' + escape(document.referrer) + '&d=' + (new Date()).getTime();
		        } else if ((typeof _initialOEWA) == 'string') {
		        	var imgurl = _initialOEWA + '?r='+escape(document.referrer) + '&d=' + (new Date()).getTime();
		        } else {
		        	var pos = pixel.src.indexOf("?");
		        	var imgurl = pixel.src.substring(0, pos) + '?r='+escape(document.referrer) + '&d='+(new Date()).getTime();
		        }
		        pixel.src = imgurl;
		        return;
		    }
		} 
		var pixel = document.getElementById("ivwpixel");
		if (pixel != null) {
	       	var ref = document.referrer;
	       	if ((reloadIVW.arguments) && (reloadIVW.arguments.length == 2)) {
	       		var _agof = reloadIVW.arguments[0]; 
	       		var _ivw = reloadIVW.arguments[1]; 
	       		var imgurl = "http://rtl.ivwbox.de/cgi-bin/ivw/CP/"+_agof+';'+_ivw+'?r='+ref+'&d=' + Math.round(1000000*Math.random());
	       	} else if (((typeof _initialAGOF) == 'string') && ((typeof _initialIVW) == 'string')) {
	       		var imgurl = "http://rtl.ivwbox.de/cgi-bin/ivw/CP/"+_initialAGOF+';'+_initialIVW+'?r='+ref+'&d=' + Math.round(1000000*Math.random());
	       	} else {
	       		var pos = pixel.src.indexOf("&");
	       		var imgurl = pixel.src.substring(0, pos) + "&d=" + Math.round(1000000*Math.random());
	       		imgurl = imgurl.replace(/r=&/, "r="+ref+"&");
	       	}
	        pixel.src = imgurl;
	    }
}

var suggestPageLink = '';
function suggestPage()
{
	$("#suggestPageLayer").dialog('open');
	
	$("#suggestEmailSender").val('');
	$("#suggestNameSender").val('');
	$("#suggestEmailReciever").val('');
	$("#suggestNameReciever").val('');
	
	$("#suggestEmailSenderError").html('');
	$("#suggestNameSenderError").html('');
	$("#suggestEmailRecieverError").html('');
	$("#suggestNameRecieverError").html('');
	
	$("#suggestPageOk").hide();
	$("#suggestPageError").hide();
	$('#clearSuggestPageLayer').show();
	
	suggestPageLink = suggestPage.arguments[0];
}

/**
 * oeffnet das wettervorhersage druckfenster
 */
function windowWetterdruckinfo() 
{
  var wetterdruckinfo = window.open(windowWetterdruckinfo.arguments[0], 'windowWetterdruckinfo', 'width=700, height=750, scrollbars=yes, resizeable=no');
  wetterdruckinfo.focus();
}