
/*
 * -------------------------------------------------------------------------
 * @file 		dom.js
 * @version 	1.0.0
 * @date 		2010-08-24 12:39:52 +0200 (Tue, 24 Aug 2010)
 * @author 		Matthias Mertiens <mail@mertiens.biz>
 * --------------------------------------------------------------------------
 * Copyright (c) 2010 mertiens. ideas for communication <http://mertiens.biz>
 * -------------------------------------------------------------------------- 
 */

 

$(document).ready(function() {



	// hide #back-top first
	$("#back-top").hide();
	
	// fade in #back-top
		$(window).scroll(function () {
			if ($(this).scrollTop() > 400) {
				$('#back-top').fadeIn();
			} else {
				$('#back-top').fadeOut();
			}
		});

		// scroll body to 0px on click
		$('#back-top a').click(function () {
			$('body,html').animate({
				scrollTop: 0
			}, 800);
			return false;
		});



/*
function isEmail(str) {
  return ((str != "") && (str.indexOf("@") != -1) && (str.indexOf(".") != -1));}
*/


function isEmail(s)
{
 var a = false;
 var res = false;
 if(typeof(RegExp) == 'function')
 {
  var b = new RegExp('abc');
  if(b.test('abc') == true){a = true;}
  }

 if(a == true)
 {
  reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
                   '(\\@)([a-zA-Z0-9\\-\\.]+)'+
                   '(\\.)([a-zA-Z]{2,4})$');
  res = (reg.test(s));
 }
 else
 {
  res = (s.search('@') >= 1 &&
         s.lastIndexOf('.') > s.search('@') &&
         s.lastIndexOf('.') >= s.length-5)
 }
 return(res);
} 


/* 	Easy Form Validation   -------------------------------------------------------------					 */
						
						
$.fn.bindForm = function(link) {

	$(this).bind('submit', function(event) {
	 
			var submitButton = $('#submitButton');

			event.preventDefault();
	 	
 			var goSubmit = true;
			var cf = this;
	 			
	 		$('.required',cf).each(function(){
	 				
	 				
	 			var elid = $(this).attr('id');	

				$(this).val( $.trim($(this).val()) );
	 				
	 				 						 			
	 			if( $(this).val()=="" || ( $(this).hasClass('email') && !isEmail( $(this).val() )) || ( $(this).attr('type')=='checkbox' && !$(this).attr('checked') ) ) {
	 				
	 				var parentP = $(this).parent('p');
	 				
	 				var errorMsg = $('<span class="errormsg">Bitte ausfüllen!</span>');
	 				
	 				if( $(this).hasClass('email') && !isEmail( $(this).val()) )
	 				var errorMsg = $('<span class="errormsg">Bitte tragen Sie eine gültige Mail-Adresse ein!</span>');
	 				
	 				
	 				if( $('.errormsg',parentP).length == 0)
	 				parentP.addClass('error').append( $(errorMsg).hide().fadeIn(800) );
	 					
/* 	 				$('label[for="' + elid + '"]').addClass("error"); */

/* 	 				$(this).addClass("error") */
	 				$(this).keyup(function() { 
/* 	 							$(this).removeClass("error").parent('p') */

					

								if( ($.trim($(this).val())!="" &&  !( $(this).hasClass('email') && !isEmail( $(this).val() ) ) )	 || 	( $(this).attr('type')=='checkbox' && $(this).attr('checked') )  ) {
	 							parentP.removeClass("error");
	 							errorMsg.remove();
	 							}
/* 	 							$('label[for="' + elid + '"]').removeClass("error"); */
	 						});
	 				
	 				goSubmit = false;
	 					
	 				} // /if	
	 			});
	 			
	 			
	 		
	 		
	 		// Formular senden ---------------------		
	 		if( goSubmit ) {	

				submitButton.replaceWith('<img id="loading" src="/resources/img/ajax-loader.gif" alt="Loading" >');

	 			var formulardata = $(cf).serialize();
	 			var url = $(cf).attr('action'),
	 				on_success = function() { $('#loading')
	 												.replaceWith('<p class="gesendet">Die E-Mail wurde versendet.</p>'); 
	 												$('fieldset,#hinweis',cf).not('.buttonbox').animate({ opacity: 0.2 }); 
	 												$(link).unbind().click(function(){return false;}).css({ opacity: 0.3 }); 
	 												};
	 												
	 			//alert(formulardata);									
	 			$.ajax({ 	
	 					type: 'POST',
	 					url: url, 
	 					data: formulardata, 
	 					success: on_success 
	 					});

	 			}
	 				
			});

	return this;
} 
/* fct bindForm 	--------------------------------------------------------------------------------------					 */			


$('#ContactFormSet').bindForm(null);	








	$('li', 'menu').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
		);

	$('.external_link').attr('target','_blank');
						
									
			
		$('.write-an-email')
				.animate({ opacity: 0.9 },400)
				.hover(
					function() { $(this).stop().animate({ opacity: 1 },80 ); },
					function() { $(this).stop().animate({ opacity: 0.9 },400 ); }
				);



		$('#search')
				.animate({ opacity: 0.9 },400)
				.hover(
					function() { $(this).stop().animate({ opacity: 1 },80 ); $('input','#search').focus(); },
					function() { $(this).stop().animate({ opacity: 0.9 },400 );  }
				);


		$('#tel')
				.animate({ opacity: 0.9 },400)
				.hover(
					function() { $(this).stop().animate({ opacity: 1 },80 ); $('input','#search').focus(); },
					function() { $(this).stop().animate({ opacity: 0.9 },400 );  }
				);



	//$('#references_sortable,#references').preloader();



