// JSLint Settings
/*jslint
	passfail: false,
	white: true,
	onevar: true,
	browser: true,
	widget: false,
	sidebar: false,
	rhino: false,
	safe: false,
	adsafe: false,
	
	debug: false,
	evil: false,
	laxbreak: false,
	forin: false,
	sub: false,
	css: false,
	cap: false,
	on: false,
	fragment: false,
	
	undef: true,
	nomen: true,
	eqeqeq: true,
	plusplus: true,
	bitwise: true,
	regexp: true,
	strict: true,
	newcap: true,
	immed: true
*/
/*global
	window,
	$,
	jQuery
*/
'use strict';
/*
Namespace: CLEAR

About: Version
	1.0

Description:
	A collection of shared functions to be used in multiple pages

Requires:
	jQuery 1.3 <http://jquery.com>

*/

// Global Namespace
var CLEAR = window.CLEAR || {};

// Project Subnamespace
CLEAR.clearwire = CLEAR.clearwire || {};

// When the DOM is ready.
(function () {
	
	// Storing a variable to reference
	var $space = CLEAR,
		$self = CLEAR.clearwire;
	
	// Shared variables
	$self.vars = $.extend($space.vars, {
		JS_ENABLED_CLASS : "js-enabled",
		NO_BOX_SHADOW_CLASS : "no-box-shadow-support",
		EASING : "easeInOutExpo",
		PARENT : "#header",
		TEXT : ":text",
		SUBMIT : ":submit",
		NOLABEL : "nolabel",
		SEARCH_STRING : "Search"
	});
	
	// A legacy namespace to be used if Clear has any legacy dependencies
	$self.legacy = $.extend($self.legacy, {});
	
	// Shared utilities
	$self.utils = $.extend($space.utils, {});
	
	// Global definitions
	$self.global = function () {
		
		// Unqueue inline functions
		CLEAR.unqueue();
		
		// Override default easing
		jQuery.easing.def = $self.vars.EASING;
		
		if ($.browser.msie) {
			try {
				// Enable background image cache
			    document.execCommand("BackgroundImageCache", false, true);
			} catch (ex) {}
		}
	};
	
	// Header Namespace
	// Adds the animation found on the header section
	$self.header = function () {
		
		// Local variables
		var parent = $($self.vars.PARENT),
		    text = parent.find($self.vars.TEXT);
		
		// Prevent search from adding search label if text exists
		text.bind("blur", function () {
			var el = $(this);
			if (el.val().length > 0) {
				el.parent().addClass($self.vars.NOLABEL);
			} else {
				el.parent().removeClass($self.vars.NOLABEL);
			}
		});
		
		text.bind("focus", function () {
			$(this).parent().addClass($self.vars.NOLABEL);
		});
		
		text.each(function () {
			var el = $(this);
			if (el.val() === $self.vars.SEARCH_STRING) {
				el.val("");
			} else {
				el.parent().addClass($self.vars.NOLABEL);
			}
		});
		
		text.trigger("blur");
		
		// Remove accessible submit button
		parent.find($self.vars.SUBMIT).hide();
		
	};
	
	$self.formatColumns = function () {
		var columns = $("#primary-content, #secondary-content, #primary-content .row-b");
		
		CLEAR.utils.applyColumnCaps(columns);
		
		$('#primary-content .row').each(function (i) {
			var top = 0;
			$(this).find('.see-all, .details').each(function (i) {
				top = Math.max(top, $(this).position().top);
			}).each(function (i) {
				var offset = Math.floor(top - $(this).position().top);
				if (offset !== 0) {
					$(this).before('<div class="shim" />').prev().height(offset);
				}
			});
		});
	};
	
	if ($.browser.msie && parseInt($.browser.version, 10) < 8) {	
		$self.arrows = function () {
			$('.arrow-append').append(' ▸');
			$('.arrow-prepend').prepend('▸ ');
		};
	}
	
	// Initialize
	$space.utils.init($self);
	
}.call(CLEAR.clearwire));