var sites = {"#programm":1, "#news":3, "#onlinespiele":4, "#wochenplaner":1, "#videos":1, "#forumpage":5, "#spiele":4, "#genre":1, "#show":2, "#serien":2, "#listinghighlight":1, "#webform":4};
var categoryid = 0;
var hoverid = 0;
var menupicpath = "/sites/default/themes/tnt-serie/gif/menu/level1/";
var showheight = 0;

$(document).ready(function(){
	jQuery.each(sites, function(siteid,menuid) {
		if ($(siteid).size() > 0) {
			categoryid = menuid;
		}
	});
	showheight = ($('div.view-id-showmenu tr').size()*17)+48;
	if ($("#wochenplaner").size() > 0) {
		planer.init();
	} else if ($("#listinghighlight").size() > 0) {
		var videourl = $("#videourl").text();
				$("#videoplayer").attr('href','/'+videourl);
			flowplayer("videoplayer", "/sites/default/themes/tnt-serie/videoplayer/flowplayer.commercial-3.2.7.swf", {
			key: '#$78a68ecbaa49753f6da',
			contextMenu: ['TNT Serie Video Player'],
			logo: {
				url: 'tntserielogo.png',
				fullscreenOnly: false,
				displayTime: 10,
				top: 10,
				right: 10,
				opacity: 0.6
			},
			play: {
				replayLabel: 'Nochmal abspielen'
			},
    clip: {
        pageUrl: window.location.href,
				//ipadUrl: '',
        autoBuffering: true,
        autoPlay: true
    },
    plugins: {
        sharing: {
            url: 'flowplayer.sharing-3.2.1.swf',
            buttons: {
                color:'rgba(0,0,0,0.1)',
                overColor:'rgba(183,20,35,1)',
                fontColor:'rgba(255,255,255,0.8)',
                lineColor:'rgba(255,255,255,1)'
            },
            email: false,
            embed: false
        },
        dock: {
            height: '20%',
            autoHide: {
    					hideDelay: 2000,
    					mouseOutDelay:	1000,
    					hideDuration:	8000
						}
        }
    }
}).ipad();
		
	} else if ($("#home").size() > 0) {
		home.init();
		newshelper();
	} else if ($("#show").size() > 0) {
		show.init();
	} else if ($("#webform").size() > 0) {
		//lottery();
	} else if ($("#news").size() > 0) {
		newshelper();
	} else if($("#forum").size() > 0) {
		$("#forum li.forum a").text("Neuen Beitrag erstellen");
		$("#forum li.forum").show();
	} else if($("#forumpage").size() > 0) {
		if ($(".forum-topic-navigation").size() > 0) { $("#forumback").show(); }
	} else if($("#user-login").size() > 0) {
		$("#user-login").before('<div id="login-buttons"><a href="/user/register">Registrieren</a><a href="/user/password">Neues Passwort</a></div>');
	} else if($("#edit-pass-pass1-wrapper").size() > 0) {
		$("span.password-strength").appendTo(".password-confirm");
	} else if($(".view-Sitemap").size() > 0) {
		sitemap();
	}
	$("#edit-profile-serie01-wrapper").before('<div>Wählen Sie hier Ihre Lieblingsserien aus und wir halten Sie auf dem Laufenden</div>');
	hoverid = categoryid;
	menuSwap();
	menuhighlights();
	searchSite();
	if($("#skyscraper")[0]){
	if (($("#skyscraper").html().indexOf("120x600")>0)||($("#skyscraper").html().indexOf('"120"')>0)) {
		$("#skyscraper").css("border","none");
	}
	}
	if ($("#permission-denied").size()>0) {
		$(".block-views").hide();
	}    
	$("#searchterm INPUT").focus(function(){
		$(this).attr('value','');
	});
	setTimeout("correctAd()",500);
});

function correctAd() {
	$("body").removeAttr("style");
	bodybg();
}

