/**
 *	[x+1] JavaScript Utilities
 * 
 * 	(c) 2009 x+1 Inc. - all rights reserved
 * 	http://www.xplusone.com
 * 
 *  $Id: xplusone.utils.js 251 2010-04-08 18:33:03Z hieronymus $
 */
 
/**
 * Create Namespace
 */
var XPLUSONE = {};

/**
 * Cross Promo Cycler
 */
XPLUSONE.crosspromo = function()
{
	
	return {
				
		timer : function()
		{
			if (XPLUSONE.crosspromo_count > (XPLUSONE.crosspromo_data.length - 1))
			{
				XPLUSONE.crosspromo_count = 0;
			}

			this.loadImage(XPLUSONE.crosspromo_data[XPLUSONE.crosspromo_count]);
			XPLUSONE.crosspromo_count++;

		},

		loadImage : function(content)
		{
			XPLUSONE.cross_container.fadeOut('slow', function(){
				XPLUSONE.cross_container.html(content);
				XPLUSONE.cross_container.fadeIn('slow', function(){
					window.setTimeout(function() {
					 XPLUSONE.crosspromo.timer();
					}, XPLUSONE.cross_delay);
				});
			});
		}
		
	};
		
}();


/**
 * Navigation
 * 
 * @author shinjuku
 * @version $Rev$
 */
XPLUSONE.navigation = function () 
{

	var container 	= '';
	var children	= [];
	
/**
 * Swaps image name in SRC attr
 * @private
 */	
	toggle_image = function(element, state)
	{
		if ($(element).find('img').attr('class') !== 'nav_select')
		{
			imgsrc = $(element).find('img').attr('src').split('_');
			imgsrc[imgsrc.length - 1] = state + ".png";
			imgsrc = imgsrc.join('_');			
			$(element).find('img').attr('src', imgsrc);
		}

	};
	
/**
 * Derives rollover image id from calling id and toggles display
 * @private
 */	
	toggle_product_rollover = function(parent, state)
	{
		var parent_id = parent.id.split('_');
		var root_id = parent_id.slice(1,parent_id.length);
		var target_name = ['#rollover'];
		var target_id = target_name.concat(root_id).join('_');
		
		if (state == 1)
		{
			$(target_id).show();
		}
		else
		{
			$(target_id).hide();
		}
	};
	
/**
 * Zip through list of selectors and set to active if href is this page
 * @private
 */
	find_active_link = function(list, url)
	{
		$.each(list, function(index, value){
			
			var a = $(value).children('a');
			
			if (a.attr('href') === url)
			{
				// Cache elements
				var img = a.find('img');
				var src = img.attr('src')	;			
				var imgsrc = src.split('_');
				
				// Rebuild SRC
				imgsrc[imgsrc.length - 1] = "on.png";
				imgsrc = imgsrc.join('_');
				img.attr('src', imgsrc);
				
				img.attr('class','nav_select');
			}

		});
	};

/**
 * PUBLIC METHODS
 */
	return  {
		
	/**
	 * Public interface to find_active_link
	 */
		find_active : function(topnav, this_url)
		{
			find_active_link(topnav, this_url);
		},
	
	
	/**
	 * Instantiates rollover state for top nav
	 */	
		init_nav_rollovers : function (c)
		{
			children = $(c + ' li a');

			if (children.length > 0)
			{
				children.each(function(i)
				{			
					$(this).hover(			
						// Over
						function()
						{
							toggle_image(this, 'on');
						},

						// Out
						function()
						{
							toggle_image(this, 'off');
						}			
					);
				});
			}			
		},
		
	/**
	 * Instantiates hover state for product images
	 */
		init_product_rollovers : function(classname)
		{
			var c = $(classname);

			if (c.length > 0)
			{
				c.each(function(i){

					$(this).hover(

						function()
						{
							toggle_product_rollover(this, 1);
						},

						function()
						{
							toggle_product_rollover(this, 0);
						}

					);

				});
			}
		}
		
	};

}(); 