$(document).ready(function () {
	
	init_menu();	
	spawn_madness();
	init_media();
    position_woopstrafel();
    
    $(window).resize(function(){
      position_woopstrafel();
    });
    
    $("#intro_directions").animate({ color: "#ffffff" }, 4000).animate({ top: "-200px", opacity: 0 }, 400, "linear", function () {
    	$(this).remove();
    });

    $("#social a").css("opacity", "0.5").hover(function () {
    	$(this).stop().animate({
    		opacity: 1.0
    	},200);
    }, function () {
    	$(this).stop().animate({
    		opacity: 0.5
    	}, 400);
    });

});

function send_mail(obj) {
	obj = $(obj).parent();
	var spam = $(obj).find("input[name=akischmetsch]").val();
	
	if (spam.length > 0) {
		alert("Thou art spammish!");
	} else {
	
	    var name = $(obj).find("input[name=name]").val();
	    var email = $(obj).find("input[name=email]").val();
	    var msg = $(obj).find("textarea[name=message]").val();
	    
	    var send = true;
	    
	    if ($.trim(name).length == 0) {
	    	$(obj).find("input[name=name]").addClass("error");
	    	
	    	send = false;
	    } else {
	    	$(obj).find("input[name=name]").removeClass("error");
	    }
	    
	    if ($.trim(email).length == 0) {
	    	$(obj).find("input[name=email]").addClass("error");
	    	
	    	send = false;
	    } else {
	    	$(obj).find("input[name=email]").removeClass("error");
	    	
	    	if (!isValidEmailAddress($.trim(email))) {
	    		$(obj).find("input[name=email]").addClass("error");
		    	
		    	send = false;
	    	}
	    }
	    
	    
	    if ($.trim(msg).length == 0) {
	    	$(obj).find("textarea[name=message]").addClass("error");
	    	
	    	send = false;
	    } else {
	    	$(obj).find("textarea[name=message]").removeClass("error");
	    }
	    
	    if (send == true) {
	    	$.post("ajax/mimumail.php", {
	    		name: name,
	    		email: email,
	    		msg: msg
	    	}, function () {
	    		$(obj).html("<br />Thanks for contacting!<br />I'll get back to you ASAP!");
	    		Cufon.refresh("#contact");
	    		Cufon.now();
	    	});
	    }
	}
	
    return false;
}

function position_woopstrafel() {
    var view_height = $(window).height();
    
    if (view_height > 640) {
        
        var margin = Math.floor((view_height - 600) / 2);
        
        $("#woopstrafel").css("margin", margin + "px auto");
    } else {
        $("#woopstrafel").css("margin", "20px auto");
    }
}

function init_menu() {
	var menu_selector = $("<div id='menu_selector'></div>").appendTo($("#ls-a"));
	var left_menu = $("<div class='left_menu'></div>").appendTo($(menu_selector));
	var right_menu = $("<div class='right_menu'></div>").appendTo($(menu_selector));
	var middle_menu = $("<div class='middle_menu'></div>").appendTo($(menu_selector));
}

function switch_page(nr) {
    
   var i = 0;
    
   $("#playfield .page").each(function () {
       var top = (860 * ( (nr - 1) - i ) * -1);

       $(this).stop().animate({
            top: top
       }, 1000, "easeInOutCubic"); 
       
       i++;
   });
    
}

function goto_menu(nr) {
	var menu_selector = $("#menu_selector");
	var menu = $("#ls-a");
	
	var i = 1;
	$(menu).find("li").each(function() {
		if (i == nr) {
			var newWidth = $(this).innerWidth();
			var posLeft = $(this).position().left - $(menu).position().left;
            
            $(menu_selector).unbind("click");
            $(menu_selector).click(function() {
                switch_page(nr);
            });
            
			$(menu_selector).stop().animate({
				width: (newWidth + 10) + "px",
				left: (posLeft - 55) + "px"
			}, 300, "easeOutQuint");
		}
		i++;
	});	
}

