function cufonCode()
{
	Cufon.replace('h2, .overlay');
	Cufon.replace('.sidebar div.status, .sidebar div.ticket-date, .sidebar .sidebar-link span');
	Cufon.replace('#genres, #subgenres .sidebar-link', {hover: true});
}



function fixFlash()
{
	var wmode = "opaque";
	
	$("embed").attr("wmode", wmode);

	var embedTag;
    $("embed").each(function(i) {
        embedTag = $(this).attr("outerHTML");
        if ((embedTag != null) && (embedTag.length > 0)) {
            embedTag = embedTag.replace(/embed /gi, "embed wmode=\"" + wmode + "\" ");
            $(this).attr("outerHTML", embedTag);
        }
        // This "else" was added
        else {
            $(this).wrap("<div></div>");
        }
    });
    
    $("iframe").each(function() {
    	if ($(this).attr("src").indexOf("youtube") != -1) {
    		$(this).attr("src", $(this).attr("src") + "?wmode=Opaque");
    	}    	
    })
}

function contactSubmit()
{
	window.location = "/contact/succes";
}

var tweening = true;
//setInterval( "slideSwitch()", 4000 );
$(document).ready( function() {
	$("ul.genres a").hover(function(){
		
	}, function(){
		/*$("ul.sidebar li").addClass("hover");*/
	});
	
	$("ul#subgenres").hover(function(){
		$("ul.sidebar li").addClass("hover");
	}, function(){
		
	});
	
	$("ul#subgenres li a").hover(function() {
		$(this).parent().parent().find("img").show();
	}, function() {
		$(this).parent().parent().find("img").hide();
	});
	
	$("div#vlak").hover(function(){
		$("ul#subgenres").show();
		$("ul.sidebar li").addClass("hover");
	},function() {
		$("ul.sidebar li").removeClass("hover");
		$("ul#subgenres").hide();		
	});

	fixFlash();
	if ($('#ipadslideshow').children().length > 1) {
		$('#ipadslideshow img:gt(0)').hide();
	    setInterval(function(){
	      $('#ipadslideshow :first-child').fadeOut()
	         .next('img').fadeIn()
	         .end().appendTo('#ipadslideshow');}, 
	      3000);
	}
	if ($('#ipadbuilding').children().length > 1) {
	    $('#ipadbuilding img:gt(0)').hide();
	    setInterval(function(){
	      $('#ipadbuilding :first-child').fadeOut()
	         .next('img').fadeIn()
	         .end().appendTo('#ipadbuilding');}, 
	      3000);
	}
	$("#submit_form").click(function(){
		form = $(this).attr("rel");
		return validate_form(form);	
	});
	
	$("#genre-select").change(function(){
		window.location = "/programma/genre/" + id + "/subgenre/" + $(this).val();
	});

	$('#form_event_surfer, #form_contact').submit( function() {
		form = this.id;
		return validate_form(form);
	});
	
	cufonCode();
	
	if (!ipad) {
		tooltipCode();
		hoverCode();
		clickCode();
		toolsCode();
	}
	
	newsletterCode();
	searchCode();
	
	if (controller == "text") {
		$("#nav-" + textId).addClass("rollover");
		//$("#nav-" + textId + " ul").show();
	}
	
	$.fn.colorbox.settings.bgOpacity = "0.9";
	$("a.colorbox").colorbox({transition:"fade"});
	$("a.main").hover(function(){
		$(".overlay").show();
	}, function() {
		
	})
	
	$(".overlay").hover(function(){
		
	}, function() {
		$(".overlay").hide();
	
	})

});

function clickCode()
{
	$(".link, #pers li, .row, .blikvanger, #news li, #surfers li, #binnenkort tr").click(function(event) {
		var link = $(this).attr("id");
		var rel = $(this).attr("rel");
		
		if (rel == "1") {
			window.open(link);
		} else {
			window.location = link;
		}
	});
}

function onCalendarClick(data, type)
{
	hideTooltip();

	if(type == "fiche") {	
		window.location = data;	 
	} else if(type == "date") {
		window.location = "/programma/datum/" + data;	
	}
}


function toolsCode()
{
	$("#print").click(function(event){
		event.preventDefault();
		print();
	});
	
	$("#cart").click(function(event){
		event.preventDefault();
		openWin();
	});
	
	$("#largetext").fontscale("body", "up", {cookie:true,unit:"em"});
	
	$("#smalltext").fontscale("body", "down", {cookie:true,unit:"em"});
	
	$("#largetext").click(function(event){
		event.preventDefault();
		cufonCode();
	});
	
	$("#smalltext").click(function(event){
		event.preventDefault();
		cufonCode();
	});
}