function bodybg() {
	if($(".bg-1").size()>0) {
		$("body").css("background-color","#79140c");
	} else if($(".bg-2").size()>0) {
		$("body").css("background-color","#8d5b09");
	} else if($(".bg-3").size()>0) {
		$("body").css("background-color","#005379");
	}
}

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());
	$("div.view-id-showmenu div.views-field-title a").each(function(){
		if($(this).attr('title')=='[adultswim]') {
			$(this).attr('href','http://adultswim.de').attr('target','_blank');
		}
		if($(this).attr('title')=='Falling Skies'&&$(this).find('span.show-An')[0]) {
			$(this).attr('href','http://www.fallingskies.de').attr('target','_blank');
		}
	});
	
	// override highlights
	$(".level2-ul1-li3 a").attr("href",($("#block-views-highlightsmenu-block_1 .views-row-1 .field-content a").attr("href")));
	$("#menu-news, #newslist-all a").attr("href",($("#block-views-newsmenu-block_1 .views-row-1 .field-content a").attr("href")));
	$("#newslist-header a").attr("href",($("#block-views-newsmenu-block_1 .views-row-1 .field-content a").attr("href")));
	$("#menu-programm").attr("href","/programm/wochenplaner");
	$("#block-views-highlightsmenu-block_1, #block-views-newsmenu-block_1").remove();
}

