/* Body onLoad */
$(document).ready(function () {	
	initialize ();
});
/* Body onLoad END */

/* Initialize */
function initialize () {
	if ($("#hover_menu").length > 0) {
		if ($("#hover_menu.projects").length > 0) {
			$(window).resize(function () {
				calculate_center ("projects");
			});
			calculate_center ("projects");
		}
		else if ($("#hover_menu.products").length > 0) {
			$(window).resize(function () {
				calculate_center ("products");
			});
			calculate_center ("products");
		}
	}

	
	bind_events ();
	
	$("#ajax_content").hide();
	
	/*$(".box img, .box_square img").hide();
	show_pics();		
	
	$("#content").hide().slideDown();
	$("#content_white").hide().slideDown();*/
	
	$(".main_bg").load(function () {
		$(".main_bg:last").fadeOut();
	});	
	
	$("#big_arrow_right, #big_arrow_left").fadeTo("fast", 0);
	init_latest_work_detail ();
	init_products_detail ();
}
/* Initialize END */

/* Bind Events */
function bind_events () {
	// Audioplay Click
	//$(".big_box a").bind("click", audioplay_click);
	
	// Contact Call Hover
	$("#contactcall").hover(
		function () {
			$(this)
				.stop()			
				.animate({
					"top":"-7px",
					"right":"-7px"
				}, "normal", function () {
					$(this).attr({"src":"css/img/EN_contactcall_hover.png"});
				});
		}, 
		function () {
			$(this)
				.stop()			
				.animate({
					"top":"-35px",
					"right":"-10px"
				}, "normal" , function () {
					$(this).attr({"src":"css/img/EN_contactcall.png"});
				});
		}
	);	
	
	//  Box Hover
	//$(".box_dummy_player_hover").css({"opacity":"0"});
	$("#content .box a .box_overlay, #content .box_square a .box_overlay")
		.css({"opacity":"0.5"})
		.hover(
			function () {
				$(this).stop().fadeTo("fast", 0);
				//$(this).prev(".box_voice_actors_container").children(".box_dummy_player_hover").fadeTo("fast", 1);
				//$(this).prev(".box_voice_actors_container").children(".box_dummy_player").fadeTo("fast", 0);
			},
			function () {
				$(this).stop().fadeTo("normal", 0.5);
				//$(this).prev(".box_voice_actors_container").children(".box_dummy_player_hover").fadeTo("fast", 0);
				//$(this).prev(".box_voice_actors_container").children(".box_dummy_player").fadeTo("fast", 1);				
			}		
		);	
	
	// Big Box Hover
	$("#content .big_box a")
		.css({"opacity":"0.7"})
		.bind("mouseover", {opacity:"1", speed:"normal"}, hover_to)
		.bind("mouseout", {opacity:"0.7", speed:"fast"}, hover_to);

	
	// Filter Menu Hover
	/*$("#hover_menu")
		.css({"opacity":"0.3"})
		.hover(
			function (e) {
				$(this).stop().fadeTo("fast", 0.6);
			},
			function (e) {
				$(this).stop().fadeTo("normal", 0.3);
			}		
		);*/

	$(	"#hover_menu button, #hover_menu #latest_work_all_none span, #hover_menu #products_all_none span, #hover_menu.detail #hover_menu_left, #hover_menu.detail #hover_menu_right a")
		.css({"opacity":"0.7"})
		.hover(
			function (e) {
				$(this).stop().fadeTo("fast", 1);
			},
			function (e) {
				$(this).stop().fadeTo("fast", 0.7);
			}		
		);		
	
	// Search Form Click
	$("#search_input")
		.click(function () {
			if ($(this).attr("value") == "SEARCH...") {
				$(this).attr({"value":""});
			}
		})
		.blur(function () {
			if ($(this).attr("value") == "") {
				$(this).attr({"value":"SEARCH..."});
			}
		});		
		
	/*if (!window.ActiveXObject) {
		modify_links();
		
		$("#left ul li a").click(function () {
			$("#left ul li a").removeClass("active");
			$(this).addClass("active");
		});		
	}*/		
	
	// bindet click-event an alle links
	/*$("a").blur().bind('click', function(e) {
		e.preventDefault(); // verhindert die ausführung von standart html events
		url = $(this).attr("href"); // href des angeklickten menupunkt auslesen

		$('html,body').animate({scrollTop: 0}, 300, function () { // browser fenster hochscrollen
			top.location.href = url;
		});		
	});*/	
	
	// Latest Work
	$("#hover_menu .latest_work").bind("click", function (e) {
		e.preventDefault();
		if ($(this).find("img").attr("src") == "css/img/btn_red.gif") {
			$(this).find("img").attr("src", "css/img/btn_green.gif");
			$(this).prev("input").val($(this).val());
		}
		else {
			$(this).find("img").attr("src", "css/img/btn_red.gif");
			$(this).prev("input").val(0); 
		}
		
		get_str = "";
		i = 0;
		$.each($(this).parent("form").children("input"), function () {
			if (i != 0) {
				get_str += ",";
			}
			i++;
			get_str += $(this).val();			
		});
		http_request_latest_work(get_str);
	});
	
	$("#hover_menu #latest_work_all_none .hover_menu_filter_none").bind("click", function (e) {
		e.preventDefault();
		$(this).parent().next("form").find("img").attr("src", "css/img/btn_red.gif");
		$(this).parent().next("form").find("input").val(0);
		
		get_str = "";
		i = 0;
		$.each($(this).parent("form").children("input"), function () {
			if (i != 0) {
				get_str += ",";
			}
			i++;
			get_str += $(this).val();			
		});
		http_request_latest_work(get_str);		
	});
	
	$("#hover_menu #latest_work_all_none .hover_menu_filter_all").bind("click", function (e) {
		e.preventDefault();
		$(this).parent().next("form").find("img").attr("src", "css/img/btn_green.gif");
		$(this).parent().next("form").find("button").each(function() {
			$(this).prev("input").val($(this).val());
		});
		
		get_str = "";
		i = 0;
		$.each($(this).parent().next("form").children("input"), function () {
			if (i != 0) {
				get_str += ",";
			}
			i++;
			get_str += $(this).val();			
		});
		http_request_latest_work(get_str);		
	});
	
	// Products
	$("#hover_menu .products").bind("click", function (e) {
		e.preventDefault();
		if ($(this).find("img").attr("src") == "css/img/btn_red.gif") {
			$(this).find("img").attr("src", "css/img/btn_green.gif");
			$(this).prev("input").val($(this).val());
		}
		else {
			$(this).find("img").attr("src", "css/img/btn_red.gif");
			$(this).prev("input").val(0); 
		}
		
		get_str = "";
		i = 0;
		$.each($(this).parent("form").children("input"), function () {
			if (i != 0) {
				get_str += ",";
			}
			i++;
			get_str += $(this).val();			
		});
		http_request_products(get_str);
	});
	
	$("#hover_menu #products_all_none .hover_menu_filter_none").bind("click", function (e) {
		e.preventDefault();
		$(this).parent().next("form").find("img").attr("src", "css/img/btn_red.gif");
		$(this).parent().next("form").find("input").val(0);
		
		get_str = "";
		i = 0;
		$.each($(this).parent("form").children("input"), function () {
			if (i != 0) {
				get_str += ",";
			}
			i++;
			get_str += $(this).val();			
		});
		http_request_products(get_str);		
	});
	
	$("#hover_menu #products_all_none .hover_menu_filter_all").bind("click", function (e) {
		e.preventDefault();
		$(this).parent().next("form").find("img").attr("src", "css/img/btn_green.gif");
		$(this).parent().next("form").find("button").each(function() {
			$(this).prev("input").val($(this).val());
		});
		
		get_str = "";
		i = 0;
		$.each($(this).parent().next("form").children("input"), function () {
			if (i != 0) {
				get_str += ",";
			}
			i++;
			get_str += $(this).val();			
		});
		http_request_products(get_str);		
	});	
}
/* Bind Events END */

