/**
 * global javascript functions and actions
 * 
 **/

$(document).ready(function() {

var voting = $('.voting_box');
if (voting) {

   voting.find('.collapsable-content').hide();
   voting.find('.radiobox input:checked').parentsUntil('.voting_block').prev().addClass('checked');

   voting.find('.collapsable-header').click(function() {
      $(this).next('.collapsable-content').toggle();
      $(this).toggleClass('act');
   });

   voting.find('.radiobox input').click(function() {
      $.ajax({
         url: '/codebase/ajax/krone_remember.jsp',
         data: $(this).attr('name')+'='+$(this).val()
      });
      $(this).parentsUntil('.voting_block').prev().addClass('checked');
   });
}

   /* videolightbox 'hohe auflösung' javascript ala wdr.de */
   if( window.innerWidth ) {
   //Non-IE
     var width  = window.innerWidth; 
     var height = window.innerHeight; 
   } 
   else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
   //IE 6+ in 'standards compliant mode'
     var width  = document.documentElement.clientWidth;
     var height = document.documentElement.clientHeight;
   } 

   var positionX = ((width / 2) - 960 / 2);
   var positionY = ((height / 2) - 564/ 2);
   var positionCloseX = ((width / 2) - 960 / 2)+623;
   var positionCloseY = ((height / 2) - 564/ 2)-30;
   var positionX = (positionX)+"px";
   var positionY = (positionY)+"px";
   var positionCloseX = (positionCloseX)+"px";
   var positionCloseY = (positionCloseY)+"px";

  // HQ Player
  if(jQuery('.hqOpenLink').length > 0) {

    $('<div></div>').attr({ id: 'hqContainer' }).css({ height: ($('#wsFooterOuterBG').offset().top + $('#wsFooterOuterBG').height() + 100)/16 + 'em' }).appendTo('body').hide();
    
    var $cssColorObj = { color: $('.wsRubrik h2').eq(0).css('color'), backgroundColor: $('.wsRubrik h2').eq(0).css('backgroundColor') };
    
    $('<div></div>').attr({ id: 'hqDiv' }).css({ top: positionY, left: positionX, color: $cssColorObj.color, backgroundColor: $cssColorObj.backgroundColor }).appendTo('body').hide();
    
    $('<a href="#">X schließen</a>').attr('id','hqCloseLink').css({ top: positionCloseY, left: positionCloseX, color: $cssColorObj.color, backgroundColor: $cssColorObj.backgroundColor })	.click(function() {
    					$('#hqContainer').hide();
    					$('#hqDiv').hide('');
    					$('#hqDiv').html('');
    					$(this).hide();    					
    					$('object').css('visibility','');    					
    					return false;
    				}).appendTo('body').hide();
    
    jQuery('.hqOpenLink').each(function(pIdx) {
    	$(this).click(function() {
	  window.scroll(0,0);
          $('#hqDiv').html('');    		
          $('object').each(function(pIdx){
          $(this).css('visibility','hidden');
          try { document.getElementsByTagName('object')[pIdx].mediaStop(); }
          catch(pErr) {  }
});

    		$('#hqContainer').show();    		    		
    		$('#hqDiv').html($getHQPlayerHTML($(this).attr('href').split('?')[1],$(this).attr('rel').split('base#')[1])).show();
    		$('#hqCloseLink').show();
    		
    		return false;
    	});
    });

    var $getHQPlayerHTML = function(pQuery, pBase) {
      jumptitle = $('.trailerboxjump h3 a').html();
      var tmpPlayerHTML = '';
      if(/*@cc_on ! @*/false) { 
        tmpPlayerHTML += '<object width="960" height="564" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="wsHQPlugin">';
        tmpPlayerHTML += '<param name="movie" value="http://www.wdr.de/themen/global/flashplayer/wsPlayer.swf" />';
      } else {
        tmpPlayerHTML += '<object width="960" height="564" type="application/x-shockwave-flash" data="http://www.wdr.de/themen/global/flashplayer/wsPlayer.swf" id="wsHQPlugin">';
      }             
      tmpPlayerHTML += '<param name="base" value="/" />';
      tmpPlayerHTML += '<param name="quality" value="autohigh" />';
      tmpPlayerHTML += '<param name="wmode" value="window" />';
      tmpPlayerHTML += '<param name="allowScriptAccess" value="always" />';
      tmpPlayerHTML += '<param name="allowNetworking" value="all" />';
      tmpPlayerHTML += '<param name="allowFullScreen" value="true" />';
      tmpPlayerHTML += '<param name="flashvars" value="' + pQuery + '&amp;skinURL=http://www.wdr.de/themen/global/flashplayer/wsControls.swf&amp;cfgFile=/codebase/player/black/skin.cfg" />';
      tmpPlayerHTML += '</object>';      
      return tmpPlayerHTML;
    };

} // End of HQ Links
/* end videolightbox */

});

function getScriptState() {
	return true; /* initialer Test des Players, ob JavaScript aktiviert ist */
};
function getReferer() {
	return escape(location.href); /* Seiten URL wird als Referer an das Zählpixel übergeben */
};
/* onunload */
window.onunload = function() {
	/* Schleife, die alle eingebetteten Player korrekt terminiert */
	var objArr = document.getElementsByTagName('object');
	for(var i = 0; i < objArr.length; i++) {
		if(typeof objArr[i].stopAllMedia == 'function') { objArr[i].stopAllMedia(); }
	}
};

function validateVote() {
	valid = true;
	if ( document.votingform.answer.value == "") {
		alert("Bitte beantworte die Anti-Spam-Frage.");
		valid = false;
	}
	return valid;
}

function clock(jahr, monat, tag, stunde) {
	// achtung monate gehen von 0 bis 11
	var date = new Date(jahr, monat-1, tag, stunde, 0, 0);
	var now = new Date();
	var diff = date.getTime() - now.getTime();
	var days = Math.floor(diff / (1000 * 60 * 60 * 24));
	var hours = Math.floor(((diff-days) / (1000 * 60 * 60 ))-(days*24));
	var minutes = Math.floor(((diff-days-hours) / (1000 * 60 ))-(days*24*60+hours*60));
	var seconds = Math.floor(((diff-days-hours-minutes) / (1000 ))-(days*24*60*60+hours*60*60+minutes*60));
        if (now.dst() && !date.dst()) {
           hours = hours-1;
        }
        if (!now.dst() && date.dst()) {
           hours = hours+1;
        }
	if(days<0){
		var countdown = "00:00:00";
                $('.countdown').hide();
	} else {
		var countdown = hours+":"+addZero(minutes)+":"+addZero(seconds);
                $('.countdown .count:first').html(days);
	}
	$('#clockdown').html(countdown);
        if (timerStopped != 1) {
	        window.setTimeout(function(){clock(jahr, monat, tag, stunde)}, 1000);
        }
}

timerStopped = 0;
function stopClock() {
	timerStopped = 1;
}

function addZero(time) {
	if (time<10) {
		time = "0"+time;
	}
	return time;
}

Date.prototype.stdTimezoneOffset = function() {
	var jan = new Date(this.getFullYear(), 0, 1);
	var jul = new Date(this.getFullYear(), 6, 1);
	return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
Date.prototype.dst = function() {
	return this.getTimezoneOffset() < this.stdTimezoneOffset();
}

