/**
 * @author metalmatze.de
 */
serverurl = "http://www.audio-machinery.com/";
//serverurl = "http://192.168.2.100/Aptana/audio-machinery/web/";
$(document).ready(function() {
	$(".jsremove").remove();
	if ($("#introlayer").is(":visible")) {
		$("#introlink").css("display", "none");
		$("#introlink").fadeIn(3000).delay(1500).fadeOut(2000, function() {
			$("#introlayer").fadeOut(2000);
			$.get(serverurl + "ajax.php?todo=intro");
		});
	}
	$("#introlink").live("click", function(e){
		e.preventDefault();
	});
    $("#notify_confirm > a").live("click", function(e){
        e.preventDefault();
        $("#notify_div").stop().slideUp("slow");
     });
    // L O G I N
    $("#loginlink").live("click", function(e) {
        e.preventDefault();
        $("#formlogindiv").slideToggle("slow");
    });
    $("#logincancel").live("click", function(e) {
        $("#formlogindiv").slideUp("slow");
    });
    $("#loginsubmit").live("click", function(e) {
    	e.preventDefault();
    	$("#notify_div").slideUp("fast");
		$.post(serverurl + "ajax.php?todo=login", {
			user : $("#loginuser").val(),
			pass : $("#loginpass").val()
		}, function(login) {
			if (login.success) {
				window.location.replace(serverurl + "admin"); 
			}
			else {
				$("#notify_message").html(login.message);
				$("#notify_div").slideDown("slow");
			}
		}, "json");
    });
    // L O G I N  E N D E
    $(".social_div a").qtip({
        style: { 
            name: 'dark',
            width: 220,
            tip: 'bottomMiddle',
            
            border: {
                width: 0,
                radius: 2
            }
        },
        position: {
            corner: {
                opacity: 0.4,
                target: 'topMiddle',
                tooltip: 'bottomMiddle'
                    }
        },
        show: { effect: { length: 250 } },
        hide: { effect: { length: 500 } },
    });
    $(".social_div").removeClass("highlight");
    $(".social_icon").append('<span class="hover amsprite" />').each(function() {
        var $span = $("> span.hover", this).css("opacity", 0);
        $(this).hover(function() {
            // on hover
            $span.stop().fadeTo(250, 1);
        }, function() {
            // off hover
            $span.stop().fadeTo(1000, 0);
        });
    });

    $('a.elasticfancybox').live('mouseover', function(){ $(this).fancybox({
    	'titleShow'     : true,
    	'transitionIn'  : 'elastic',
    	'transitionOut' : 'elastic',
    	'overlayColor'  : '#000',
    	'overlayOpacity': '0.8',
    	'padding'       : 0
    }) });

	$("#studio-map_overlay").live("mousemove", function(e){
        var offset = $(this).offset();
        var x = e.pageX - offset.left;
        var y = e.pageY - offset.top;
        // Regie A
        if (y > 3 && y < 78 && x > 93 && x < 248) {
            $(this).css("backgroundPosition", "0 -191px");
		}
        // Aufnahme
        else if (y > 81 && y < 151 && x > 119 && x < 214) {
            $(this).css("backgroundPosition", "0 -382px");
        }
        // Maschinen
        else if (y > 81 && y < 151 && x > 94 && x < 117) {
            $(this).css("backgroundPosition", "0 -573px");
        }
        // Gang
        else if (y > 81 && y < 151 && x > 216 && x < 247) {
            $(this).css("backgroundPosition", "0 -764px");
        }
        // Regie B
        else if (y > 3 && y < 51 && x > 256 && x < 307) {
            $(this).css("backgroundPosition", "0 -955px");
        }
        // Lounge 1
        else if (y > 3 && y < 51 && x > 307 && x < 396) {
            $(this).css("backgroundPosition", "0 -1146px");
        }
        // Lounge 2
        else if (y > 51 && y < 99 && x > 256 && x < 396) {
            $(this).css("backgroundPosition", "0 -1146px");
        }
        // Eingang
        else if (y > 102 && y < 151 && x > 255 && x < 323) {
            $(this).css("backgroundPosition", "0 -1337px");
        }
        // Kueche
        else if (y > 102 && y < 151 && x > 324 && x < 395) {
            $(this).css("backgroundPosition", "0 -1528px");
        }
        // Bad
        else if (y > 154 && y < 188 && x > 282 && x < 394) {
            $(this).css("backgroundPosition", "0 -1719px");
        }
        // Terrasse
        else if (y > 0 && y < 154 && x > 0 && x < 92) {
            $(this).css("backgroundPosition", "0 -1910px");
        }
		else {
			//$(this).css("backgroundPosition", "0 0");
			switch ($("#studio-active").html()) {
				case 'regiea':
					$(this).css("backgroundPosition", "0 -191px");
					break;
				case 'aufnahme':
					$(this).css("backgroundPosition", "0 -382px");
					break;
				case 'maschinen':
					$(this).css("backgroundPosition", "0 -573px");
					break;
				case 'gang':
					$(this).css("backgroundPosition", "0 -764px");
					break;
				case 'regieb':
					$(this).css("backgroundPosition", "0 -955px");
					break;
				case 'lounge':
					$(this).css("backgroundPosition", "0 -1146px");
					break;
				case 'eingang':
					$(this).css("backgroundPosition", "0 -1337px");
					break;
				case 'kueche':
					$(this).css("backgroundPosition", "0 -1528px");
					break;
				case 'bad':
					$(this).css("backgroundPosition", "0 -1719px");
					break;
				case 'terrasse':
					$(this).css("backgroundPosition", "0 -1910px");
					break;
				default:
					$(this).css("backgroundPosition", "0");
					break;
			}
		}
    });
	$("#studio-map_overlay").live("click", function(e){
        var offset = $(this).offset();
        var x = e.pageX - offset.left;
        var y = e.pageY - offset.top;
        // Regie A
        if (y > 3 && y < 78 && x > 93 && x < 248) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=regiea", function(data){
				$("#studio-map_text").html(data.text);
				$("#studio-map_pics").html(data.pics);
				$("#studio-active").html("regiea");
			});
        }
        // Aufnahme
        else if (y > 81 && y < 151 && x > 119 && x < 214) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=aufnahme", function(data){
                $("#studio-map_text").html(data.text);
                $("#studio-map_pics").html(data.pics);
                $("#studio-active").html("aufnahme");
            });
        }
        // Maschinen
        else if (y > 81 && y < 151 && x > 94 && x < 117) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=maschinen", function(data){
                $("#studio-map_text").html(data.text);
                $("#studio-map_pics").html(data.pics);
                $("#studio-active").html("maschinen");
            });
        }
        // Gang
        else if (y > 81 && y < 151 && x > 216 && x < 247) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=gang", function(data){
                $("#studio-map_text").html(data.text);
                $("#studio-map_pics").html(data.pics);
                $("#studio-active").html("gang");
            });
        }
        // Regie B
        else if (y > 3 && y < 51 && x > 256 && x < 307) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=regieb", function(data){
                $("#studio-map_text").html(data.text);
                $("#studio-map_pics").html(data.pics);
                $("#studio-active").html("regieb");
            });
        }
        // Lounge 1
        else if (y > 260 && y < 308 && x > 817 && x < 906) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=lounge", function(data){
                $("#studio-map_text").html(data.text);
                $("#studio-map_pics").html(data.pics);
                $("#studio-active").html("lounge");
            });
        }
        // Lounge 2
        else if (y > 51 && y < 99 && x > 256 && x < 396) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=lounge", function(data){
                $("#studio-map_text").html(data.text);
                $("#studio-map_pics").html(data.pics);
                $("#studio-active").html("lounge");
            });
        }
        // Eingang
        else if (y > 102 && y < 151 && x > 255 && x < 323) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=eingang", function(data){
                $("#studio-map_text").html(data.text);
                $("#studio-map_pics").html(data.pics);
                $("#studio-active").html("eingang");
            });
        }
        // Kueche
        else if (y > 102 && y < 151 && x > 324 && x < 395) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=kueche", function(data){
                $("#studio-map_text").html(data.text);
                $("#studio-map_pics").html(data.pics);
                $("#studio-active").html("kueche");
            });
        }
        // Bad
        else if (y > 154 && y < 188 && x > 282 && x < 394) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=bad", function(data){
                $("#studio-map_text").html(data.text);
                $("#studio-map_pics").html(data.pics);
                $("#studio-active").html("bad");
            });
        }
        // Terrasse
        else if (y > 0 && y < 154 && x > 0 && x < 92) {
            $.getJSON(serverurl + "ajax.php?todo=rooms&room=terrasse", function(data){
                $("#studio-map_text").html(data.text);
                $("#studio-map_pics").html(data.pics);
                $("#studio-active").html("terrasse");
            });
        }
	});
	
	// C O N T A C T
	$("[name=contactmail]").live("keyup", function(){
		formmail = $("[name=contactmail]").val();
		formmailcheck = formmail.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/g);
		if (formmailcheck) {
			$("[name=contactmail]").css("border", "1px solid #000");
		} else {
			$("[name=contactmail]").css("border", "1px solid #ff0000");
		}
	});
	$("[name=contactname], [name=contactmessage]").live("keyup", function(){
		if ($(this).val() != "") {
			$("[name=contactmessage]").css("border", "1px solid #000");
		} 
	});

	$("[name=contactform]").live("submit", function(e) {
		e.preventDefault();
		$("#notify_div").slideUp();
		lang = $("#contactformlang").html();
		contactname = $("[name=contactname]").val();
		contactmail = $("[name=contactmail]").val();
		contactmessage = $("[name=contactmessage]").val();
		contactwebsite = $("[name=contactwebsite]").val();
		
		notify_message_de = "";
		notify_message_en = "";
		
		if(contactmessage != "") {
			contactmessagesubmit = true;
			$("[name=contactmessage]").css("border", "1px solid #000");
		}else {
			$("[name=contactmessage]").css("border", "1px solid #ff0000");
			contactmessagesubmit = false;
			notify_message_de = "Es wurde keine Nachricht geschrieben.";
			notify_message_en = "There is no message.";
		}
		contactmailcheck = contactmail.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/g);
		if (contactmailcheck) {
			contactmailsubmit = true;
			$("[name=contactmail]").css("border", "1px solid #000");
		} else {
			$("[name=contactmail]").css("border", "1px solid #ff0000");
			contactmailsubmit = false;
			notify_message_de = "Die Email ist nicht g&uuml;ltig.";
			notify_message_en = "This email is not valid.";
		}
		if(contactname != "") {
			contactnamesubmit = true;
			$("[name=contactname]").css("border", "1px solid #000");
		}else {
			$("[name=contactname]").css("border", "1px solid #ff0000");
			contactnamesubmit = false;
			notify_message_de = "Es wurde kein Name angegeben.";
			notify_message_en = "There is no name.";
		}
		
		if (lang == "de") {
			notify_message = notify_message_de;
		} else {
			notify_message = notify_message_en;
		}
		if (contactmessagesubmit == true && contactmailsubmit == true && contactnamesubmit == true) {
			$("[name=contactname]").attr("disabled", "disabled");
			$("[name=contactmail]").attr("disabled", "disabled");
			$("[name=contactmessage]").attr("disabled", "disabled");
			$("#contactsend").attr("disabled", "disabled");
			
			$.post("http://audio-machinery.com/ajax.php", { 
				contactname : contactname,
				contactmail : contactmail,
				contactmessage : contactmessage,
				contactwebsite : contactwebsite
			}, function(data){
				alert(data);
			}, "json");
		}else {
			$("#notify_message").html(notify_message);	
			$("#notify_div").slideDown();			
		}
	});
	// C O N T A C T   E N D
});