;(function($){
    var timerCount = 0;
    
    var timerMax = 0;
    

	var timer = false;
	
	if( timerMax > 0 ) {
		UpdateTimerText( timerMax );

		timer = $.timer( 1000, function ( timer ) {
			timerCount++;
			
			UpdateTimerText( timerMax - timerCount );
			
			if( timerCount == timerMax ) {
				ShowGame( timer );
			}
		});
	}
	else {
		$( "#octogames-widget-preroll-timer" ).hide();
	}	
   	
    $( '#octogames-widget-preroll-stoper' ).click( function( event ) { 
		ShowGame( timer );
		event.stopPropagation();
	});	
 	
	$( '#octogames-widget-preroll' ).click( function( event ) {
		if( $( '#octogames-widget-preroll' ).hasClass( ':visible' ) ) {
			openPlatformLink();
		}
		
		event.stopPropagation();
		
		return true;		
	});
	
	function UpdateTimerText( seconds ) {
		$( "#octogames-widget-preroll-stoper" ).show();
		$( '#octogames-widget-preroll-timer' ).text( ( seconds ) + " Sekunden" );
	}

	function openPlatformLink()	{
		 window.open( 'http://octogames.de/Spiel+Skate_girl+27531' );
	}
	
	function ShowGame( timer ) {
		if( timer ) {
			timer.stop();
		}
			
		$( '#octogames-widget-preroll' ).hide();
		$( '#octogames-widget-game' ).css( "display", "block" );
	}
})(jQuery);
