


$(document).ready(function() {

	simple_tooltip(".tip","tooltip");

	$("#accordianWrap").css('display', 'block');
	
	 /**
		  * Accordian toggle script
		  */
		 $(".toggler").click (function () {
		 	$(".element").each (function () {
		 		$(this).hide();
		 	});
		 	$(".toggler").each (function () {
		 		$(this).removeClass('on');
		 	});
		 	$(this).addClass('on');
		 	$(this).next().addClass('box-black');
		 	if ($(this).next().hasClass('login-black')) {
		 		$(this).next().next().slideDown(500);
		 	}
		 	$(this).next().slideDown(500);
		 });

	$(function() {
		
		var api = $("#overlay").overlay({api:true});
	
		// define function that opens the overlay
		window.openOverlay = function() {
			api.load();
		}

	});
	
		/**
	* Check if user needs to login to view a page
	*/
	$(".lightboxlogin").click (function () {
		
		var requestedPage = Url.encode(this.href);
		var cftoken = getToken();
		var key = getKey();
		var overlay = false;
		
		 $.ajax({
		 async: false,
		 url: 'http://www.engineersaustralia.org.au/app_templates/check.cfm?token=' + cftoken + '&shadokey=' + key + '&requested_page=' + requestedPage,
		 type: 'GET',
		 processData: false,
		 success: function (response) {
			 	$(response).find('status').each (function () {
			 		status = $(this).text();
			 	});
			 	if ((status == 'loggedin') || (status == 'error'))  { 
			 		//User is logged in, or an error of some kind occurred. Set overlay to false
			 		overlay = false;
			 	} else if (status == 'loggedout') {
			 		$(response).find('loginrequired').each (function () {
						login = $(this).text();
					});
					if (login == 'true') {
							$("#overlay > .wrap").load('/app_templates/login2.cfm?requested_page=' + requestedPage);
							window.openOverlay();
							overlay = true;
			 		} else {
			 			overlay = false;
			 		}
			 	} 
		 	}
		 	
		 });
		 if (overlay == true) {
		 	return false;
		 }
		//fall through and operate like normal link
	});
	
	/**
	* end logn check
	*/
	
		/* division bell
		 * 
		 */
		$(".divisionbell").click (function () {
				//$(".mapnav").css('display', 'block');
				$(".mapnav").fadeIn("slow");
				return false;
		});
		$(".mapnav").mouseleave( function () {
				//$(".mapnav").css('display', 'none');
				$(".mapnav").fadeOut("slow");
		});
		
		/* division bell 2 (for the infrastructure divisions)
		 * 
		 */
		$(".statebell").click (function () {
				$(".mapnav2").fadeIn("slow");
				return false;
		});
		$(".mapnav2").mouseleave( function () {
				//$(".mapnav").css('display', 'none');
				$(".mapnav2").fadeOut("slow");
				return false;
		});
		
		
		
		
		$(".addtocalendar").click (function () {
				$("#result").load(this.href);
				return false;
		});
		
		
	
		/** 
		 * The login lightbox
		 */
		$(function() { 
			
			loadLogin = function()	{
				var wrap = this.getContent().find("div.wrap"); 
	        	wrap.load(this.getTrigger().attr("href")); 
			}
			
	    	$("a[rel]").overlay({
	    		expose: true,
	    		onBeforeLoad: loadLogin
	    		});
		}); 
	
		
		/**
		 * JS for displaying the navigation floats for the 
		 * top navigation.
		 */
		$(".section").mouseenter (function() {
			
			var baseRight = (screen.width - 882) / 2;
			var link = $(this).children("a");
			
			$(".subnav").hide();
			
			if (link.hasClass('icon-about'))  {
				right = baseRight + 130;
				$("#aboutMenu").css('right', right + "px");	
				$("#aboutMenu").load('/app_templates/ajax.cfm?ajaxTemplate=floatmenu_about.cfm');
				$("#aboutMenu").fadeIn();
				$(this).attr('rel', 'over');
				
			} else if (link.hasClass('icon-colleges')) {
				right = baseRight + 35;
				$("#collegesMenu").css('right', right + "px");
				$("#collegesMenu").load('/app_templates/ajax.cfm?ajaxTemplate=floatmenu_colleges.cfm');
				$("#collegesMenu").fadeIn();
				$(this).attr('rel', 'over');
				
			} else if (link.hasClass('icon-groups')) {
				right = baseRight + -50;
				$("#groupsMenu").css('right', right + "px");
				$("#groupsMenu").load('/app_templates/ajax.cfm?ajaxTemplate=floatmenu_groups.cfm');
				$("#groupsMenu").fadeIn();
				$(this).attr('rel', 'over');
			}
			//setInterval("$('.sunbav').hide()", 400);
			//setTimeout("$('.subnav').fadeOut()", 4000);
		});
		
		$(".subnav").mouseenter (function () {
			var id = $(this).attr('id');
			$("#" + id).attr('rel', 'over');
		});
		
		$(".subnav").mouseleave (function () {
			
			$(".subnav").fadeOut(50);
			/*var id = $(this).attr('id');
			$("#" + id).attr('rel', 'out');
			
			isMouseOn = function()
			{
				if (id == 'aboutMenu') {
					link = $(".icon-about").parent('li');
					rel = link.attr('rel');
					if (rel == 'out') {
						$("#aboutMenu").fadeOut();
					}
				} else if (id == 'collegesMenu') {
					link = $(".icon-colleges").parent('li');
					rel = link.attr('rel');
					if (rel == 'out') {
						$("#collegesMenu").fadeOut();
					}
				} else if (id == 'groupsMenu') {
					link = $(".icon-groups").parent('li');
					rel = link.attr('rel');
					if (rel == 'out') {
						$("#groupsMenu").fadeOut();
					}
				}
			}
			setTimeout("isMouseOn()", 200); */
		}); 
		
		$(".section").mouseleave (function () {
			var link = $(this).children("a");
			
			isMouseOver = function ()
			{
				if (link.hasClass('icon-about')) {
					var par = $(link).parent();
					par.attr('rel', 'out');
					rel = $("#aboutMenu").attr('rel');
					if (rel == 'out') {
						$("#aboutMenu").fadeOut();
					}
				
				} else if (link.hasClass('icon-colleges')) {
					var par = $(link).parent();
					par.attr('rel', 'out');
					rel = $("#collegesMenu").attr('rel');
					if (rel == 'out') {
						$("#collegesMenu").fadeOut();
					}
				
				} else if (link.hasClass('icon-groups')) {
					var par = $(link).parent();
					par.attr('rel', 'out');
					rel = $("#groupsMenu").attr('rel');
					if (rel == 'out') {
						$("#groupsMenu").fadeOut();
					}
				}
			}
			setTimeout("isMouseOver()", 50);
		}); 
		
		/**
		 * end navigation floats
		 */
		 
		 /**
		  * Accordian toggle script
		  */
		 
		
		
		/**
		 * Organisation links ajax
		 */
		 
		 $("#organisationTitles").load("/app_templates/ajax.cfm?ajaxTemplate=ajaxpagelets_orgs.cfm&letter=A")
		 
		 $(".organisationLink").click(function () {
		 	
		 	$("#organisationTitles").load(this.href);
		 	
		 	$("#organisationContent").html("&nbsp;");
		 	
		 	return false;
		 });
		 
		 $(".organisationLink2").click (function () {
		 	
		 	$("#organisationContent").load(this.href);
		 	
		 	return false;
		 });
	
		/**
		 * Load ajax for the FAQs
		 */
		$(".faqLinks").click(function() {
			$("#faq-answers").load(this.href);	
			return false;
		});
	
		/**
		 * Ajax Tabs
		 */	
		$(function() { 
 
		    $("ul.css-tabs").tabs("div.css-panes > div", function(i) { 
		 
		        // get the pane to be opened 
		        var pane = this.getPanes().eq(i); 
		 
		        // if it is empty .. 
		        if (pane.is(":empty")) { 
		 
		            // load it with a page specified in the tab's href attribute 
		            pane.load(this.getTabs().eq(i).attr("href")); 
		        } 
		 
		    }); 
 
		});
	
		/**
		 * Accordian tabs
		 */
		$("#accordion").tabs("#accordion div.pane", { 
	    tabs: 'h2',  
	    effect: 'slide' 
		});
	
	
		/** 
		 * Dropdown box functionality
		 */
   		$(".dropdownBox").hide();
   		$('#collegesMenu').jqm({overlay:0, modal:false});
   		$("#groupsMenu").jqm({overlay:0, modal:false});
   		
   		links = $(".menulink");
   		links.each (function() {
   			$(this).hover( 
   				function () {
   				classes = this.className.split(" ");
   				left = ($(this).offset().left);
				top = ($(this).offset().top);
				$("#" + classes[1] + "Menu").css('left', left - 100);
				$("#" + classes[1] + "Menu").css('top', top + 30);
				$("#" + classes[1] + "Menu").jqmShow(); 
				//setTimeout('$("#" + classes[1]  + "Menu").jqmHide();', 500)
				$('#' + classes[1] + "Menu").hover (function () {
   					//do nothing
   				},
   				function () {
   					setTimeout("$('#' + classes[1] + 'Menu').jqmHide();", 500);
   				});	
   			},
   			function () {
   				classes = this.className.split(" ");
   				$('#' + classes[1] + "Menu").hover (function () {
   					//do nothing
   				},
   				function () {
   					setTimeout("$('#' + classes[1] + 'Menu').jqmHide();", 500);
   				});	
   				//setTimeout("$('#' + classes[1] + 'Menu').jqmHide();", 500);
   				
   			});
   		});
   		divs = $(".jqmWindow");
   		divs.each( function () {
   			$(this).hover( 
   			function () {
   				$("#" + this.id).jqmShow();
   			},
   			function () {
   				setTimeout('$("#" + this.id).jqmHide();', 500);
   			});
   		});
 });
  
function toggleDropdown(id)
{
	if ($("#title" + id + " .dright a").text() == '+') {
		$("#title" + id + " .dright a ").text('-');
		$("#dropdown"  + id).slideDown(200);
	} else {
		$("#title" + id + " .dright a ").text('+');
		$("#dropdown" + id).slideUp(200);
	}
}


function simple_tooltip(target_items, name) {
 $(target_items).each(function(i){
		$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);

		$(this).removeAttr("title").mouseover(function(){
				my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(400);
		}).mousemove(function(kmouse){
				my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
		}).mouseout(function(){
				my_tooltip.fadeOut(400);
		});
	});
}