function lottery() {
		$("#webform-component-gewinnspielbedingungen LABEL.option").html('<input type="checkbox" name="submitted[gewinnspielbedingungen][Ich habe die Teilnahmebedingungen gelesen und akzeptiere sie.]" id="edit-submitted-gewinnspielbedingungen-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_30Rock.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 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() {
	$("#content, #top").bind("mouseover",function(){
		$("#menu, #level2-shows, #level2-ul3, #level2-ul1, #level2 ul").removeAttr('style');
		hoverid= 0;
		resetmainbuttons();
	});
	if (categoryid == 1) {
		$("#menu-programm").children("img").attr("src",menupicpath+"/b_programm_2.png");
	} else if (categoryid == 2) {
		$("#menu-serien").children("img").attr("src",menupicpath+"/b_serien_2.png");
	} else if (categoryid == 3) {
		$("#menu-news").children("img").attr("src",menupicpath+"/b_news_2.png");
	} else if (categoryid == 4) {
		$("#menu-spiele").children("img").attr("src",menupicpath+"/b_spiele_2.png");
	} else if (categoryid == 5) {
		$("#menu-forum").children("img").attr("src",menupicpath+"/b_forum_2.png");
	}
	$("#menu-programm").mouseover(function(){
				resetmainbuttons();
		if (categoryid != 1) { $(this).children("img").attr("src",menupicpath+"/b_programm_3.png"); }
		if (hoverid != 1) {
		$("#level2 ul").fadeOut(100);
        $("#level2-shows").hide();
        $("#menu").animate({ "height": 26}, 150, "swing", function(){
        	$("#level2-ul1").fadeIn(100);
        	$("#menu").animate({ "height": 50}, 150, "swing");
        });
        }
        hoverid = 1;
	});
	$("#menu-serien").mouseover(function(){
				resetmainbuttons();
		if (categoryid != 2) { $(this).children("img").attr("src",menupicpath+"/b_serien_3.png"); }
		$("#level2 ul").fadeOut(100);
        $("#level2-shows").fadeIn(100);
        
        $("#menu").animate({ "height": showheight}, 150, "swing");
        hoverid= 2;
	});
	$("#menu-news").mouseover(function(){
				resetmainbuttons();
		if (categoryid != 3) { $(this).children("img").attr("src",menupicpath+"/b_news_3.png"); }
		$("#level2 ul").fadeOut(100);
        $("#level2-shows").hide();
        $("#menu").animate({ "height": 26}, 150, "swing");
        hoverid= 3;
	});
	$("#menu-spiele").mouseover(function(){
				resetmainbuttons();
		if (categoryid != 4) { $(this).children("img").attr("src",menupicpath+"/b_spiele_3.png"); }
		$("#level2 ul").fadeOut(100);
        $("#level2-shows").hide();
        if($("#level2-ul4")[0]){
        $("#menu").animate({ "height": 26}, 150, "swing", function(){
        	$("#level2-ul4").fadeIn(100);
        	$("#menu").animate({ "height": 50}, 150, "swing");
        });
        } else {
        $("#menu").animate({ "height": 26}, 150, "swing");
        }
        hoverid = 4;
	});
	$("#menu-forum").mouseover(function(){
				resetmainbuttons();
		if (categoryid != 5) { $(this).children("img").attr("src",menupicpath+"/b_forum_3.png"); }
		$("#level2 ul").fadeOut(100);
        $("#level2-shows").hide();
        $("#menu").animate({ "height": 26}, 150, "swing");
        hoverid = 5;
	});
	$(".level2-ul1-li3").mouseover(function(){
        $("#menu").animate({ "height": 74}, 150, "swing");
        $("#level3-ul1").show();
        $("#level3-ul2").hide();
      });
  $("#level3-ul1").mouseover(function(){
  	$(".level2-ul1-li3 a").css("color","#CCCCCC");
  }).mouseout(function(){
  	$(".level2-ul1-li3 a").css("color","#FFFFFF");
  });
	$(".level2-ul1-li2").mouseover(function(){
        $("#menu").animate({ "height": 50}, 150, "swing");
      });
     
	$(".level2-ul1-li1").mouseover(function(){
        $("#menu").animate({ "height": 50}, 150, "swing");
      });
     $(".level2-ul3-li3").mouseover(function(){
        $("#menu").animate({ "height": 74}, 150, "swing");
        $("#level3-ul2").show();
        $("#level3-ul1").hide();
      });
     
	$(".level2-ul3-li2").mouseover(function(){
        $("#menu").animate({ "height": 50}, 150, "swing");
      });
     
	$(".level2-ul3-li1").mouseover(function(){
        $("#menu").animate({ "height": 50}, 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");
	});
	$("#level1-logout a").mouseover(function(){
		$(this).children("img").attr("src",menupicpath+"/b_logout_2.png");
	}).mouseout(function(){
		$(this).children("img").attr("src",menupicpath+"/b_logout_1.png");
	});
}

function resetmainbuttons() {
		if (categoryid != 1) {
		$("#menu-programm img").attr("src",menupicpath+"/b_programm_1.png");
		}
		if (categoryid != 2) {
		$("#menu-serien img").attr("src",menupicpath+"/b_serien_1.png");
		}
		if (categoryid != 3) {
		$("#menu-news img").attr("src",menupicpath+"/b_news_1.png");
		}
		if (categoryid != 4) {
		$("#menu-spiele img").attr("src",menupicpath+"/b_spiele_1.png");
		}
		if (categoryid != 5) {
		$("#menu-forum img").attr("src",menupicpath+"/b_forum_1.png");
		}
	
}

function newshelper() {
	$("#newslist .views-row").each(function(){
			var link = $(this).find(".views-field-title a").attr("href");
			$(this).bind("click",function(){
			window.document.location =  link;
			});
			if ($(this).find(".views-field-field-newsteaserimg-fid a").size() < 1) {
				$(this).find(".views-field-title").css("width","290px");
			}
	});
}

function sitemap() {
	$(".view-Sitemap h3").eq(0).text("Gewinnspiele").show();
	$(".view-Sitemap h3").eq(1).text("Serien").show();
	$(".view-Sitemap h3").eq(2).text("Seiten").show();
	$(".view-Sitemap h3").eq(3).html("&#220;bersicht").show();
	$(".view-Sitemap h3").eq(4).text("News").show();
	$(".view-Sitemap h3").eq(5).text("Highlights").show();
	$(".view-Sitemap h3").eq(6).html("Forenbeitr&#228;ge").show();
}
