$(function () {
	$('.lazy').lazyload({
		 placeholder : "/site/theme/blank.gif" 
	 });
	
	$('.matpopup').fancybox({
		'padding'			: 50,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'			:'iframe',
		'width'			:600,
		'height'		:500
	});
	
	$('.popup_simple').fancybox({
		'padding'			: 40
	});
	
	$('.popup').fancybox({
		'padding'			: 50,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'hideOnContentClick':true	
	});
	
	
	$('.iload').fancybox({
		'padding'			: 25,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic'
	});
	
	$('.iframe').fancybox({
		'padding'			: 50,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'			:'iframe'
	});

	$('#city_select').fancybox({
		'width'				: '80%',
		'height'			: '80%',
        'autoScale'     	: false,
        'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'overlayOpacity'	:0.2,
		'speedIn' 			:500,
		'speedOut'			:500,
		'opacity'			:true,
		'padding'			: 50,
		'onComplete'		:function(){
			$('#fancybox-content #citylist a').live('click',function(){
				var city=$(this).attr('rel');
				$.post('/store/index/input/',{city:city},function(data){
					$('#city_select span').html(data['label']);
					$('#salon_counter').html(data['count']);
					$('#salon_popup').attr('href',data['href']);
					$.fancybox.close();
				},'json');
				return false;
			});
		}
	});

	

	
	
	$('.wherebuy a').fancybox({
		'width'				: '80%',
		'height'			: '80%',
        'autoScale'     	: false,
        'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'overlayOpacity'	:0.2,
		'speedIn' 			:500,
		'speedOut'			:500,
		'opacity'			:true,
		'padding'			: 50,
		'onComplete'		: function(){
			
			$('#fancybox-content #citylist a').die('click');
			$('#fancybox-content #citylist a').live('click',function(){
				return true;
			});
			
		}
	});
	
	
	
	$('#sort_wares a').live('click',function(){
		
		if(!$(this).closest('li').hasClass('active')){
			$('#sort_wares li').removeClass('active');
			$(this).closest('li').addClass('active');
			var clas=$(this).attr('rel');
			$('#wares_items').fadeOut(function(){
				$('#wares_items').removeClass('table');
				$('#wares_items').removeClass('list');
				$('#wares_items').addClass(clas);	
				$('#wares_items').fadeIn();	
			
			});
		}
		return false;
	});
	
	$('#show_wares a').live('click',function(){
		if(!$(this).closest('li').hasClass('active')){
			$('#show_wares li').removeClass('active');
			$(this).closest('li').addClass('active');
			var clas=$(this).attr('rel');
			if(clas=='3d'){
				$('.image_wrap').hide();
				$('#item_3d').show();
			}else{
				$('.image_wrap').show();
				$('#item_3d').hide();
				
			}
			
				
		}
		return false;
	});
	
	
	$("#item_near .scrollable").scrollable();
	
	$("#salon_list .scrollable").scrollable({
		vertical:true
	});
	
	$("#item_imgs .scrollable").scrollable();
	$("#item_imgs .scrollable a").click(function(){
		var wrap = $("#item_imgs .image_wrap");
		if(wrap.css('display')=="none"){
			$('#show_wares a[rel="photo"]').trigger('click');
		}
		
		
		if ($(this).hasClass("active")){ 
			return false; 
		}
		var url = $(this).attr("href");
		var href = $(this).attr("rel");
		
		wrap.find('img').fadeTo("fast", 0,function()
		{
			var img = new Image();
			img.src = url;
			
			img.onload = function() {
				wrap.find('img').attr('src',url);
				wrap.attr('href',href);
				wrap.find('img').fadeTo("fast", 1);
			};
			
		}
		);
		

		$("#item_imgs .scrollable a").removeClass("active");
		$(this).addClass("active");
		return false;
		
	});
	
	if($("#item_imgs .scrollable a:eq(0)").size()>0){
		$("#item_imgs .scrollable a:eq(0)").addClass('active');
	}else{
		$("#item_imgs .scrollable a:first").addClass('active');
	}
	
	$('#wares_item #sizes_change a').live('click',function(){
		if(!$(this).closest('li').hasClass('active')){
			$('#wares_item #sizes_change li').removeClass('active');
			$(this).closest('li').addClass('active');
			var clas=$(this).attr('rel');
			$('#wares_item .sizes a').css('display','none');
			$('#wares_item .sizes .'+clas).css('display','block');
		}
		return false;
	});
	
	
	
	
	
	$('#q').focus(function(){
		var val=$(this).val();
		if(val=="Поиск по сайту"){
			$(this).val('')
		}
		
	}).focusout(function(){
		var val=$(this).val();
		if(val==""){
			$(this).val('Поиск по сайту');
		}	
	});
	
	$('#search').submit(function(){
		var val=$(this).val();
		if(val=""){
			return false;
		}
		
	});
	
	
	$('.roundCorners').append('<div class="tl"></div><div class="tr"></div><div class="br"></div><div class="bl"></div>');
	$('#main_menu .folder').each(function(){
			var text=$(this).html();
			text+="&nbsp;&nbsp;<img src='/site/theme/img/menu_arrow.png' alt=''/>";
			$(this).html(text);
	});
	
	$('#main_menu>ul>li').hover(function(){
		$('#main_menu li').removeClass('active');
		$(this).addClass('active');
		if($(this).find('ul').size()>0){
			
			var submenuWidth=$(this).width();
			
			$(this).find('ul').css('width',submenuWidth);
			
			
		}}
	);
	
	var activemenu=$('#main_menu>ul>li.active');
	if(activemenu.size()>0){
		if(activemenu.find('.hideme').size()>0){
			$('#main_menu>ul>li').removeClass('active');
			var activemenu=$('#main_menu>ul>li.active');
		}else{
		activemenu.trigger('mouseover');
		}
		
	}
	
	$('#main_menu').mouseleave(
	function(){
		$('#main_menu>ul>li').removeClass('active');
		activemenu.addClass('active');
	});
	
	
	

});
