/* **************************************************************************************************************/
/* GLOBAL VALUES */
/* **************************************************************************************************************/

var clientHeight = document.documentElement.clientHeight;
var clientWidth = document.documentElement.clientWidth;

/* **************************************************************************************************************/


function button_select(x){
	nav_menu_rollover(x); 
	submenu_rollover(x);
}
function button_deselect(){
	nav_menu_rollout(); 
	submenu_rollout();
}


var menuOneAlignment = "57px";
var menuTwoAlignment = "219px";
var menuThreeAlignment = "401px";
var menuFourAlignment = "582px";
var menuFiveAlignment = "816px";

/* **************************************************************************************************************/
/* Index Navingation Buttons and Image Holders */
/* **************************************************************************************************************/
function nav_menu_rollover(x){
	 if(!x){
		x = document.getElementById('current_button').innerHTML;
	}
	
	 var rollover_image = document.getElementById('menu_links_rollover_image');
	 
	 if(x == 'button1'){
		rollover_image.style.left=menuOneAlignment;
		rollover_image.style.height="31px";
		rollover_image.style.width="126px";
		rollover_image.style.display="inline-block";
	 }
	 if(x == 'button2'){
		rollover_image.style.left=menuTwoAlignment;
		rollover_image.style.height="31px";
		rollover_image.style.width="126px";
		rollover_image.style.display="inline-block";
	 }
	 if(x == 'button3'){
		rollover_image.style.left=menuThreeAlignment;
		rollover_image.style.height="31px";
		rollover_image.style.width="126px";
		rollover_image.style.display="inline-block";
	 }
	 if(x == 'button4'){
		rollover_image.style.backgroundPosition="0px -32px";
		rollover_image.style.left=menuFourAlignment;
		rollover_image.style.height="31px";
		rollover_image.style.width="199px";
		rollover_image.style.display="inline-block";
	 }
	 if(x == 'button5'){
		rollover_image.style.left=menuFiveAlignment;
		rollover_image.style.height="31px";
		rollover_image.style.width="126px";
		rollover_image.style.display="inline-block";
	 }
}
function nav_menu_rollout(x){
	document.getElementById('menu_links_rollover_image').style.display="none";
	document.getElementById('menu_links_rollover_image').style.backgroundPosition="0px 0px";
}


/* Submenu Functions */
/* **************************************************************************************************************/
var menu_button_selected; /* set global variable so all functions can access it */


function submenu_rollover(x, y){
	
	if(!x){
		x = document.getElementById('current_button').innerHTML;
	}
	document.getElementById('current_button').innerHTML=x;
	menu_button_selected = document.getElementById(x); /* set a value to the global variable */
	menu_button_selected.style.color="#fff6ef"; /* set selected button's font color to highlight */
	submenu = document.getElementById('links_submenu');
	submenu_links = document.getElementById(x+"_links");
	submenu_holder = document.getElementById(x+"_holder");
	submenu_holder.style.width="130px";
	submenu.style.backgroundPosition="0px 0px";
	submenu.style.backgroundRepeat="no-repeat";
	submenu.style.display="inline-block";
	submenu_links.style.display="inline-block";
	

	var li = submenu_links.getElementsByTagName('li').length //adjust the submenu structure depending on the amount of links. MAX 4 links only!
	switch(li){	
		case 0:
		submenu.style.display="none";
		submenu_holder.style.display="none";
		break;
		
		case 1:
		submenu.style.height="45px";
		submenu.style.backgroundPosition="0px -324px";
		submenu_holder.style.display="inline-block";
		break;
		
		case 2:
		submenu.style.height="76px";
		submenu.style.backgroundPosition="0px -247px";
		submenu_holder.style.display="inline-block";
		break;
		
		case 3:
		submenu.style.height="104px";
		submenu.style.backgroundPosition="0px -142px";
		submenu_holder.style.display="inline-block";
		break;
		
		case 4:
		submenu.style.height="141px";
		submenu.style.backgroundPosition="0px 0px";
		submenu_holder.style.display="inline-block";
		break;
	}
	
	
		 
	if(x == 'button1'){
		submenu.style.left=menuOneAlignment;
		submenu_links.style.left="57px";
		submenu_holder.style.left="57px";
	}
	if(x == 'button2'){
		submenu.style.left=menuTwoAlignment;
		submenu_links.style.left="217px";
		submenu_holder.style.left="217px";
	}
	if(x == 'button3'){
		submenu.style.left=menuThreeAlignment;
		submenu_links.style.left="398px";
		submenu_holder.style.left="398px";
	}
	if(x == 'button4'){
		submenu.style.left=menuFourAlignment;
		submenu_links.style.left="582px";
		submenu_holder.style.left="575px";
		submenu_holder.style.width="199px";
	}
	if(x == 'button5'){
		submenu.style.left="676px";
		submenu_links.style.left="675px";
		submenu_holder.style.left="812px";
	}

}
function submenu_rollout(){
	menu_button_selected.style.color="#bb8057";
	for(i=1; i<=5; i++){
		document.getElementById("button"+i+"_links").style.display="none";
		document.getElementById("button"+i+"_holder").style.display="none";
		document.getElementById("links_submenu").style.display="none";
	}
	
}