/*
		$('#meta_navigation')
				.hover(
					function() { $(this).stop().css({ background: '#847d72' }); },
					function() { $(this).stop().css({ background: '#9e9587 url(../img/bg_teaserbox.jpg) repeat'  }); }
				);
*/


					
/*
		$('a','#main_navigation ul')
					.css({ backgroundPosition: "0 0" })
					.hover(
						function(){	$(this).stop().animate({ backgroundPosition: "0 -240px"}, 250); }, 
						function(){	$(this).stop().animate({ backgroundPosition: "0 0" }, 300); }
						);
								
		$('a','#sub_navigation ul')
					.css({ backgroundPosition: "0 0" })
					.hover(
						function(){	$(this).stop().animate({ backgroundPosition: "0 -240px"}, 250); }, 
						function(){	$(this).stop().animate({ backgroundPosition: "0 0" }, 300); }
						);
*/
						
						
/*
		$('ul#myRoundabout').roundabout({
         			reflect: false
      				});
*/
		
	
});	



(function($) {

  $.fn.sorted = function(customOptions) {
    var options = {
      reversed: false,
      by: function(a) { return a.text(); }
    };
    $.extend(options, customOptions);
    $data = $(this);
    arr = $data.get();
    arr.sort(function(a, b) {
      var valA = options.by($(a));
      var valB = options.by($(b));
      if (options.reversed) {
        return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
      } else {		
        return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
      }
    });
    return $(arr);
  };


})(jQuery);




// DOMContentLoaded
$(function() {

  // bind radiobuttons in the form
  var $filterType = $('#filter input[name=type]');
  var $filterSort = $('#filter input[name=sort]');

  // get the first collection
  var $applications = $('#references_sortable');

  // clone applications to get a second collection
  var $data = $applications.clone();

	//alert($data.html());
	
	// Filter einfärben ---------------	
	//$('label','#filter').removeClass('checked');
	//alert($(':checked','#filter').length);
	$(':checked','#filter').parent().addClass('checkeditem');
	$('#references_list').css( 'height', $('#references_list').height() );

  // attempt to call Quicksand on every form change
  $filterType.add($filterSort).bind('change', function(e) {
  
  	//alert($data.html());
  
    if ($($filterType+':checked').val() == 'all') {
      var $filteredData = $data.find('li');
    } else {
      var $filteredData = $data.find('li[data-type=' + $($filterType+":checked").val() + ']');
    }

    // if sorted by size
    if ($('#filter input[name="sort"]:checked').val() == "Datum") {
      var $sortedData = $filteredData.sorted({
        by: function(v) {
          var vormals = $(v).find('span[data-type=Datum]').text();
		  var jetzt = vormals.replace(/-/g, "");
          return parseFloat(jetzt);
        },
        reversed: true
      });
    } else {
      // if sorted by name
      var $sortedData = $filteredData.sorted({
        by: function(v) {
          return $(v).find('.projektname').text().toLowerCase();
        }
      });
    }   

	//alert($data.html());
	$('label','#filter').removeClass('checkeditem');
	$(':checked','#filter').parent().addClass('checkeditem');


    // finally, call quicksand
    $applications.quicksand($sortedData, {
      duration: 800,
      easing: 'easeInOutQuad'
    });

  });

});
