$(function()
{	
	/*
	$('ul').each(function()
	{	if ($('li',this).size() === 0)
		{	$(this).remove();
		}
	});

	if ( typeof navlist_active === 'undefined' )
	{	navlist_active = -1;
	}

	$('ul#navlist > li:not(:nth-child('+(navlist_active+1)+'))').removeAttr('id').find('ul:first').hide();
	$('ul#navlist > li > a').click(function()
	{	var p = $(this).parent();
		if ($(p).attr('id')!='active')
		{	$(this).parents('ul').find('li#active').removeAttr('id').find('ul:first').slideUp();
			if ( $(p).attr('class')=='clk' )
			{	return true;
			}
			$(p).attr('id','active').find('ul:hidden').slideDown();
		}
		return $(this).next('ul').length == 0;
	});
	*/

	/*
	$('ul#programmlisting > li').each(function(i){
			if (programmlisting_aktuell == i) {
				$(this).find('li').show();
			$(this).find('a.aktuell').addClass('aktuell');
			} else {
			$(this).find('li').hide();
			$(this).find('a.aktuell').removeClass('aktuell');
		}
		});
	$('ul#programmlisting > li > a').click(function(){
		if ($(this).is('.aktuell')) {
			$(this).parent().find('li').hide();
			$(this).removeClass('aktuell');
		} else {
			$(this).parent().find('li').show();
			$(this).addClass('aktuell');
		}
		return $(this).next('ul').length > 0 ? false : true;
	});
	*/
	
	// Links als Popup aufmachen. An den Link einfach #BREITExHOEHE hängen
	$('a[href*=#]').click(function()
	{	var org = $(this).attr('href');
		if ( org.match(/(.*)#(\d+x\d+)$/) )
		{	var href = RegExp.$1,
				size = RegExp.$2.split('x');
			open(href,'Redrum','width='+size[0]+',height='+size[1]);
			return false;
		}
	});
	
  // Popups (Pokito style mit lang="BREITExHOEHE" und target="_blank"
  $('a[target=_blank][href][lang]').click(function()
  {   var wh =[800,600],
          hr = $(this).attr('href') || '';
      if (! hr)
      {    return false;
      }
      if ( $(this).attr('lang').match(/^\d+x\d+/) )
      {   wh = $(this).attr('lang').split('x');
      }
      var rtl2pop = window.open(hr,'rtl2Popup'+wh.join(''),'resizable=yes,width='+wh[0]+',height='+wh[1]);
      rtl2pop.focus();
      return false;
  });

	
	
	$('.startnewsmiddle,.startnewsright').hide();
	$('#startnewsmiddle_news,#startnewsright_news').show();
	$('#startnewslist > li > a').click(function(){
		$('#startnewslist > li > a').removeClass('startnewscurrent');
		$(this).addClass('startnewscurrent');
		$('#startmehrnews > a').attr('href', $(this).attr('href'));
		$('.startnewsmiddle,.startnewsright').hide();
		$('#startnewsmiddle_' + $(this).attr('name')).show();
		$('#startnewsright_' + $(this).attr('name')).show();
		return false;
	});

	// Yahoo-Suche
	if (typeof(Ytop5) != "undefined" && Ytop5 !== '' )
	{	$('#YT5').attr('src','http://www.'+'rtl2.de/Y_CI/'+Ytop5+'.html');
	}
	// /Yahoo-Suche

	// Klicktracker
	/*$('a[href^=http://]:not([href^='+'http://www.'+'rtl2.de],[href^='+'http://www.'+'rtl2-interaktiv.de])').each(function()
	{	var eL	= $(this).attr('href'),
			eT	= $(this).attr('title'),
			tT	= $(this).text(),
			iA	=	'',
			iT	=	'',
			iS	= '';
		if ($(this).find('img').size()>0)
		{	var im = $(this).find('img:first');
			iA	= im.attr('alt');
			iT	= im.attr('title');
			iS	= im.attr('src').replace(/.*\/(.*)/,'$1');
		}
		var lnkN = eT||iT||iA||iS||'';
		$(this).attr('href','http://www.'+'etracker.de/lnkcnt.php?et=k3Kzyx&url='+escape(eL)+(lnkN?'&lnkname='+escape(lnkN):'')).text(tT);
	});*/
});

function inArray(array,key)
{	for (var i=0; i<array.length; i++)
	{	if (array[i].toLowerCase() == key.toLowerCase())
		{	return true;
		}
	}
	return false;
}


jQuery.fn.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* To get the document params:
* @example value = $(document).getUrlParam("paramName");
* 
* To get the params of a html-attribut (uses src attribute)
* @example value = $('#imgLink').getUrlParam("paramName");
*/
 getUrlParam: function(strParamName){
	  strParamName = escape(unescape(strParamName));
	  
	  var returnVal = new Array();
	  var qString = null;
	  
	  if ($(this).attr("nodeName")=="#document") {
	  	//document-handler
		
		if (window.location.search.search(strParamName) > -1 ){
			
			qString = window.location.search.substr(1,window.location.search.length).split("&");
		}
			
	  } else if ($(this).attr("src")!="undefined") {
	  	
	  	var strHref = $(this).attr("src")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else if ($(this).attr("href")!="undefined") {
	  	
	  	var strHref = $(this).attr("href")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else {
	  	return null;
	  }
	  	
	  
	  if (qString==null) return null;
	  
	  
	  for (var i=0;i<qString.length; i++){
			if (escape(unescape(qString[i].split("=")[0])) == strParamName){
				returnVal.push(qString[i].split("=")[1]);
			}
			
	  }
	  
	  
	  if (returnVal.length==0) return null;
	  else if (returnVal.length==1) return returnVal[0];
	  else return returnVal;
	}
});



//Oster Spiel 2009
function gup(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];
}
function CookieUtil(name, duration, path, domain, secure) {
  this.affix = "";
  if (duration) {      
    var date = new Date();
    var curTime = new Date().getTime();
    date.setTime(curTime + (1000 * 60 * duration));
    this.affix = "; expires=" + date.toGMTString();
  };
  if (path) {
    this.affix += "; path=" + path;
  };
  if (domain) {
    this.affix += "; domain=" + domain;
  };
  if (secure) {
    this.affix += "; secure=" + secure;
  };
  function getValue() {
    var m = document.cookie.match(new RegExp("(" + name + "=[^;]*)(;|$)"));
    return m ? m[1] : null;   
  };
  this.cookieExists = function() {
    return getValue() ? true : false;
  };
  this.expire = function() {
    var date = new Date();
    date.setFullYear(date.getYear() - 1);
    document.cookie=name + "=noop; expires=" + date.toGMTString(); 
  };
  this.setSubValue = function(key, value) {
    var ck = getValue();
    if (/[;, ]/.test(value)) {
      value = window.encodeURI ? encodeURI(value) : escape(value);
    }
    if (value) {
      var attrPair = "@" + key + value;
      if (ck) {
        if (new RegExp("@" + key).test(ck)) {
          document.cookie =
          ck.replace(new RegExp("@" + key + "[^@;]*"), attrPair) + this.affix;
        } else {
          document.cookie =
          ck.replace(new RegExp("(" + name + "=[^;]*)(;|$)"), "$1" + attrPair) + this.affix;
        }
      } else {
        document.cookie = name + "=" + attrPair + this.affix;
      }
   } else {      
      if (new RegExp("@" + key).test(ck)) {
        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), "") + this.affix;
      }
    }
  };
  this.getSubValue = function(key) {
    var ck = getValue();
    if (ck) {
      var m = ck.match(new RegExp("@" + key + "([^@;]*)"));
      if (m) {
        var value = m[1];
        if (value) { 
          return window.decodeURI ? decodeURI(value) : unescape(value);
        }
      }
    }
  };
}
function string_to_hex(s) {
  var r = "0x";
  var hexes = new Array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
  for (var i=0; i<s.length; i++) {r += hexes [s.charCodeAt(i) >> 4] + hexes [s.charCodeAt(i) & 0xf];}
  return r;
}
function rand(min, max) {
  var argc = arguments.length;
  if (argc < 2) {
    min = 0;
    max = 2147483647;
  }
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
var ostergewinnspiel = new CookieUtil('ostergewinnspiel', 60, '/');
if (gup('ostern') == 'jetzt') {
  new CookieUtil('ostergewinnspiel', 60, '/').expire();
  ostergewinnspiel.setSubValue('ostern' , 'jetzt');
} 
if (ostergewinnspiel.cookieExists()) {
  $(document).ready(function() {
		var symbols = new Array(
		  'ca9a906be027febba6e5f7ebdb5f240c',
		  'd9a889aefb18edc084c5f7352149091c',
		  'f711f55a26bda5f0d4c43dfd407e56a6',
		  'e521cfae36ba57754bae4a5b7c36a42f',
		  'd6a1d8890cd6a826e25dd383e7066051',
		  '9059c8caa87be31ed59e14431c667c97',
		  'e46fc3b743217afba6d75b37507e3291',
		  'f4e4aa30674aae97f1e6089023181a1e',
		  'f59165f3c0771593285dd1e6bb50394b',
		  '8180e189813698638b64976c19e557ed'
		);
		var images = {
		  'ca9a906be027febba6e5f7ebdb5f240c': 'http://www.rtl2.de/_ostersuche/10_ostergewinn_motiv.png',
	    'd9a889aefb18edc084c5f7352149091c': 'http://www.rtl2.de/_ostersuche/9_ostergewinn_motiv.png',
	    'f711f55a26bda5f0d4c43dfd407e56a6': 'http://www.rtl2.de/_ostersuche/8_ostergewinn_motiv.png',
	    'e521cfae36ba57754bae4a5b7c36a42f': 'http://www.rtl2.de/_ostersuche/7_ostergewinn_motiv.png',
	    'd6a1d8890cd6a826e25dd383e7066051': 'http://www.rtl2.de/_ostersuche/6_ostergewinn_motiv.png',
	    '9059c8caa87be31ed59e14431c667c97': 'http://www.rtl2.de/_ostersuche/5_ostergewinn_motiv.png',
	    'e46fc3b743217afba6d75b37507e3291': 'http://www.rtl2.de/_ostersuche/4_ostergewinn_motiv.png',
	    'f4e4aa30674aae97f1e6089023181a1e': 'http://www.rtl2.de/_ostersuche/3_ostergewinn_motiv.png',
	    'f59165f3c0771593285dd1e6bb50394b': 'http://www.rtl2.de/_ostersuche/2_ostergewinn_motiv.png',
	    '8180e189813698638b64976c19e557ed': 'http://www.rtl2.de/_ostersuche/1_ostergewinn_motiv.png'
		};
		var links = {
		  'ca9a906be027febba6e5f7ebdb5f240c': 'http://www.rtl2-interaktiv.de/ostergewinnspiel/?symbol=ca9a906be027febba6e5f7ebdb5f240c',
	    'd9a889aefb18edc084c5f7352149091c': 'http://www.rtl2-interaktiv.de/ostergewinnspiel/?symbol=d9a889aefb18edc084c5f7352149091c',
	    'f711f55a26bda5f0d4c43dfd407e56a6': 'http://www.rtl2-interaktiv.de/ostergewinnspiel/?symbol=f711f55a26bda5f0d4c43dfd407e56a6',
	    'e521cfae36ba57754bae4a5b7c36a42f': 'http://www.rtl2-interaktiv.de/ostergewinnspiel/?symbol=e521cfae36ba57754bae4a5b7c36a42f',
	    'd6a1d8890cd6a826e25dd383e7066051': 'http://www.rtl2-interaktiv.de/ostergewinnspiel/?symbol=d6a1d8890cd6a826e25dd383e7066051',
	    '9059c8caa87be31ed59e14431c667c97': 'http://www.rtl2-interaktiv.de/ostergewinnspiel/?symbol=9059c8caa87be31ed59e14431c667c97',
	    'e46fc3b743217afba6d75b37507e3291': 'http://www.rtl2-interaktiv.de/ostergewinnspiel/?symbol=e46fc3b743217afba6d75b37507e3291',
	    'f4e4aa30674aae97f1e6089023181a1e': 'http://www.rtl2-interaktiv.de/ostergewinnspiel/?symbol=f4e4aa30674aae97f1e6089023181a1e',
	    'f59165f3c0771593285dd1e6bb50394b': 'http://www.rtl2-interaktiv.de/ostergewinnspiel/?symbol=f59165f3c0771593285dd1e6bb50394b',
	    '8180e189813698638b64976c19e557ed': 'http://www.rtl2-interaktiv.de/ostergewinnspiel/?symbol=8180e189813698638b64976c19e557ed'
		};
		var available_symbols = symbols.slice();
		for (var symbol in symbols) {
		  if (ostergewinnspiel.getSubValue(symbols[symbol])) {
		    available_symbols.splice(symbol, 1);
		  }
		}
		var current_url = string_to_hex(location.href);
		if (!ostergewinnspiel.getSubValue(current_url)) {
		  if (rand(0, 1) == 1) {
		    var symbol = available_symbols[rand(0, available_symbols.length - 1)];
		    var container = document.createElement('div');
		    container.className = 'special';
		    document.getElementById('leftcolumn').appendChild(container);
		    container.innerHTML = '<a href="' + links[symbol] + '" onclick="ostergewinnspiel.setSubValue(\'' + current_url + '\', 1); ostergewinnspiel.setSubValue(\'' + symbol + '\', 1);"><img src="' + images[symbol] + '" alt="Ostermotiv" border="0" style="display: block; margin: auto;" /></a>';
		  }
		}
	});
}
