$(document).ready(function(){
	Forms.init();
	if($('#nav').length > 0) {
		Navigation.init();
	}
});


//Form clearing
function clickClear(field, defaulttext) {
	if (field.value == defaulttext) {
		field.value = "";
	}
}
function clickRecall(field, defaulttext) {
	if (field.value == "") {
		field.value = defaulttext;
	}
}

var Forms = {
	init: function() {
		Forms.formatSelectors();
		Forms.formatCheckboxes();
		if($('#UserMobileNumber').length > 0) {
			Forms.formatPhoneNumber($('#UserMobileNumber').get(0));
			$('#UserMobileNumber').keyup(Forms.onMobileNumberKeyUp);
		}
	},
	formatSelectors: function() {
		if( ! jQuery.support.opacity ) return; 
		
		$('.selector select').each(function(){	
			if(this.id != null) {
				$(this).addClass('replaced');
				$(this).after('<span id="'+this.id+'-label"></span>');
				$('#'+this.id+'-label').text($(this.options[this.selectedIndex]).text());
				$(this).change(function(){ $('#'+this.id+'-label').text($(this.options[this.selectedIndex]).text()); });
			}
		});
	},
	formatCheckboxes: function() {
		$('.checkbox').each(function(){
			var hidden = $(this).find('input[type=hidden]');
			var checkbox = $(this).find('input[type=checkbox]');
			var checkedClass = '';
			hidden.get(0).value = 0;
			if(checkbox.get(0).checked) {
				hidden.get(0).value = 1;
				checkedClass = 'checked';
			}
			
			checkbox.replaceWith('<span class="cbox '+checkedClass+'" id="'+checkbox.get(0).id+'"></span>');
			$(this).find('span.cbox').click(function(){ 
				if($(this).hasClass('checked')) {
					$(this).removeClass('checked');
					$(this).siblings('input[type=hidden]').get(0).value = 0;
				} else {
					$(this).addClass('checked');
					$(this).siblings('input[type=hidden]').get(0).value = 1;
				}
			});
		});
	},
	formatPhoneNumber: function(el) {
		if( el.value.match(/^[0-9]{10}$/) ) {
			el.value = '('+el.value.substring(0,3)+')'+el.value.substring(3,6)+'-'+el.value.substring(6,10);
		}
	},
	onMobileNumberKeyUp: function(e) {
		if(this.value.length == 3 && this.value.indexOf('(') != 0) {
			this.value = '('+this.value+')';
		}
		if(this.value.length == 8 && e.which != 8) {
			this.value = this.value+'-';
		}
	}
};

var Navigation = {
	activeTip: { id: null, el: null },
	windowPadding: { width: 10, height: 20 },
	init: function() {
		Navigation.addTip('.hit_point');
		Navigation.preloadImages();
	},
	preloadImages: function() {
		var images = ['/img/tooltip/tooltip_bottom_left', 
									'/img/tooltip/tooltip_top_right',
									'/img/tooltip/tooltip_bottom_right_bg',
									'/img/tooltip/tooltip_top_left_bg'
								];
		var ext = '.png';
		if(jQuery.browser['msie'] && jQuery.browser.version == 6.0) {
			ext = '.gif';
		}
		for(var i = 0; i < images.length; i++) {
			var img = new Image();
			img.src = images[i]+ext;
		}
	},
	addTip: function(selector) {
		$(selector).mouseover(Navigation.showTip);
	},
	showTip: function(e) {
		Navigation.setActiveTip(this.id);
		Navigation.updateTip(e);
		Navigation.activeTip.el.css({ display: 'block', 'z-index': 1000 });
		$(document).mousemove( Navigation.updateTip );
		$(this).mouseout( Navigation.hideTip );
	},
	updateTip: function(e) {
		var leftPos = e.pageX + 10;
		var topPos = e.pageY - ( Navigation.activeTip.el.outerHeight(true) + 10 );
		var windowWidth = $(window).width();
		if(jQuery.browser['safari']) {
			windowWidth = windowWidth - 15;
		}
		
		if( ( leftPos + Navigation.activeTip.el.outerWidth(true) ) > ( $(document).scrollLeft() + windowWidth ) ) {
			leftPos = e.pageX - ( Navigation.activeTip.el.outerWidth(true) + 10 );
		}
		
		if( topPos < ( $(document).scrollTop() + 5 ) ) {
			topPos = ( $(document).scrollTop() + 5 )
		}
		
		Navigation.activeTip.el.css({ left: leftPos, top: topPos });
	},
	hideTip: function() {
		if(Navigation.activeTip.el != null) {
			Navigation.activeTip.el.css({display: 'none'});
			$(Navigation.activeTip).unbind( 'mouseout', Navigation.hideTip );
			$(document).unbind('mousemove', Navigation.updateTip );
			Navigation.setActiveTip(null);
		}
	},
	setActiveTip: function(elId) {
		if( elId != null ) {
			Navigation.activeTip.id = elId+'-tip';
			Navigation.activeTip.el = $('#'+Navigation.activeTip.id);
		} else {
			Navigation.activeTip.id = null;
			Navigation.activeTip.el = null;
		}
	}
}