/* RESEN */
/* Javascript Functions
----------------------------------------------------------------------------- */


	$(document).ready(function() {
	
		browserDetect();
		lightbox();
		actions();
		replaceSubmitButtons();
		placeholder();
		
	});
	
	$(window).load(function() {

		tabs();
		alerts();
		$('body').removeClass('loading');
		password();
		email();
					
	});
		

/* FUNCTION: LINK BEHAVIORS
----------------------------------------------------------------------------- */

	
	function actions() {

		
/* Back Link */


		$('a[href="#back"]').click(function() {
		
			history.go(-1);
			return false;
		
		});
		
		
/* Social Bookmarks */
			

		if ($('ul.share').length > 0) {
		
			var itemTitle = $('ul.share').parent().children('h4').text();
		
			$('ul.share .facebook a').attr('href', 'http://www.facebook.com/sharer.php?u=' + location.href);
			$('ul.share .delicious a').attr('href', 'http://del.icio.us/post?v=4&noui&jump=close&url=' + location.href + '&title=' + itemTitle);
			$('ul.share .stumbleupon a').attr('href', 'http://www.stumbleupon.com/submit?url=' + location.href + '&title=' + itemTitle);
			$('ul.share .digg a').attr('href', 'http://digg.com/submit?phase=2&url=' + location.href + '&title=' + itemTitle);

		}
		
	}
	
	
/* FUNCTION: TABS
----------------------------------------------------------------------------- */


	function tabs() {
	
	
/* Remove Empty Tabs */


		$('.tabbed > ul.tabs').children('li').each(function() {
		
			var pane = $(this).find('a').attr('href');
			if ($(this).parent().siblings(pane).children().length == 0) { 
			
				$(this).parent().siblings(pane).remove();
				$(this).remove();
				
			}
	
		});
		
	
/* Initiate Tabs */


		$('.tabbed > ul.tabs').tabs();
		
		
/* Fix Tabbed Paging */

		
		$('.tabbed .pane ul.paging a, .tabbed .pane ul.pages a').each(function() { 
		
			var pane = $(this).closest('.pane').attr('id');
			$(this).attr('href', $(this).attr('href') + '#' + pane); 
	
		});
		
		
	}
	
	
/* FUNCTION: REPLACE SUBMIT BUTTONS
----------------------------------------------------------------------------- */


	function replaceSubmitButtons() {
	
	
/* Replace Buttons */

	
		$('input[type=submit]').each(function() {
					
			var language = $(this).attr('value');
			$(this).parent().addClass('submit');
			$(this).after('<a href="#submit">' + language + '</a>');

		});
		

/* Button Click & Validate */

	
		$('ul.actions .submit > *').click(function() {
		
			$('label.error').removeClass('error');
			$('div.alert').remove();
		
			$(this).closest('form').find('label.required input:not([type=hidden]), label.required select').each(function() {
			
				if (!$(this).attr('disabled')) {
				
					if ($(this).val() == "") { $(this).closest('label').addClass('error'); }
				
				}
				
			});

			if ($(this).closest('form').find('label.error').length > 0) {

				$('body').append('<div class="alert error"><h3>You forgot something! Missing information is highlighted in red.</h3></div>');
				alerts();
				return false;
				
			} else {
			
				$(this).parents('form').submit();
				return false;
			
			}
			
		});
		
		
/* Form Action Swap */


		$('form:not(.cart form)').submit(function() {
			
			if ($(this).attr('action').match('functions')) {
			
				var action = window.location.href.replace(window.location.protocol + "//" + window.location.hostname, '').replace(':8888', '');
				$(this).attr('action', action);
				
			}
			
		});
		
	
	}
	
	
/* FUNCTION: ALERT
----------------------------------------------------------------------------- */


	function alerts() {
	
		$('div.alert').each(function() {
		
			$(this).prependTo('body').slideDown('fast');
		
		});
		
		$('div.alert').click(function() {
		
			$(this).slideUp('fast');
		
		});
			
	}
	
	