/* Init Latest Work Detail */
function init_latest_work_detail () {
	$(".bg_box_latest_work_detail:not(.active)").fadeTo("normal", 0.5);
	$(".bg_box_latest_work_detail.active").fadeTo("normal", 1);
	
	if ($(".bg_box_latest_work_detail:last").hasClass("active")) {
		$(".latest_work_scroller #big_arrow_right")
			.unbind("mouseenter")
			.unbind("mouseleave")
			.unbind("click")
			.fadeTo("fast", 0)
			.css("cursor", "default");
	}
	else {
		$(".latest_work_scroller #big_arrow_right")
			.bind("mouseenter", big_arrow_over)
			.bind("mouseleave", big_arrow_out)
			.bind("click", big_arrow_right_click)
			.css("cursor", "pointer");
	}
		
	if ($(".bg_box_latest_work_detail:first").hasClass("active")) {
		$(".latest_work_scroller #big_arrow_left")
			.unbind("mouseenter")
			.unbind("mouseleave")
			.unbind("click")
			.fadeTo("fast", 0)
			.css("cursor", "default");
	}
	else {
		$(".latest_work_scroller #big_arrow_left")
			.bind("mouseenter", big_arrow_over)
			.bind("mouseleave", big_arrow_out)
			.bind("click", big_arrow_left_click)
			.css("cursor", "pointer");
	}			
}
/* Init Latest Work Detail END */

