$(document).ready(function() {

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');
  }
  
  // Comments
  $("#toggle-comments").click(
    function() {
      $("#comments-wrapper").slideToggle(750);
    }
  );
  
  
  
  $(".toggle-write-comment").click(function(){
    $(this).parent().parent().children(".comment-form").slideToggle(750);
    return false;
  });
  
  
  $(".display-further-comments").toggle(
    function() {
      $(this).parent().parent().children(".further-comments").slideDown(1000);
      $(this).html('Nur letzte Kommentare anzeigen');
      return false;
    },
    function() {
      $(this).parent().parent().children(".further-comments").slideUp(1000);
      $(this).html('Alle Kommentare anzeigen');
      return false;
    }
  );
  
  // Kommentarfelder
  
  $("#suchfeld").focus(function(){
    if ( $(this).attr('value') == 'Suche' ) {
      $(this).attr('value', '');
      $(this).css('color', '#333333');
    }
  });
  
  $("#suchfeld").focusout(function(){
    if ( $(this).attr('value') == '' ) {
      $(this).attr('value', 'Suche');
      $(this).css('color', '#CCCCCC');
    }
  });
  
  
  $(".comment-name ").focus(function(){
    if ( $(this).attr('value') == 'Name' ) {
      $(this).attr('value', '');
    }
    $(this).css('color', '#333333');
  });
  $(".comment-name").focusout(function(){
    if ( $(this).attr('value') == '' ) {
      $(this).attr('value', 'Name');
      $(this).css('color', '#CCCCCC');
    }
  });
  
  
  $(".comment-email").focus(function(){
    if ( $(this).attr('value') == 'E-Mail' ) {
      $(this).attr('value', '');
    }
    $(this).css('color', '#333333');
  });
  $(".comment-email").focusout(function(){
    if ( $(this).attr('value') == '' ) {
      $(this).attr('value', 'E-Mail');
      $(this).css('color', '#CCCCCC');
    }
  });
  
  $(".comment-comment").focus(function(){
    if ( $(this).attr('value') == 'Kommentar' ) {
      $(this).attr('value', '');
    }
    $(this).css('color', '#333333');
  });
  $(".comment-comment").focusout(function(){
    if ( $(this).attr('value') == '' ) {
      $(this).attr('value', 'Kommentar');
    }
  });
  
  
  $(".comment-form-form").submit(function(event) {
    // prepare data
    var inputs = [];
    $(':input', this).each(function() {
      inputs.push(this.name + "=" + escape(this.value));
    });
    var params = inputs.join('&');
    
    var articleComments = $(this).parent().parent();
    
    if ($(this).find(".comment-name").val() == 'Name'
        || $(this).find(".comment-name").val() == ''
        || $(this).find(".comment-email").val() == 'E-Mail'
        || $(this).find(".comment-email").val() == ''
        || $(this).find(".comment-comment").val() == 'Kommentar'
        || $(this).find(".comment-comment").val() == ''
       ) {
         articleComments.find(".comment-messages").css({
           'color'       : '#9FD032',
           'padding-top' : '14px'
         });
         articleComments.find(".comment-options-bottom").children(".comment-messages").html('Es wurden nicht alle Felder ausgefüllt.');
         event.preventDefault();
         return;
       }
    
    jQuery.ajax({
      type:     "POST",
      beforeSend: function() {
        articleComments.find(".comment-messages").css({
          'color'       : '#9FD032',
          'padding-top' : '14px'
        });
        articleComments.find(".comment-messages").html('<img src="/web30/media/layout/ajax_load.gif" class="ajax-load" />');
      },
      data:    params,
      url:     this.action,
      error:    function() {
        articleComments.find(".comment-messages").html('Beim Versenden der Nachricht ist ein Fehler aufgetreten.');
      },
      success:   function(data) {
        articleComments.find(".comment-messages").css({
          'color'       : '#9FD032',
          'padding-top' : '14px',
          'display'     : 'block'
        });
        if( data.indexOf('###') == -1 ){
          articleComments.find(".comment-messages").html('Vielen Dank. Ihr Kommentar wurde versendet.');
          articleComments.find(".comment-form").slideUp(750, function(){            
            resetForm(articleComments.find(".comment-form"));
            window.setTimeout(function(){
              articleComments.find(".comment-options-bottom").before(data);
              articleComments.find(".comment.new").slideDown(750);
              articleComments.find(".comment-messages").slideUp(750);
            },500);
          }, 500);
          
        }else{
          articleComments.find(".comment-messages").html(data.replace('###', ''));
        }
      }
    });
    event.preventDefault(); 
  });
  

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;
		});



// endof .ready()
});


var steve;
steve = {
		log: function(message){
			if( window.console ){
				console.debug(message);
			}else{
				//alert(message);
			}
		}
			
};


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'));
  });
  
  /*
  var target = getFlashMovie('flashConnect');
  console.log(target);
  //target.myJump(1);
  console.log($("#animation4_a"));
  $("#animation4_a").myJump(1);
  */
}

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);
  
}