function searchIt()
{
	var val = $("input#search-input").val();
	
	if (val != "" && val != "zoek in de site") {
		$("input#search-input").hide();
		$("#search-arrow").hide();
		$("div.ac_results").hide();

		var genres = "";
		
		if ($("#advancedSearch").attr("id")) {
			var arr = new Array("voorstellingen", "nieuws", "tekstpaginas");
			
			for (var i = 0; i < arr.length; i++) {
				if (genres) {
					genres += "-";
				}
				genres += arr[i]
			}
		}
			
		if (val && val != "zoek in de site") {
			val = val.split(" ").join("_");
			val = escape(val);
			val = val.split("+").join("&plus;");
		
			var location = "/zoeken/" + val + "/";
			
			if (genres) {
				location += "geavanceerd/" + genres + "/";
			}
			
			window.location = location;
		} else {
			$("input#search-input").removeClass("normal");
			$("input#search-input").addClass("error");
			$("input#search-input").focus();
		}
	}
}

function searchCode()
{
	$("input#search-input").click(function(){
		$(this).val("");	
	});

	$("input#search-input").keypress(function(e){
		
		$(this).removeClass("error");
		$(this).addClass("normal");
		if (e.which == 13) {
			searchIt();	
		}
	});
	
	$("#search-arrow").click(function(){
		searchIt();
	});
	
	$("#search-input").autocomplete(data, {selectFirst: false});
	
	$("#search-input").result(function(event, data, formatted) {
		searchIt();
	});
}

function hoverCode()
{
	$("#slideshowHolder").hover(function(){
		if (textId != "0" ) {
			$("#nav-" + textId).removeClass("rollover");
			$("#nav-" + textId + " ul").hide();
		}
	}, function() {
		if (textId != "0") {
			$("#nav-" + textId).addClass("rollover");
			//$("#nav-" + textId + " ul").show();
		}
	});
	
	$("#navigation li").hover(function(){
		var id = $(this).attr("id").split("nav-").join("");
		var rel = $(this).attr("rel");
		
		if (rel == "main" && textId != "0" && textId != id && id != "") {
			$("#nav-" + textId).removeClass("rollover");
			$("#nav-" + textId + " ul").hide();
		}
		
		if ($(this).children().length > 1) {
				var obj = $(this).children();
				obj = $(obj[1]);
				var offset = $(this).offset();
				var parent = $("#navigation").offset();
		
				var x = offset.left-parent.left;
				obj.css("left", x);
				obj.show();
				
				$(this).addClass("rollover");
			}	
		}, function(){
			var rel = $(this).attr("rel");
			var id = $(this).attr("id").split("nav-").join("");
			
			if ($(this).children().length > 1) {
				var obj = $(this).children();
				obj = $(obj[1]);
				obj.hide();
				$(this).removeClass("rollover");
			}
			
			if (rel == "main" && textId != "0" && id != "") {
				$("#nav-" + textId).addClass("rollover");
				//$("#nav-" + textId + " ul").show();
			}
		}
	);
	$(".blikvanger, tr.row, div#news ul li, div#surfers ul li, div#pers ul li, li.sidebar, div#binnenkort ul li").hover(
		function(){
			var id = $(this).parent().attr("id");

			if (id != "genre-holder") {
				$(this).addClass("hover");
			}
		},
		function(){
			var id = $(this).parent().attr("id");
			
			if (id != "genre-holder") {
				$(this).removeClass("hover");
			}
		}
	);
	
	$("div.search tr, div#binnenkort tr, div#detail-relations tr").hover(
			function(){
				showTooltipThumb(this);
			}, 
			function(){
				hideTooltipThumb();
			}
	);
}


function showTooltipThumb(evt)
{
	var obj = $(evt);
	var left = obj.offset().left;
	var top = obj.offset().top;
	$("#tooltip-thumb").css("left", left-100);
	
	var rel = obj.attr("rel");
	
	if (rel) {
		var content = "<img src='" + rel + "' class='tooltip' />";
		$("#tooltip-thumb").css('z-index', 1000);
		$("#tooltip-thumb").html(content);
		$("#tooltip-thumb").show();
	}
}

function hideTooltipThumb()
{
	$("#tooltip-thumb").hide();
}

