//Basic actions for the CenterShop

jQuery.fn.fadeToggle = function(speed, easing, callback) { 
   return this.animate({opacity: 'toggle'}, speed, easing, callback); 
};

jQuery.fn.highlight = function() { 
   return this.css('border','dashed red 1px'); 
}; 

var actions = 
{
	prepare : function()
	{
		
		$('#catalogue .items-outer').each(function(){
			var c_pos = 0;
			var old_c_pos=0;
			var $products = $('ul.items > li', this);
			var maxpos = Math.ceil($products.length / 4)-1;
			
			//init: hide 5-... products
			$products.filter(':gt(3)').hide();
			$('a.rewind', this).css('opacity', 0.5);
			if (maxpos==0) $('a.forward', this).css('opacity', 0.5);
					
			//Loading another items in shop
			$('> a', this).click(function(){
			
				c_pos += $(this).hasClass("rewind")? -1: +1;
				
				if (c_pos < 0) c_pos=0;
				if (c_pos > maxpos) c_pos=maxpos;
				
				if (c_pos != old_c_pos){
					old_c_pos = c_pos;
					var start = c_pos*4;
					var end = (c_pos+1)*4;
					
					//if ($.support.opacity){
						$products.parent().animate({opacity:0}, 400, function(){
							$products.hide();
							$products.slice(start, end).show();
							//if (!$products[start+1]) $products[start].addClass('open');
						}).animate({opacity:1}, 400, function(){
							$(this).css('filter', '');
						});
					//} else { //IE
					//	$products.hide();
					//	$products.slice(start, end).show();
					//}
					
					//switch btn state
					$('a.rewind', $(this).parent()).css('opacity', c_pos==0? 0.5:1);
					$('a.forward', $(this).parent()).css('opacity', c_pos==maxpos? 0.5:1);
				}
				
				return false;
			});
		});
		
		//Show tags
		$("a[rev='tags']").click(function()
		{			
			if (IEmode == true)
			{
					
				$("#backlog").toggle();
				$("#thirdnav").toggle();
			}
			else
			{
				/*if ($("#tags").css('display')=='block')
					$("#tags").fadeOut(100);
				*/	
				$("#backlog").fadeToggle(500);
				$("#thirdnav").fadeToggle(1000);
			}
			setTimeout(600,$("#tags").fadeIn('slow'))
			return false;
		});
	
		//Shows tips
		$(".helpers .tips").click(function()
		{
//			if ($("#popup-tips").css('display')=='none')

			/*if (document.getElementById("popup-tips") == null)
			{
				var tips = document.createElement("div");
				tips.id = "popup-tips";
				document.body.appendChild(tips);
			}*/
			
			$("#popup-tips").fadeToggle();
			
			return false;
		});
		$("#popup-tips").click(function()
		{
//			if ($("#popup-tips").css('display')=='none')

			/*if (document.getElementById("popup-tips") == null)
			{
				var tips = document.createElement("div");
				tips.id = "popup-tips";
				document.body.appendChild(tips);
			}*/
			
			$("#popup-tips").fadeToggle();
			
			return false;
		});
/*		$(".helpers .help").click(function()
		{		
			if ($("#tags").css('display')=='block')
					$("#tags").fadeOut(100);
				
		});*/
		//Shows backlog and menu
		$(".helpers .help").click(function()
		{
			if (IEmode == true)
			{
					
				$("#backlog").toggle();
				$("#tags").toggle();			}
			else
			{
				/*if ($("#tags").css('display')=='block')
					$("#tags").fadeOut(100);
				*/	
				$("#backlog").fadeToggle(500);
				$("#tags").fadeToggle(1000);
			}
			
			return false;
		});
		
		/*function showpopup(){
			$(this).prev().click();
			return false;
		}*/
		
		//show price on image click
		$('ul.items > li > a:not(.zoom)').click(function(){
			if ($(this).parent('li').hasClass('open')){
				$(this).prev().click();
			}
			else{
				$('li.open:visible', this.parentNode.parentNode).removeClass('open');
				$(this).parent('li').addClass('open');
			}
			//$('li.open:visible > a:not(.zoom)', this.parentNode.parentNode).unbind('click', showpopup);
			//$(this).click(showpopup);
			
			return false;
		});
		//init open item
		//$('ul.items > li.open > a:not(.zoom)').click(showpopup);
		
		//init open listing
		$('ul.items-list > li > a:not(.zoom)').click(function(){
			$(this).prev().click();
			return false;
		});

		//Zooming of images
		$("a.zoom").fancybox({"overlayShow" : true, 
							  "overlayOpacity" : 0.8, 
							  "zoomOpacity" : true,
							  "frameWidth" : 650,
							  "frameHeight" : 560,
							  "hideOnContentClick": false,
							  'funnythirdoption': true
							  });
		
		//init: roublize				  
		actions.roublize();
		
		//Form submits by link
		$("a[rel=submit]").click(function()
		{
			$(this).parents("form").submit();
		});
	},
	
	roublize : function()
	{
		//Modifies rouble sign
		$("li div em").each(function()
		{			
			var price = this.innerHTML.match(/(\d+)\s*(?:Р|руб|р)/);
			if (price != null)
			{
				/*if (IEmode == true)
					this.innerHTML = price[1] + " <span class=\"rouble-h\">&ndash;</span><span class=\"rouble\">p</span>";
				else
					this.innerHTML = price[1] + " <span class=\"rouble-h\"></span><span class=\"rouble\">p</span>";
				*/
				if (IEmode == true)
					this.innerHTML = price[1] + " <span class=\"rouble\">pуб.</span>";
				else
					this.innerHTML = price[1] + " <span class=\"rouble\">pуб.</span>";	
				this.setAttribute("roublized", "true");
			}
		});
	},
	
	show_loader : function(sender, classify)
	{
		var load = document.createElement("span");
		load.className = "loader-box " + classify;
		load.innerHTML = "Загрузка";
		$(sender).after(load);
		return load;
	},
	
	hide_loader : function(loader)
	{
		$(loader).remove();
	}
}

$(document).ready(function() { actions.prepare(); } );