String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

var url = new Array();
function getQS(forceString) {
	if(forceString)
		var query = forceString.substring(1);
	else
		var query = window.location.search.substring(1);
	var params = query.split('&');
	for (var i=0; i<params.length; i++) {
		var pos = params[i].indexOf('=');
		if (pos > 0) {
			var key = params[i].substring(0,pos);
			var val = params[i].substring(pos+1);
			url[key] = val;
		}
	}
}

Position.getPageSize = function() {
	var xScroll, yScroll, scrollOffsetY;

	if (window.innerHeight && window.scrollMaxY) {  
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
        
	if (self.pageYOffset) {
		scrollOffsetY = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		scrollOffsetY = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		scrollOffsetY = document.body.scrollTop;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	} 

	// for small pages with total height less then height of the viewport
	pageHeight = Math.max(windowHeight, yScroll);

	// for small pages with total width less then width of the viewport
	pageWidth = Math.max(windowWidth, xScroll);

	return {page: {width: pageWidth, height: pageHeight}, 
		window: {width: windowWidth, height: windowHeight},
		scroll: {top: scrollOffsetY}};
}

// Window functions
function addOverlay() {
	var p = Position.getPageSize();
	/*offset = $('content_container').offsetHeight + 80 + 60;*/
	offset = 1;
	overlayHeight = Math.max(p.page.height,offset);
	
	var overlay = Builder.node('div', {id: 'overlay', className: 'win_overlay', style: 'width:'+p.window.width+'px;' + 'height:'+overlayHeight+'px;'});
	(document.body ? document.body : document.documentElement).appendChild(overlay);
	new Effect.Opacity('overlay', {from: 0, to: 0.7, duration: 0.5});
}

function removeOverlay() {
	$('overlay').remove();
}

// Form display functions
function showFT(field) {
	var fromError = arguments[1] || 0;
	if($(field + '_ft').getStyle('opacity') == 0)
		new Effect.Opacity($(field + '_ft'), {from: 0, to: 0.9, duration: 0.5});

	if(!fromError) {
		$(field + '_ft').siblings().each(function(e) {
			if(e.hasClassName('ft_container'))
				e.setStyle({opacity: 0});
		});
	}
}

function hideFT(field) {
	new Effect.Opacity($(field + '_ft'), {from: 0.9, to: 0, duration: 0});
}

// Set height of content frame if it's smaller than the viewport
function setContentHeight() {
	var p = Position.getPageSize();
	var currentContentHeight = $('content_container').offsetHeight;

	if(currentContentHeight < p.window.height) {
		var newContentHeight = p.page.height - currentContentHeight - 170;
		$('content_container').setStyle({height: newContentHeight+'px'});
	}
}

// Set nav top height
function setNavHeight() {
	var p = Position.getPageSize();
	if($('content_container').offsetHeight + 170 < p.window.height) {
		var navHeight = p.window.height - 350 - 130 - 10;
	}
	else
		var navHeight = $('content_container').offsetHeight - 320;
	$('nav_top').setStyle({height: navHeight+'px'});
}

function setFooterPosition() {
	var p = Position.getPageSize();
	if($('content_container').offsetHeight + 170 < p.window.height) {
		var footerPostion = p.window.height - 170;
	}
	else
		var footerPostion = $('content_container').offsetHeight;	
	$('footer_container').setStyle({top: footerPostion+'px'});
}

function setLootStreamHeight() {
	var p = Position.getPageSize();
	if($('content_container').offsetHeight + 170 < p.window.height) {
		var lsHeight = p.window.height - 325 - 176 - 35;
	}
	else
		var lsHeight = $('content_container').offsetHeight - 325 - 10 - 35;
	$('lootstream').setStyle({height: lsHeight+'px'});
}

function setOpacity(object, value) {
	object.style.opacity = value/10;
	object.style.filter = 'alpha(opacity=' + value*10 + ')';
}

// Set redirect cookie
function setRedirectCookie() {
	createCookie('aaron_redir', window.location, 1);
}

function readRedirectCookie(del) {
	redir = readCookie('aaron_redir');
	if(del)
		eraseCookie('aaron_redir');
	if(redir == null)
		redir = 'index.php';
	return redir;
}

// Logout function
function logout(user_id) {
	var date = new Date();
	date.setTime(date.getTime()-1);
	var expires = "; expires="+date.toGMTString();
	
	if(location.hostname == 'localhost')
		document.cookie = "aaron_user="+user_id+expires+"; path=/";
	else
		document.cookie = "aaron_user="+userID+expires+"; domain=.revealyourwheels.com; path=/";
	window.location.reload();
}

// Form function for changing container and input style on focus
function focusFormField(formField) {
	$(formField).up(1).setStyle({
		borderStyle: 'solid'
	});
	
	$(formField).addClassName('ff_input_focus');
	$(formField).up().next('.ff_help_container').innerHTML = $(formField).title;
}

// Form function for in progress validation and style cleanup on blur
function blurFormField(formField, skipValidate) {
	if(!skipValidate) skipValidate = 0;
	
	if(!skipValidate && $F(formField) != 0 && $F(formField) != "") {
		formName = formField.up('form').name;
		
		new Ajax.Request('php/ajax_'+formName+'.php',
		{
			method:'post',
			parameters: {inProgressValidateField: $(formField).name, inProgressValidateValue: $F(formField)},
			onSuccess: function(transport){
				var response = transport.responseText;
				// Error
				if(response != '') {
					$(formField).up().setStyle({
						backgroundImage: 'URL(images/form_validation_error.gif)',
						backgroundPosition: '95% 50%',
						backgroundRepeat: 'no-repeat'
					});
					$('ff_'+$(formField).name+'_container').innerHTML = response;
				}
				// No error
				else {
					$(formField).up().setStyle({
						backgroundImage: 'URL(images/form_validation_check.gif)',
						backgroundPosition: '95% 50%',
						backgroundRepeat: 'no-repeat'
					});
					$('ff_'+$(formField).name+'_container').innerHTML = '';
				}
			},
			onFailure: function(transport){
				// Do Nothing
			}
		});
	}
	
	$(formField).up(1).setStyle({
		border: '1px dashed #c9c9c9'
	});
	
	
	$(formField).up().next('.ff_help_container').innerHTML = '';
	$(formField).removeClassName('ff_input_focus');
}

// Dynamic tooltip generation
function createTT(tt_id, tt_text, tt_dir, bound_id) {
	// Build and insert TT node into DOM
	var tt = Builder.node('div', {id: tt_id, className: 'tt_container'},
		[Builder.node('div', {className: 'tt_top'}), 
		Builder.node('div', {id: tt_id+'_inner', className: 'tt_mid'}),
		Builder.node('div', {className: 'tt_bottom'})]
	);
	
	document.body.appendChild(tt);
	$(tt_id).setStyle({opacity: 0});
	$(tt_id + '_inner').innerHTML = tt_text;
	
	// Position TT based on the position of its binding object
	var bound_position = Position.cumulativeOffset($(bound_id));
	var bound_dimentions = $(bound_id).getDimensions();
	var tt_dimensions = $(tt_id).getDimensions();
		
	if(tt_dir == 'l')
		var tt_pos_x = bound_position[0] - tt_dimensions.width - 1;
	else
		var tt_pos_x = bound_position[0] + bound_dimentions.width + 1;
	var tt_pos_y = bound_position[1] - tt_dimensions.height + (bound_dimentions.height / 2);
	$(tt_id).setStyle({top: tt_pos_y+'px', left: tt_pos_x+'px', display: 'none'});
	
	// Bind mouse events to the binding object
	Event.observe(bound_id, 'mouseover', function() {
		if($(tt_id).getStyle('opacity') == 0) {
			$(tt_id).setStyle({display: 'block'});
			new Effect.Opacity(tt_id, {from: 0.0, to: 0.99, duration: 0.2, queue: 'front'});	
		}
	});
	Event.observe(bound_id, 'mouseout', function() {
		new Effect.Opacity(tt_id, {from: $(tt_id).getStyle('opacity'), to: 0.0, duration: 0.1, queue: 'end',
		afterFinish: function() { $(tt_id).setStyle({display: 'none'}); }
		});	
	});
}

function updateTT(tt_id, tt_text, tt_dir, bound_id) {
	var tt_children = $(tt_id).immediateDescendants();
	tt_children[1].innerHTML = tt_text;
	
	// Position TT based on the position of its binding object
	var bound_position = Position.cumulativeOffset($(bound_id));
	var bound_dimentions = $(bound_id).getDimensions();
	var tt_dimensions = $(tt_id).getDimensions();
		
	if(tt_dir == 'l')
		var tt_pos_x = bound_position[0] - tt_dimensions.width - 2;
	else
		var tt_pos_x = bound_position[0] + bound_dimentions.width + 2;
	var tt_pos_y = bound_position[1] - tt_dimensions.height + (bound_dimentions.height / 2);
	$(tt_id).setStyle({top: tt_pos_y+'px', left: tt_pos_x+'px'});
}