var off_wide   = 202; 	// breite eines item - lange box
var off_short  = 199; 	// breite eines item - kurze box
var visi_wide  = 4; 	// items visible - lange box
var visi_short = 3; 	// items visible - kurze box
var temp = 0;
function checkHeight(div,padding){
	$("#col3_content").height("auto");
	if($("#col3_content").height()<($(div).height()+padding)){
		$("#col3_content").height(($(div).height()+padding));
	}
	else {
		$("#col3_content").height("auto");
	}
}
$(document).ready(function() {
	/* Header-Bild Shuffle */
	var rndNum = Math.floor(Math.random() * 4)+1;
	$("#header").css("background-image", "url(/_img/header/header"+rndNum+".jpg)");
	/* Header-Bild Shuffle Ende */
	$("#nav .hlist a").hover(function(){
		$(this).children(".hover").css("top",0)
	},function(){
		$(this).children(".hover").css("top",29);
	});

	$("p").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});

	$(".close").click( function() {
		$(this).hide();
		$("#big").hide();

	});
	checkHeight("#mein_uci",30);
	function filter(obj){
		$(".no_filter").hide();
		$(".box-movie").fadeOut("fast");
		if($("."+obj).length == 0){
			$(".no_filter").fadeIn("fast");
		}
		$("."+obj).fadeIn("fast",function(){
		  checkHeight("#col1",130);
		  });
	}
	$(".filter a").not("#spielzeit a").click( function() {
		$(".box-movie a").removeClass("red");
		$(".filter a").removeClass("active");
		$(this).addClass("active");
		filter($(this).attr("title"));
		return false;
	});
	$("#spielzeit a").click( function() {
		$(".filter a").removeClass("active");
		$(".box-movie a").removeClass("red");
		temp = $(this).attr("title");
		$(this).addClass("active");
		filter($(this).attr("title"));

		if(temp=="14"){
			for(i=8;i<14;i++){
				$(".box-movie a."+i).addClass("red");
			}
		}
		if(temp=="18"){
			for(i=14;i<18;i++){
				$(".box-movie a."+i).addClass("red");
			}
		}
		if(temp=="22"){
			for(i=18;i<22;i++){
				$(".box-movie a."+i).addClass("red");
			}
		}
		if(temp=="24"){
			for(i=22;i<24;i++){
				$(".box-movie a."+i).addClass("red");
			}
			for(i=0;i<4;i++){
				$(".box-movie a."+i).addClass("red");
			}
		}
		temp=null;
		return false;
	});
	$("#movie-tabs a").not(".alle, .digital3d, .moviepoints").click( function() {
		ReloadPixelImages('Programm'+ $(this).attr("class"),'Krankikom');
		$(".box-movie a").removeClass("red");
		$("#movie-tabs li").removeClass("active");
		$(".filter div").hide();
		$(this).parent("li").addClass("active");
		$("#"+$(this).attr("class")).slideDown("fast");
		return false;
	});
	$("#movie-tabs a.digital3d, #movie-tabs a.moviepoints").click( function() {
		ReloadPixelImages('Programm'+ $(this).attr("class"),'Krankikom');
		$(".box-movie a").removeClass("red");
		$("#movie-tabs li").removeClass("active");
		$(".filter div").hide();
		$(this).parent("li").addClass("active");
		filter($(this).attr("class"));
		return false;
	});
	$("#movie-tabs a.alle").click( function() {
		ReloadPixelImages('Programm'+ $(this).attr("class"),'Krankikom');
		$(".no_filter").hide();
		$(".box-movie a").removeClass("red");
		$("#movie-tabs li").removeClass("active");
		$(".filter div").hide();
		$(this).parent("li").addClass("active");
		$(".box-movie").not(".no_filter").fadeIn("fast");
		return false;
	});
	$('div.newsEventsBox').each(function(){floatersLights(this)});
	if($.cookie("switch")=="open"){
		switchSizes();
		$("#mein_uci .tipps").slideToggle("fast",function(){
			if($("#main").height()<$("#mein_uci").height()){
				$("#main").height($("#mein_uci").height());
			}	else {
				$("#main").height("auto");
			}
		});
	}
	$(".switch").click( function() {
		switchSizes();
		$("#mein_uci .tipps").slideToggle("fast",function(){
			if($("#main").height()<$("#mein_uci").height()){
				$("#main").height($("#mein_uci").height());
			}	else {
				$("#main").height("auto");
			}
		});
		return false;
	});

	/*$("#cinema_select li a").not("#cinema_select li li a").click( function() {
	$("#cinema_select li").not("#cinema_select li li").toggleClass("hover");
	return false;
	});
	$("#cinema_select li li a").click( function() {
	$("#cinema_select li a").not("#cinema_select li li a").text($(this).text())
	$("#cinema_select").addClass("selected");
	$("#cinema_select li").toggleClass("hover");
	return false;
	});
	$("#cinema_select li li select").change( function() {
	$("#cinema_select li a").not("#cinema_select li li a").text($(this).val())
	$("#cinema_select").addClass("selected");
	$("#cinema_select li").toggleClass("hover");
	return false;
	});

	$("#cinema_select li a").click( function() {
	$(".cinema_select").not(".at").load("fb_cinema_select.php");
	$(".at").load("fb_cinema_select_at.php");

	$(".cinema_select").fadeIn("fast",function(){
	$(".cinema_select a").click( function() {
	$(".cinema_select").hide();
	$("#cinema_select li a").text($(this).text())
	$("#cinema_select").addClass("selected");
	$("#cinema_select li").toggleClass("hover");

	return false;
	});
	$(".cinema_select select").change( function() {
	$(".cinema_select").hide();
	$("#cinema_select li a").text($(this).val())
	$("#cinema_select").addClass("selected");
	$("#cinema_select li").toggleClass("hover");
	return false;
	});
	});

	return false;
	});
	*/


	$("table tr:even").addClass("even");
	$("table tr").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});

	$('a[rel*=facebox]').facebox();
	


});

