/**
 * @author martinhochreiter, www.buero17.at
 */

$(document).ready( function() {
	
	/*
	 * init gallery
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	var galleries = $('.ad-gallery').adGallery(
			{
				loader_image: '/javascript/jquery.ad-gallery.1.2.4/loader.gif',
				width: 814,
				height: 576,
				thumb_opacity: 1,
				thumb_opacity_active: .3,
				display_back_and_forward: false,
				slideshow: false
			});
	
	$('.ad-image-wrapper')
		.mouseenter(function(e){
			$('.ad-image-description:not(:animated)').animate({bottom:0},400);
		})
		.mouseleave(function(e){
			$('.ad-image-description:not(:animated)').animate({bottom:-55},400);
		});
	
	/*
	 * scrolling
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	$('li.light a').click(function(e){
		var scrollto = $( '#'+ $(this).attr('rel') );
			
		$('body').scrollTo( scrollto,750 );
		
		return false;
	});
	
	$('h1').click(function(e){
		$('body').scrollTo( 0,750 );
	});

	
	/*
	 * news teaser
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	var wrapper = $('#news-wrapper');
	
	$('#news .next').click(function(e){
		var pos = wrapper.position();
		var elements = wrapper.children().length;
		$('#news .prev').show();
		
		if( pos.left <= (-816*(elements-2)) ){
			$(this).hide();
		}else{
			$(this).show();
		}
		
		wrapper.animate( {left:'-=816px'},400 );
		return false;
	});
	
	$('#news .prev').click(function(e){
		var pos = wrapper.position();
		$('#news .next').show();
		
		if(pos.left >= -816){
			$(this).hide();
		}else{
			$(this).show();
		}
		
		wrapper.animate( {left:'+=816px'},400 );
		return false;
	});
	
	/*
	 * login
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
	$('#login a.open')
		.mouseover(
			function(e){
				$('#login').stop().animate({
					top:0
				},300);
			}
		);
	
	$('#login')
		.mouseleave(
			function(e){
				$(this).stop().animate({
					top:-106
				},200);
			}
	);
	*/
	
	/*
	 * contact form
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	$('#contact-submit').click(function(e){
		if( validateForm() ){
			$.ajax({
				type: "POST",
				url: "/ajax/processContact.php",
				data: "text="+$('#contact-text').val()+"&email="+$('#contact-email').val(),
				success: function(msg){
				     if( msg == 'true' ){
				    	 $('#contact .right').html('<div id="ok">Thank you for your inquiry.</div>');
				     }
				}
			});
		}
		
		return false;
	});
	
	$('#contact-email, #contact-text').click(function(e){
		$(this).removeClass('error');
	});
	
	
	/*
	 * Links
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	var links = $('div#links ul');
	
	links
		.find('a')
		.mouseover(function(e){
			//links.find('a').
			$(this).animate({opacity: 1},300);
			links.find('a').not($(this)).animate({opacity: 0.2},300);
			return false;
		});
	
	links.mouseleave(function(e){
		links.find('a').animate({opacity: 1},300);
	});
	
	
	/*
	 * ie6
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	if( $.browser.msie && $.browser.version < 7){
		var html = 'Please update your browser to view this website correctly';
		$('body').append('<div class="message">'+ html +'</div>');
	}
	 
});

function protectMail( s ){
	var n = 0;
	var r = "";
	for( var i = 0; i < s.length; i++){
		n = s.charCodeAt( i );
		if( n >= 8364 ){
			 n = 128;
		}
	r += String.fromCharCode( n - 1 );
	}
	return r;
}	

function linkTo_UnCryptMailto( s ){
	location.href=protectMail( s );
}

function validateForm(){
	var bool = true;
	
	if( $('#contact-email').val() == '' ){
		$('#contact-email').addClass('error');
		bool = false;
	}else{
	
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test( $('#contact-email').val() ) == false) {
			$('#contact-email').addClass('error');
			bool = false;
		}
		
	}   
	
	if( $('#contact-text').val() == '' ){
		$('#contact-text').addClass('error');
		bool = false;
	}
	
	return bool;
}

function debug(v){
	if( console ){
		console.log(v);
	}
}



