﻿(function($) 
{
	$.fn.createButtonRollOver = function()
	{
	    ToggleContent = function toggleContent(t, str){
			var id = $(t).attr('id');
				
			// Swap "hide" / "show" text and arrow image on toggle
			if ($('.showText', t).hasClass('hidden'))
			{
				$('.showText', t).removeClass('hidden');
				$('.hideText', t).addClass('hidden');
				$(t).parent().addClass('collapse-off');
				$('#' + id + '-content').addClass('hidden');
			}
			else
			{
				$('.showText', t).addClass('hidden');
				$('.hideText', t).removeClass('hidden');
				$(t).parent().removeClass('collapse-off');
				$('#' + id + '-content').removeClass('hidden');
			}						
						
			$('.content').each(function()
			{
				if ($('.rc_collapseBar', this).length > 0)
				{					
					var height = $(this).height();
					
					if (height != 0)
					{
						$(this).contents().find('.rc_top:eq(0)').height(height);
					}
				}
			});
		}
		// Toggle collapsing content if a class of 'toggleOnLoad' is applied
		$('.rc_collapseBar').each(function()
		{	
			var id = $(this).attr('id') + '-content';
			var isClosing;
			
			if ($('#' + id).attr('class') != null)
			{
				($('#' + id).attr('class').indexOf('toggleOnLoad') > -1) ? isClosing = true : isClosing = false;
			}
			
			if (isClosing)
			{
				ToggleContent(this, "init");
			}
			
		});
		
		$('.DownArrow').click(function()
		{
			var dd_id = $('a:first', this).attr('id');
			var ctn_id	= dd_id + '-menu';
								
			$('#' + ctn_id).toggle();
			return false;
			
		});
		
		//$('.rc_whiteBtnLeftArrow, .rc_whiteBtnRightArrow, .rc_whiteBtn, .rc_whiteBtnMore, .rc_blueBtn, .rc_collapseBar, .rc_moreBtnGallery').mouseover(function()
		//$('.rc_whiteBtn, .rc_blueBtn, .rc_collapseBar, .rc_moreBtnGallery').mouseover(function()
		$('.rc_whiteBtn, .rc_blueBtn, .rc_collapseBar, .rc_moreBtnGallery').hover(function()
		{			
			if ($(this).parent().parent().attr('class').indexOf('LeftDisabled') == -1 && $(this).parent().parent().attr('class').indexOf('RightDisabled') == -1)
			{
				$(this).css({cursor: 'pointer'});
				// Create a usable string from the array
				var classString = $(this).attr('class');
				// Create an array with all classes listed
				// Running it through the __getCleanArray to ensure no blank space
				var class_arr = __getCleanArray($(this).attr('class').split(' '));
				
				// Apply the original class string and add "-over" to the last class
				// which needs to be the button class
				$(this).attr('class', classString + ' ' + class_arr[class_arr.length - 1] + '-over');
			}
			else
			{				
				$(this).click(function()
				{				    
					return false;
				});
			}
		
		}, function()
		//}).mouseout(function() 
		{			
			// Create an array with all classes listed
			// Running it through the __getCleanArray to ensure no blank space
			var class_arr = __getCleanArray($(this).attr('class').split(' '));
			// Create a class name string excluding the last item from the array
			var classString = __getClassString(class_arr);
						
			if (classString == "")
			{
				classString = "rc_whiteBtn";
			}
						
			// Apply the class string formatted
			$(this).attr('class', classString);
		});
		
		var isReturn = false;
		
		// Create the click function to show / hide collapsing content
		$('.rc_collapseBar').click(function()
		{	
			if (isReturn)
			{
				return true;
			}
			else
			{
				ToggleContent(this, "");
				return false;
			}
		});				
		
		$('.rc_collapseBar a').click(function()
		{
			isReturn = true;
		});								
		
		
		// Cleaning out the blank spaces
		function __getCleanArray(arr)
		{
			var cleanArr = new Array();
			for (var i = 0; i < arr.length; i++)
			{
				if (arr[i] != '') { cleanArr.push(arr[i]); }
			}
			return cleanArr;
		}
		
		// Format a string from an array with blank spaces between each item
		function __getClassString(arr)
		{
			var str = '';
			for (var i = 0; i < arr.length - 1; i++)
			{
				str += arr[i] + ' ';
			}
			return str;
		}
	};
})(jQuery);
