jQuery.fn.nextUntil = function(expr) { 
   var match = []; 
   this.each(function(){ 
       for( var i = this.nextSibling; i; i = i.nextSibling ) { 
           if ( i.nodeType != 1 ) continue; 
           if ( jQuery.filter( expr, [i] ).length ) break; 
           match.push( i ); 
       } 
   }); 
   return this.pushStack( match, arguments ); 
}; 

var 
	listingDay = new Array(),
	listingMonth = new Array(),
	listingYear = new Array(),
	weekDay = new Array(),
	listingheights = new Array(),
	genreindex = {"":0,"Action":1,"Comedy":2,"Comedy ":2,"Historical":3,"Drama":4,"Fantasy":5,"Sci-Fi & Fantasy":5,"Sci-Fi":5,"Romance":6,"Thriller":7,"Western":8,"Specials":9,"Specials & Documentary":9},
	activecolumn = 0,
	selectedWeek = 0,
	highlightactive = 1,
	previoush,
	slidingTimer,
	previouse,
	planer = {
	loadnextweek : function() {
			selectedWeek++;
			planer.changeWeek(selectedWeek);
	},
	loadpreviousweek : function() {
			selectedWeek--;
			planer.changeWeek(selectedWeek);
	},
	init : function() {
		selectedWeek = $("#weekno").text()
		setTimeout("planer.highlightfader();",5000);
		planer.changeWeek(selectedWeek);
		$("#next-week-button").bind('click', function(){
			planer.loadnextweek();
		});
		$("#previous-week-button").bind('click', function(){
			planer.loadpreviousweek();
		});
		$("#overview-week").bind('click', function(){
			planer.weekview();
		});
		$("#previous-day-button").bind('click', function(){
			activecolumn--;
			if (activecolumn < 1) {
				planer.loadpreviousweek();
			} else {
				planer.dayview(activecolumn);
			}
		});
		$("#next-day-button").bind('click', function(){
			activecolumn++;
			if (activecolumn > 7) {
				planer.loadnextweek();
			} else {
				planer.dayview(activecolumn);
			}
		});
		$("#slider-wrapper .slider").slider({ 
			orientation: 'vertical',
			min: -10,
			max: 10,
			value: 0,
			change: function(){
				planer.slidingGenre($(this).attr("id"),$(this).slider('option', 'value'));
			},
			animate: true
		});
		$("#daytime-slider").slider({ 
			orientation: 'horizontal',
			min: -24,
			max: 24,
			value: 0,
			slide: function(){
				planer.slidingContent($(this).attr("id"),$(this).slider('option', 'value'),true);
			},
			animate: true
		});
		$("#planer-scroller").slider({ 
			orientation: 'vertical',
			min: -24,
			max: 24,
			value: 0,
			slide: function(){
				planer.slidingContent($(this).attr("id"),$(this).slider('option', 'value'),true);
			},
			animate: true
		});
		$("#planer-content-listing, #planer-timeline").css("top", "-700px");
	},
	slidingGenre : function(id, value) {
		var slidervalue = Array();
		
		$("#planer-content-listing  div.views-row").each(function(){
			$(this).attr('ref','0.8');
		});
		
		$("#slider-wrapper .slider").each(function(i){
			var slidervalue = $(this).slider('option', 'value');
			$('#planer-content-listing div.genre-'+(i+1)).each(function(){
				var newval = parseFloat($(this).attr('ref')) + Math.floor((slidervalue/(10*$(this).attr('rel')))*80)/100;
				$(this).attr('ref',newval);
			});
		});
		
		$("#planer-content-listing  div.views-row").each(function(){
			var opacityval = parseFloat($(this).attr('ref'));
			if(opacityval<0.1){
				$(this).fadeTo(0,0.1);
			} else {
				$(this).fadeTo(0,opacityval);
			}
		});
	},
	slidingContent : function(id, value, stop) {
		clearTimeout(slidingTimer);
		if(!stop&&id === "daytime-slider"){
		value=$("#daytime-slider").slider('option', 'value');
		} else if(!stop&&id === "planer-scroller") {
		value=$("#planer-scroller").slider('option', 'value');
		}
		if (id === "daytime-slider") {
			$("#planer-scroller").slider('option', 'value', (value*(-1)));
		} else {
			$("#daytime-slider").slider('option', 'value', (value*(-1)));
		}
		sliderval = parseInt(($("#daytime-slider").slider('option', 'value')+29)*1.08);
		topval = 28*sliderval*(-1);
		$("#planer-content-listing, #planer-timeline").css("top", topval+"px");
		if(stop){
		slidingTimer = setTimeout(function(){
			planer.slidingContent(id, value, false);
		},500);
		}
	},
	highlightfader : function() {
		highlightold = highlightactive;
		if (highlightactive === 3) { highlightactive = 1;}
		else { highlightactive++ }
		$("#block-views-highlightteasertriple-block_1 div.views-row-"+highlightold).animate({ opacity: "hide"}, 1000);
		$("#block-views-highlightteasertriple-block_1 div.views-row-"+highlightactive).animate({ opacity: "show"}, 1000,function(){
			setTimeout("planer.highlightfader();",7000);
		});
	},
	changeWeek : function(gweek) {
		//$("#slider-wrapper .slider").slider("option","value",0);
		helper.getDateRangeOfWeek(gweek);
		planer.weekview();
		$(".planner-day-wrapper").html("");
		planer.loadListing(1);
	},
	loadListing : function(i) {
		if (i <= 7) {
		$("#planer-overlay").show();
		urlparam = "/listing/"+listingYear[i]+"-"+listingMonth[i]+"-"+listingDay[i]+"T04:00--P25H";
		$.ajax({
			type: "GET",
			url: urlparam,
			dataType: "html",
			success: function(html) {
				if (parseInt($(html).find(".view-content").size()) > 0) {
					$("#column"+i).html($(html).find(".view-content").html());
					planer.formatListing(i);
				} else {
					$("#column"+i).html('<div style="margin-top:300px;">Keine Programmdaten vorhanden.</div><div style="margin-top:300px;">Keine Programmdaten vorhanden.</div><div style="margin-top:300px;">Keine Programmdaten vorhanden.</div><div style="margin-top:300px;">Keine Programmdaten vorhanden.</div><div style="margin-top:300px;">Keine Programmdaten vorhanden.</div><div style="margin-top:300px;">Keine Programmdaten vorhanden.</div>');
				}
				i++;
				planer.loadListing(i);
			}
		});
		} else {
				$("#planer-overlay").hide();
				planer.slidingGenre();
		}
	},
	formatListing : function(columnindex) {
			$("#column"+columnindex+" .views-row").each(function(){
			newheight = parseInt($(this).find(".views-field-field-duration-value .durationvalue").text());
			$(this).css("height", newheight+"px");
			
			var genres = $(this).find("div.views-field-field-genre-value span").text().replace('Genre: ','').replace(', Sonstige','').split(', ');
			$(this).attr('rel',genres.length);
			var elem = $(this);
			$.each(genres,function(){
				elem.addClass('genre-'+genreindex[this]);
			});
			elem.attr('ref','1');
			
			if($(this).find("div.views-field-sticky span.hidden").text()=='Ja'){
				$(this).addClass('highlight');
			}
			
			$(this).addClass("genre"+$(this).find(".views-field-field-genre-value-1 span").text());
			});
			var column = $("#column"+columnindex);
			var firstelem = column.find(".views-row").eq(0);
			var daystart = column.find(".views-row").eq(0).find(".date-display-single .date-display-start").text();
			var segments = daystart.split(":");
			var offset = (parseInt(segments[0]*60)+parseInt(segments[1]));
			column.prepend('<div class="genreSo" style="height:'+offset+'px;margin-buttom:3px;"></div>');
			planer.listingdetail(columnindex);
		$("#column"+columnindex+" .views-row").eq(0).html("").css("background-color","#777777");
		column.append('<div class="genreSo" style="height:400px;margin-buttom:3px;"></div>');
	},
	dayview : function(columnid) {
		//elem.unbind("click");
		activecolumn = columnid;
		column = $("#column"+columnid);
		$(".planner-day-wrapper").not(column).animate({
			width: "0px",
			opacity: "hide"
		}, 600);
		column.animate({ 
        	width: "730px"
      	},600, function(){
      		planer.formatday();
      	});
		$("#title-week").hide();
		$("#title-day").show();
		$("#bottom-content").css("background", "url(/sites/default/themes/tnt-film/gif/planer/programmbgday.png) no-repeat");
		$("#selected-day").html(weekDay[columnid]);
	},
	formatday : function() {
		$(".views-field-field-airtime-value-1, .views-field-title, .views-field-field-year-value").addClass("largerfont");
		$("#column"+activecolumn+" .views-row").each(function(){
			if ($(this).find(".views-field-field-imagelink-url span").text().substring(0,4) === "http") {
			$(this).children(".views-field-field-synopsis-value").prepend('<img src="'+$(this).find(".views-field-field-imagelink-url span").text()+'"/>').append('<div style="clear:both;"></div>');
			$(this).children(".views-field-field-imagelink-url").html("");
			}
		});
	},
	listingdetail : function(iterator){
		$("#column"+iterator+" .views-row").bind("click", function(){
			if($(this).hasClass('active-row')){
				$(this).removeClass('active-row');
				planer.weekview();
			} else {
			$(this).addClass('active-row');
			$("#planer-timeline").animate({opacity: "0.2"},600);
			activecolumn = $(this).parent().attr("id").substring(6,7);
			planer.dayview(iterator);
			oldheight = parseInt($("#previouselem").find(".views-field-field-duration-value .durationvalue").text());
			$("#previouselem").css("height", oldheight+"px");
			$(this).attr("id","previouselem");
			$(".views-field-field-duration-value, .views-field-field-actors-value, .views-field-field-synopsis-value, .views-field-field-genre-value, .views-field-field-imagelink-url").hide();
			$(".views-field-field-directors-value, .views-field-field-airtime-value-1, .views-field-title, .views-field-field-year-value").removeAttr("style");
			$(this).find(".views-field-field-directors-value").css("background","#f9f9f9").css("color","#111").css("font-size","12px").hide();
			$(this).find(".views-field-field-airtime-value-1, .views-field-title, .views-field-field-year-value").css("float","left");
			activerow = $(this);
			$(this).find(".views-field-field-duration-value, .views-field-field-actors-value, .views-field-field-synopsis-value, .views-field-field-genre-value, .views-field-field-directors-value").animate({ opacity: "show" }, 600, function(){
				activerow.removeAttr("style");
			});
			}
		});
	},
	weekview : function() {
			$(".views-field-field-airtime-value-1, .views-field-title, .views-field-field-year-value").removeClass("largerfont");
			oldheight = parseInt($("#previouselem").find(".views-field-field-duration-value .durationvalue").text());
			$("#previouselem").css("height", oldheight+"px");
			$(".views-field-field-duration-value, .views-field-field-actors-value, .views-field-field-synopsis-value, .views-field-field-genre-value, .views-field-field-imagelink-url").hide();
			$(".views-field-field-directors-value, .views-field-field-airtime-value-1, .views-field-title, .views-field-field-year-value").removeAttr("style");
		$("#column"+activecolumn).animate({
			width: "102px"
		}, 600);
		$(".planner-day-wrapper").animate({
			width: "102px",
			opacity: "show"
		}, 600);
			$("#planer-timeline").animate({opacity: "1.0"},600);
		planer.weekviewheader();
	},
	weekviewheader : function() {
		$("#title-week").show();
		$("#title-day").hide();
		$("#bottom-content").css("background", "url(/sites/default/themes/tnt-film/gif/planer/programmbgweek.png) no-repeat");
	}
	},
	helper = {
		getDateRangeOfWeek : function(weekNo){
			var d1 = new Date();
			numOfdaysPastSinceLastMonday = eval(d1.getDay()- 1);
			d1.setDate(d1.getDate() - numOfdaysPastSinceLastMonday);
			var weekNoToday = $("#weekno").text();
			var weeksInTheFuture = eval( weekNo - weekNoToday );
			d1.setDate(d1.getDate() + eval( 7 * weeksInTheFuture ));
			for (i=1;i<8;i++) {
				weekDay[i] =  d1.getDate()+"."+eval(d1.getMonth()+1)+ "."+d1.getFullYear();
				listingDay[i] = d1.getDate();
				listingMonth[i] = eval(d1.getMonth()+1);
				listingYear[i] = d1.getFullYear();
				d1.setDate(d1.getDate() + 1);
			}
			$("#selected-week").html('<span id="firstweekday">'+weekDay[1] + "</span> - "+weekDay[7]);
		}
};
