jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});



$(document).ready(function(){

	
	
		
	/*
	$('a.loadable_content').livequery('click', function() {
	var id = $(this).attr('rel');
	var url = '/event/jsonevent/id/'+id;
	$.getJSON(url, 
			function(msg) {
				var return_data = msg;
				//var return_data = $.parseJSON(msg, true);
				if (return_data.error == 1) {

				} else {
					 $('#aktuell div.content_loadable').html(return_data.data);
				}
			}
 		);
	//$('ul#grid').shuffle();
	 
	 $('#aktuell div.aktuell').animate({ 
	        height: "0px",

	      }, 1000 );
	
	 $('#aktuell div.termin').animate({ 
	        height: "500px",
	      }, 1000 );
	      
		 return false;
	  });
	
	$('#aktuell h2.col_heading').livequery('click', function() {
		$('#aktuell div.aktuell').animate({ 
	        height: "500px",
	      }, 1000 );
	
	 $('#aktuell div.termin').css('display','none');
	      
		 return false;
	});
	
		$('#termine h2.col_heading').livequery('click', function() {
		$('div.aktuelletermine').animate({ 
	        height: "500px",
	      }, 1000 );
	
	 $('div.terminarchiv').css('display','none');
	      
		 return false;
	});
	
	$('a.eventarchive').live('click', function() {
		var lang_id = $(this).attr('rel');
		$('div.aktuelletermine').animate({ 
	        height: "0px",
	      }, 1000 );
	     
	     	var id = $(this).attr('rel');
			var url = '/event/jsoneventarchive/langid/'+lang_id;
			$.getJSON(url, 
			function(msg) {
				var return_data = msg;
				//var return_data = $.parseJSON(msg, true);
				if (return_data.error == 1) {

				} else {
					 $('div.terminarchiv').html(return_data.data);
				}
			}
 		);
	     
	     $('div.terminarchiv').animate({ 
	        height: "500px",
	      }, 1000 );
		      
		 return false;
	});
	*/
	Math.rounda = function(number, precision)
	{
		precision = Math.abs(parseInt(precision)) || 0;
		var coefficient = Math.pow(10, precision);
		return Math.round(number*coefficient)/coefficient;
	}
	
	function injectimages(count) {
		for (i=0;i<=count;i++)
		{
			$('ul#grid').append('<li><img src="/images/grid/bg_0'+ $.random(6) +'.jpg" width="240" height="320" alt="1"></li>');
		}
		
	};
	
  	function doSomething() {

	  //
		
		var images = $('ul#grid li').size();
		var winsize = $(window).width() *$(window).height();
		
		if(images*76800 < winsize){
			
			var images_to_inject = Math.rounda(( winsize - images*76800) /76800 , 0);			
			injectimages(images_to_inject);
		}
	  };
  
  
  
  
  var resizeTimer = null;
  
  $(window).bind('resize', function() {
  
  if (resizeTimer) clearTimeout(resizeTimer);
  
  resizeTimer = setTimeout(doSomething, 100);
  
  });
	$('ul#grid').shuffle();
	//get
	var sessid = $.cookies.get( 'beenhere' );
	
	if(sessid){
		var ani_time = 100;
	}else{
		var ani_time = 4000; 
		$.cookies.set( 'beenhere', 'dh3tr62fghe' ); //A cookie by the name 'sessid' now exists with the value 'dh3tr62fghe'
	
	}
	
	$('img:hidden').each(
	    function(){
			setTimeout(function() {
							$('img:hidden:last').fadeIn($.random(ani_time));	        //access to form element via $(this)
			  }, $.random(ani_time));
	});
	

});


