/*------------------------------------------------------------------------------

	thunder.client
	
	(c) 2010 thunder::tech
	http://www.thundertech.com/

	Each method is documented using the following terms.
	
	hook: JQuery/CSS hook that activates the functionality on an element
	output: The change that the functionality results in; almost always in the form of a JQuery/CSS hook change

------------------------------------------------------------------------------*/

var thunder = { client: { project: { utils: {} }, modify: {}, checks: {} } };

/*------------------------------------------------------------------------------

	thunder.client.modify
	Create dynamic interactivity by modifying existing markup

------------------------------------------------------------------------------*/

	// method thunder.client.modify.selfLabelFields()
	// hook: .thunder-self-labeled
	// uses existing value attribute as label
	// sets thunder.markup:label attribute
	// output: toggling .thunder-label-cleared
	thunder.client.modify.selfLabelFields = function()
	{
		// Auto-label search field
		$(".thunder-self-labeled").each(function(i)
		{
			this.setAttribute('thunder.markup:label',this.value)
			if(this.type=="password")
			{
			   try
			   {
				   this.type = "text";
				   $(this).addClass('thunder-label-password');
			   }
			   catch(e)
			   {
			   }
			}
		}).focus(function()
		{
			if(this.value==this.getAttribute('thunder.markup:label'))
			{
				this.value='';
				$(this).addClass('thunder-label-cleared');
				if($(this).hasClass('thunder-label-password'))
				{
					this.type = "password";
				}
			}
		}).blur(function()
		{
			if(this.value=='')
			{
				this.value=this.getAttribute('thunder.markup:label');
				$(this).removeClass('thunder-label-cleared');
				if($(this).hasClass('thunder-label-password'))
				{
					this.type = "text";
				}
			}
		});	
	};
	
	// method thunder.client.checks.selfLabelFilled(field):Boolean
	// works on both self-labeled via this script and non-self-labeled fields
	// returns: true if filled, false if empty
	thunder.client.checks.selfLabelFilled = function(field)
	{
		if(field.value===''||field.value==field.getAttribute('thunder.markup:label'))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	// method thunder.client.modify.knapikMenu()
	// hook: .thunder-knapik-menu
	// output: toggling .thunder-nav-on
	thunder.client.modify.knapikMenu = function()
	{
		$('.thunder-knapik-menu').find('li').mouseenter(function()
		{
			$(this).addClass('thunder-nav-on');
		}).mouseleave(function()
		{
			$(this).removeClass('thunder-nav-on');
		});
	};

	// method thunder.client.modify.tabSet()
	// hooks: .thunder-tab, .thunder-tab-window
	// correlator: (tab) thunder.markup:item="css-hook" e.g. #this-tab-window or .these-tab-windows
	// output: toggling .thunder-tab-on, .thunder-tab-window-on
	thunder.client.modify.tabSet = function()
	{
		$('.thunder-tab').click(function()
		{
			$('.thunder-tab').removeClass('thunder-tab-on');
			$('.thunder-tab-window').removeClass('thunder-tab-window-on');
			$(this).addClass('thunder-tab-on');
			$(this.getAttribute('thunder.markup:item')).addClass('thunder-tab-window-on');
		});
	}
	
	// method thunder.client.modify.linkOptions()
	// hook: .thunder-link-options
	// output: changes window.location to value attribute
	thunder.client.modify.linkOptions = function()
	{
		$('.thunder-link-options').change(function()
		{
			var u, i;
			for(i=0;i<this.options.length;i++)
			{
				if(this.options[i].selected==true) window.location = this.options[i].value;
			}
		});
	}

	// method thunder.client.modify.rollImages()
	// hook: .thunder-roll-over
	// output: modifies src to and and remove -over from .jpg, .gif, .png
	thunder.client.modify.rollImages = function()
	{
		$('.thunder-roll-over').mouseenter(function()
		{
			var i;
			i = this.src;
			i = i.replace('.jpg', '-over.jpg');
			i = i.replace('.gif', '-over.gif');
			i = i.replace('.png', '-over.png');
			this.src = i;
		}).mouseleave(function()
		{
			var i;
			i = this.src;
			i = i.replace('-over.jpg', '.jpg');
			i = i.replace('-over.gif', '.gif');
			i = i.replace('-over.png', '.png');
			this.src = i;
		});
	};

	// method thunder.client.modify.requireFields()
	// hooks: .thunder-required, .thunder-requiree
	// output: modifies the requiree's disabled property
	//			adds .thunder-requiree-met, .thunder-required-met when requirements met
	thunder.client.modify.requireFields = function()
	{
		var requireChecker = function()
		{
			var disableRequirees = false;
			$('.thunder-required').each(function()
			{
				if(thunder.client.checks.selfLabelFilled(this)==false)
				{
					//alert("disabled");
					disableRequirees = true;
					$(this).removeClass('thunder-required-met');
				}
				else
				{
					//alert("enabled");
					$(this).addClass('thunder-required-met');
				}
			});
			$('.thunder-requiree').each(function()
			{
				if(disableRequirees==true)
				{
					//alert("!disabled!");
					$(this).removeClass('thunder-requiree-met');
				}
				else
				{
					//alert("!enabled!");
					$(this).addClass('thunder-requiree-met');
				}
				this.disabled = disableRequirees;
			});
		};
		requireChecker();
		$('.thunder-required').change(requireChecker);
		$('.thunder-required').keypress(requireChecker);
	};

