$(window).load(function(){
  var w = 0;
  $('#client > ul > li').each(function(ind, el){
			w += $(el).children('img').width() + parseInt($(el).css('margin-left')) + parseInt($(el).css('margin-right'));
  });
  $('#client > ul').children('li').clone().appendTo('#client > ul');
  $('#client > ul').width(2*w);
	$('#client > ul').children('.clear').remove();
  $("#client").everyTime(20, function(i) {
    var left = parseInt($('#client > ul').css('left'))-1;
    $('#client > ul').css('left', left+'px');
    if(left*-1 >= $('#client > ul').width()/2){
      $('#client > ul').css('left',0);
    }
  });
});

$(document).ready(function(){
	init();
	replaceEmail();
});


function replaceEmail(){
	var email = 'support@';
	var email = email+'internet-menu.ru';
	var str = '<a href="mailto:'+email+'">'+email+'</a>';
	$('span.email').html(str);
}

function init(){
	$('#portfolio > ul').children('li').eq(0).css('display', 'block');
	var cnt = $('#portfolio > ul').children('li').length;
	$('#portfolio').everyTime(4500, function(i){
		i = (i+cnt) % cnt;
		$('#portfolio > ul').children('li').eq(i-1).animate({'opacity':0}, 800, function(){
			$('#portfolio > ul').children('li').eq(i-1).css({'display':'none'});
			$('#portfolio > ul').children('li').eq(i).css({'opacity':0, 'display':'block'}).animate({'opacity':1}, 800);
		})
	});
  
	$('[name=sendbrif]').submit(function() { 
		if (checkbrif()) {
			$(this).ajaxSubmit({
				url: '/?sendbrif',
				target: '#brif',
				beforeSubmit: function(){
					var html = '<div style="width:283px;height:155px;padding-top:20px;text-align:center"><img src="/img/loading.gif" alt="Loading..." /></div>';
					$('#brif').html(html);
				},
				success: function(data){
				
				},
				timeout: 5000
			});
		}else{
		}
		return false;
	}); 
	
	if ($('[name=fcontacts]').length) {
  	$('[name=fcontacts]').validate({
  		submitHandler: function(){
  			$('[name=fcontacts]').ajaxSubmit({
  				url: '/contacts/?ajax=1&send',
  				target: '#formcontacts',
  				beforeSubmit: function(){
  					var html = '<div style="width:283px;height:155px;padding-top:20px;text-align:center"><img src="/img/loading.gif" alt="Loading..." /></div>';
  					$('#formcontacts').html(html);
  				},
  				complete: function(data){
						init();
  				},
  				timeout: 5000
  			});
  			return false;
  		},
  		rules: {
  			name: "required",
  			mail: {
  				required: true,
  				email: true
  			},
  			message: "required"
  		},
  		messages: {
  			name: 'Пожалуйста представтесь',
  			mail: {
  				required: 'Укажите  E-mail',
  				email: 'Укажите корректный  e-mail'
  			},
  			message: 'Напишите свое сообщение'
  		}
  	});
  }
	
	if ($('[name=sendbrif]').length) {
  	$('[name=sendbrif]').submit(function(){
  		if (checkbrif()) {
  			$(this).ajaxSubmit({
  				url: '/?sendbrif',
  				target: '#brif',
  				beforeSubmit: function(){
  					var html = '<div style="width:283px;height:155px;padding-top:20px;text-align:center"><img src="/img/loading.gif" alt="Loading..." /></div>';
  					$('#brif').html(html);
  				},
  				success: function(data){
  					init();
  				},
  				timeout: 5000
  			});
  		}
  		return false;
  	});
  }
	
	$('[name=email]').focus(
		function(){
			removeVal(this, 'Ваш email');
	}).blur(function(){
			addVal(this, 'Ваш email');
	});
}

function removeVal(ob, val){
	if($(ob).val() == val) $(ob).val('');
}

function addVal(ob, val){
	if ($(ob).val() == '') {
  	$(ob).val(val);
  }
}

function checkbrif(){
	var error = [];
	var value = $('[name=email]').val();
	reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
	if (!value.match(reg)) {
		error.push("Пожалуйста, введите свой настоящий e-mail");
	}
	
	if($('[name=brif]').val() == ''){
		error.push('Файл с брифом не выбран');
	}
	
	
	if(error.length > 0){
		$('#brif').children('.error').remove();
		$('<div class="error">'+error.join("<br/>")+'</div>').insertBefore($('#brif').children('form'));
		return false;
	}
	else return true;
}
