/**
 * @author adgamemedia:RF
 */

var footer_boxes = new Array();
footer_boxes[0] = "#getintouch";
footer_boxes[1] = "#getinside";
footer_boxes[2] = "#getthenews";
var min_height;
var header_height;
var footer_height;
var content_width;

/*
 * (Fehler-)Meldungen in allen Sprachen
 */
var languages = new Object();
languages.en = new Object();
languages.de = new Object();
languages.en.mail_warning = "Please check your email address!";
languages.de.mail_warning = "Bitte überprüfen Sie Ihre E-Mail Adresse!";
languages.en.subject_warning = "Please select one or more subjects!";
languages.de.subject_warning = "Bitte wählen Sie ein oder mehrere Themen!";
languages.en.username_warning = "Please check your username!";
languages.de.username_warning = "Bitte überprüfen Sie Ihren Benutzernamen!";
languages.en.password_warning = "Please check your password!";
languages.de.password_warning = "Bitte überprüfen Sie Ihr Passwort!";

var texts = new Object();
texts = languages.en;
var subdomain = '';
if(location.href.search(/beta/) != -1) {
	subdomain = '-beta';
}

$(document).ready( function() {
	if($.browser.safari) {
		$("#header_right").css("background-position", "20px 50%");
	}
	min_height = $("#heightmaker").outerHeight();
	header_height = $("#header").outerHeight();
	footer_height = $("#footer").outerHeight();
	content_width = $("#content").outerWidth();
	window.setInterval("setFooterBottom()", 50);

	$(".deutsch").fadeOut(0);
	$("imprint_link").children(".close").fadeOut(0);
	$("#switch_to_german").click( function() {
		$(".english").fadeOut(0);
		$(".english").addClass("absolute");
		$(".deutsch").removeClass("absolute");
		$(".deutsch").fadeIn("slow");
		texts = languages.de;
	});
	$("#switch_to_english").click( function() {
		$(".deutsch").fadeOut(0);
		$(".deutsch").addClass("absolute");
		$(".english").removeClass("absolute");
		$(".english").fadeIn("slow");
		texts = languages.en;
	});

	$("#content > .casebox").hover( function() {
		$(this).children(".color-img").fadeIn("slow");
	}, function() {
		$(this).children(".color-img").fadeOut("slow");
	});

	$(".footer_box > h1").click( function() {
		hideFootBoxes('#' + $(this).parent().attr("id"))
		$(this).parent().children("div").slideToggle("slow");
		$(this).parent().children(".topline").toggleFade("slow");
		$(this).children(".yeli").toggleFade("slow");
	});

	$(".checkbox").click( function() {
		if($(this).attr("class") == "checkbox_a") {
			$(this).attr("class", "checkbox_o");
		} else {
			$(this).attr("class", "checkbox_a");
		}
	});
	$(".checkbox").hover( function() {
		if($(this).attr("class") != "checkbox_a") {
			$(this).attr("class", "checkbox_o");
		}
	}, function() {
		if($(this).attr("class") != "checkbox_a") {
			$(this).attr("class", "checkbox");
		}
	});
	
	$("input").hover( function() {
		$(this).attr("class", "input_hover");
	}, function() {
		$(this).attr("class", "");
	});
	
	$("button").hover( function() {
		$(this).attr("class", "button_hover");
	}, function() {
		$(this).attr("class", "");
	});

	/*
	 * nach Absenden des Loginformulars
	 */
	$("#login_button").click( function() {
		login();
	});
	$("#password").keyup( function(event) {
		if (event.keyCode == '13') {
			login();
		}
	});
	$("#username").keyup( function(event) {
		if (event.keyCode == '13') {
			login();
		}
	});
	
	$("#getinside").find(".close").click( function() {
		$("#login_answer").slideUp("slow");
		$("#login_success").fadeOut("fast");
		$("#login_error").fadeOut("fast");
		$("#login_timeout").fadeOut("fast");
	});

	$("#newsletter_button").click( function() {
		if ($("#email").val().search(/.*@..*\....*/) == -1) {
			alert(texts.mail_warning);
		} else if($("#general").attr("class") == "checkbox" && $("#cases").attr("class") == "checkbox" && $("#angebote").attr("class") == "checkbox") {
			alert(texts.subject_warning);
		} else {
			$("#newsletter_answer").slideDown("slow");
			$.post("ajax/newsletter_entry.php", {
				general: $("#general").attr("class"),
				cases: $("#cases").attr("class"),
				angebote: $("#angebote").attr("class"),
				email: $("#email").val()
			}, function(data){
				newsletter_response(data);
			}, "text");
		}
	});
	
	$("#getthenews").find(".close").click( function() {
		$("#newsletter_answer").slideUp("slow");
		$("#newsletter_success").fadeOut("fast");
		$("#newsletter_error").fadeOut("fast");
	});

	$("#imprint_link > .open").click( function() {
		$("#imprint").slideToggle("slow");
		$(this).toggleFade("slow");
		$(this).next().toggleFade("slow");
		$("#brown_shade").fadeTo(0, 0);
		$("#brown_shade").css("display", "block");
		$("#brown_shade").fadeTo("slow", 0.9);
	});
	$("#imprint_link > .close").click( function() {
		$("#imprint").slideToggle("slow");
		$(this).prev().toggleFade("slow");
		$(this).toggleFade("slow");
		$("#brown_shade").fadeOut("slow");
	});
	$("#brown_shade").click( function() {
		$("#imprint").slideToggle("slow");
		$("#imprint_link > .open").toggleFade("slow");
		$("#imprint_link > .close").toggleFade("slow");
		$(this).fadeOut("slow");
	});
});