/* Init Products Detail */
function init_products_detail () {
	$(".bg_box_products_detail:not(.active)").fadeTo("normal", 0.5);
	$(".bg_box_products_detail.active").fadeTo("normal", 1);
	
	if ($(".bg_box_products_detail:last").hasClass("active")) {
		$(".products_scroller #big_arrow_right")
			.unbind("mouseenter")
			.unbind("mouseleave")
			.unbind("click")
			.fadeTo("fast", 0)
			.css("cursor", "default");
	}
	else {
		$(".products_scroller #big_arrow_right")
			.bind("mouseenter", big_arrow_over)
			.bind("mouseleave", big_arrow_out)
			.bind("click", big_arrow_right_click_square)
			.css("cursor", "pointer");
	}
		
	if ($(".bg_box_products_detail:first").hasClass("active")) {
		$(".products_scroller #big_arrow_left")
			.unbind("mouseenter")
			.unbind("mouseleave")
			.unbind("click")
			.fadeTo("fast", 0)
			.css("cursor", "default");
	}
	else {
		$(".products_scroller #big_arrow_left")
			.bind("mouseenter", big_arrow_over)
			.bind("mouseleave", big_arrow_out)
			.bind("click", big_arrow_left_click_square)
			.css("cursor", "pointer");
	}			
}
/* Init Products Detail END */

/* Latest Work Detail Event Functions */
function big_arrow_over() {
	$(this).fadeTo("fast", 1);
}

function big_arrow_out() {
	$(this).fadeTo("fast", 0);
}

function big_arrow_left_click() {
	$("#big_arrow_right, #big_arrow_left").unbind("click");
	var padd = $("#scroller").css("paddingLeft");
	padd = padd.split("px");
	padd_int = parseInt(padd[0])+700;
	$(".bg_box_latest_work_detail.active").removeClass("active").prev().addClass("active");
	$("#scroller").animate({"paddingLeft":""+padd_int+"px"}, function () {
		init_latest_work_detail ();
	});		
}

function big_arrow_right_click() {
	$("#big_arrow_right, #big_arrow_left").unbind("click");
	var padd = $("#scroller").css("paddingLeft");
	padd = padd.split("px");
	padd_int = parseInt(padd[0])-700;
	$(".bg_box_latest_work_detail.active").removeClass("active").next().addClass("active");
	$("#scroller").animate({"paddingLeft":""+padd_int+"px"}, function () {
		init_latest_work_detail ();
	});
	alert("kdflfjkdfjk");
}

function big_arrow_left_click_square() {
	$("#big_arrow_right, #big_arrow_left").unbind("click");
	var padd = $("#scroller").css("paddingLeft");
	padd = padd.split("px");
	padd_int = parseInt(padd[0])+473;
	$("#scroller").animate({"paddingLeft":""+padd_int+"px"}, function () {
	$(".bg_box_products_detail.active").removeClass("active").prev().addClass("active");	
		init_products_detail ();
	});		
}

