var sites = {"#programm":1, "#fifty":1, "#news":3, "#autoren":3, "#onlinespiele":4, "#wochenplaner":1, "#videos":1, "#blog":3, "#spiele":4, "#genre":1, "#filmdatenbank":2, "#listinghighlight":1, "#blogauthor":3, "#webform":4, "#berlinale":4, "#and-the-winner-is":4, "#ausstellung":5};
var categoryid = 0;
var hoverid = 0;
var menupicpath = "/sites/default/themes/tnt-film/gif/menu/level1/";

$(document).ready(function(){
	jQuery.each(sites, function(siteid,menuid) {
		if ($(siteid).size() > 0) {
			categoryid = menuid;
		}
	});
	if ($("#filmdatenbank").size() > 0) {
		moviedb();
	} else if ($("#blogauthor").size() > 0) {
		markAuthor();
	} else if ($("#wochenplaner").size() > 0) {
		planer.init();
	} else if ($("#listinghighlight").size() > 0) {
		$("#block-views-onairnow-block_1 h3").after("<h4>TNT-Film zeigt jetzt:</h4>");
		$("#block-views-latestblogentry-block_1 .views-field-body").appendTo("#block-views-latestblogentry-block_1 .views-field-title").show();
		var videourl = $("#videourl").text();
		$('#videoplayer').flash({
			swf: '/sites/default/themes/tnt-film/flash/flvplayer.swf',
			height: 315,
			width: 560,
			flashvars: {
				flvheight: "315",
				flvwidth: "560",
				flvURL: "/"+videourl
			}
		});
	} else if ($("#home").size() > 0) {
		home.init();
	} else if ($("#newsoverview").size() > 0) {
		news.init();
	} else if ($(".ident").size() > 0) {
		genres.init();
	} else if ($("#webform").size() > 0) {
		lottery();
	} else if ($("#fifty").size() > 0) {
		fifty.init();
	} else if($('#block-views-blogoverview-block_1, .view-id-archive.view-display-id-page').size()>0){
		$(".views-field-nid a").each(function(){
			$(this).attr('href',$(this).attr('href')+'#comments');
		});
		$("#column-right").attr('id','blog-right');
		$("#column-left").attr('id','blog-left');
	}
	
	if ($("#comments").size() > 0) {
		$(".comment_reply a, .comment_delete a, .comment_edit a").each(function(){
			if ($(this).attr("href").substring(0,8) == "/comment") {
			}
		});
		//$(".comment_add a").attr("href", $(".comment_add a").attr("href").substring);
	}
	hoverid = categoryid;
	menuSwap();
	menuhighlights();
	searchSite();
	if (($("#skyscraper").html().indexOf("120x600")>0)||($("#skyscraper").html().indexOf('"120"')>0)) {
		$("#skyscraper").css("border","none");
	}
	if ($("#permission-denied").size()>0) {
		$(".block-views").hide();
	}
	$("#skyscraper, #superbanner").css("border","none");
	checkWallpaper();
	setTimeout("correctAd()",1000);
});


function checkWallpaper() {
if (typeof(adtype) != 'undefined') { 
	if (adtype == 'wallpaper') {
	$("#bannerright").css("position","relative").css("top","-23px");
	} if (adtype == 'expwallpaper') {
	$("#bannerright").css("position","relative").css("top","-114px").css("left","120px");
	$("#superbanner").css("position","relative").css("left","-40px");
} }
if (typeof(bgcolor) != 'undefined' && bgcolor != null) {
	$("#bodywrap").css("backgroundColor",bgcolor);
	$("#bodywrap").css("backgroundImage","none");
}
if (typeof(bgimage) != 'undefined' && bgimage != null) {
	$("#bodywrap").css("backgroundImage","url('"+bgimage+"')");
}
if (typeof(adclick) != 'undefined' && adclick != null) {
	//to be implemented
}
}

function markAuthor() {
	$("#block-views-blogauthors-block_1 .views-field-field-authorfirstname-value .field-content").each(function(){
		if ($(this).text() == $("#authornickname").text()) {
			$(this).parent().parent().addClass("views-row-active");
		}
	});
}

function correctAd() {
	$("body").removeAttr("style");
}