/* FUNCTION: PLACEHOLDER TEXT
----------------------------------------------------------------------------- */

	
	function placeholder() {
	
		if (!navigator.userAgent.match('WebKit')) {
	
			$('input[type=text], input[type=password]').each(function() {
		
				try {
						
					if (!$(this).parent().hasClass('error') && $(this).attr('placeholder')) {
										
						var placeholder = $(this).attr('placeholder');
						var size = $(this).attr('size');
						
						if ($(this).attr('type') == "password") {
						
							$(this).hide();
							$('<input type="text" class="placeholder_text" value="' + placeholder + '" size="' + size + '" />').insertAfter($(this));
							var $placeholder = $(this).next('input');
							
							$placeholder.focus(function() {
							
								$(this).hide();
								$(this).prev('input').show().focus();
							
							});

							$(this).blur(function() {
							
								if (!$(this).val()) {
								
									$(this).hide();
									$(this).next('input').show();
								
								}
							
							});
							
						} else {
						
							if (!$(this).val()) $(this).val(placeholder).addClass('placeholder_text');
										
							$(this).focus(function() {
							
								if ($(this).val() == placeholder) $(this).val('').removeClass('placeholder_text');
							
							});
							
							$(this).blur(function() {
							
								if (!$(this).val()) $(this).val(placeholder).addClass('placeholder_text');
							
							});
						
						}
					
					}
					
				} catch(error) { }
			
			});
		
		}
		
		$('form').submit(function() {
		
			$('input.placeholder_text').val('');
		
		});
		
	}
	
	
/* FUNCTION: BROWSER DETECTOR
----------------------------------------------------------------------------- */


	function browserDetect() {
	
		if ($.browser.mozilla) var browser = "mozilla";
		if ($.browser.webkit) var browser = "webkit";
		if ($.browser.msie) var browser = "msie";
		
		$('body').addClass(browser);
	
	}
	
	
/* FUNCTION: SHOW PASSWORD
----------------------------------------------------------------------------- */


	function password() {


/* Validation */


		$('label.password input').keyup(passwordValidate);
		$('label.password input').change(passwordValidate);
		$('label.password input').blur(function() {
		
			if ($(this).siblings('strong.weak').length > 0) {
			
				$(this).closest('label').addClass('error');
				
			} else {
			
				$(this).closest('label').removeClass('error');
			
			}
		
		});

		
/* Toggle Visibility */

	
		$('label.password + br + fieldset input[type=checkbox]').click(function() {
		
			if ($(this).attr('checked')) {
			
				var password = $('label.password input[type=password]').val();
				$('label.password input[type=password]').attr('disabled','disabled').hide();
				$('label.password input[type=text]').removeAttr('disabled').val(password).show();

			} else {
			
				var password = $('label.password input[type=text]').val();
				$('label.password input[type=text]').attr('disabled','disabled').hide();
				$('label.password input[type=password]').removeAttr('disabled').val(password).show();
			
			}
		
		});
	
	}
	
	function passwordValidate() {
	
		$(this).parent().removeClass('error');
		$(this).siblings('strong').remove();
		var password = $(this).val();
		
		if (password.length < 6) {
		
			$(this).parent().addClass('error');
			$(this).parent().append('<strong class="weak">Too short</strong>');

		} else if (!password.match(/\d+/)) {

			$(this).parent().addClass('error');
			$(this).parent().append('<strong class="weak">Must contain 1 number</strong>');

		} else if (password.match(/password/ig)) {
		
			$(this).parent().addClass('error');
			$(this).parent().append('<strong class="weak">Too Weak</strong>');

		} else if (password.match(/[A-Z]+/g) && password.match(/\d+/)) {

			$(this).parent().append('<strong class="strong">Strong</strong>');
		
		} else {
		
			$(this).parent().append('<strong class="okay">Okay</strong>');

		}
	
	}


/* FUNCTION: EMAIL
----------------------------------------------------------------------------- */

	
	function email() {
	
		$('a[rel="mail"]').each(function() {
		
			var email = $(this).attr('href').split('#');
			var email = email[0] + "@" + email[1] + "." + email[2];
			
			if ($(this).attr('href') == $(this).text()) {
			
				$(this).text(email);
			
			}
			
			$(this).attr('href', 'mailto:' + email);
					
		});
	
	}