function big_arrow_right_click_square() {
	$("#big_arrow_right, #big_arrow_left").unbind("click");
	var padd = $("#scroller").css("paddingLeft");
	padd = padd.split("px");
	padd_int = parseInt(padd[0])-473;
	$(".bg_box_products_detail.active").removeClass("active").next().addClass("active");	
	$("#scroller").animate({"paddingLeft":""+padd_int+"px"}, function () {
		init_products_detail ();
	});
	
}
/* Latest Work Detail Event Functions END */

/* Audioplay Event Functions */
/*function audioplay_click (e) {
	e.preventDefault();
	$(".big_box a").unbind("click");
	
	if (!$(this).hasClass("active")) {
		$(this).addClass("active")
			.unbind("mouseover").unbind("mouseout");
		
		url = $(this).attr("href");		
		var dummy = url.split("&audio_id=");
		var audio_id = dummy[1];	

		if ($("a:not(.active)").parent(".big_box").size() > 0) {
			$("a:not(.active)").parent(".big_box").slideUp(function () {
				http_request_audioplay(audio_id);
				$(".big_box a").bind("click", audioplay_click);
			});
		}
		else {
			http_request_audioplay(audio_id);
			$(".big_box a").bind("click", audioplay_click);		
		}
	}
	else {
		$("#ajax_content").slideUp(function () {
			$("a:not(.active)").parent(".big_box").slideDown();
			$(".big_box a").bind("click", audioplay_click);
		});

		$(this).removeClass("active")
			.bind("mouseover", {opacity:"1", speed:"normal"}, hover_to)
			.bind("mouseout", {opacity:"0.7", speed:"fast"}, hover_to);			
	}
}*/
/* Audioplay Event Functions END */

/* Private Functions */
function hover_to (e) {
	$(this).stop().fadeTo(e.data.speed, e.data.opacity);
}

function calculate_center (site) {
	if (site == 'projects') {
		var divisor = 281;
	}
	else {
		var divisor = 167;
	}
	var n = Math.floor(($("body").width() - $("#left").width() -50 ) / divisor);
	var center_width = divisor * n;
	if (center_width < 550) {
		center_width = 550;
	}
	$("#header, #center").css({"width":""+center_width+"px"});
}

/*function calculate_box_space () {
	var width_content = $("#content").width();
	var n = Math.floor(width_content / (273+8));
	var extra_margin = Math.floor((width_content  - n * (273+8)) / (n - 1)) + 10;
	var count_box = $(".box").length;
	for (i=0; i<count_box; i++) {
		if ((i+1) % n != 0) {
			$(".box:eq("+i+")").css({"margin-right":"" + extra_margin + "px"});
			$(".box:eq("+i+")").css({"margin-bottom":"" + extra_margin + "px"});
		}
		else {
			$(".box:eq("+i+")").css({"margin-right":"8px"});
			$(".box:eq("+i+")").css({"margin-bottom":"" + extra_margin + "px"});
		}
	}
}*/

/*function change_bg () {
	if ($(".main_bg").attr("src") == "css/img/main_bg_0.jpg") {
		$(".main_bg")
			.before('<img class="main_bg" src="css/img/main_bg_1.jpg" alt="" />')
			.fadeOut("slow", function () {
				$(this).remove();
			}
		);
	}
	else {
		$(".main_bg")
			.before('<img class="main_bg" src="css/img/main_bg_0.jpg" alt="" />')
			.fadeOut("slow", function () {
				$(this).remove();
			}
		);
	}
}*/

function show_pics () {
	$(".box img:hidden:first, .box_square img:hidden:first").fadeIn("fast");
	if ($(".box img:hidden, .box_square img:hidden").length > 0) {
		window.setTimeout("show_pics()", 30);
	}
}
/* Private Functions END */
	
	
	
	
/* Audioplay Navigation */	
var please_wait = null;

function http_request_audioplay(audio_id) {
	if (window.XMLHttpRequest) {
		link = new XMLHttpRequest(); // Mozilla, Safari, Opera
	} else if (window.ActiveXObject) {
		try {
			link = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		} catch (e) {
			try {
				link = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			} catch (e) {}
		}
	}
	
	if (link == undefined) {
		return false;
	}

	link.onreadystatechange = function() { response_audioplay(); }
	link.open("GET", "include/audioplays_kat.php?audio_id="+audio_id+"", true);
	link.send(null);
}

