$(document).ready(function() {
	$('.img2').each(function(){
	var $this = $(this);
	var width = $this.find('img').width();
	var height = $this.find('img').height();
	var ratio = 2.5;
	var newWidth = ratio*width;
	var newHeight = ratio*height;
	$this.hover(
		function(){
			$this.find('img').stop().css('position','absolute');
			$this.find('img').stop().css('z-index','150');
      			$this.find('img').stop().animate({'width': newWidth+"px", 'height': newHeight+"px"}, 400);
    	},
		function(){
			$this.find('img').stop().css('z-index','1');
			$this.find('img').stop().animate({'width': width+"px", 'height': height+"px"}, 400);
			
		}
	);
});
	$("a.catphotos").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	false
	});
	
	$('#firstRegForm input, #firstRegForm').hover(function() { 
    		$('#firstRegForm').stop().animate({"opacity": 1}); 
		},function() { 
   		$('#firstRegForm').stop().animate({"opacity": 0.3}); 
	});
	checkTopCart();
});
$(window).bind("load", function() {
    $("div#mygalone").slideView({easeFunc: "easeInOutBack",
    easeTime: 600}) // ttOpacity can be 0.1 to 1.0
});
function isValidEmail (email, strict)
{
	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 (!email.match(reg)) {
		alert("Введен некорректный E-mail"); 
		return false;
	}	
}

function addToCart(id, num) {
	$("#addbtn"+id).html('<img src="/system/img/galka.png" alt=""/>');
	$.ajax({
		url: "/cms/admin/basket.php?pl_plugin_order[1_"+id+"]="+num,
		success: function(){
			checkTopCart(1);
		}
	});
}

function orderCheck() {
	$('#showOrdForm').slideUp('fast');
	$('#ordForm').slideDown('fast');
}

function regCont() {
	reg_name = $('#reg_name').val();
	reg_mail = $('#reg_mail').val();
	reg_login = $('#reg_login').val();
	var url = "/users/registration.php?n="+reg_name+"&m="+reg_mail+"&l="+reg_login;
	$(location).attr('href',url);
}
function checkTopCart(update) {
	$.ajax({
  		url: '/basket/check.php',
  		success: function(data) {
			if (data != '') {
				if (update == 1) {
					$('#topcart').slideUp('fast', function() {
						$('#topcart').html(data);
  					});
					
				} else {
					$('#topcart').html(data);
				}
				$('#topcart').slideDown('fast');
			}
  		}
	});
	$.ajax({
  		url: '/basket/check.php?down=1',
  		success: function(data) {
			if (data != '') {
				if (update == 1) {
					$('#minikorz').slideUp('fast', function() {
						$('#minikorz').html(data);
  					});
					
				} else {
					$('#minikorz').html(data);
				}
				$('#minikorz').slideDown('fast');
			}
  		}
	});
}
function cartRemove(id) {
	$.ajax({
		url: "/cms/admin/basket.php?pl_plugin_order[1_"+id+"]=0",
		success: function(){
			$('#row'+id).remove();
			checkTopCart(1);
			totalRecount();
		}
	});
}
function cartRecount(id) {
	num = $('#bas'+id).val();
	if (typeof num ==='undefined'){num=0;}
	$.ajax({
		url: "/cms/admin/basket.php?pl_plugin_order[1_"+id+"]="+num,
		success: function(){
			$.ajax({
				url: "/catalog/cost.php?id="+id,
				success: function(data){
					$('#ccost'+id).html(data*num+' руб.');
					totalRecount();
				}
			});
			checkTopCart(1);
		}
	});
}
function totalRecount() {
	total = 0;
	$('.stoim').each(function() {
		var text = $(this).text().replace(' руб.','');
		total += parseFloat(text);
	});
	$('#summa').html(total);
}
function cartCheck() {
	var errormes = '';
	var iserror = 0;
	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])?/;
	phone_reg = /^( +)?((\+?7|8) ?)?((\(\d{3}\))|(\d{3}))?( )?(\d{3}[\- ]?\d{2}[\- ]?\d{2})( +)?/;
	if ($('#cName').val().length < 4) {iserror=1; errormes += 'Имя слишком короткое\n';}
	if (!$('#cMail').val().match(reg)) {iserror=1; errormes += 'Введен некорректный E-mail\n';}
	if (!$('#cPhone').val().match(phone_reg)) {iserror=1; errormes += 'Введен некорректный номер телефона';}
	if (iserror == 1) {
		alert(errormes);
	} else {
		$('#cartForm').submit();
	}
}
