$(document).ready(function() {
	
	if ($("#background-outer")) {
		repositionBackground();
		$(window).resize(function() {
			repositionBackground();
		});
	}
	
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
		$("#footer").css("position", "static");
	}

	$("#cursor-hint").fadeTo(5000, 0.01);
	
	$.ajaxSetup({cache: false});
	
	$("#project-list > li").hover(
		function(){
			$(this).find(".info").css('display', 'block');
		},
		function(){
			$(this).find(".info").css('display', 'none');
		}
	);
	
	$("#scroll-left").hover(
		function(){
			if($('#elements-preview-scroll').scrollLeft() != 0)$(this).attr("src", "../media/layout/left_h.gif");
		},
		function(){
			$(this).attr("src", "../media/layout/left_l.gif");
		}
	);
	
	$("#scroll-right").hover(
		function(){
			if( $('#elements-preview-scroll').scrollLeft() != ($('.element').length - 5) * 122 )$(this).attr("src", "../media/layout/right_h.gif");
		},
		function(){
			$(this).attr("src", "../media/layout/right_l.gif");
		}
	);
	
	$("#scroll-right").click(function(){
		$("#elements-preview-scroll").scrollTo({top:'0px', left:'+=610px'}, 1000);
	});
	
	$("#scroll-left").click(function(){
		$("#elements-preview-scroll").scrollTo({top:'0px', left:'-=610px'}, 1000);
	});


	// give the scroll-div the real width 4 IE
	$elementsAnz = $('.element').length;
	$('#elements-preview-scroll-inner').css('width', ($elementsAnz * 122 - 10)+'px' );
	$('#elements-scroller').css('width', ($elementsAnz * 600)+'px' );
	
	$("#logo img").hover(
		function(){
			if ($(this).attr("src") == "media/layout/logo_low.gif") {
				$(this).attr("src", "media/layout/logo_high.gif");
			}
			if ($(this).attr("src") == "../media/layout/logo_low.gif") {
				$(this).attr("src", "../media/layout/logo_high.gif");
			}
		},
		function() {
   		 	if ($(this).attr("src") == "media/layout/logo_high.gif") {
				$(this).attr("src", "media/layout/logo_low.gif");
			}
			if ($(this).attr("src") == "../media/layout/logo_high.gif") {
				$(this).attr("src", "../media/layout/logo_low.gif");
			}
		}
	);
	
	if ($("#elements-preview-scroll-inner").width() <= 122) {
		$("#scroll-left").css('visibility', 'hidden');
		$("#scroll-right").css('visibility', 'hidden');
	}
	
	
	var selfHref = window.location.href;
	if( selfHref.indexOf('/referenzen/') != -1 ){
		$(document).keyup(function(e){
			var pos		= parseInt($('#slideshow_position').val());
			var max		= $('#elements-scroller .element').length - 1;
			var pagesPos	= 0;
			
			if( e.which == 37 ){
				if( pos > 0){
					displayItem(pos-1);
					if( (pos % 5 == 0) && (pos-1 != 1) ){
						$("#elements-preview-scroll").scrollTo({top:'0px', left: '-=610px'}, 800);
					}
				}else{
					pagesPos = Math.ceil((max+1) / 5) * 610;
					displayItem(max);
					$("#elements-preview-scroll").scrollTo({top:'0px', left: pagesPos+'px'}, 800);
				}
			}else if( e.which == 39 ){
				if( pos < max ){
					displayItem(pos+1);
					if( ((pos+1) % 5 == 0) && pos+1 != 0 ){
						$("#elements-preview-scroll").scrollTo({top:'0px', left: '+=610px'}, 800);
					}
				}else{
					displayItem(0);
					$("#elements-preview-scroll").scrollTo({top:'0px', left: '0px'}, 800);
				}
			}
		});
	}

	// AJAX-Post@submit
	$("#newsletter").submit(function(event) {
		
		var email = $('#email', this).val();
		var fade_duration = 2800;
		
		if ( email <= 3 || email.indexOf('@') == -1 || email.indexOf('.') == -1 ) {
			$(".newsletter-info", this).css('display','block').html('Bitte geben Sie eine gültige E-Mail Adresse ein.').fadeOut(fade_duration);
			event.preventDefault();
			return;
		}
		
		jQuery.ajax({
			type: 		"POST",
			beforeSend:	function() {
							//$('#newsletter .newsletter-info').css('display','block').html('<p>Versende Nachricht...</p>').fadeOut(fade_duration);
						},
			data:		'email='+escape(email),
			url:		this.action,
			error:		function() {
							$('#newsletter .newsletter-info').css('display','block').html('<p>Beim Versenden der Nachricht ist ein Fehler aufgetreten.</p>').fadeOut(fade_duration);
						},
			success: 	function(r) {
							$('#newsletter .newsletter-info').css('display','block').html(r).fadeOut(fade_duration);
						}
		});
		event.preventDefault();
		return;
	});
	
});



function resetForm(element){
	element.find(':input:not(input[type=submit], input[type=hidden])').each(function(){
		$(this).val($(this).attr('rval'));
		$(this).css('color', '#CCCCCC');
	});
}

function displayItem(pos) {
	$('#slideshow_position').val(pos);
	$("#elements").stop();
	
	setContinent();
	var myPlayer = document.getElementById('mediaspace');
	if (myPlayer) {
		if (myPlayer.pauseVideo) {
			myPlayer.pauseVideo();
		}
	}
	var elementWidth = 600;
	var scrollX = elementWidth * pos + 'px';
	$("#elements").scrollTo({ top:'0px', left:scrollX }, 800, function(){
		$('#slideshow_animation').val('true');
	});
}

function setContinent() {
	$('object').each(function(index) {
		$(this).attr('data', $(this).attr('data'));
	});
}

function getFlashMovie( movieName ) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function loadBlogGalleryImage(element, src) {
	element.parent().parent().children('img').attr('src', src);
	$("body").scrollTo(element.parent().parent().parent(), 500);
}

function repositionBackground() {
	var windowWidth = $(window).width();
	if (windowWidth < $("#wrapper").width()) windowWidth = $("#wrapper").width();
	var posX = (windowWidth - $("#background-inner").width()) / 2;
	posX = Math.floor(posX) - 140;	
	$("#background-inner").css("left", posX + 'px');
}