/* **************************************************************************************************************/
/* Index Image Buttons and Image Holders */
/* **************************************************************************************************************/


/* Pre-Load background images */
/* **************************************************************************************************************/
function loader(){
	setCurrentImage('image_button1');
}


function index_image_button_rollover(x){
	var image_button = document.getElementById(x);
	var image_button_selected = document.getElementById('image_button_selected');
	
	if(image_button_selected.innerHTML == x){
		image_button.className='index_image_button_sel';
	} else {
		if(image_button.className=='index_image_button'){	
			image_button.className='index_image_button_sel';
		} else {
			image_button.className='index_image_button';
		}
	}
}

/* Background Image Fade Effect */
/* **************************************************************************************************************/
function setOpacity(level){  
	var rounded_value = Math.round(level * 100) / 100; <!-- round the level value to two decimal places. to round to one place device by 10 -->
	element = document.getElementById('previous_index_background_span');
	element.style.opacity = rounded_value;
	element.style.MozOpacity = rounded_value;
	element.style.KhtmlOpacity = rounded_value;
	element.style.filter = "alpha(opacity=" + (rounded_value * 100) + ");";

	if(rounded_value < "0.10"){
		element = document.getElementById('previous_index_background_span');
		element.style.opacity = 0;
		element.style.MozOpacity = 0;
		element.style.KhtmlOpacity = 0;
		element.style.filter = "alpha(opacity=0);";
	}
}


function fadeOut(){
	element = document.getElementById('previous_index_background_span');
	element.style.opacity = 1;
	element.style.MozOpacity = 1;
	element.style.KhtmlOpacity = 1;
	element.style.filter = "alpha(opacity=100);";
	var duration = 1000;  /* 1000 millisecond fade = 1 sec */
	var steps = 50;       /* number of opacity intervals   */

		for (i = 0; i <= 1; i += (1 / steps)) {
			fadeTimeout = setTimeout("setOpacity(" + (1 - i) + ")", i * duration);
		}
}

/* **************************************************************************************************************/
function setCurrentImage(x)
{	
	delay = 10000;
	/*************************************************************************/
	if(x == 'image_button1'){
		if(!document.getElementById('index_background_span').style.background){
			document.getElementById('index_background_span').style.background='url(images/index_'+x+'.jpg)';
		}
		setTimeout("fadeOut()", delay);
		setTimeout("setCurrentImage('image_button2')", delay);
		setTimeout("document.getElementById('index_background_span').style.background='url(images/index_image_button2.jpg)'", delay);
		setTimeout("document.getElementById('previous_index_background_span').style.background='url(images/index_image_button1.jpg)'", 1000);
		index_image_button_rollover('image_button1');
		document.getElementById('image_button2').className='index_image_button';
		document.getElementById('image_button3').className='index_image_button';
		document.getElementById('image_button4').className='index_image_button';		
	}
	
	if(x == 'image_button2'){
		setTimeout("fadeOut()", delay);
		setTimeout("setCurrentImage('image_button3')", delay);
		setTimeout("document.getElementById('index_background_span').style.background='url(images/index_image_button3.jpg)'", delay);
		setTimeout("document.getElementById('previous_index_background_span').style.background='url(images/index_image_button2.jpg)'", 1000);
		index_image_button_rollover('image_button2');
		document.getElementById('image_button1').className='index_image_button';
		document.getElementById('image_button3').className='index_image_button';
		document.getElementById('image_button4').className='index_image_button';
	}
	
	if(x == 'image_button3'){
		setTimeout("fadeOut()", delay);
		setTimeout("setCurrentImage('image_button4')", delay);
		setTimeout("document.getElementById('index_background_span').style.background='url(images/index_image_button4.jpg)'", delay);
		setTimeout("document.getElementById('previous_index_background_span').style.background='url(images/index_image_button3.jpg)'", 1000);
		index_image_button_rollover('image_button3');
		document.getElementById('image_button1').className='index_image_button';
		document.getElementById('image_button2').className='index_image_button';
		document.getElementById('image_button4').className='index_image_button';
	}
	
	if(x == 'image_button4'){
		setTimeout("fadeOut()", delay);
		setTimeout("setCurrentImage('image_button1')", delay);
		setTimeout("document.getElementById('index_background_span').style.background='url(images/index_image_button1.jpg)'", delay);
		setTimeout("document.getElementById('previous_index_background_span').style.background='url(images/index_image_button4.jpg)'", 1000);
		index_image_button_rollover('image_button4');
		document.getElementById('image_button1').className='index_image_button';
		document.getElementById('image_button2').className='index_image_button';
		document.getElementById('image_button3').className='index_image_button';
	}

	/*************************************************************************/
}


