// JavaScript Document

// GLOBALS
var request = null;
var rootPath = "/";
var _AJAX_ROOT = location.protocol + "//" + location.host + rootPath;
var dispBox = false;

// INIT FUNCTION
$(document).ready(function(){
	
});

window.onload = function() {
	while(!dispBox) {
		dispBox = document.getElementById("mainBox");
	}
	
	if(location.search) {
		getSubNav(location.search.substring(1));
	}
	else {
		var strFil = location.href.split("/").pop();
		if(strFil == "" || !strFil)
			sendData("index");
		else
			sendData(strFil.split(".")[0]);
	}
};

// SUB NAV FUNCTION
function getSubNav(trgId) {
	var aElements = document.getElementById("submenu").getElementsByTagName("a");
	for (var i = 0; i < aElements.length; i++) {
		if(aElements[i].id == trgId)
			aElements[i].style.color = "#981a4d";
		else
			aElements[i].style.color = "#000000";
	}
	sendData(trgId);
}

// AJAX FUNCTION - Get Page Content
function sendData(param) {
	$.get(_AJAX_ROOT + 'getContent.php',{id:param},function(Data){
		dispBox.innerHTML = "";
		dispBox.innerHTML = Data;
		if(param == 'jobs'){
			showJobs();
		}
	});
}

// Jobb functions
function showJobs()
{
	$.get(_AJAX_ROOT + 'ajaxjobs.php',{typ:'showall'},function(Data){
		$('#jobsMainBox').html(Data);
		$('#jobNyttPopUp').hide();
		$('div.jobRedPopUp').hide();
		
		$('div.jobContent').mouseenter(function(){
			if(checkCookie('Admin')){
				var os = $(this).offset();
				var id = $(this).attr('id');
				$('div.jobRedPopUp').filter(function(index){
					return $(this).attr('rel') == id;
				  }).css({top:os.top + 'px', left:(os.left + 500) + 'px'})
					.mouseenter(function(){$(this).show();})
	  				.mouseleave(function(){$(this).hide();})
	  				.click(function(){
						$.fancybox({
							'showCloseButton'   : true,
							'hideOnContentClick': false,
							'autoDimensions'    : false,
							'scrolling'         : 'no',
							'transitionIn'	    : 'elastic',
							'transitionOut'	    : 'elastic',
							'speedIn'           : 300,
							'speedOut'          : 300,
							'orig'              : $(this),
							'width'             : 480,
							'height'            : 520,
							'href'			    : _AJAX_ROOT + 'ajaxeditjob.php?pid=' + $(this).attr('rel'),
							'type'			    : 'iframe',
							'onClosed'          : function(){showJobs();}
						});
					}).show();
			}
		}).mouseleave(function(){
			var id = $(this).attr('id');
			$('div.jobRedPopUp').filter(function(index){return $(this).attr('rel') == id;}).hide();
		});
		
		$('.jobContent').hide();
		
		$('.jobHeadLnk').click(function(event){
			event.preventDefault();
			$('#' + $(this).attr('rel')).slideToggle("slow");
		});
		
		$('#jobsMainHead').mouseenter(function(){
			if(checkCookie('Admin')){
				var s = $(this).offset();
				$('#jobNyttPopUp')
					.css({top:(s.top - 10) + 'px', left:(s.left + 150) + 'px'})
					.mouseenter(function(){$(this).show();})
					.mouseleave(function(){$(this).hide();})
					.click(function(){
						$.fancybox({
							'showCloseButton'   : true,
							'hideOnContentClick': false,
							'autoDimensions'    : false,
							'scrolling'         : 'no',
							'transitionIn'	    : 'elastic',
							'transitionOut'	    : 'elastic',
							'speedIn'           : 300,
							'speedOut'          : 300,
							'orig'              : $(this),
							'width'             : 480,
							'height'            : 520,
							'href'			    : _AJAX_ROOT + 'ajaxeditjob.php?pid=0',
							'type'			    : 'iframe',
							'onClosed'          : function(){showJobs();}
						});
					}).show();
			}
		}).mouseleave(function(){
			if($('#jobNyttPopUp').is(':visible')){
				$('#jobNyttPopUp').hide();
			}
		});
	});
}

function checkCookie(c_name){
	var retVal = false;
	if(document.cookie.length > 0){
  		var c_start = document.cookie.indexOf(c_name + "=");
  		if(c_start != -1){
			retVal = true;
		}
	}
	return retVal;
}