function newsletterCode()
{
	$("input#newsletter-input").click(function(){
		$(this).val("");	
	});

	$("#form-newsletter input").click(function() {
		$(this).val("");
	});
	if ($("input#newsletter-email-input").attr("id")) {		
		if (newsletter) {
			newsletterSuccess();
		} else {
			$("#advancedNewsletter").hide();
			
			$("div#newsletter input").each(function(){
				$(this).val($(this).attr("rel"));
				$(this).attr("name", $(this).attr("id"));
				$(this).click(function() {
					if (tweening) {
						timing = true;
						$("#advancedNewsletter").show("slow", function() {timing = false;});
					} else {
						$("#advancedNewsletter").show();
					}
				});
				
				$(this).focus(function() {
					if (tweening) {
						timing = true;
						$("#advancedNewsletter").show("slow", function() {timing = false;});
					} else {
						$("#advancedNewsletter").show();
					}
				});
				
				$(this).keypress(function(e) {
					if ($(this).val() == $(this).attr("rel")) {
						$(this).val("");
					}
					removeFieldError(this.id);
					
					if (e.which == 13) {
						newsletterIt();	
					}
				});
			});
		}
	}
	
	$("#newsletter-arrow").click(function(){
		newsletterIt();
	});
	
}

function checkField(field)
{
	var val = $("input#" + field).val();
	
	if (val && val != $("input#" + field).attr("rel")) {
		return val;
	} else {
		setFieldError(field);
		return false;
	}
}

function setFieldError(field)
{
	$("input#" + field).removeClass("normal");
	$("input#" + field).addClass("error");
	$("input#" + field).focus();
}

function removeFieldError(field)
{
	$("input#" + field).removeClass("error");
	$("input#" + field).addClass("normal");
}

function isEmail(str) {

	if (str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		
		if (str.indexOf(at)==-1){
		   return false;
		}
	
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}
	
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}
	
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }
	
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }
	
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }
	}
	
	return true;					
}


function newsletterSuccess()
{
	$("#newsletter-form").html("<p>" + $("#newsletter-form").attr("rel") + "</p>");	
}

function newsletterIt()
{
	$("div#newsletter input").each(function(){
		removeFieldError(this.id);
	});
	
	var params = $("#form-newsletter").serialize();

	var email = checkField("newsletter-email-input");
	var emailRepeat = checkField("newsletter-email-repeat-input");
	var firstname = checkField("newsletter-firstname-input");
	var name = checkField("newsletter-name-input");
	
	if (email != emailRepeat) {
		setFieldError("newsletter-email-input");
		setFieldError("newsletter-email-repeat-input");
	}

	if (!isEmail(email)) {
		setFieldError("newsletter-email-input");
	}
	
	if (!isEmail(emailRepeat)) {
		setFieldError("newsletter-email-repeat-input");
	}
	
	if (isEmail(email) && firstname && name && email && (email == emailRepeat)) {
		$("#newsletter-arrow").hide();
		$.ajax({
		   type: "POST",
		   url: "/ajax/validate-newsletter/",
		   data: params,
		   success: function(msg){
				newsletterSuccess();
		   }
		 });
		
	}
}

function tooltipCode()
{
	$("#videoHolder, #imageHolder").hover(
		function(){
			showTooltip($(this).attr("rel"));
		},function(){
			hideTooltip();
		}
	);
		

	$("body").mousemove(function(e){
		var posLeft = e.pageX - $("#tooltip").width() - 15;
		if (posLeft < 10) {
			posLeft = e.pageX+10;
		}
		$("#tooltip-thumb").css("top", e.pageY - 30);
		$("#tooltip").css("left", posLeft);
		$("#tooltip").css("top", e.pageY - 5);
		
	});
}

function hideTooltip()
{
	$("#tooltip").hide();
}
 
function showTooltip(content)
{	
	if(document.body){
		$("#tooltip").remove();
		var aElement=document.createElement("div");
		aElement.id = "tooltip";
		document.body.appendChild(aElement);
	}
	
	$("#tooltip").html(content);
	$("#tooltip").fadeIn();
}
/*
function slideSwitch() {
    var $active = $('div.jSlideshow img.active');
    if ( $active.length == 0 ) $active = $('div.jSlideshow img:last');
    var $next =  $active.next().length ? $active.next() : $('div.jSlideshow img:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 500, function() {
            $active.removeClass('active last-active');
        });
}*/
