$(document).ready(function(){
	$("a[href^=http]").each(function(){
      if(this.href.indexOf(location.hostname) == -1) {
        $(this).attr('target', '_blank');
      }
    });
    
    $(".fancybox").fancybox({
    	'transitionIn'	:	'elastic',
    	'transitionOut'	:	'elastic',
    	'titlePosition'	:	'over'
    });
    
    
    
    $("#contact").submit(function(ev){
		var errorcount = 0;
		$("input.text").each(function(){
			if($(this).hasClass('skip'))
			{
			}
			else
			{
			var errortrue = 0;
			var currentvalue = $(this).val();
			if(currentvalue == "")
			{
				errorcount++;
				errortrue++;
			}
			if(errortrue > 0)
			{
				$(this).addClass("error");
				$(this).parent('fieldset').children('legend').addClass('error');
			}
			}
		});
		
		$("textarea").each(function(){
			if($(this).hasClass('skip'))
			{
			}
			else
			{
			var errortrue = 0;
			var currentvalue = $(this).val();
			if(currentvalue == "")
			{
				errorcount++;
				errortrue++;
			}
			if(errortrue > 0)
			{
				$(this).addClass("error");
				$(this).parent('fieldset').children('legend').addClass('error');
			}
			}
		});
		
		if(errorcount > 0)
		{
			$("#contact h3:first").before('<p class="error">Please fill in all fields.</p>');
			$("p.error").delay(3000).fadeOut();
			ev.preventDefault();
		}
	});
	
	$("#contact input, #contact textarea, #contact select").focus(function(){
		$(this).removeClass("error");
		$(this).parent('fieldset').children('legend').removeClass('error');
	});
	

		
	$("a.youtube").click(function() {
				$.fancybox({
				'padding'			: 10,
				'centerOnScroll'	: true,
				'autoScale'			: false,
				'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/')+'&autoplay=1',
                'type'                : 'swf',
                'swf'                 : {'allowfullscreen':'true'}
				});
				return false;
			});
		$(".vimeo").click(function() {
				$.fancybox({
				'padding'			: 10,
				'centerOnScroll'	: true,
				'autoScale'			: false,
				'href'				: this.href.replace(new RegExp("vimeo.com/", "i"), 'vimeo.com/moogaloop.swf?clip_id=')+'&autoplay=1',
				'type'				: 'swf',
				'swf'                 : {'allowfullscreen':'true'}
				});
				return false;
	});
	
	var divcount = parseInt($("#clients .slider ul li").size()) - 5;
	var max = 1;
	var active = 1;
	var min = parseInt("-" + divcount);
	
	var on = 0;
	$("#clients .slider ul li").each(function(){
		var amount = 228 * on;
		$(this).css("margin-left", amount+'px');
		on++;
	});
	
	$("a.next").addClass("disabled").attr("disabled", "");
	$(".next-more").hide();
	
	$("a.prev").click(function(){
		if($(this).hasClass("disabled"))
		{
			return;
		}
		$("a.next").removeClass("disabled").removeAttr("disabled");
		$(".next-more").fadeIn();
		active--;
		if(active === min)
		{
			$(this).addClass("disabled").attr("disabled", "");
			$(".prev-more").fadeOut();
		}
		$("#clients .slider ul li").each(function(){
				var currentmargin = $(this).css("margin-left");
				var currentmargin2 = currentmargin.replace('px', '');
				var newmargin = parseInt(currentmargin2) - 228;
				$(this).animate({
					marginLeft: newmargin+"px"
				},200);
			});
	});
	
	$("a.next").click(function(){
		if($(this).hasClass("disabled"))
		{
			return;
		}
		$("a.prev").removeClass("disabled").removeAttr("disabled");
		$(".prev-more").fadeIn();
		active++;
		if(active === max)
		{
			$(this).addClass("disabled").attr("disabled", "");
			$(".next-more").fadeOut();
		}
		$("#clients .slider ul li").each(function(){
				var currentmargin = $(this).css("margin-left");
				var currentmargin2 = currentmargin.replace('px', '');
				var newmargin = parseInt(currentmargin2) + 228;
				$(this).animate({
					marginLeft: newmargin+"px"
				},200);
			});
	});
	
	$("#carousel").CloudCarousel(		
		{			
			xPos: 450,
			yPos: 50,
			reflHeight: 30,
			reflGap: -2,
			bringToFront: true
		}
	);
});
