$(document).ready(function() {
	$('HTML').addClass('JS');
	$(".gallery a").colorbox();
	$(".lightbox").colorbox();
	$(".add_pic .cont a").colorbox();
	
	$(".print").click(function(){
		window.print();
		return false;
	});
	
	$('#slider .prew').click(function(){
		$('#slider .cont').scrollTo('-=208px', 500);
		return false;
	});
	$('#slider .next').click(function(){
		$('#slider .cont').scrollTo('+=208px', 500);
		return false;
	});
	
	$('.add_pic .prew').click(function(){
		$('.add_pic .cont').scrollTo('-=103px', 500);
		return false;
	});
	$('.add_pic .next').click(function(){
		$('.add_pic .cont').scrollTo('+=103px', 500);
		return false;
	});
	
	$('.rec_prod .prew').click(function(){
		$('.rec_prod .cont').scrollTo('-=178px', 500);
		return false;
	});
	$('.rec_prod .next').click(function(){
		$('.rec_prod .cont').scrollTo('+=178px', 500);
		return false;
	});
	
	
	$(".pay_cols .ttl").click(function(){
		$(".pay_cols .ttl").removeClass("act");
		$(this).addClass("act");
		$("#pay_type").val($(this).attr("rel"));
	});
	
	$("#pay_continue_button").click(function(){
		window.location = $(this).attr('href')+"?pay_type="+$("#pay_type").val();
		return false;
	});
	
	$(".product_info .more_info .line .color").click(function(){
		$(this).children('div').toggle();
	});
	
	$(".product_info .more_info .line .color span").click(function(){
		$(this).parent('div').parent('div').css("backgroundColor", $(this).attr("rel"));
		$(this).parent('div').parent('div').attr("rel", $(this).attr("rel"));
		$(".tut_color_ludzu").html('* '+$(this).html());
	});
	
	$(".tut_color_ludzu").hover(
		function(){
			$(this).children('img').show();
		},
		function(){
			$(this).children('img').hide();
		}
	);
	
	$(".button_bar .cart").click(function(){
		window.location = $(this).attr('href') + "&color=" + $('.line .color').attr('rel').replace('#','');
		return false;
	});
	
	$(".cart_line input").keyup(function(){
		if(parseInt($(this).val()) < 1) $(this).val('1');
		$.ajax({
			type: "POST",
			url: '/changeCart.php',
			data: {'quant': $(this).val(), 'id': $(this).parent("div").parent("div").attr("rel")},
			success: function(msg){
				if(msg.responce){
					
					$("#total"+msg.id).html(msg.total_pr);
					$("#weight"+msg.id).html(msg.weight_pr);
					
					$("#order_price").html(msg.order_price);
					$("#delivery_price").html(msg.delivery_price);
					$("#total_price").html(msg.total_price);
					$("#order_weight").html(msg.order_weight);

					$(".top_cart .cart").load('/smallCart.php?lang_id=' + $("#in_lang_id").val());
					
					if(msg.count_err == "yes"){
						alert($('#count_err').val());
						$("#cnt"+msg.id).val(msg.max_count);
					}
					
					return true;
				}else{return false;}
			},
			dataType: 'json'
		});
	});
	$(".cmp_checkbox").click(function(){
		var act = "del";
		if($(this).attr("checked")){ 
			act = "add";
			$(this).parent("label").addClass("act");
		}else{
			$(this).parent("label").removeClass("act");
		}
		
		
		$.ajax({
			type: "POST",
			url: '/ajaxCompare.php ',
			data: {'action': act, 'id': $(this).attr("rel")},
			success: function(msg){
				$(".top_cart .cmp span").load('/smallCompare.php', function(msg){
					if(msg != "0") $(".top_cart .cmp").show();
					else  $(".top_cart .cmp").hide();
				});
			},
			dataType: 'json'
		});
	});
	
	$(".top_cart .cart").load('/smallCart.php?lang_id=' + $("#in_lang_id").val(), function(msg){
		if(msg != "0") $(".top_cart .cart").show();
		else  $(".top_cart .cart").hide();
	});
	$(".top_cart .cmp span").load('/smallCompare.php', function(msg){
		if(msg != "0") $(".top_cart .cmp").show();
		else  $(".top_cart .cmp").hide();
	});
	
	
	$("#login_form").submit(function(){
		
		$.ajax({
			type: "POST",
			url: '/check_login.php',
			data: {'login': $("#login_form input[name=login]").val(), 'password': $("#login_form input[name=password]").val() },
			success: function(msg){
				if(msg == 1){
					window.location = './rewiev.html';
				}else{
					alert($("#login_form_error").val());
				}
			}
		});
		
		return false;
	});
	$("#login_form_btn").click(function(){
		$("#login_form").submit();
		return false;
	});
	
	$("#save_new_address").click(function(){
		$.ajax({
			type: "POST",
			url: '/changeCartAddr.php',
			data: {'country': $("input[name=country]").val(), 'del_addr': $("textarea[name=delivery_address]").val() },
			success: function(msg){
				window.location = './pay.html';
				return true;
			}
		});
		return false;
	});
	
	$('.pr_info_down').click(function(){
		$('.mega_scroll').scrollTo( {top:'+=100px', left:'0px'}, 800 );
		return false;
	});
	$('.pr_info_up').click(function(){
		$('.mega_scroll').scrollTo( {top:'-=100px', left:'0px'}, 800 );
		return false;
	});
	
	//$(...).scrollTo( {top:'-=100px', left:'+=100'}, 800 )
});

