/*
*	All  JavaScript should use jQuery or the $scout namespace
*	http://jquery.com/
*/

/****************************************************************************
*	$SCOUT NAMESPACE
****************************************************************************/

var $scout = { };

/****************************************************************************
*	CONSTANTS
****************************************************************************/

$scout.ACTIVE_CLASS		= "active",
$scout.HOVER_CLASS 		= "hover",
$scout.BACKGROUND_COLOR = "#ffffff",
$scout.HIGHLIGHT_COLOR 	= "#fdfedc"

/****************************************************************************
*	SECUREURL
****************************************************************************/

$scout.secureURL = function(env, siteId, path) {
	return "https://secure." + env + "/a.z?s=" + siteId + "&amp;" + path;
};

/****************************************************************************
*	USER HEADER MENU
****************************************************************************/

$scout.userHeaderMenu = function(env, siteId) {
	var menu = "";
	
	if ($.cookie("tisession")) {
		var email = $.cookie("email");
		
		if (email.length >= 20) {
			email = email.substr(0, 16) + "...";
		}
		
		menu += '<ul>';
		menu += '	<li>' + email + '</li>';
		menu += '	<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=9&amp;redirecturl=" + escape(document.location.href)) + '">Sign Out</a></li>';
		menu += '	<li>';
		menu += '		<a class="parent" href="' + $scout.secureURL(env, siteId, "p=5&amp;c=7") + '">My Profile</a>';
		menu += '		<ul>';
		menu += '			<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=7") + '">My Profile Home</a></li>';
		menu += '			<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=4") + '">Member Profile</a></li>';
		menu += '			<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=13") + '">Subscriptions</a></li>';
		menu += '			<li><a href="http://my.' + env + '/hotlist.aspx?s=' + siteId + '&amp;p=16">Hotlist Preferences</a></li>';
		menu += '			<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=18") + '">Contact Us</a></li>';
		menu += '		</ul>';
		menu += '	</li>';
		menu += '</ul>';
	}
	else {
		menu += '<ul>';
		menu += '	<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=1&amp;redirecturl=" + escape(document.location.href)) + '">Sign In</a></li>';
		menu += '	<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=3&amp;redirecturl=" + escape(document.location.href)) + '"><img src="http://media.scout.com/media/images/scout/sign-up.gif" alt="Sign Up" /></a></li>';
		menu += '</ul>';
	}
	
	return menu;
};

/****************************************************************************
*	USER FOOTER MENU
****************************************************************************/

$scout.userFooterMenu = function(env, siteId) {
	var menu = "";
	
	if ($.cookie("tisession")) {
		menu += '<ul>';
		menu += '	<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=7") + '">My Profile Home</a></li>';
		menu += '	<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=4") + '">Member Profile</a></li>';
		menu += '	<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=13") + '">Subscriptions</a></li>';
		menu += '	<li><a href="http://my.' + env + '/hotlist.aspx?s=' + siteId + '&amp;p=16">Hotlist Preferences</a></li>';
		menu += '	<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=18") + '">Contact Us</a></li>';
		menu += '</ul>';
	}
	else {
		menu += '<ul>';
		menu += '	<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=1&amp;redirecturl=" + escape(document.location.href)) + '">Sign In</a></li>';
		menu += '	<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=3&amp;redirecturl=" + escape(document.location.href)) + '">Sign Up</a></li>';
		menu += '	<li><a href="' + $scout.secureURL(env, siteId, "p=5&amp;c=18") + '">Contact Us</a></li>';
		menu += '</ul>';
	}
	
	return menu;
};

/****************************************************************************
*	IE6 HEADER HOVER STATES
****************************************************************************/

if ($.browser.msie && $.browser.version == "6.0") {
	$(document).ready(function() {
		$("#header ul > li").each(function() {
			$(this).hover(
				function() {
					$(this).addClass($scout.HOVER_CLASS);
				},
				function() {
					$(this).removeClass($scout.HOVER_CLASS);
				}
			);
		});
	});
}

/****************************************************************************
*	HEADER MENU PARENT LINK REMOVAL
****************************************************************************/

$(document).ready(function() {
	$("#header #header-main-menu ul li a.parent").each(function() {
		$(this).click(function() { return false; });
	});

	$("#header #header-top-menu ul li a.parent").each(function() {
		$(this).click(function() { return false; });
	});
});

/****************************************************************************
*	TABLE HIGHLIGHT
****************************************************************************/

