/*
* Copyright (C) 2009 Joel Sutherland
* Licenced under the MIT license
* http://www.newmediacampaigns.com/page/autosprites-jquery-menu-plugin
*/
// IE 6 doesn't work with russian comments
(function($) {
	$.fn.autosprites = function(settings) {
		settings = $.extend({
			offset: '100%',
			orientation: 'horizontal',
			over: { opacity: 'show' },
			overSpeed: 400,
			out: { opacity: 'hide' },
			outSpeed: 400,
			activeState: false,
			activeClass: 'item_active',
			activeSprites: false,
			singleSprite: false
		}, settings);

		function rempx(string){ return Number(string.substr(0, string.length - 2));	}
		function addpx(number){	return number + 'px'; }
		
		$(this).each(function(){
		// cycle <ul> with given id
			var backgroundImage = $(this).css('background-image');
			
			// Solves IE6 stacking context bug
			$(this).css('zIndex', 100).find('a').css('zIndex', 99);

			var totalPositionOffset = 0;
			
			// added  (By Destroyer)(for unlimited elements in the list)
			var i=0; // li counter
			$(this).children().each(function(){
			// cycle through all <li> in current <ul>
				// hack for opera, because it doesnt correctly recognize display:inline in css for horisontal menu
				if (settings.orientation == 'horizontal')
				{
				// move every (li) on its width
				var new_left = i * rempx($(this).css('width'));
				$(this).css('left', new_left);
				i++;
				} // if (settings.orientation == 'horizontal')
				
			// end of added (By Destroyer)
			
				if (!settings.singleSprite)
				{
				var positionOffset = '-' + addpx(totalPositionOffset);
				}
				else
				{var positionOffset = "0px";}
				// Assign the background image to each menu item and give it the correct background-position
				var baseOffset = '0px';
				
				//If this child is active we need to adjust the base offset to the active level
				if (!settings.singleSprite)
				{
					if(settings.activeState && $(this).hasClass(settings.activeClass)){
						baseOffset = '-' + addpx(rempx(settings.offset));
						if(settings.activeSprites)
							baseOffset = '-' + addpx(rempx(settings.offset) * 2);
					}
				}
				
				
				
				
				var position = settings.orientation == 'horizontal' ? positionOffset + ' ' + baseOffset : baseOffset + ' ' + positionOffset;
				var offsetPosition = settings.orientation == 'horizontal' ? positionOffset + ' -' + settings.offset : '-' + settings.offset + ' ' + positionOffset;

				
				$(this).css({
					backgroundImage: backgroundImage,
					backgroundPosition: position
				});
				
				// Build the Hover Divs so that the background can be animated
				var width = $(this).css('width');
				var height = $(this).css('height');
				
				
				/* width = rempx(width) - 10;
				width = addpx(width);
				
				var custom_top = height;
				var sel_h = 60;
				custom_top = Math.round(rempx(height)/2) - sel_h/2;
				custom_top = addpx(custom_top);
				
				height = rempx(height) - 10;
				height = addpx(height); */
				
				
				//width = this.clientWidth;
				//height = this.clientHeight;
				//width = getElementWidth('selected');
				
				var hover = $('<div>&nbsp;</div>').css({
						cursor: 'hand',
						zIndex: 1,
						position: 'absolute',
						top: 0,
						left: 0,
						width: width,
						height: height,
						backgroundImage: backgroundImage,
						backgroundPosition: offsetPosition
					}).hide();
				$(this).prepend(hover);
				
				
				$(this).hover(function(){
					$(this).find('div').stop(false, true).animate(settings.over, settings.overSpeed);
				}, function(){
					$(this).find('div').stop(false, true).animate(settings.out, settings.outSpeed);

				});
				
				// (By Destroyer)
				// highlight current url
				var page_url = window.location;
				
				var curr_url = $(this).find('a').attr('href');
				curr_url = 'http://'+document.domain+curr_url;
				
				if (page_url == curr_url)
				{
				$(this).find('div').stop(false, true).animate(settings.over, settings.overSpeed);
				//$(this).removeClass('topmenu_item');
				$(this).addClass(settings.activeClass);
				
				
				$(this).mouseleave(function () {
				$(this).find('div').stop(false, true).animate(settings.over, settings.overSpeed);
				});
				
				}
				
				// end of (By Destroyer)
	
				if (!settings.singleSprite)
				{
				totalPositionOffset += settings.orientation == 'horizontal' ? rempx(width) : rempx(height);
				}
				
				// added (By Destroyer)
				// set class for selected element
				$(this).click(function () {
				//reset the selected item
				$(this).removeClass(settings.activeClass);
				$(this).find('div').stop(false, true).animate(settings.out, settings.outSpeed);
				
				//select the current item
				$(this).addClass(settings.activeClass);
				});
								
				
				}); // </li>
			
			
		//If the mouse leaves the menu, reset the floating bar to the selected item
		//$('#topmenu').mouseleave(function () {
		
		
	//	$(this).mouseleave(function () {
	//	$('#topmenu li#topmenu_item_active').find('div').stop(false, true).animate(settings.over, settings.overSpeed);
		
		
		//$(this).li('topmenu_item_active').find('div').stop(false, true).animate(settings.over, settings.overSpeed);
		/*
		$(this).find('div').stop(false, true).animate(settings.over, settings.overSpeed);
		//Retrieve the selected item position and width
		default_left = Math.round($('#lava li.selected').offset().left - $('#lava').offset().left);
		default_width = $('#lava li.selected').width();
		
		//Set the floating bar position, width and transition
		$('#box').stop(false, true).animate({left: default_left},{duration:1500, easing: style});	
		$('#box .head').stop(false, true).animate({width:default_width},{duration:1500, easing: style});		
		*/
		
	//	});	
		// end of added (By Destroyer)
		//$('#topmenu li#topmenu_item_active').find('div').stop(false, true).animate(settings.over, settings.overSpeed);
		
		
		}); // </ul>
	}
})(jQuery);