function pop_textbox(text_content, selector) {
	var textbox = $(selector + " .textbox");
	
	//check if textbox already exists
	if ($(textbox).length == 0) {
		
		//generate text box html
		var textbox = $("<div class='textbox'></div>").appendTo($(selector));
		var boxLeftTop = $("<div class='left_top corner'></div>").appendTo($(textbox));
		var boxTop = $("<div class='top bartb'></div>").appendTo($(textbox));
		var boxRightTop = $("<div class='right_top corner'></div>").appendTo($(textbox));
		var boxLeft = $("<div class='left barlr'></div>").appendTo($(textbox));
		var boxRight = $("<div class='right barlr'></div>").appendTo($(textbox));
		var boxLeftBottom = $("<div class='left_bottom corner'></div>").appendTo($(textbox));
		var boxBotom = $("<div class='bottom bartb'></div>").appendTo($(textbox));
		var boxRightBottom = $("<div class='right_bottom corner'></div>").appendTo($(textbox));
		var boxBackground = $("<div class='background'></div>").appendTo($(textbox));
		
		var boxContent = $("<div class='content'></div>").appendTo($(textbox));
		
		$(boxBackground).css("opacity", 0.7);
		
	} else {
		$(textbox).animate({
			opacity: 0
		}, 200);	
	}
	
	//determine width and height
	var temp_box = $("<span class='temp_text'>" + text_content + "</span>").css({
		"opacity": 0
	}).appendTo("#playfield");
	Cufon.refresh(".temp_text");
	Cufon.now();
	var widthBox = $(temp_box).innerWidth();
	widthBox = 800;
	if (widthBox > 600) {
		$(temp_box).remove();
		var temp_box = $("<div class='temp_text'>" + text_content + "</div>").css({
			"opacity": 0,
			width: "600px"
		}).appendTo("#playfield");
		widthBox = 600;
		Cufon.refresh(".temp_text");
		Cufon.now();
	}
	var heightBox = $(temp_box).innerHeight();
	$(temp_box).remove();
	
	//set the content
	var boxContent = $(textbox).find(".content");
	$(boxContent).html(text_content);
	
	//determine top
	var topBox = (430 / 2) - ((heightBox + 20) / 2);
    var leftBox = (800 / 2) - ((widthBox + 20) / 2);
	
	$(textbox).css({
		width: (widthBox + 20) + "px",
		height: (heightBox + 20) + "px",
		top: topBox,
        left: leftBox,
		opacity: 0
	});
	
	Cufon.refresh(".textbox .content");
	//Cufon.now();
	
	$(textbox).stop().animate({
		opacity: 1
	}, 600);
}

function spawn_madness() {
	var backgroundThing = $("<div id='background_madness'></div>").appendTo($("#playfield .page:first-child"));
	$("<div class='clouds'></div>").appendTo(backgroundThing);
	$("<div class='box_s'></div>").appendTo(backgroundThing);
	$("<div class='box_m'></div>").appendTo(backgroundThing);
	$("<div class='box_l'></div>").appendTo(backgroundThing);
	
	$("html").mousemove(function(e){
    	var xCoord = e.clientX;
        var pos = $("#playfield").position();
        
        var top = $("#playfield .page").css("top");
        top = top.replace("px", "");
        
        if (xCoord > pos.left && xCoord < (pos.left + $("#playfield").innerWidth()) && top == 0) {
            var clientWidth = $("#playfield").innerWidth();
        	var ratio = (xCoord - pos.left) / clientWidth;
        	
        	$("#background_madness div").each(function() {
        		$(this).css({
        			left: ((($(this).css("width").replace("px", "") - 800) * ratio) * -1) + "px"	
        		});
        	});   
        }
    	   	 
    });

}

function init_media() {
    $("#tvset").attr("src", "images/media_tv.jpg");
    $("#tapedeck").attr("src", "images/media_tape.jpg");
    $("#video_list, #audio_list").unbind('mouseenter mouseleave');
    $("#video_list, #audio_list").unbind('click');
    $("#label_video, #label_audio").css("display", "none");
    $("#video_list, #audio_list").css({display: "block", cursor: "pointer"});
    $("#audio_list, #video_list").animate({
        opacity: 0
    }, 500);
    $("#tv_screen").css("z-index", 30).html("");
    $("#video_list ul li").unbind("click");
    $("#audio_list li").each(function() {
        var obj = $(this).find("object");
        
        $(obj).clone().appendTo($(this));
        
        $(obj).remove();
    });
    
	$("#audio_list").hover(function() {
		$("#tvset").attr("src", "images/media_tv.jpg");
		$("#label_video").css("display", "block");
    }, function() {
    	$("#tvset").attr("src", "images/media_tv_gray.jpg");
    	$("#label_video").css("display", "none");
	});
	
	$("#video_list").hover(function() {
		$("#tapedeck").attr("src", "images/media_tape.jpg");
		$("#label_audio").css("display", "block");
    }, function() {
    	$("#tapedeck").attr("src", "images/media_tape_gray.jpg");
    	$("#label_audio").css("display", "none");
	});
    
    $("#video_list").click(function() {
        activate_audio();
    });
    
    $("#audio_list").click(function() {
        activate_video();
    });
}

function activate_audio() {
    $("#video_list, #audio_list").unbind('mouseenter mouseleave');
    $("#video_list, #audio_list").unbind('click');
    
    $("#audio_list").stop().animate({ opacity: 1 }, 500);
    $("#tapedeck").attr("src", "images/media_tape.jpg");
    
    $("#label_audio").css("display", "block");
    $("#label_video").css("display", "none");
}

function activate_video() {
    $("#video_list, #audio_list").unbind('mouseenter mouseleave');
    $("#video_list, #audio_list").unbind('click');
    
    $("#tv_screen").css("z-index", 50);
    
    $("#video_list").stop().animate({ opacity: 1 }, 500);
    $("#tvset").attr("src", "images/media_tv.jpg");
    
    $("#label_video").css("display", "block");
    $("#label_audio").css("display", "none");
    
    $("#video_list ul li").click(function() {
        var id = $(this).attr("rel");
        
        var video = $("#video_list div[rel=" + id + "]").html();
        
        $("#tv_screen").html(video);
    });
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