function menuhighlights() {
	$(".level3-ul1-li1").html($("#block-views-highlightsmenu-block_1 .views-row-1 .field-content").html());
	$(".level3-ul1-li2").html($("#block-views-highlightsmenu-block_1 .views-row-2 .field-content").html());
	$(".level3-ul1-li3").html($("#block-views-highlightsmenu-block_1 .views-row-3 .field-content").html());
	$(".level3-ul1-li4").html($("#block-views-highlightsmenu-block_1 .views-row-4 .field-content").html());
	$(".level3-ul1-li5").html($("#block-views-highlightsmenu-block_1 .views-row-5 .field-content").html());
	// override highlights
	$(".level2-ul1-li3 a").attr("href",($("#block-views-highlightsmenu-block_1 .views-row-1 .field-content a").attr("href")));
	$(".level3-ul2-li1").html($("#block-views-authorsmenu-block_1 .views-row-1 .field-content").html());
	$(".level3-ul2-li2").html($("#block-views-authorsmenu-block_1 .views-row-2 .field-content").html());
	$(".level3-ul2-li3").html($("#block-views-authorsmenu-block_1 .views-row-3 .field-content").html());
	$(".level3-ul2-li4").html($("#block-views-authorsmenu-block_1 .views-row-4 .field-content").html());
	$(".level3-ul2-li5").html($("#block-views-authorsmenu-block_1 .views-row-5 .field-content").html());
	$(".level3-ul2-li6").html($("#block-views-authorsmenu-block_1 .views-row-6 .field-content").html());
	$(".level3-ul2-li7").html($("#block-views-authorsmenu-block_1 .views-row-7 .field-content").html());
	$(".level3-ul2-li8").html($("#block-views-authorsmenu-block_1 .views-row-8 .field-content").html());
	$(".level3-ul2-li9").html($("#block-views-authorsmenu-block_1 .views-row-9 .field-content").html());
	$(".level3-ul2-li10").html($("#block-views-authorsmenu-block_1 .views-row-10 .field-content").html());
	//override authors
	$(".level2-ul3-li3 a").attr("href",($("#block-views-authorsmenu-block_1 .views-row-1 .field-content a").attr("href")));
	$("#menu-programm").attr("href","/programm/wochenplaner");
	$(".level2-ul1-li5 a").attr("href",$("#block-views-fiftymoviemenu-block_1 .views-row-1 a").attr("href"));
	$("#block-views-fiftymovies-block_1 .views-row-last").html('<a href="'+$("#block-views-fiftymoviemenu-block_1 .views-row-1 a").attr("href")+'">'+$("#block-views-fiftymovies-block_1 .views-row-last").text()+'</a>');
	$("#block-views-authorsmenu-block_1, #block-views-highlightsmenu-block_1, #block-views-fiftymoviemenu-block_1").remove();
}

function lottery() {
		$("#webform-component-gsb LABEL.option").html('<input type="checkbox" name="submitted[gsb][Ich habe die Teilnahmebedingungen gelesen und akzeptiere sie.]" id="edit-submitted-gsb-Ich-habe-die-Teilnahmebedingungen-gelesen-und-akzeptiere-sie." value="Ich habe die Teilnahmebedingungen gelesen und akzeptiere sie."   class="form-checkbox" /> Ich habe die <a href ="/sites/default/files/Teilnahmebedingungen_Gewinnspiel.pdf" target="_blank">Teilnahmebedingungen</a> gelesen und akzeptiere sie.');
		labeltext = $(".form-item label").eq(0).html().replace(/:/,"");
		$(".form-item label").eq(0).html(labeltext);
}

function searchSite() {
	$("#searchform").bind("submit", function(){
		window.location = "/search/node/"+$("#searchterm #search").attr("value");
		return false;
	});
	$("#searchbutton").bind("click", function(){
		window.location = "/search/node/"+$("#searchterm #search").attr("value");
	});
}

