$(document).ready(function(){
	var currentmenu = null;
	$("#areaSlideNav div").hide();
	$("#areaBody").mouseover(function() { 
		currentmenu = null;
		$("#areaSlideNav div").hide();
		$("#jLogin").slideUp();
		$("#button_login").removeClass("actived");
	});

	$.each($("#areaSlideNav div"), function() { slideMenu($(this).attr("id")); });
	$("#areaSlideNav div").hover(function() {
		$("."+$(this).attr("id")).addClass("actived");
	}, function() { 
		$("."+$(this).attr("id")).removeClass("actived");
	});
	$("#areaHeader").mouseover(function() {
		$("#areaSlideNav div").hide();
		currentmenu = null;
	});

	function slideMenu(name) {
		$("."+name).hover(function() { $(this).addClass("actived"); }, function() { $(this).removeClass("actived"); });
		$("."+name).click(function() {
			if(currentmenu!=name) {
				$("#areaSlideNav div").hide();
				$("#"+name).slideDown();
				currentmenu = name;
			}
		});
	}

	$('#button_login').click(function() {
		if($(this).attr("href")!="#") return true;
		$("#jLogin").slideToggle();
		if($(this).hasClass("actived")) {
			$(this).removeClass("actived");
		} else {
			$(this).addClass("actived");
		}
		return false;
	});

	$(".Level-2").click(function() { 
		var target = $(this).attr("href");
		if(target && $(this).hasClass("actived")) {
			return true;
		} else {
			$(this).next().slideToggle("fast", function(){
				if($(this).prev().hasClass("actived")) {
					$(this).prev().removeClass("actived");
				} else {
					$(this).prev().addClass("actived");
				}
			});
			return false;
		}
	});


	$("#tabsJ a.MemberProfile").click(function() {
		if(!$(this).parent().parent().hasClass("actived")) {
			$("#tabsJ a.History").parent().removeClass("actived");
			$(this).parent().addClass("actived");
			$("#tableHistory").slideUp("fast", function() { $("#tableProfile").slideDown(); });
		}
		return false;
	});
	$("#tabsJ a.History").click(function() {
		if(!$(this).parent().hasClass("actived")) {
			$("#tabsJ a.MemberProfile").parent().removeClass("actived");
			$(this).parent().addClass("actived");
			$("#tableProfile").slideUp("fast", function() { $("#tableHistory").slideDown(); });
		}
		return false;
	});

	$("#linkPrint").click(function() {
		var params = location.href.substring(location.href.lastIndexOf('?')+1);
		var target = location.href.substring(0,location.href.lastIndexOf('.php')+4);
		if(params!=target) {
			params = "&"+params;
		} else {
			params = "";
		}
		window.open(target + "?print=true"+params);
		return false;
	});


	$('#btnLogin').click(function(e) {
		var flag = true;
		if($('#login_email').val()==""  || $('#login_email').val()=="username / email address") {
			$('#login_email').css("border","1px solid #F00");
			flag = false;
		} else {
			$('#login_email').css("border","1px solid #B6B68A");
		}
		if($('#login_password').val()=="") {
			$('#login_temp').css("border","1px solid #F00");
			$('#login_password').css("border","1px solid #F00");
			flag = false;
		} else {
			$('#login_temp').css("border","1px solid #B6B68A");
			$('#login_password').css("border","1px solid #B6B68A");
		}
		return flag;
	});

	$('#btnLogout').click(function(e) {

	});

	if($('#errorModal').html()!="") {
		$.fn.nyroModalManual({
			bgColor: '#777',
			width: 200,
			minHeight: 100,
			content: "<div><p>"+$('#errorModal').html()+"</p></div>"
		});
	}
});

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}

function pagenext(table, page) {
	var t = document.getElementById(table);
	var res;
	if (page=="previous") {
		res=Table.pagePrevious(t);
	} else if (page=="next") {
		res=Table.pageNext(t);
	} else {
		res=Table.page(t,page);
	}
	var currentPage = res.page+1;
	scroll(0,0);
}

// Datepicker
$.datepicker.setDefaults($.extend({
	inline: false,
	dateFormat: "yy-mm-dd",
	numberOfMonths: 1,
	changeMonth: true,
	changeYear: true,
	showOn: 'button', 
	buttonImage: '_images/icon-calendar.gif', 
	buttonImageOnly: true,
	buttonText: 'Show Calender'
}));

date_obj_time = (function(date) {
	return (function(hours, mins, secs) {
		if (hours< 10) { hours = "0" + hours; }
		if (mins < 10) { mins = "0" + mins; }
		if (secs < 10) { secs = "0" + secs; }
		return '' + hours + ':' + mins + ':' + secs + '';
  })(date.getHours(), date.getMinutes(), date.getSeconds());
})(new Date());

document.write('<div id="loading"></div>');

// Created by: Simon Willison | http://simon.incutio.com/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  document.getElementById("loading").style.display="none";
});
