//SET UP CUSTOM JQUERY ALERT MESSAGE 
function malert(message, field) {
	if (message == '') {
		return false;
	}
	$('#modalalert').html(message);
	$('#modalalert').dialog('open');
	if (field != '') {
		$('#modalalert').dialog('option', 'close', function() {
			$('#'+field).focus();	
		})
	}
}
function jqmConfirm(message,callback, params, parentid) {
	$('#confirmtext').html(message);
	$('#confirm')
		.dialog('open')
		.end()
	$('#confirm').find('#confirmbut')
	  .click(function(){
		confirmcheck = this.value;
		callback(params, parentid, this.value);
		$('#confirm').dialog('close');
	})
}	
//SUBMENU SWITCH
function switchMenu (dom) {
	$('li.active').removeClass('active');
	$(dom).parent().addClass('active');
	var id = $(dom).attr('id').replace('link', '');
	$('#submenu'+id).fadeIn().addClass('active');
	$('#submenu').slideDown('normal', function() {
		$('#submenu').animate({
			height: $('#submenu'+id).height()+30},
			500
		);
	});
}
//ONLOAD FUNCTIONS
$(function() {	
	$('.closesub a').live('click', function(e) {
		e.preventDefault();
		$('#submenu').slideUp();
		$('li.active').removeClass('active');
		$('.submenu').fadeOut().removeClass('active')
		$(this).parent().removeClass('active');
	})	
	$('#loginlink').live('click', function(e) {
		e.preventDefault();
		$('#loginmodal').dialog('open');
	})
	$('#passrecall').live('click', function(e) {
		e.preventDefault();
		$('#loginmodal').load('http://www.lrs.org/passrecall.php');
	})
	$('#loginsubmit').live('click', function(e){
		e.preventDefault();
		if ($('#username').val() == '' || $('#password').val() == '') {
			malert('Username & Password are Both Required');
			return false;
		}
		var str = $('#loginform').serialize();
		$.post('http://www.lrs.org/staff/process/login.php?jslogin=on', str, function(data, textStatus){
			var thisdata = String(data);
			if (thisdata == 'success') {
				location.reload()
			} else if (data == 'updatepass') {
				// It is necessary to append a random number in order to prevent IE from using the cache
				$('#secondmodal').load('http://www.lrs.org/includes/forms/update_pass.php?rand=' + Math.floor(Math.random()*1000), '', function() {
					$('#secondmodal').dialog('open');
				});
			} else if (data.substring(0, 7) == 'newuser') {
				var splitd = data.split('-');
				$('#secondmodal').load('http://www.lrs.org/includes/forms/newuser.php', '', function() {
					$('#newwname').val(splitd[1]);
					$('#secondmodal').dialog('open');
				})
			} else {
				malert(data);
			}
		})
	})
	//HOVER STATE FOR JQUERY UI
	$('.ui-state-default').live('mouseover', 
		function () {$(this).addClass("ui-state-hover")}
	)
	$('.ui-state-default').live('mouseout', 
		function () {$(this).removeClass('ui-state-hover')}
	)		
	//SHOWMENU
	$('.showdiv').live('click', function(e){
		e.preventDefault();
		var thisid = $(this).attr("id").replace('show_', '');
		$("#"+thisid).toggle('slow');
		return false;
	})	
	//CSS PREPARATION
	$('#jobline, #joblinetab, #fastfacts, #fftab, #news, #newstab, #datatools, #datatoolstab').css('display', 'block');
	$('#nojsslideshow').css('display', 'none');
	$('#slideshow').css('display', 'block'); 		
	$('#contentright #righttabs').tabs({});
	$('header').corner('top cc:#d9cfb8');
	$('header').corner('br cc:#d9cfb8');
	//SUBMENU
	$(".sublink").click(function(e) {
		e.preventDefault();
		var thisone = $(this);
		var id = $(this).attr('id').replace('link', '');
		if ($('#submenu'+id).hasClass('active')) {
			$('#submenu').slideUp();
			$('li.active').removeClass('active');
			$('#submenu'+id).removeClass('active').fadeOut();
			$(this).parent().removeClass('active');
		} else if ($('.submenu.active').size() > 0) {
			$('.submenu.active').removeClass('active').fadeOut('normal', function() {
				switchMenu(thisone);	
			});
		} else {
			switchMenu(this)
		};
	})
	//LOGIN JAVASCRIPT
	$('#loginmodal').dialog({
		autoOpen: false, 
		hide: 'slow', 
		modal: true, 
		position: ['center', 40] 
	});
	//ALERT JAVASCRIPT
	$('#modalalert').dialog({
		autoOpen: false,
		hide: 'slow',
		modal: true, 
		buttons: {"OK": function() {$(this).dialog('close')}},
		close: function() {$('#modalalert').html('');}
	})
	//FIRSTMODAL JAVASCRIPT
	$('#firstmodal').dialog({
		autoOpen: false,
		hide: 'slow',
		modal: true
	})	
	//SECONDMODAL JAVASCRIPT
	$('#secondmodal').dialog({
		autoOpen: false,
		hide: 'slow',
		modal: true, 
		close: function() {$('#secondmodal').html('');},
		position: ['center', 120], 
		width: 500 
	})		
	//DOCTYPES
	$('a[href$=".xls"], a[href*="public/benchmarks.php"], a[href*="public/totals.php"], a[href$=".XLS"], a[href*="excel=Excel"], a[href*="format=xls"]').append('<img class="fileicon" src="images/doc_types/icon_xls.gif" alt="Excel document">')
	$('a[href$=".pdf"], a[href$=".PDF"], a[href*="public/benchmarkspdf.php"], a[href*="public/totalspdf.php"], a[href*="pdf=PDF"], , a[href*="pdf=pdf"],  a[href*="format=pdf"]').append('<img class="fileicon" src="images/doc_types/icon_pdf.gif" alt="PDF document">')
	$('a[href$=".doc"]').append('<img class="fileicon" src="images/doc_types/icon_doc.gif" alt="Word document">')
	$('a[href$=".ppt"]').append('<img class="fileicon" src="images/doc_types/icon_ppt.gif" alt="PowerPoint document">')
	//TOOLTIP
	$(".tooltip").qtip({
		position: {
			target: 'mouse',
			adjust: {
				screen: true
			},
			corner: {
				target: 'bottomMiddle',
				tooltip: 'bottomMiddle'
			}
		},
		style: {
			background: '#e2dbc7',
			color: '#033363', 
			width: {min: 100, max: 350},
			border: {width: 2, radius: 3, color: '#033363', opacity: 0.5},
			tip: {
				corner: 'bottomMiddle',
				size: {x: 5, y: 5}
			}
		}
	});
	//SEARCH BUTTON TEXT DISAPPEAR
	$('#searchinp').focus(function() {
		if ($.trim($(this).val()) == 'Search LRS') {
			$(this).val('').css('color', '#000000')
		}
	});
	$('#searchinp').blur(function() {
		if ($.trim($(this).val()) == '') {
			$(this).val('Search LRS').css('color', '#988C68')
		}
	});
	//BIG SEARCH BUTTON TEXT DISAPPEAR
	$('#bigs').focus(function() {
		if ($.trim($(this).val()) == 'Search LRS') {$(this).val('').css('color', '#000000')}
	});
	$('#bigs').blur(function() {
		if ($.trim($(this).val()) == '') {
			$(this).val('Search LRS').css('color', '#988C68')
		}
	});
	$('.noicon img.fileicon').remove();
	
	$('#confirm').dialog({
		//bgiframe: true,
		autoOpen: false,
		width: 400,
		hide: 'slow'
	});
	
	$('#loading').remove();
	$('#loadedcontent').fadeIn();
	var newwidth = $('header').width();
	newwidth = newwidth - 25;
	$('.body_fullscreen footer').css('width', newwidth);
	$('.body_fullscreen #submenu').css('width', newwidth-10)
	$('.body_fullscreen header').css('width', newwidth+10);
})