function response_audioplay() {
 	if (link.readyState == 4) {
		$("#ajax_content").html("").html(link.responseText).slideDown("fast");
	}	
}
/* Audioplay Navigation END */
	
	
	
	
/* Ajax Navigation */
var please_wait = null;

//function open_url(url, target) {
function open_url(e) {
	url = e.data.url;
	target = e.data.target;


 	if ( ! document.getElementById(target)) {
  		return false;
 	}

	var $content = $('#content');
	change_bg();
    if( $content[0] && $content[0].firstChild ) {
		$("#content").children().stop().fadeOut("fast", function () {
			http_request(url, target);
		});			
	}
	else {
		http_request(url, target);
	}
	
}

function http_request(url, target) {
	if (window.XMLHttpRequest) {
		link = new XMLHttpRequest(); // Mozilla, Safari, Opera
	} else if (window.ActiveXObject) {
		try {
			link = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		} catch (e) {
			try {
				link = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			} catch (e) {}
		}
	}
	
	if (link == undefined) {
		return false;
	}

	link.onreadystatechange = function() { response(url, target); }
	link.open("GET", url, true);
	link.send(null);
}

function response(url, target) {
 	if (link.readyState == 4) {
		$("#content").html("").html(link.responseText);
		$(".box img").hide();		
		$("#content").stop().fadeIn("fast");
		show_pics();
	}	
}

function modify_links () {	
	$("a:not([attr*=javascript:void(0)])").each(function () {
		url = $(this).attr("href");		

		switch (url) {
			case "index.php?c=latest_work":
				file = "latest_work.php";
				break;
			case "index.php?c=who":
				file = "who.php";
				break;
			case "index.php?c=contact":
				file = "contact.php";
				break;
			default:
				file = "latest_work.php";
				break;				
		}
		
		$(this).attr({"href":"javascript:void(0)"})
			.bind("click", {url:"./include/"+file+"", target:"content"}, open_url);
	});
}
/* Ajax Navigation END */

/* Ajax LatestWork */
function http_request_latest_work(filter) {
	if (window.XMLHttpRequest) {
		link = new XMLHttpRequest(); // Mozilla, Safari, Opera
	} else if (window.ActiveXObject) {
		try {
			link = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		} catch (e) {
			try {
				link = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			} catch (e) {}
		}
	}
	
	if (link == undefined) {
		return false;
	}

	link.onreadystatechange = function() { response_latest_work(); }
	link.open("GET", "include/latest_work_ajax.php?filter="+filter, true);
	link.send(null);
}

function response_latest_work() {
 	if (link.readyState == 4) {
		$("#content").html("").html(link.responseText);
		//bind_events ();
	$("#content .box a .box_overlay")
		.css({"opacity":"0.5"})
		.hover(
			function () {
				$(this).stop().fadeTo("fast", 0);
			},
			function () {
				$(this).stop().fadeTo("normal", 0.5);
			}		
		);			
		$("#content").stop().fadeIn("fast");
	}	
}
/* Ajax LatestWork END */

/* Ajax Products */
function http_request_products(filter) {
	if (window.XMLHttpRequest) {
		link = new XMLHttpRequest(); // Mozilla, Safari, Opera
	} else if (window.ActiveXObject) {
		try {
			link = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		} catch (e) {
			try {
				link = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			} catch (e) {}
		}
	}
	
	if (link == undefined) {
		return false;
	}

	link.onreadystatechange = function() { response_products(); }
	link.open("GET", "include/products_ajax.php?filter="+filter, true);
	link.send(null);
}

function response_products() {
 	if (link.readyState == 4) {
		$("#content").html("").html(link.responseText);
		//bind_events ();
	$("#content .box_square a .box_overlay")
		.css({"opacity":"0.5"})
		.hover(
			function () {
				$(this).stop().fadeTo("fast", 0);
			},
			function () {
				$(this).stop().fadeTo("normal", 0.5);
			}		
		);			
		$("#content").stop().fadeIn("fast");
	}	
}
/* Ajax Products END */