/* NEWS & EVENTS BOX */
function floatersLights (obj) {
	whichBox = $(obj).hasClass('wide') ? 'wide' : 'short';

	itemCount = $('div.newsEventsBox.' + whichBox + ' .floaters ul > *').length;
	itemVisible = (whichBox == 'wide') ? visi_wide : visi_short;

	lightsCount = (itemCount / itemVisible) > Math.floor(itemCount / itemVisible) ? Math.floor(itemCount / itemVisible) + 1 : itemCount / itemVisible;

	for (var i = 0; i < lightsCount; i++) {
		$('div.newsEventsBox.' + whichBox + ' .head .titel').append('<span class="light l' + i + ' off">&nbsp;</span> ');
		$('div.newsEventsBox.' + whichBox + ' .head .titel span.light.l' + i).click(function () { floatersLightsClick(this); });
	}

	floatersLightsOn(0, obj);
}

function floatersLightsOn (nr, obj) {
	whichBox = $(obj).hasClass('wide') ? 'wide' : 'short';
	$('div.newsEventsBox.' + whichBox + ' .head .titel .light').each( function(){
		if($(this).hasClass('on')) {
			$(this).removeClass('on');
			$(this).addClass('off');
		}
	});
	$('div.newsEventsBox.' + whichBox + ' .head .titel .light.l' + nr).attr('class', 'light l' + nr + ' on');

	/* Pfeile ausgrauen: */
	if(nr == 0){ $('div.newsEventsBox.' + whichBox + ' .body .arrow.left').addClass('disabled');    }
		  else { $('div.newsEventsBox.' + whichBox + ' .body .arrow.left').removeClass('disabled'); }
	if(nr == (lightsCount-1)){ $('div.newsEventsBox.' + whichBox + ' .body .arrow.right').addClass('disabled');    }
		  				else { $('div.newsEventsBox.' + whichBox + ' .body .arrow.right').removeClass('disabled'); }
}

function floatersLightsClick(obj) {
	whichBox = $(obj).parent().parent().parent().attr('class').split(' ')[1];
	currentLight = $('div.newsEventsBox.' + whichBox + ' .head .titel .light.on').attr('class').split(' ')[1].split('l')[1];
	clickedLight = $(obj).attr('class').split(' ')[1].split('l')[1];

	movingTo = currentLight - clickedLight;
	floatersMove(movingTo, obj);
}

function floatersMove(cnt, obj) {
	whichBox = $(obj).parent().parent().parent().attr('class').split(' ')[1];

	var itemVisible = (whichBox == 'wide') ? visi_wide : visi_short;
	var itemOff = (whichBox == 'wide') ? off_wide : off_short;

	if (!$('div.newsEventsBox.' + whichBox + ' .floaters').is(':animated')) {
		dist = cnt * itemVisible * itemOff;

		xOld = $('div.newsEventsBox.' + whichBox + ' .floaters').css('marginLeft');
		itemCount = $('div.newsEventsBox.' + whichBox + ' .floaters ul > *').length;

		allWidth = itemCount * itemOff;
		maxMargin = (allWidth - itemVisible * itemOff) * -1;

		xOld = Number(xOld.split('px')[0]); xNew = xOld + dist;
		if (xNew <= maxMargin) xNew = maxMargin;
		if (xNew >= 0) xNew = 0;
		marginNew = xNew + "px";

		if ((xNew <= 0) && (xNew >= maxMargin))
		$('div.newsEventsBox.' + whichBox + ' .floaters').animate({ marginLeft: marginNew }, 1000, function(){
			lightOn = Math.abs(xNew / (itemVisible * itemOff));
			lightOn = lightOn - Math.floor(lightOn) > 0 ? Math.floor(lightOn) + 1 : lightOn;
			floatersLightsOn(lightOn, $('div.newsEventsBox.' + whichBox));
		});
	}
}

function switchSizes() {
	$('div.newsEventsBox .head .titel .light').each( function(){ $(this).remove(); });
	$('div.newsEventsBox .floaters').animate({ marginLeft: 0 }, 1000);

	$('div.newsEventsBox').each(function(){

		if($(this).hasClass('wide')) {
			$(this).removeClass('wide');
			$(this).addClass('short');
			$.cookie("switch", "open",{path:'/'});
			$(".switch").addClass("open");
		} else {
			$(this).removeClass('short');
			$(this).addClass('wide');
			$.cookie("switch", "closed",{path:'/'});
			$(".switch").removeClass("open");
		}
	});

	$('div.newsEventsBox').each(function(){ floatersLights($(this)); });
}
