

$(document).ready(function(){
	$('input[type="text"],input[type="password"]').addClass('input_text');
	$('input[type="submit"],input[type="reset"],input[type="button"]').addClass('input_button');
	$('.inner-button').wrap('<span class="inner-button-wrap" />');

	$('header nav > ul > li').addClass('parent');
	$('header nav > ul > li').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
	$('header nav li:first').css({marginLeft:0});

	$('header nav li, .catlist .title, .catalog_items_list .title,.item_tabs li,#personal-nav li').each(function(){
		var navItem = $(this).height();
		var navItemH = $('b',this).height();
		$('b',this).css({padding:(navItem-navItemH)/2+'px 0'});
	});

	$('.catlist li,.catalog_items_list li').prepend('<img src="/images/crn_catlist.png" class="corner" alt="" />');
	$('.section_list_items .img').prepend('<img src="/images/crn_catlistitem.png" class="corner" alt="" />');

	$('.item_tabs li:first').addClass('current');
	$('.item_details .item_details_i:first').show();
	$('.item_tabs li a').click(function(){
		$('.item_tabs li').removeClass('current');
		$('.item_details .item_details_i').hide();
		$(this).parent().addClass('current');
		var showTab = $(this).attr('href');
		$(showTab).show();
		return false;
	});

	$('.left_menu li.current a:first').addClass('first');
	
	$('#show_section_description').click(function(){
		var text = $(this).text();
		if(text=='Скрыть')
		{
			$(this).text('Подробнее');
		}
		else
		{
			$(this).text('Скрыть');
		}
		$('.second_description').slideToggle();
		return false;
	});

	$(".delivery_more_links").fancybox();
	$(".img_for_fancybox").fancybox();

	$('#nav-top > ul > li:first-child,footer nav li:first-child').addClass('first');
	$('#nav-top > ul > li:last-child').addClass('last');
	
	//add to basket
	$('.buy').click(function(){
		var href = $(this).attr('href').split('?');
		$.ajax({
			url: "/inc_areas/add2basket.php",
			data: href[1],
			type: 'get',
            dataType:'json',
			success: function(msg){
				$('.small_basket').html(msg.basket_count);
				$('#buy_info').html(msg.message);
				$.fancybox({
					'showCloseButton'   : false,
					'padding' : 50,
					'href' : '#buy_info',
				});
				setTimeout(function() { $.fancybox.close(); }, 1000)
				// $.fancybox.close();
			}
		});
		return false;
	});
	
	//fast order
	$('.easy_order').fancybox({
		'padding' : 30,
		'href' : '#dialog_form',
	});
	
	$('.easy_order_button').click(function(){
		var name = $('#dialog_form input[name="name"]').val();
		var phone = $('#dialog_form input[name="phone"]').val();
		var link = $('#dialog_form input[name="link"]').val();
		var item = $('#dialog_form input[name="item"]').val();
		$.ajax({
			url: "/inc_areas/easy_order.php",
			data: {name : name, phone : phone, link : link, item : item},
			type: 'post',
            dataType:'json',
			success: function(msg){	
				if(msg.error!='')
				{
					$('.order_error').html(msg.error);
				}
				else
				{
					$('#dialog_form').html(msg.message);
					setTimeout(function() { $.fancybox.close(); }, 4500);
				}
			}
		});
		return false;
	});
	
	//add review
	$('.add_new_review').fancybox({
		'padding' : 30,
		'href' : '#add_new_review',
	});
	
	$('.star_grey').mousemove(function(event){
		var mouse_pos = event.layerX;
		var mouse_pos_int = Math.ceil(mouse_pos/20);
		var star_pos = -100 + mouse_pos_int*20;
		$('.star_orange').css('background-position', star_pos+'px 0');
		$('#add_new_review input[name="rank"]').val(mouse_pos_int);
	});

	$('.new_review_button').click(function(){
		var name = $('#add_new_review input[name="name"]').val();
		var comment = $('#add_new_review textarea[name="comment"]').val();
		var rank = $('#add_new_review input[name="rank"]').val();
		var item = $('#add_new_review input[name="item"]').val();
		
		$.ajax({
			url: "/inc_areas/add_review.php",
			data: {name : name, comment : comment, rank : rank, item : item},
			type: 'post',
            dataType:'json',
			success: function(msg){	
				if(msg.error!='')
				{
					$('.review_error').html(msg.error);
				}
				else
				{
					$('#add_new_review').html(msg.message);
					setTimeout(function() { $.fancybox.close(); }, 2500);
				}
			}
		});
		return false;
	});
	
	//review navigation
	var review_nav_ajax = function(){
		var nav_ajax_href = $(this).attr('href');
		$.ajax({
			url: nav_ajax_href,
			data: "AJAX_REVIEW=Y",
			type: 'post',
			success: function(msg){	
				$('.product_review_list').html(msg);
				$('.review_nav a').click(review_nav_ajax);
			}
		});
		return false;
	};
	$('.review_nav a').click(review_nav_ajax);
	
	//add to favorites
	$('.favorites_button a').click(function(){
		return false;
	});
	
	$('.favorites_button a').click(function(){
		var item = $('.favorites_button input[name="favorite_item_id"]').val();
		$.ajax({
			url: "/inc_areas/add2favorites.php",
			data: {item : item},
			type: 'post',
            dataType:'json',
			success: function(msg){
				$('#favorites_info').html(msg.message);
				$.fancybox({
					'showCloseButton'   : false,
					'padding' : 50,
					'href' : '#favorites_info',
				});
				setTimeout(function() { $.fancybox.close(); }, 1500)
				if(msg.error!='Y')
				{
					var favorites_add_text = $('.favorites_button span').text();
					$('.favorites_button').html('<span>'+favorites_add_text+'</span>');
				}
			}
		});
		return false;
	});
	
});

/*
$(document).ready(function(){bind(this);});var bind = function(context){
$('input[type="text"], textarea').each(function(){$(this).attr('default',$(this).val());});
$('input[type="text"], textarea').focus(function(){if($(this).val()==$(this).attr('default')){$(this).val('');}});
$('input[type="text"], textarea').blur(function(){if($(this).val()==''){$(this).val($(this).attr('default'));}});}
*/