// popups
$(document).ready(function(){
   //align element in the middle of the screen
   $.fn.alignCenter = function() {
      //get margin left
      var marginLeft =  - $(this).width()/2 + 'px';
      //get margin top
      var marginTop =  - $(this).height()/2 + 'px';
	  
	  if (parseInt(marginTop) < 50 ) marginTop = 50 + 'px';
      //return updated element
      return $(this).css({'margin-left':marginLeft, 'top':marginTop});
   };

   $.fn.togglePopup = function(url){
     //detect whether popup is visible or not
     if($('#popup').hasClass('hidden'))
     {
       //hidden - then display
       //when IE - fade immediately
       if($.browser.msie)
       {
         $('#opaco').height($(document).height()).toggleClass('hidden')
                    .click(function(){$(this).togglePopup();});
       }
       else
       //in all the rest browsers - fade slowly
       {
         $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7)
                    .click(function(){$(this).togglePopup();});
       }
       $('#popup')
         .load($(this).attr("href"), '', function(){$('#popup').alignCenter();})//+'&ajax=yes')
         .alignCenter()
         .toggleClass('hidden');
     }
     else
     {
       //visible - then hide
       $('#opaco').toggleClass('hidden').removeAttr('style').unbind('click');
       $('#popup').toggleClass('hidden');
	   $('#popup').html("&nbsp;");
     }
   };

   $(".top_cart .cmp").click(function(){
		$(this).togglePopup();
		return false;
   });
   
   $(".popup_btn").click(function(){
		$(this).togglePopup();
		return false;
   });
   
	//$("#register_popup").togglePopup();
});

function popCmpDelete(elem){
	var act = "del";
		
		$.ajax({
			type: "POST",
			url: '/ajaxCompare.php',
			data: {'action': act, 'id': elem.attr("rel")},
			success: function(msg){
				$(".top_cart .cmp span").load('/smallCompare.php', function(str){if(str == "0") $(".compare_popup .no_elem").show();});
				elem.parent('div').parent('div').parent('div').hide();
				
			},
			dataType: 'json'
		});
}

function registerSubmit(){
	err = true;
	if ($('#registration_form input[name=name]').val().length < 2) 
	{ 
		$('#registration_form input[name=name]').addClass("input_error"); 
		err = false; 
	}else{
		$('#registration_form input[name=name]').removeClass("input_error");
	}
	
	if ($('#registration_form input[name=surname]').val().length < 2) 
	{ 
		$('#registration_form input[name=surname]').addClass("input_error"); 
		err = false; 
	}else{
		$('#registration_form input[name=surname]').removeClass("input_error");
	}
	if ($('#registration_form input[name=email]').val().length < 2) 
	{ 
		$('#registration_form input[name=email]').addClass("input_error"); 
		err = false; 
	}else{
		$('#registration_form input[name=email]').removeClass("input_error");
	}
	if ($('#registration_form input[name=phone]').val().length < 2) 
	{ 
		$('#registration_form input[name=phone]').addClass("input_error"); 
		err = false; 
	}else{
		$('#registration_form input[name=phone]').removeClass("input_error");
	}
	if ($('#registration_form input[name=country]').val().length < 2) 
	{ 
		$('#registration_form input[name=country]').addClass("input_error"); 
		err = false; 
	}else{
		$('#registration_form input[name=country]').removeClass("input_error");
	}
	if ($('#registration_form textarea[name=address]').val().length < 2) 
	{ 
		$('#registration_form textarea[name=address]').addClass("input_error"); 
		err = false; 
	}else{
		$('#registration_form textarea[name=address]').removeClass("input_error");
	}
	

	
	if(!err) alert($('#input_error_dialog').val());
	else{
		$.ajax({
			type: "POST",
			url: '/registration.php',
			data: {
				'name': $('#registration_form input[name=name]').val(), 
				'surname': $('#registration_form input[name=surname]').val(), 
				'email': $('#registration_form input[name=email]').val(), 
				'phone': $('#registration_form input[name=phone]').val(), 
				'country': $('#registration_form input[name=country]').val(), 
				'address': $('#registration_form textarea[name=address]').val()
				},
			success: function(msg){
				$(".registration_popup").html(msg);
	
			}
		});
	
	}
	return false;
}

function registerSubmitBtn(){
	if(registerSubmit()) $('#registration_form').submit();
}

$(window).load(function () {
	var col1Scroll = $('.text-content');
	if(col1Scroll.length > 0){
		col1Scroll.tinyscrollbar();
	}
});