function populatenews(j) {
		var news = new Array();
		$.ajax({
			type: "GET",
			url: "/sites/default/files/news/data.xml",
			dataType: "xml",
			success: function(xml) {
				$(xml).find("item").each(function(i){
					if ($(this).find("category").text() != "Music") {
						var url = $(this).find("enclosure").attr("url");
						var id = $(this).find("guid").text();
						if(url){
						var appenStr = '<div class="newslist-item" id="'+id.substring(8,id.length)+'"><a href="/news?newsid='+id.substring(8,id.length)+'"><div class="newslist-img"><img src="/sites/default/files/news/thumbs/'+url.substring(7,url.length)+'" /></div><div class="newslist-text"><h2>'+$(this).find("title").text()+'</h2></div></a></div>';
						news.push(appenStr);
						}
					}
				});
				
				news = $.shuffle(news);
				for(k=0;k<6;k++) {
					$(news[k]).appendTo("#newslist");
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
  // typically only one of textStatus or errorThrown 
  // will have info
},
	timeout: 1000
			
		});
		
	}
	
	
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function menuSwap() { 
	$("#top, #content").mouseover(function(){
		$("#menu, #level2-ul5, #level2-ul4, #level2-ul3, #level2-ul1, #level2 ul").removeAttr('style');
	});
	if (categoryid == 1) {
		$("#menu-programm").children("img").attr("src",menupicpath+"/b_programm_2.png")
	} else if (categoryid == 2) {
		$("#menu-filmdatenbank").children("img").attr("src",menupicpath+"/b_filmdatenbank_2.png")
	} else if (categoryid == 3) {
		$("#menu-tnt-filmblog").children("img").attr("src",menupicpath+"/b_tnt-filmblog_2.png")
	} else if (categoryid == 4) {
		$("#menu-spiele").children("img").attr("src",menupicpath+"/b_spiele_2.png")
	} else if (categoryid == 5) {
		$("#menu-events").children("img").attr("src",menupicpath+"/b_events_2.png")
	}
	$("#menu-programm").mouseover(function(){
		$(this).children("img").attr("src",menupicpath+"/b_programm_2.png");
		if (hoverid != 1) {
		$("#level2 ul").fadeOut(100);
        $("#menu").animate({ "height": 26}, 150, "swing", function(){
        	$("#level2-ul1").fadeIn(100);
        	$("#menu").animate({ "height": 51}, 150, "swing");
        });
        }
        hoverid = 1;
	}).mouseout(function(){
		if (categoryid != 1) {
		$(this).children("img").attr("src",menupicpath+"/b_programm_1.png")
		}
	});
	$("#menu-filmdatenbank").mouseover(function(){
		$(this).children("img").attr("src",menupicpath+"/b_filmdatenbank_2.png");
		$("#level2 ul").fadeOut(100);
        $("#menu").animate({ "height": 26}, 150, "swing");
        hoverid= 2;
	}).mouseout(function(){
		if (categoryid != 2) {
		$(this).children("img").attr("src",menupicpath+"/b_filmdatenbank_1.png")
		}
	});
	$("#menu-tnt-filmblog").mouseover(function(){
		$(this).children("img").attr("src",menupicpath+"/b_tnt-filmblog_2.png");
		if (hoverid != 3) {
		$("#level2 ul").fadeOut(100);
        $("#menu").animate({ "height": 26}, 150, "swing", function(){
        	$("#level2-ul3").fadeIn(100);
        	$("#menu").animate({ "height": 51}, 150, "swing");
        });
        }
        hoverid= 3;
	}).mouseout(function(){
		if (categoryid != 3) {
		$(this).children("img").attr("src",menupicpath+"/b_tnt-filmblog_1.png")
		}
	});
	$("#menu-spiele").mouseover(function(){
		$(this).children("img").attr("src",menupicpath+"/b_spiele_2.png");
		if (hoverid != 4) {
		$("#level2 ul").fadeOut(100);
        $("#menu").animate({ "height": 26}, 150, "swing", function(){
        	$("#level2-ul4").fadeIn(100);
        	$("#menu").animate({ "height": 51}, 150, "swing");
        });
        }
        hoverid = 4;
	}).mouseout(function(){
		if (categoryid != 4) {
		$(this).children("img").attr("src",menupicpath+"/b_spiele_1.png")
		}
	});
	$("#menu-events").mouseover(function(){
		$(this).children("img").attr("src",menupicpath+"/b_events_2.png");
		if (hoverid != 5) {
		$("#level2 ul").fadeOut(100);
        $("#menu").animate({ "height": 26}, 150, "swing", function(){
        	$("#level2-ul5").fadeIn(100);
        	$("#menu").animate({ "height": 51}, 150, "swing");
        });
        }
        hoverid= 5;
	}).mouseout(function(){
		if (categoryid != 5) {
		$(this).children("img").attr("src",menupicpath+"/b_events_1.png")
		}
	});
	$(".level2-ul1-li3").mouseover(function(){
        $("#menu").animate({ "height": 76}, 150, "swing");
        $("#level3-ul1").show();
        $("#level3-ul2, #level3-ul5").hide();
      });
     
	$(".level2-ul1-li2, .level2-ul1-li4, .level2-ul1-li5").mouseover(function(){
        $("#menu").animate({ "height": 51}, 150, "swing");
      });
     
	$(".level2-ul1-li1").mouseover(function(){
        $("#menu").animate({ "height": 51}, 150, "swing");
      });
     $(".level2-ul3-li3").mouseover(function(){
        $("#menu").animate({ "height": 76}, 150, "swing");
        $("#level3-ul2").show();
        $("#level3-ul1, #level3-ul5").hide();
      });
     $(".level2-ul5-li1").mouseover(function(){
        $("#menu").animate({ "height": 76}, 150, "swing");
        $("#level3-ul5").show();
        $("#level3-ul1, #level3-ul2").hide();
      });
     $(".level2-ul5-li2, .level2-ul5-li3").mouseover(function(){
     	$("#menu").animate({ "height": 51}, 150, "swing");
      });
     
	$(".level2-ul3-li2").mouseover(function(){
        $("#menu").animate({ "height": 51}, 150, "swing");
      });
     
	$(".level2-ul3-li1").mouseover(function(){
        $("#menu").animate({ "height": 51}, 150, "swing");
      });
      
    $("#level1-empfangen a").mouseover(function(){
		$(this).children("img").attr("src",menupicpath+"/b_empfangen_2.png");
	}).mouseout(function(){
		$(this).children("img").attr("src",menupicpath+"/b_empfangen_1.png")
	});
	$("#level1-meinkonto a").mouseover(function(){
		$(this).children("img").attr("src",menupicpath+"/b_mein-konto_2.png");
	}).mouseout(function(){
		$(this).children("img").attr("src",menupicpath+"/b_mein-konto_1.png")
	});
	$("#genrefinder a").each(function(){
		$(this).mouseover(function(){
			srcstr = $(this).children("img").attr("src");
			$(this).children("img").attr("src",srcstr.substring(0,48)+"_o.png");
		}).mouseout(function(){
			$(this).children("img").attr("src",srcstr.substring(0,48)+".png");
		});
	});
}

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);
