
// polyfilling input placeholder for older browser
if(!Modernizr.input.placeholder){
	$.webshims.polyfill('forms details');	
}

$(document).ready(function() {
/*
	$('button').click(function(){
		$(this).closest('form').submit()
	})
*/

	// form validation any form
	// give the required input fields a class 'required', and in case of special field like email, add a class 'email'.
	// more details http://docs.jquery.com/Plugins/validation
	if($('.form').length){
		$('.form').each(function(){			
			$(this).validate({
				submitHandler: function(form){
					$(form).find("input[type='submit']").attr('disabled', 'disabled');
					form.submit();
				}
			})			
		})
	}	
	
	$('#newslettersubscribe').validate({		
		submitHandler: function(form){
			$(form).find("button").css('display', 'none');		
			$(form).find("#loader").css('display','block');
			$.ajax({
				type: $(form).attr('method'),
				url: $(form).attr('action'),
				data: $(form).serialize(),
				dataType: 'json',
				cache: false,
				success: function(result){
					if(result.success){
						$(form).fadeOut(200, function(){
							$(form).after(result.message);
							$('#body_holder').css('margin-bottom','5px');
							$('#newsletter a').click(function(){
								$('#body_holder').remove();
								$(this).remove();								
								$('form.newsletter input[name=email]').val('');
								$(form).fadeIn();
								return false;
							})
							$(form).find("#loader").css('display','none');
							$(form).find("button").css('display', 'block');															
						})
					} 
				}
			})			
		}
	});

/*
	$('#newslettersubmit').click(function() {
		//alert('asdf');
		//$('form#newslettersubscribe').submit();
		return false;
	});
*/	
	
	yepnope([{
		test : $('#localmenu').length,
		yep: '/site/js/jquery.scrollTo-1.4.2-min.js'
	}, { 
		test : $('#localmenu').length,
		yep: '/site/js/jquery.localscroll-1.2.7-min.js',
		callback : function(){
			$('#localmenu').localScroll({
				duration: 400,
				offset: -15
			});
		}
	}])


	yepnope({
		test : $('.colorbox-video, .colorbox-photo').length,
		yep: '/site/js/jquery.colorbox-min.js',
		callback : function(){
			$('.colorbox-video').colorbox({
				current: '',
				rel: 'group',
				iframe: true,innerWidth:425, innerHeight:344,
				onLoad: function (){
					$('#colorbox').addClass('colorbox-iframe')
				}
			})
			$('.colorbox-photo').colorbox({
				current: '',
				rel: 'group',
				onLoad: function(){
					$('#colorbox').removeClass('colorbox-iframe')
				}
			})
		}
	})

	yepnope({
		test : $('.cycle').length,
		yep  : '/site/js/jquery.cycle.all.js',
		callback : function(){			
			$('.cycle').each(function(){
				$('.cycle img').css('display','block');
				var cycle = $(this).find('.cycle-items');
				cycle.cycle({
					pause: true,
					fx: 'scrollHorz',
					pager: cycle.prev(),
					speed: 350,
					timeout: 6000,
					before: function(curr, next, opts, fwd){
				        //get the height of the current slide
				        var $ht = $(next).height();
				        //set the container's height to that of the current slide
				        $(next).parent().animate({'height': $ht});
			        }
				});
			})
		}
	});
	
	if($('#topimage').length){
		$(window).trigger('resize')
	}
	$('#contactform textarea').autoResize({
		extraSpace: 0,
		animate: {duration: 100}
	})
});

$(window).load(function() {
});

$(window).resize(function() {
	//resize
	if($('#topimage').length){
		targetHeight = $(window).height() * 0.33;
		if($('#topimage img').height() > targetHeight){
			offsetHeight = (targetHeight - $('#topimage img').height()) / 2;
			$('#topimage').height(targetHeight).find('img').css({
				'margin-top': offsetHeight
			})
		} else {
			$('#topimage').css('height', '').find('img').css('margin-top', '')
		}
	}
});
