$(document).ready(function(){
	
	
	if($('.video-navi a')){
		
		
		$('div.video-navi a').click(function(e){
			e.preventDefault();
			$('div.video-navi a.active').removeClass('active');
			$(this).addClass('active');
			$('div.element.active').removeClass('active');
			$('div#'+this.rel+'.element').addClass('active');
			
			//$('div.picturesColor div.active').animate({opacity:0}).removeClass('active');
			//$('div.picturesColor div#'+this.rel).addClass('active').animate({opacity:1});
			
		});
	}
	
	
	if($('#fix_height')){
	
		$('#fix_height').jScrollPane({showArrows:true, scrollbarWidth:13, dragMaxHeight:43});
	
	}
	
	$('.add_cart').click(function(event){
		event.preventDefault();
		$('#basket').load('/?Event=AddToCart&id='+$(this).attr('rel'), function() {
 			 alert('Товар добавлен в корзину!')
		});
	});
	
	$('.del_cart').live('click',function(event) {
		event.preventDefault();
		$.post('/basket',{Event:'Recalculate','product_delete':$(this).attr('rel')}, function(data){
			$('#basketForm').html(data);
		});
		$('#basket').load('/?Event=AddToCart&id=0');
	});
	
	$('.refresh').live('click',function(event) {
		event.preventDefault();
		$('#loading').show();
		$.post('/basket',$('#basketForm').serialize(), function(data){
			$('#basketForm').html(data);
			$('#loading').hide();
		});
		$('#basket').load('/?Event=AddToCart&id=0');
	});
	
	$('.arrow').click(function(event){
		event.preventDefault();
		
		$(this).toggleClass('opened');
		$('#'+$(this).attr('rel')).slideToggle('slow');
	});
	
	/*$('.aside').click(function(event){
	
		if(login == 0){
			alert('Авторизируйтесь, чтоб отложить товар!');		
			$(this).attr('checked', '');
			
		}			
	});*/
	
	$('#login_link').click(function(e) {
		e.preventDefault();
		$('#login_box').addClass('display');
	});
	
	$('.close_form').click(function(e) {
		$(this).parent().parent().removeClass('display');
	});
	
	$('#login_form').submit(function(e){
		e.preventDefault();
		$.post('/user-account.html', $(this).serialize(), 
				function(jsonObj) {
					if(jsonObj.success){
						$('#login').html(jsonObj.user_info);
					}else{
						$('#login_error').removeClass('no-display');
					}
				},
				"json");
	});
	
	$('#ContactForm').submit(function() {
		
		var validation = $("#ContactForm .valid");
		var error = false;
		
		if(validation){
			validation.removeClass('error-ico');
					
			validation.each(function(index, el){
				
				if($(this).hasClass('email')){
					 validRegExp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
					 strEmail = el.value;
					 if (strEmail.search(validRegExp) == -1){
					 	error = true;
					 	$(this).addClass('error-ico');
					 } 
				}else{
					if ($(this).hasClass('textarea')) {
						if (!el.value || el.value == el.title) {
							error = true;
							$(this).addClass('error-ico');
						}
					}
					if(!el.value || el.value == el.alt){
						error = true;
						$(this).addClass('error-ico');							
					}						
				}					
			});
		}
		if(!error){
			$('#dl_cont_form').css('display','none');
			$('#ajax_container_contact').addClass('ajax-form');
			if(validation){validation.removeClass('error-ico');}
			
			var str = $('#ContactForm').serialize();
			$.post(	'/',	str, 
				function(response){
					var params;
					eval("params = "+response);	
					if(params.success){
						
						$('#ajax_container_contact').removeClass('ajax-form');
						$('#ajax_container_contact .thanks').css('display','block');
						$('#ajax_container_contact').css('position','relative');
						
						setTimeout(function() { 
								$('#ajax_container_contact .thanks').css('display','none');
								$('#dl_cont_form').css('display','block');
								$('#ajax_container_contact').css('position','');
							}, 
							3000
						);
						var validation = $("#ContactForm .valid");
						validation.each(function(index, el){
							if($(el).hasClass('textarea')){
								$(el).val(el.title);							
							}else{
								$(el).val(el.alt);						
							}					
						});	
						$("#message").html('');
						
					}else{
						$('#ajax_container_contact .thanks').html('<p>К сожалению произошла ошибка!</p><p>Попробуйте отправить запрос ещё раз!</p>');
						
						$('#ajax_container_contact').removeClass('ajax-form');
						$('#ajax_container_contact .thanks').css('display','block');
						$('#ajax_container_contact').css('position','relative');
						
						setTimeout(function() { 
								$('#ajax_container_contact .thanks').css('display','none');
								$('#ajax_container_contact').css('position','');
								$('#dl_cont_form').css('visibility','visible');
								$('#ajax_container_contact .thanks').html('<p>Спасибо за запрос.</p> <p>В ближайшее время с вами свяжется менеджер.</p>');
							}, 
							3000
						);
					}
				}
			);
		}
		return false;
	});	
	
	$('#regform').submit(function() {
			
		var validation = $("#regform .valid");
		var error = false;
		var errortext = '';
		
		if(validation){
			
			validation.removeClass('error-ico');
			$('#err').attr('src', '');		
			
			validation.each(function(index, el){
				
				if($(this).hasClass('email')){
					 validRegExp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
					 strEmail = el.value;
					 if (strEmail.search(validRegExp) == -1){
					 	error = true;
						errortext += el.title+'<br/>';
					 	$(this).addClass('error-ico');
					 } 
				}else if($(this).hasClass('phone')){
					 validRegExp = /^\d{10,13}$/i;
					 strPhone = el.value;
					 if (strPhone.search(validRegExp) == -1){
					 	error = true;
						errortext += el.title+'<br/>';
					 	$(this).addClass('error-ico');
					 }
				}else if($(this).hasClass('rules')){
					if(!el.checked){
						error = true;
						errortext += el.title+'<br/>';
						alert('Для регистрации необходимо принять условия использования сайта.');
					}
				}else if($(this).hasClass('day') || $(this).hasClass('month') || $(this).hasClass('year')){
					if(isNaN(el.value)){
						error = true;
						$('#err').attr('src', '/public/images/error.gif');
					}
				}else if ($(this).hasClass('textarea')) {
					if (!el.value || el.value == el.title) {
						error = true;
						errortext += el.title+'<br/>';
						$(this).addClass('error-ico');
					}
				}else{
					if(!el.value || el.value == el.alt){
						error = true;
						errortext += el.title+'<br/>';
						$(this).addClass('error-ico');							
					}						
				}					
			});
		}
		if(!error){
			$(".validateTips",$(this)).empty();
		}else{
			$(".validateTips",$(this)).html('Вы не заполнили обязательные поля:<br/>'+errortext);
			//$('#captcha').load('/registration.html?Event=ChangeCaptcha');
			return false;
		}	
	});
	
	$('.validation-form').submit(function() {
		
		var validation = $(".valid",this);
		var error = false;
		
		if(validation){
			
			validation.removeClass('error-ico');
			//$('#err').attr('src', '');		
			
			validation.each(function(index, el){
				
				if($(this).hasClass('email')){
					 validRegExp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
					 strEmail = el.value;
					 if (strEmail.search(validRegExp) == -1){
					 	error = true;
					 	$(this).addClass('error-ico');
					 } 
				}else if($(this).hasClass('phone')){
					 validRegExp = /^\d{10,13}$/i;
					 strPhone = el.value;
					 if (strPhone.search(validRegExp) == -1){
					 	error = true;
					 	$(this).addClass('error-ico');
					 }
				}else if($(this).hasClass('rules')){
					if(!el.checked){
						error = true;
						alert('Для регистрации необходимо принять условия использования сайта.');
					}
				}else if($(this).hasClass('day') || $(this).hasClass('month') || $(this).hasClass('year')){
					if(isNaN(el.value)){
						error = true;
						$('#err').attr('src', '/public/images/error.gif');
					}
				}else{
					if(!el.value || el.value == el.alt){
						error = true;
						$(this).addClass('error-ico');							
					}						
				}					
			});
		}
		if(!error){
		
		}else{
			//$('#captcha').load('/registration.html?Event=ChangeCaptcha');
			return false;
		}	
	});
	
});




function unset(el,text,textarea){
	if(textarea){
		if(el.innerHTML == text){
			el.innerHTML = '';	
		}
	}else{
		if(el.value == text){
			el.value= '';		
		}			
	}
}

function set(el,text,textarea){
	if(textarea){
		if(el.innerHTML == '' && el.value == ''){
			el.innerHTML = text;
		}
	}else{
		if(el.value == ''){
			el.value = text;		
		}			
	}
}
