/**
 * SwapIt
 *
 * Swapit is a quick and easy plugin designed to make 
 * simple mouseover image swapping easy.
 *
 * @example     $('.classname').swapit('_over');
 */
$.fn.swapit = function(a) {
	return this.each(function() {
		if($(this).get(0).tagName.toUpperCase() == 'IMG') {
			var original = $(this).attr("src");
			if(original.indexOf('_over') == -1) {
				var over = original.split('.');
				if(over.length >= 2) {
					over[over.length-2] += a;
					over = over.join('.');
				} else {
					over = original + a;
				}
				$(this).mouseover(function() { $(this).attr('src', over); }).mouseout(function() { $(this).attr('src', original); });
			}
		}
	});
}

$.rand = function (min, max) {
    if( max ) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    } else {
        return Math.floor(Math.random() * (min + 1));
    }
}

$.fn.hoverClass = function(c) {
   return this.each(function(){
      $(this).hover( 
         function() { $(this).addClass(c);  },
         function() { $(this).removeClass(c); }
      );
   });
};

$.preloadimages = function()
{	for(var i = 0; i<arguments.length; i++)
	{	img = new Image();
		img.src = arguments[i];
	}
}

$(document).ready(function(){
	
	if (document.all) {
		$("#dropdownMenu > li > ul").fadeOut("fast");
		$("#dropdownMenu li").hover(
			function(){ $(this).find("ul:first").fadeIn("fast"); }, 
			function(){ $(this).find("ul:first").fadeOut("fast"); } 
		);
	} else {
		$("#dropdownMenu li").hoverClass("sfHover");
	}
	  
	/**
	 *	Menu Highlighting when on Related Pages
	 *	@start
	 */
	
	/**
	 *	The following breaks down the current page to the bare essentials
	 *	@example /adash/products_afr_light.html becomes "products" (minutes the quotes)
	 */
	var _local = document.location.pathname;
		_local = String(String(String(_local).split('/').pop()).split('.').shift()).split('_').shift();
	
	var _img = $('#nav-'+_local).attr('src');
		_img = String(String(_img).split('/').pop()).split('.');
		_img = _img[0]+'_over.'+_img[1];
	var _path = String($('#nav-'+_local).attr('src')).split('/');
	
	_path[(_path.length-1)] = _img;
	
	$('#nav-'+_local).attr('src', _path.join('/'));
	$('#nav-'+_local).removeClass("swap");
	
	//	@end 
	
	/**
	 *	Referral from support page back to correct product page
	 *	@start
	 */
	var _local = String(document.location.pathname).split('/').pop();
	var _refer = String(document.referrer).split('/').pop();
	
	switch (_local) {
		case 'products_hardware.html':
		case 'products_pricing.html':
		case 'products_competitor.html':
		case 'support.html':
			switch (_refer) {
				case 'products_afr_light.html':
					$('p.bbreadcrumb').html('<a href="'+_refer+'">Return to ADASHI Lite 3.0</a>').show();
					break;
				case 'products_first_response.html':
					$('p.bbreadcrumb').html('<a href="'+_refer+'">Return to ADASHI First Response 3.0</a>').show();
					break;
				case 'products_command_post.html':
					$('p.bbreadcrumb').html('<a href="'+_refer+'">Return to ADASHI Command Post 3.0</a>').show();
					break;
			}
		break;
	}
	// 	@end
	
	$('.swap').swapit('_over');
	
	if ($('div#successStories').length) {
		$('div#successStories div').each(function () { $(this).hide(); });
		$('div#successStories div').eq($.rand(0, $('div#successStories div').length-1)).show();
	}
});