$(document).ready(function(){
	//Cufon.replace('h1, h4, h2, h3, #phone_block .js-tel');
    $('a.zoom').fancybox({
    	transitionIn	:	'elastic',
		transitionOut	:	'elastic'
    });
    $('a.zoom2').fancybox({
    	transitionIn	:	'elastic',
    	transitionOut	:	'elastic',
    	padding:0
    });
    $('a.js-loadPlenka').fancybox({
			'padding'		: 20,
			'titleShow'		: false,
			'width'			: 900,
			'height'		: 630,
			'autoScale' 	: false,
			'autoDimensions': false
	});
    blurInput('.js-blur');

    $('#basket_block').hover(function(){
    	$('.basket_ico_hide').stop(true).animate({opacity:1}, 150);
    },
    function(){
    	$('.basket_ico_hide').stop(true).animate({opacity:0}, 150);
    });


    $('#left_menu ul ul').each(function(i) { // Check each submenu:
		if ($.cookie('submenuMark-' + i)) {  // If index of submenu is marked in cookies:
			$(this).show().prev().removeClass('collapsed').addClass('expanded'); // Show it (add apropriate classes)
		}else {
			$(this).hide().prev().removeClass('expanded').addClass('collapsed'); // Hide it
		}
		$(this).prev().addClass('collapsible').click(function() { // Attach an event listener
			var this_i = $('#left_menu ul ul').index($(this).next()); // The index of the submenu of the clicked link
			if ($(this).next().css('display') == 'none') {
				$(this).next().slideDown(300, function () { // Show submenu:
					$(this).prev().removeClass('collapsed').addClass('expanded');
					cookieSet(this_i);
				});
			} else {
				$(this).next().slideUp(300, function () { // Hide submenu:
					$(this).prev().removeClass('expanded').addClass('collapsed');
					cookieDel(this_i);
					$(this).find('ul').each(function() {
						$(this).hide(0, cookieDel($('#left_menu ul ul').index($(this)))).prev().removeClass('expanded').addClass('collapsed');
					});
				});
			}
		return false; // Prohibit the browser to follow the link address
		});
	});

    $('.js-open_text').click(showText);

    $(window).scroll(function () {
    	id = $.browser.webkit ? 'body' : 'html';
    	if($(id).scrollTop() >= 600) $('a#up_page').css('display', 'block');
    	else $('a#up_page').css('display', 'none');
	});

    $('a[href*=#]:not([href=#])').bind('click', function (event) {

    	if($.browser.webkit) $('body').stop().animate( { scrollTop: 0}, 1100 );
		else		     	 $('html').stop().animate( { scrollTop: 0}, 1100 );

    	return false;
    });

   // $('#js-back').click(backCompare);
   // $('#js-forward').click(forwardCompare);
    /*if($.cookie('gold_ny')) {

    } else {
	    setTimeout(function(){
			$('#ny_art').trigger('click');
			$.cookie('gold_ny', '1', {expires: null, path: '/'});
		}, 5000);
    }*/
});


function blurInput(el) {
	$(el).focus(function(){
		if($.trim($(this).val()) == $(this).attr('data-def')) $(this).val('');
	})
	$(el).blur(function(){
		if($.trim($(this).val()) == '') $(this).val($(this).attr('data-def'));
	})
}


function cookieSet(index) {
	$.cookie('submenuMark-' + index, 'opened', {expires: null, path: '/'}); // Set mark to cookie (submenu is shown):
}
function cookieDel(index) {
	$.cookie('submenuMark-' + index, null, {expires: null, path: '/'}); // Delete mark from cookie (submenu is hidden):
}


function showText() {
	if($(this).parent().prev().hasClass('js-hidden_text')) {
		$(this).parent().prev().slideToggle(400);
	}
	return false;
}


function backCompare() {
	var all = $('#compare_block .js-wrap_compare').length;
	var current = $('#compare_block div.current').index()-2;

	$('#compare_block > div').removeClass('current');

	if((current+1) > 1)
		$('#compare_block .js-wrap_compare').eq(current-1).addClass('current');
	else
		$('#compare_block .js-wrap_compare:eq('+(all-1)+')').addClass('current');

	return false;
}

function forwardCompare() {
	var all = $('#compare_block .js-wrap_compare').length;
	var current = $('#compare_block div.current').index()-1;

	$('#compare_block > div').removeClass('current');

	if(current < all)
		$('#compare_block .js-wrap_compare').eq(current).addClass('current');
	else
		$('#compare_block .js-wrap_compare:eq(0)').addClass('current');

	return false;
}



function loadPlenka(id) {
	$.post('/tpl/ajax.php',{m:'loadPlenka',id:id},
            function(d){
				if(d) {
					$('#js-body').append(d);
					$('#js-loading').remove();
				}
            });
}


function checkNDS() {
	if($('#ID_PAY_SYSTEM_ID_3:checked').val())
		$('#js-nds').css('display', 'table-row');
	else
		$('#js-nds').css('display', 'none');
}
/*function anchor(el) {
	$(el).click(function () {
		elementClick = $(this).attr('href');
		destination = $(elementClick).offset().top;

		if($.browser.safari) $('body').animate( { scrollTop: destination }, 1100 );
		else		     	 $('html').animate( { scrollTop: destination }, 1100 );

		return false;
    });
}*/
