$(document).ready(function(){
	
	// load login box for MyBIT link
	$.get('/includes/content/login.php',function(data,success){
		// only run if GET successful and user is not logged in (returns 'false')
		if(success == 'success' && data != 'false' && !window.opera) {
			// make the html
			var html = '<div class="bitLoginContainer">'+
							'<div class="bitLoginContainerInner">'+
								'<div class="bitLogin">'+
								data+
								'</div>'+
							'</div>'+
						'</div>';
			
			$('li.jsLoginTrigger').closest('li').append(html);
			
			$('.bitLoginContainer').hide();
			
			$('li.jsLoginTrigger a.jsLogin').click(function(){
				$('.bitLoginContainer').toggle();
				$(this).closest('li').toggleClass('jsLogin');
				return false;
			});
		}
	});
	
	// handle the let me know link on the homepage
	$('a.let_me_know').click(function(){
		$.get($(this).attr('href'),function(data,success){
			if(success == 'success' && data) {
				$.dialogue({text:data});
			}
		});
		return false;
	});
		
	// fix mental IE6 flickering
	var m = document.uniqueID && document.compatMode && !window.XMLHttpRequest && document.execCommand; 
	try { 
		if(!!m) {m("BackgroundImageCache", false, true);} 
	}
	catch(e) {};
	
});

/*******************
 generic extensions
*******************/

(function($){
		  
	// tool tips
	$.tooltip = function(wrapper,container) {
		container = container || document.body
		$('span.jsToolTipWrapper',container)
			.hide();
			
		$(document.body).click(function() {
			$('span.jsToolTipWrapper')
				.hide().closest(wrapper).css({zIndex:''});
		});
		$('a.jsToolTipTrigger',container).click(function(e){
			$('span.jsToolTipWrapper').hide().closest(wrapper).css({zIndex:''});
			$(this).closest(wrapper).css({zIndex:'1'}).find('span.jsToolTipWrapper').show();
			return false;
		});
	}
	
	// format numbers with decimal places and commas
	$.format = function(n,dec,pad,commas) {
		if(!isNaN(n)) {
			var rounded = Math.round(n*Math.pow(10,dec))/Math.pow(10,dec);
			var str = rounded.toString();
			if(dec>0 && pad) {
				var i = 0;
				while(i<dec) {
					if(rounded*Math.pow(10,i)%1==0) {
						if(i==0) {str+='.';}
						str += '0';
					}
					i++;
				}
			}
			if(commas) {
				var tmp = '';
				var bits = str.split('.');
				for(var i=0;i<bits[0].length;i++) {
					var char = bits[0].slice(i,i+1);
					if((bits[0].length-i)%3 === 0 && tmp.length) {
						tmp += ','+char;
					}
					else {
						tmp += char;
					}
				}
				bits[0] = tmp;
				str = bits.join('.');
			}
			return str;
		}
		return '';
	}
	
	// create an alert box dialogue
	$.dialogue = function(options) {
		
		var defaults = {
			text:'Lorem ipsum dolor',
			button:'OK',
			width:null,
			cls:''
		};
		
		var opts = $.extend(defaults, options);
		
		var html = '<iframe class="overlay"></iframe>'+
			'<div class="overlay"></div>'+
			'<div class="bitPopup '+opts.cls+'">'+
				'<div class="bitPopupWrapper00">'+
					'<div class="bitPopupWrapper01">'+
						'<div class="bitPopupWrapper02">'+
							'<p>'+opts.text+'</p>'+
							'<input type="button" value="'+opts.button+'" />'+
						'</div>'+
						'<div class="bitPopupBot"></div>'+
					'</div>'+
					'<div class="bitPopupBottom01">'+
						'<div class="bitPopupBottom02">'+
						'</div>'+
					'</div>'+
				'</div>'+
			'</div>';
		
		$(document.body).append(html);
		$popup = $('.bitPopup');
		$content = $('.bitPopup .bitPopupWrapper01');
		if(opts.width) {
			$content.css({width:opts.width+'px'});
		}
		var width = $content.width(),
			height = $popup.height(),
			pos = $content.position(),
			top = ($(window).height() - height)/2 + $(document).scrollTop();
			
		$('.bitPopup .bitPopupWrapper00').css({
			marginTop:top+'px'
		});
		$('.overlay').css({
			height:$(document).height()+'px'
		});
		
		$('.bitPopup input[type="button"]').click(function(){
			$(this).closest('.bitPopup').remove();
			$('.overlay').remove();
			return false;
		});
		
	}
	
	// restrict an input field to numbers only
	$.fn.restrict = function(options) {
		
		var defaults = {
			decimals:2
		};
		
		var opts = $.extend(defaults, options);
		
		return $(this).each(function(){
			$(this).keypress(handle_keypress);
		});
	
		function handle_keypress(e){
			// allow digits, decimal point and backspace
			var numChars = [8,46,48,49,50,51,52,53,54,55,56,57];
			if(e.which && numChars.indexOf(e.which) == -1) {
				return false;
			}
			// restrict to maximum number of decimal places
			if($(this).val().split('.').length > 1 && $(this).val().split('.')[1].length == opts.decimals && e.which != 8 && e.which) {
				return false;
			}
			// only allow one decimal point
			if($(this).val().split('.').length > 1 && e.which == 46) {
				return false;
			}
		}
		
	}
	
	// set maxlength attribute on unsupported elements
	$.fn.maxlength = function(n) {
		
		return $(this).each(function(){
			var $this = $(this);
			$this.keypress(function(e){
				if($this.val().length >= n && (e.charCode || e.keyCode) && e.which != 8 && e.which) {
					return false;
				}
			});
			$this.keyup(function(e){
				if($this.val().length > n) {
					$this.val($this.val().substr(0,n));
				}
			});
		});

	}
	
})(jQuery)


/**********************
 fix for indexOf in IE
**********************/

if(typeof Array.prototype.indexOf != 'function') {
	Array.prototype.indexOf = function(o) {
		for(var i=0;i<this.length;i++) {
			if(this[i] == o) {
				return i;
			}
		}
		return -1;
	}
}