
$(document).ready(function() {

/* {{{ 404 page */
	$("#error .text").click(function(){
		$(this).css('color', '#000');
		$(this).val("");
	});	
/* }}} */	

/* {{{ dropdowns */
	$(".navList li").hover(
		function () {
			$(this).find('.navDropdown').css('display','block');
		},
		function () {
			$(this).find('.navDropdown').css('display','none');
		}
	);

/* }}} */

/* {{{ add n bg divs - jq plugin - dtw 20090416 */
	$.fn.addBg = function(path,innerStyle) {	
		var loc = {
			'_l' : 'left repeat-y',
			'_r' : 'right repeat-y',
			'_t' : 'top repeat-x',
			'_b' : 'bottom repeat-x',
			'_tl' : 'left top no-repeat',
			'_tr' : 'right top no-repeat',
			'_br' : 'right bottom no-repeat',
			'_bl' : 'left bottom no-repeat'
		};
		
		var img = path.split('.');
	
		$(this).wrap('<div></div>').parent().addClass($(this).attr('class'))
			.css({'padding':'0px','border':'none'});
		for (i in loc) { 
			$(this).removeClass().css({'backgroundColor':'transparent'}).css(innerStyle)
			.wrap('<div></div>').parent()
			.css({'background':'url(' + img[0] + i + '.' + img[1] + ') ' + loc[i]})
		}
	}

	$('.specialBoxOut').addBg(BASE_URL_PATH + 'images/global/box_out.png',{'padding':'16px'});
	$('.specialBoxIn').addBg(BASE_URL_PATH + 'images/global/box_in.png',{'padding':'16px'});
	$('.screenshot').addBg(BASE_URL_PATH + 'images/global/screenshot_border.png',{'padding':'6px 6px 3px 6px'});
	$('.quoteBubble').addBg(BASE_URL_PATH + 'images/global/quote.png',{'padding':'14px 20px 14px 40px'});
	
/* }}} */

/* {{{ hide omniture tracking pixel */
	$('img[name=s_i_rackmailtrust]').hide();
/* }}} */

/* {{{ toggle logo */
	$(".logo img").hover(function(){
		this.src = this.src.replace("_off","_on");
	},function(){
		this.src = this.src.replace("_on","_off");
	});
/* }}} */

/* {{{ site search */
	var search = 'Search';
	
	$('.searchField').focus(function(){
		if ($(this).val() == search) {
			$(this).val('').css({'color':'#000'});
		}
	}).blur(function(){
		if ($(this).val() == '' || $(this).val() == search) {
			$(this).val(search).css({'color':'#999'});
		}
	}).blur();
	
	$('form.searchForm').submit(function(){
		var q = $(this).find('.searchField').val();
		if (q == search || q == '') {
			return false;
		} else if (/[\w]+\@[\w]+\.([\w]{2,4})+/.test(q)) {
			var r = confirm('Oops! You entered an email address in the search field. \n\nWould you like to proceed to the customer login at https://apps.rackspace.com?');
			if (r == true) {
				window.location.assign('https://apps.rackspace.com');
				return false;
			} else {
				return q;
			}
		}
	});

/* }}} */

/* {{{ tooltip mouseover */

	var fade_time = 300;
	var hover_time = 300;

	$('.detailLink').mouseover(function(){
		// set location variables to check later on mouseleave
		detailpopup_hover = false;
		detaillink_hover = true;
	
		// find associated popup by id with suffix converted from Link to Popup
		popupId = $(this).attr('id').replace('Link','Popup');
		var $popup = $('#'+popupId)
			.prepend($(this).filter('.detailCaret').length < 1 ? '<div class="detailCaret"></div>' : '');

		// get position and dimensions of link and popup
		var position = $(this).position();
		var x = position.left;
		var y = position.top;
		var h_link = $(this).outerHeight();
		var w_link = $(this).outerWidth();
		var w_popup = $popup.outerWidth();

		// set relative position of popup
		if ($(this).hasClass('popupBelow')) {
			x = x + (w_link / 2) - (w_popup / 2);
			y = y + h_link + 8;
			$popup.addClass('popupBelow');
			
			//position caret halfway across popup
			$('.detailCaret').css({'left':w_popup/2 - 8});
		} else {
			// popupup left (default)
			x = x + w_link + 10;
			y = y - (h_link / 2) - 6;
		}
	
		// display popup at same position after hover
		setTimeout(function(){
			if (detaillink_hover) {
				// hide other popups
				$('.detailPopup').fadeOut(fade_time);
				// show intended popup
				$popup.css({'left':x,'top':y}).fadeIn(fade_time);
			}
		},hover_time);

		// fallback click anywhere incase it hangs
		$('body').click(function(){
			$popup.fadeOut(fade_time);
		});
	}).mouseleave(function(){
		// set location variable for reference in other event handlers
		detaillink_hover = false;

		// fade out popup if the mouse is not on the popup or link
		setTimeout(function(){
			if (!detailpopup_hover && !detaillink_hover) {
				$('.detailPopup').fadeOut(fade_time);
			}
		},hover_time);
	});

	$('.detailPopup').mouseover(function(){
		// set location variables to check later on mouseleave
		detailpopup_hover = true;
		detaillink_hover = false;
	}).mouseleave(function(){
		// set location variable for reference in other event handlers
		detailpopup_hover = false;

		// fade out popup if the mouse is not on the popup or link
		setTimeout(function(){
			if (!detaillink_hover && !detailpopup_hover) {
				$('.detailPopup').fadeOut(fade_time);
			}
		},hover_time);
	});

/* }}} */

/* {{{ Login Cookie & Omniture Filter Prop */

	$('#login').click(function() {
		// Set Customer Cookie
		var expires = new Date((new Date()).getTime() + 2592000000).toGMTString();
		document.cookie = 'rsea_cust=1;expires=' + expires + ';path=/;domain=' + CFG_COOKIE_PATH;

		// Set Omniture var to filter out login hits
		s = s_gi(CFG_OMNITURE_SUITE);
		s.prop8 = 'Home: Header: Customer Login';
		s.tl(this,'e','Home: Header: Client Login');
	});

/* }}} */

/* {{{ Rackuid Cookie */
	
	// generic check for a variable in cookie
	function getCookieValue(name) {
		var start = document.cookie.indexOf(name);
		var end = 0;
		if (start >= 0) {
			value = document.cookie.substr(start + name.length+1);
			end = value.indexOf(';') > 0 ? value.indexOf(';') : value.length;
			return value.substr(0,end);
		} else {
			return false;
		}
	}
	
	// cookie var of interest
	var id_name = 'lpUASrackuid';
	var lpUASrackuid = getCookieValue(id_name);
	
	if (!lpUASrackuid) {	
		// if not in cookie, use session cookie already set by this server
		lpUASrackuid = getCookieValue('RACKSPACE_EA_SIGNUP');

		// if session cookie is not found, use fresh one or use a fallback
		if (!lpUASrackuid) {
			// if session cookie not set, use new server-generated id
			lpUASrackuid = (fresh_lpUASrackuid !== undefined) ? fresh_lpUASrackuid : 'fallbackID-1234';
		}
		lpUASrackuid = 'RSEA' + lpUASrackuid;
		var expires = new Date((new Date()).getTime() + 15552000000).toGMTString();
		document.cookie = id_name + '=' + lpUASrackuid + ';expires=' + expires;
	}

	// Pass rackuid to liveperson
	if (typeof(lpAddVars) != 'undefined') {
		lpAddVars('session', 'RACKUID', lpUASrackuid);
	}

/* }}} */

});

Object.extend = function(dest, source) {
	for (var property in source) {
		dest[property] = source[property];
	}
	return dest;
}