$(document).ready(function() {
	$("table.row-highlight tbody tr").each(function() {
		$(this).hover(
			function() {
				$(this).animate({ backgroundColor: $scout.HIGHLIGHT_COLOR }, "fast");
			},
			function() {
				$(this).animate({ backgroundColor: $scout.BACKGROUND_COLOR }, "fast");
			}
		);
	});
});

/****************************************************************************
*	TAB CREATION
****************************************************************************/

$(document).ready(function() {
	$(".tabs").each(function() {
		$("a", this).tabs();
	});
});

/****************************************************************************
*	TABS PLUGIN
****************************************************************************/

/**
*  Creates tabs based on a group of anchors. Target panes by ID within the anchor by specifying their id directly.
* @example <div id="tabs"><ul><li><a href="#pane1">Pane1</a></li></ul></div>
* @desc HTML example of creating tabs.
* @example $("#tabs ul li a").tabs();
* @desc jQuery example of marking anchors for tabing.
*/

/**
* Add tabs to any group of "A" elements.
*/
jQuery.fn.tabs = function() {
	if (!this.length) return;
	
	var tabs = [];
	var active = null;
	
	// Gather tabs.
	this.each(function() {
		if (getPane(this)) {
			tabs.push(this);
			
			// Determine active tab.
			if ($(this).hasClass("active")) {
				active = this;
			}
		}
	});
	
	if (!tabs.length) return;
	
	// Gets the tabs pane.
	function getPane(tab) {
		if (tab.href) {
			var pane = tab.href.match(/^.*(#.*)$/);
			
			if (pane && pane[1]) {
				return $(pane[1]).get(0);
			}
		}
	}
	
	// Make sure all panes but active are hidden.
	$(tabs).each(function() {
		$(this).removeClass($scout.ACTIVE_CLASS);
		$(getPane(this)).hide();
	});
	
	// Set active tab.
	active = active || tabs[0];
	$(active).addClass($scout.ACTIVE_CLASS);
	$(active).show();
	$(getPane(active)).show();
	
	// Set events.
	$(tabs).each(function() {
		$(this).click(function(e) {
			var target = this;
			
			if ($(target).hasClass($scout.ACTIVE_CLASS)) {
				return false;	// Already active.
			}
			
			$(active).removeClass($scout.ACTIVE_CLASS);
			$(getPane(active)).hide();
			
			$(target).addClass($scout.ACTIVE_CLASS);
			$(getPane(target)).show();
			
			active = target;
			
			return false;
		});
	});
}

/****************************************************************************
*	COOKIE PLUGIN
****************************************************************************/

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/****************************************************************************
*	COLOR ANIMATIONS PLUGIN
****************************************************************************/

/**
 * jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */

(function(jQuery){

	// We override the animation for all of these color styles
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	// Color Conversion functions from highlightFade
	// By Blair Mitchelmore
	// http://jquery.offput.ca/highlightFade/

	// Parse strings looking for color tuples [255,255,255]
	function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
			return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Otherwise, we're most likely dealing with a named color
		return colors[jQuery.trim(color).toLowerCase()];
	}
	
	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);

			// Keep going until we find an element that has color, or we hit the body
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	
	// Some named colors to work with
	// From Interface by Stefan Petre
	// http://interface.eyecon.ro/

	var colors = {
		aqua:[0,255,255],
		azure:[240,255,255],
		beige:[245,245,220],
		black:[0,0,0],
		blue:[0,0,255],
		brown:[165,42,42],
		cyan:[0,255,255],
		darkblue:[0,0,139],
		darkcyan:[0,139,139],
		darkgrey:[169,169,169],
		darkgreen:[0,100,0],
		darkkhaki:[189,183,107],
		darkmagenta:[139,0,139],
		darkolivegreen:[85,107,47],
		darkorange:[255,140,0],
		darkorchid:[153,50,204],
		darkred:[139,0,0],
		darksalmon:[233,150,122],
		darkviolet:[148,0,211],
		fuchsia:[255,0,255],
		gold:[255,215,0],
		green:[0,128,0],
		indigo:[75,0,130],
		khaki:[240,230,140],
		lightblue:[173,216,230],
		lightcyan:[224,255,255],
		lightgreen:[144,238,144],
		lightgrey:[211,211,211],
		lightpink:[255,182,193],
		lightyellow:[255,255,224],
		lime:[0,255,0],
		magenta:[255,0,255],
		maroon:[128,0,0],
		navy:[0,0,128],
		olive:[128,128,0],
		orange:[255,165,0],
		pink:[255,192,203],
		purple:[128,0,128],
		violet:[128,0,128],
		red:[255,0,0],
		silver:[192,192,192],
		white:[255,255,255],
		yellow:[255,255,0]
	};
	
})(jQuery);
