$(document).ready( function() {
	
	aObj = "tm-" + active;
	$("#" + aObj).addClass("active");
	
	//set up default easing
	jQuery.easing.def = "easeInOutQuad";

	setUpMainMenu();
	setUpSlider(start);
	setUpCases();
	setUpSpecials();
	setUpSpecialsMenu();
	preloadImages();

    function setUpSlider(start) {
 		if($("#slider").length < 1) { return false; }
 	  	$('#slider').jcarousel({scroll: 1, start: start, animation: 1000});
    	}
    
	function setUpCases() {
		if($("#slider li div.case div.case-preview").length < 1) { return false; }
		$("#slider li div.case div.case-preview div div").hide();
		$("#slider li div.case div.case-preview div").hover(
			function(e) {
				$(this).find(".case-desc").fadeIn("fast");
				},
				function(e) {
					$(this).find(".case-desc").fadeOut("fast");
					}
			);
		}
	
	function setUpSpecials() {
		if($("#specials").length < 1) { return false; }
		$("#specials div div").hide();
		$("#specials div").hover(
			function(e) {
				$(this).find(".proj-desc").fadeIn("fast");
				},
				function(e) {
					$(this).find(".proj-desc").fadeOut("fast");
					}
			);
		}
	
	function setUpSpecialsMenu() {
		if($("#specials-menu").length < 1) { return false; }
		$("#specials-menu img").hover(
			function(e) {
				offsrc = $(this).attr("src");
				onsrc = offsrc.replace(/-off/g, "-on");
				$(this).attr("src",onsrc);
				},
				function(e) {
					$(this).attr("src",offsrc);
					}
			);
		}
	
	function setUpMainMenu() {		

		//set up rollovers for menu items
		$("#topmenu .menu-box .menu-item a").hover(
			function() {
				offset = $(this).position();
				arrow = $(this).parent("p").siblings("div.arrow");
				
				arrow.stop().animate(
					{ top: (parseInt(offset.top)+5)+"px", opacity:1 },
					"fast",
					"swing"
					);
			},
			function() {
				arrow = $(this).parent("p").siblings("div.arrow");
				
				arrow.stop().animate(
					{ top: "20px", opacity:1 },
					"fast",
					"swing"
					);
			}
		);

		//set up rollout
		$("#header").hover(function() {
			menuTimeoutID = setTimeout("slideDownMenu();", 300);
			}, function() {
				if(menuTimeoutID > 0) { clearTimeout(menuTimeoutID); }
				menuTimeoutID = 0;
				$("#topmenu div").fadeOut("slow", function () {
					$("#topmenu").slideUp("slow");
					});
				}
			);		
		}
	
	//preload menu images
	function preloadImages() {
		images = new Array(
			'img/site/topmenu/tm-story-on.jpg',
			'img/site/topmenu/tm-credo-on.jpg',
			'img/site/topmenu/tm-kunden-on.jpg',
			'img/site/topmenu/tm-services-on.jpg',
			'img/site/topmenu/tm-cases-on.jpg',
			'img/site/topmenu/tm-projekte-on.jpg',
			'img/site/topmenu/tm-infos-on.jpg',
			'img/site/topmenu/tm-karriere-on.jpg',
			'img/site/topmenu/tm-login-on.jpg'
			);
		if (document.images) {
			menimg=new Array();
			for (i=0; i < images.length; i++) {
				menimg[i] = new Image(140,20);
				menimg[i].src = images[i];
				$("#test").attr("src",images[i]);
				}
			}
		}
});

$(window).resize( function() {
	$("#simplemodal-container a.modalCloseImg").trigger('click');
	});

menuTimeoutID = 0;
function slideDownMenu() {
	$("#topmenu").slideDown("slow", function() {
		$("#topmenu div").fadeIn("slow");
		});
	}

initialMenuTimeout = 0;
function slideUpMenu() {
	$("#topmenu").fadeOut("slow", function () {
		$("#header").slideUp("slow");
		});
	}

function caseScroll(pos) {
	var carousel = $("#slider").data('jcarousel');
	carousel.scroll(jQuery.jcarousel.intval(pos));
	}

function sliderScroll(pos) {
	var carousel = $("#slider").data('jcarousel');
	carousel.scroll(jQuery.jcarousel.intval(pos));
	}

function showImprint() {
	$("#impressum-content").modal({
		opacity: 95,
		containerCss: {backgroundColor:"#ffffff", width:640, height:480},
		onOpen: function (dialog) {
			dialog.overlay.fadeIn('slow', function () {
				dialog.container.fadeIn('slow', function() {
					dialog.data.fadeIn('slow');
					});
				});
			},
		onClose: function (dialog) {
			dialog.data.fadeOut('fast', function () {
				dialog.container.fadeOut('fast', function() {
					dialog.overlay.fadeOut('fast', function () {
						$.modal.close();
						});
					});
				});
			}
		});	
	}

