/*Фиксируем меню top:0; */
$(window).scroll(function() { 
    if ($(this).scrollTop()>120) { 
		$('#scrollmenu').css({'position':'fixed','top':'0px'});
		$('#scrollmenu_bg').fadeIn(200);
	} else {
		$('#scrollmenu').css({'position':'static','box-shadow':'0 0 0'});
		$('#scrollmenu_bg').hide();
	}
}); 


//Подготавливаем работу с console.log() для Firebug
if (typeof(console) == 'undefined') {
    var console = {
        log: function(message) {},
        info: function(message) {},
        warn: function(message) {},
        error: function(message) {
            
        }
    }
}



$(document).mousedown(function(event)
{
	/*Для меню брендов*/
	if(	$(event.target).parents('.letter_item').length === 0 )
	{		
		$('.letter_item > a').removeClass('active');
		$('.letter_item ul').hide();
	}
	else if( $('.letter_item .active').length )
	{
		if( $(event.target).parents('.letter_item ul').length === 0 )
		{
			$('.letter_item > a').removeClass('active');
			$('.letter_item ul').hide();
		}
	}
	/* Выбор цветов для товара с оттенками */	
	if(	$(event.target).parents('#item_color_select').length === 0 &&	$(event.target).attr('id') != 'item_colors_list' )
		$('#item_colors_list').slideUp(300, function()
		{			
			$('#item_color_select').removeClass('active');
		});		
});

$(document).ready(function()
{
	/*Определение браузера*/
	if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1)
	{
		$('body').addClass('firefox');
	}
	else if (navigator.userAgent.toLowerCase().indexOf('safari') != -1)
	{
		if (!(navigator.userAgent.toLowerCase().indexOf('chrome') != -1))
		{
			$('body').addClass('safari');
		}
	}
	
	/*Клиентский счет*/
	if($("#email_input").val()!='')
	{
		$("#email_input").prev().text('');
		$("#pass_input").prev().text('');
	}	
	$("#email_input").live('focus', function()
	{
		$(this).prev().text('');
	});
	$("#pass_input").live('focus', function()
	{
		$(this).prev().text('');
	});	
	$("#email_input").live('blur', function()
	{
		if($(this).val()=='')
		$(this).prev().text('Введие свой e-mail');
	});
	$("#pass_input").live('blur', function()
	{
		if($(this).val()=='')
		$(this).prev().text('Введие пароль');
	});


	/*Меню брендов*/
	$('.letter_item > a').click(function()
	{
		$(this).toggleClass('active');
		$(this).next().slideToggle(100);
		return false;
	});



	/*Главное меню*/
	$('#main_menu .item').hover(
	function()
	{
		//alert($(".dorpdown",this).outerWidth());
	},
	function()
	{
		//$('img', this).slideDown(150);
	});
	
	/*Слайдшоу*/
	if ($('#slideshow').length > 0) {
		$('#slideshow').cycle({
			fx: 'fade',
			speed: 500,
			timeout: 5000,
			pause: 1,
			pager: '#slideshow_nav',
			pagerEvent: 'mouseover',
			pagerAnchorBuilder: function(idx, slide)
			{
				return '#slideshow_nav li:eq(' + idx + ') a'; 
			}
		});
	}


	/*Акции*/
	$('#offers li').hover(
	function()
	{
		$('.img', this).animate({top:-150}, 200);
	},
	function()
	{
		$('.img', this).animate({top:0}, 200);
	});
	
	/*Карусель*/
	function carousel_init(carousel)
	{
		carousel.clip.hover(function()
		{
			carousel.stopAuto();
    },
		function()
		{
			carousel.startAuto();
		});
	};
	if($('#carousel li').length > 5)
	{
		$('#carousel').jcarousel({auto:2, wrap:'circular', initCallback:carousel_init});
	}
	if($('#carousel_brands li').length > 4)
	{
		$('#carousel_brands').jcarousel({auto:2, wrap:'circular', initCallback:carousel_init});
	}
	
	/*Изображения товара*/
	$("#item_thumbs a").hover(
	function()
	{
		$("#item_thumbs a").removeClass('active');
		$(this).addClass('active');
		$("#item_pic a").attr('href', $(this).attr('href'));
		$("#item_pic img").attr('src', $('img', this).attr('src'));
	},
	function()
	{
	});
	
	/*Выбор цвета*/
	$('#item_color_select').click(function()
	{
		$(this).addClass('active');
		$(this).next().slideToggle(300);
	});
	
	$('#item_colors_list .color').click(function()
	{		
		$('#item_color_select').empty();
		$(this).clone().appendTo('#item_color_select');
		$('#item_colors_list').slideUp(300, function()
		{
			$('#item_color_select').removeClass('active');
		});
	});
	
	/*Отзывы*/
	$('#review_input').focus(function()
	{
		$(this).val('');
		$(this).parent().animate({height:60}, 200);
		$(this).closest('.textarea').next().slideDown(200);
	});
	
	/*Капча*/
	$('#refresh_captcha').click(function()
	{
		$(this).toggleClass('active');
		return false;
	});
	
	/*Модальное окно*/
	$('.popup_opener').bind('click', function() 
	{
		var popupID = undefined;
		if (typeof($(this).attr('href')) != 'undefined')
			popupID = $(this).attr('href');
		else
			popupID = this.id;
		
		$(popupID).fadeIn().prepend('<a href="#" class="popup_closer"></a>');
		
		var popMargTop = ($(popupID).outerHeight()) / 2;
		var popMargLeft = ($(popupID).outerWidth()) / 2;
		
		$(popupID).css({'margin-top' : -popMargTop, 'margin-left' : -popMargLeft});
		return false;
	});	

	$('.popup_closer').live('click', function()
	{
		$(this).closest('.popup').fadeOut(function()		
		{
			$(this).find('.popup_closer').remove();
		});
		return false;
	});
	
	/**
	 * добавление в корзину
	 * на странице товара
	 * */
	if ($('#item_added').length > 0) {
		$('#item_added a.cont_shopping').click(function() {
			$('#item_added .popup_closer').click();
			return false;
		});
	}

//	$('a.popup_opener').attr('href','#item_added').click(function() {
//		pageTracker._trackPageview('/add_to_basket');
//		alert('add_to_basket');
//	});
	/**
	 * [END] добавление в корзину
	 * на странице товара
	 * */

});

