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


	$(document).ready(function() {
	
		browser();
		lightbox();
		actions();
		replaceSubmitButtons();
		placeholder();
		backgroundScale();
		$(window).resize(backgroundScale);
		markup();
			
	});
	
	$(window).load(function() {

		tabs();
		alerts();
		$('body').removeClass('loading');
		backgroundScale();

	});
		

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

	
	function actions() {

		
/* Back Link */


		$('a[href="#back"]').click(function() {
		
			history.go(-1);
			return false;
		
		});
		
	}
	
	
/* 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 */

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

			if ($(this).closest('form').find('label.error').length > 0) {
			
				return false;
				
			} else {
			
				$(this).parents('form').submit();
				return false;
			
			}
			
		});
		
		
/* Form Action Swap */


		$('form:not(.cart form)').submit(function() {
			
			if ($(this).attr('action').match('actions')) {
			
				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]').each(function() {
		
				try {
						
					if (!$(this).parent().hasClass('error') && $(this).attr('placeholder').length > 0) {
										
						var placeholder = $(this).attr('placeholder');
						
						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 browser() {
	
		if ($.browser.mozilla) var browser = "mozilla";
		if ($.browser.webkit) var browser = "webkit";
		if ($.browser.msie) var browser = "msie";
		$('body').addClass(browser);
	
	}
	
	
/* FUNCTION: RESIZE BACKGROUND
----------------------------------------------------------------------------- */


	function backgroundScale() {
	
		// Get Original Dimensions
		var windowWidth = parseInt($(window).width());
		var windowHeight = parseInt($(window).height());
		var imageWidth = parseInt($('#background img').width());
		var imageHeight = parseInt($('#background img').height());

		// Calculate New Dimensions
		if (imageHeight != 0 && imageWidth != 0) {
		
			var proportion = imageHeight / imageWidth;	
			var imageHeight = windowWidth * proportion;
		
			if (imageHeight < windowHeight) {
			
				var imageHeight = windowHeight;
				var imageWidth = windowHeight / proportion;
				
			} else {
			
				var imageWidth = windowWidth;
	
			}
	
			var top = (windowHeight - imageHeight) / 2;
			var left = (windowWidth - imageWidth) / 2;
			
			// Assign Dimensions
			$('#background img').css('width', imageWidth).css('height', imageHeight).css('top', top).css('left', left);
		
		}
		
	}
	
	
/* FUNCTION: MARKUP CHANGES
----------------------------------------------------------------------------- */


	function markup() {
	
	
	
	}
	
	
/* FUNCTION: TRUNCATE
----------------------------------------------------------------------------- */


	function truncate(selector, length) {
		
		$(selector).each(function() {
		
			if ($(this).text().length > length) {
			
				var text = $(this).text().slice(0, length);
				var text = text.slice(0, text.lastIndexOf(' ')) + '&hellip;';
				$(this).html(text);
			
			}
		
		});
	
	}

	