function checkTel(stat) {
	var tel = $("#kontakt *:input[name=telefon]").val();
	var nr = tel.match(/\d+/);
	if (stat === true && (tel == "" | nr === null)) {
		alert("Dafür benötigen wir eine gültige Telefonnummer, bitte...");
		return false;
		}
		else {
			$("#kontakt *:input[name=telefon]").attr("readonly","readonly");
			$("#kontakt *:input[type=checkbox]").attr("disabled","disabled");
			}
	}

function popUp(o, w, h) {
	win = window.open(o,'','width='+w+',height='+h+',left=50,top=50');
	return false;
	}

function login() {
	$.post("../../inc/login.php", $("#loginform").serialize(), function (data) {
		if (data.error !== '') { alert(data.error); return false; }
			else { top.location.href = data.loc; }
		}, "json");
	}
	
function sendMail() {
	$.post("../../inc/mail.php", $("#kontakt").serialize(), function (data) {
		if (data.error !== '') { alert(data.error); return false; }
			else { alert(data.gut); document.forms['kontakt'].reset(); }
		}, "json");
	}
	
function showCaseDesc(cID) {
	$("#description div").css("display","none");
	$("#description #case-desc-"+cID).css("display","block");
	$("#description").css({width:500});
	$("#description").modal({
		opacity: 95,
		containerCss: {backgroundColor:"#ffffff"},
		onOpen: function (dialog) {
			dialog.overlay.fadeIn('slow', function () {
				dialog.container.fadeIn('slow', function() {
					dialog.data.fadeIn('slow');
					});
				});
			},
		onClose: function (dialog) {
			dialog.data.fadeOut('fast', function () {
				dialog.container.fadeOut('fast', function() {
					dialog.overlay.fadeOut('fast', function () {
						$.modal.close();
						});
					});
				});
			}
		});
	}

function showMedia(pn,all) {
	media = all.split(',');
	curr = parseInt($("#viewer").attr("curr"));
	next = curr+parseInt(pn);
	if (next > 0) { $("#simplemodal-container a.modalLeftImg").css("display","block"); initialL="block"; }
		else { $("#simplemodal-container a.modalLeftImg").css("display","none"); initialL="none"; }

	if (next < media.length-1) { $("#simplemodal-container a.modalRightImg").css("display","block"); initialR="block"; }
		else { $("#simplemodal-container a.modalRightImg").css("display","none"); initialR="none"; }

	if (media[next]) { mediaID = media[next]; }
		else { mediaID = media[0]; }
	
	if (next == media.length) {
		$.modal.close();
		return false;
		}
	
	$.get("inc/getmedia.php", { id:mediaID, all:all }, function(d) {
		
		w=Math.ceil(($(window).width())*80/100);
		h=Math.ceil(($(window).height())*80/100);
		mratio=d.width/d.height;
		wratio=w/h;
		
		if (mratio<wratio) {
			if (d.height >= h) { mh=h; mw=Math.ceil(h*mratio); }
				else { mh=d.height; mw=Math.ceil(d.height*mratio); }
			}
			else {
				if (d.width >= w) { mw=w; mh=Math.ceil(w/mratio); }
					else { mw=d.width; mh=Math.ceil(d.width/mratio); }
				}
			
		$("#viewer").fadeOut(30, function() {
			if (d.mhtml !== null) { $("#viewer").html(d.mhtml); }
				else { $("#viewer").html(d.phtml); }
			$("#viewer").attr("curr",d.curr);
			$("#media-desc").html(d.desc);
			$("#simplemodal-container").animate({width:mw, height:mh, top:(Math.ceil(($(window).height()-mh)/2)), left:(Math.ceil(($(window).width()-mw)/2))}, function() {
				$("#viewer").fadeIn(1000);
				});
			$("#simplemodal-container a.modalLeftImg, #simplemodal-container a.modalRightImg").css('top',(mh/2-20));
			});
				
		$("#viewer").css({width:mw, height:mh});
		$("#viewer").modal({opacity: 95, 
			maxWidth:mw,
			maxHeight:mh,
			onOpen: function (dialog) {
				dialog.overlay.fadeIn('slow', function () {
					dialog.container.fadeIn('fast', function() {
						dialog.data.fadeIn('slow');
						}).append("<a class=\"modalLeftImg\" style=\"top:"+Math.ceil(mh/2-20)+"px; display:"+initialL+"\" onclick=\"showMedia('-1','"+all+"');\"></a><a class=\"modalRightImg\" style=\"top:"+Math.ceil(mh/2-20)+"px; display:"+initialR+"\" onclick=\"showMedia('1','"+all+"');\"></a><div id=\"media-desc\">"+d.desc+"</div>");
					});
				},
			onClose: function (dialog) {
				dialog.data.fadeOut('fast', function () {
					dialog.container.fadeOut('fast', function () {
						dialog.overlay.fadeOut('fast', function () {
							$.modal.close();
							});
						});
					});
				}
		});
		}, 'json');
	}