/* **************************************************************************************************************/
/* Administration function */
/* **************************************************************************************************************/


//---------------------------------
// Access Control Functions
//---------------------------------

function display_c(start){
	window.start = parseFloat(start);
	var end = 0;
	var refresh=1000;
	if(window.start > end ){
		mytime = setTimeout('display_ct()',refresh)
	} else if(window.start === end){
		window.location = "login.php";
	}
}
        
function display_ct() {
	var days = Math.floor(window.start / 86400);
	var hours = Math.floor((window.start - (days * 86400 ))/3600);
	var minutes = Math.floor((window.start - (days * 86400 ) - (hours *3600 ))/60);
	var seconds = Math.floor((window.start - (days * 86400 ) - (hours *3600 ) - (minutes*60)));
	if(days < 10){ var days = "0"+days; }
	if(hours < 10){ var hours = "0"+hours; }
	if(minutes < 10){ var minutes = "0"+minutes; }
	if(seconds < 10){ var seconds = "0"+seconds; }
	var x = minutes+" : "+seconds;
	document.getElementById('ct').innerHTML = x;
	window.start= window.start- 1;
	tt=display_c(window.start);
}
		
function switchpassword(q){ 
	if (q){ 
		document.getElementById('passwordtext').style.display="none"; 
		document.getElementById('password').style.display="inline"; 
		document.getElementById('password').focus(); 
	} else { 
		document.getElementById('password').style.display="none"; 
		document.getElementById('passwordtext').style.display="inline"; 
	} 
} 
function switchusername(q){ 
	if (q){ 
		document.getElementById('usernametext').style.display="none"; 
		document.getElementById('username').style.display="inline"; 
		document.getElementById('username').focus(); 
	} else { 
		document.getElementById('username').style.display="none"; 
		document.getElementById('usernametext').style.display="inline"; 
	} 
} 
//---------------------------------
// End Access Control Functions
//---------------------------------

function openMenu(menu){	
	document.getElementById(menu).style.display="inline-block";
	document.getElementById(menu).style.top=clientHeight;
}

function closeMenu(menu){
	document.getElementById(menu).style.display="none";
	document.getElementById(menu).style.top=clientHeight;

}

//=================================================================
// TEXT FORMATTING
//=================================================================
function formatText(el,tag){
	var selectedText = document.selection?document.selection.createRange().text:el.value.substring(el.selectionStart,el.selectionEnd);// IE:Moz
	if (selectedText == "") {return false}
	switch(tag){
		case "page_heading": 
			var newText='<p class='+tag+'>'+selectedText+'</p>'; 
			break;
		case "page_subheading": 
			var newText='<p class='+tag+'>'+selectedText+'</p>'; 
			break;
		default : 
			var newText='<'+tag+'>'+selectedText+'</'+tag+'>'; 
			break;
	}
	
	if(document.selection){    //IE
		document.selection.createRange().text=newText;
	} else{   //Moz
		el.value=el.value.substring(0,el.selectionStart)+newText+el.value.substring(el.selectionEnd,el.value.length);
	}
}
//=================================================================
// END TEXT FORMATTING
//=================================================================

//=================================================================
// INSERT BLANK ROW AT CURSOR
//=================================================================
function insertAtCursor(el, tag) {
	//IE support
	if (document.selection) {
		el.focus();
		sel = document.selection.createRange();
		sel.text = tag+"\r\n";
	}
	//MOZILLA/NETSCAPE support
	else if (el.selectionStart || el.selectionStart == '0') {
		var startPos = el.selectionStart;
		var endPos = el.selectionEnd;
		el.value = el.value.substring(0, startPos)
		+ tag + "\n\n"
		+ el.value.substring(endPos, el.value.length);
	} else {
		el.value += tag;
	}
}
//=================================================================
// INSERT BLANK ROW AT CURSOR
//=================================================================
	
//=================================================================
// TEXT PREVIEW UPDATE
//=================================================================
function updatePreview(){
	var text = document.getElementById('new_content').value;
	
	document.body.style.overflow='hidden';
	document.getElementById('newContentPreview').innerHTML = text;
	document.getElementById('newContentPreviewDiv').style.display='inline-block'; 
}
//=================================================================
// END TEXT PREVIEW UPDATE
//=================================================================