(function($) {
  $.fn.toggleFade = function(settings)
  {
  	settings = jQuery.extend(
  		{
        speedIn: "normal",
        speedOut: settings.speedIn
  		}, settings
  	);
  	return this.each(function()
  	{
  	  var isHidden = jQuery(this).is(":hidden");
      jQuery(this)[ isHidden ? "fadeIn" : "fadeOut" ]( isHidden ? settings.speedIn : settings.speedOut);
    });
  };
})(jQuery);

function setFooterBottom() {
	if ($(window).height() > min_height + header_height + footer_height) {
		var content_height = $(window).height() - header_height - footer_height;
		$("#heightmaker").css("height", content_height + "px");
		if ($.browser.msie && $.browser.version.slice(0, 1) == 6) {
			$("#content").css("margin-top", ((content_height - min_height - 10) / 5) + "px");
		}
		else {
			$("#content").css("margin-top", ((content_height - min_height - 10) / 2) + "px");
		}
	} else {
		$("#heightmaker").css("height", min_height + "px");
		$("#content").css("margin-top", "0px");
	}
	if($(window).width() < content_width) {
		$("#content").css("left", "0");
		$("#content").css("margin-left", "0");
		$("#footer").css("left", "0");
		$("#footer").css("margin-left", "0");
	} else {
		$("#content").css("left", "50%");
		$("#content").css("margin-left", "-465px");
		$("#footer").css("left", "50%");
		$("#footer").css("margin-left", "-470px");
	}
}

function hideFootBoxes(do_not_hide) {
	for(var i=0; i<footer_boxes.length; i++) {
		if (footer_boxes[i] != do_not_hide) {
			$(footer_boxes[i]).children("div").slideUp("slow");
			$(footer_boxes[i]).children(".topline").fadeOut("slow");
			$(footer_boxes[i] + ' > h1').children(".yeli").fadeOut("slow");
		}
	}
}

function login() {
	$("#login_waiting").fadeIn("fast");
	if ($("#username").val() == "") {
		alert(texts.username_warning);
	} else if($("#password").val() == "") {
		alert(texts.password_warning);
	} else {
		$("#login_answer").slideDown("slow");
		$.getJSON("http://login" + subdomain + ".adgame-media.com/ajax/login/username/" + $("#username").val() + "/password/" + $("#password").val() + "/login/1?format=json&jsoncallback=?", function(data){
			login_response(data)
		});
		timeout = window.setTimeout("login_timeout()", 10000);
	}
}

function login_response(data) {
	$("#login_waiting").fadeOut("fast");
	window.clearTimeout(timeout);

	switch (data.result) {
		case 0:
			$("#login_error").fadeIn("slow");
		break;
		default:
			$("#login_success").fadeIn("slow");
			location.href = 'http://login' + subdomain + '.adgame-media.com';
	}
}

function login_timeout() {
	$("#login_waiting").fadeOut("fast");
	$("#login_timeout").fadeIn("slow")
}

function newsletter_response(data) {
	$("#newsletter_waiting").fadeOut("fast");
	switch (Number(data)) {
		case 1:
			$("#newsletter_success").fadeIn("slow");
			$("#general").attr("class", "checkbox");
			$("#cases").attr("class", "checkbox");
			$("#angebote").attr("class", "checkbox");
			$("#email").val("");
		break;
		default:
			$("#newsletter_error").fadeIn("slow");
	}
}