// JavaScript Document

// Main Image
var totalHeadItems = 9; // 9 is 0-8
var mimg_clock;
var headSwapTime = 6500; 
var headopacity;
var headopacity2;
var fadehead;
var img1 = 1;
var img2 = 2;
var curimg1 = 1;
var arrMain = [];


//news:
var newsClock;
var newsSwapTime = 5500; //make 10000
var currNewsItem = 0;
var totalNewsItems = 2; //auto-set
var newsopacity;
var fadenews;


var pararr = [];	

function load_ra_scripts() {	
	
	storeGet(document.URL.substring(document.URL.indexOf("?") + 1));
	
	if (ged("main_image_2")) {
		arrayMainImage();
		changeOpac(0, ged("main_image_2"));
		ged("main_image_2").style.display = "";
		var mimg_func = function(){changeHeaderText()};	
		mimg_clock = setInterval(mimg_func, headSwapTime);	
	}	
	
	if (ged("newsitem_1")) {
		var news_func = function(){changePilingNews()};	
		newsClock = setInterval(news_func, newsSwapTime);	 //make 10000
	}
	
	if (ged("section_1")) {
		startupSideMenu();
	}
	
	
	if (ged("tech_1")) {
		startupTech();	
	}	
	if (ged("profile_section_1")) {
		startupProfile();	
	}	
	if (ged("rigfleet_section_1")) {
		startupFleet();	
	}	
	if (ged("siteinv1_section_1")) {
		startupGuide();	
	}
	if (ged("main_newspage")) {		
		startupNews();
	}
	
	
	if (ged("photosearchfolder")) {
		startupPhotoViewer();	
	}
	
}


function highlightMe(el) {	
	el.style.color = "red";	
}
function unhighlightMe(el) {
	el.style.color = "black";	
}

function showActionSelect(el) {
	el.setAttribute("class", "actionmenuitem_sel");
	el.setAttribute("className", "actionmenuitem_sel");
}
function hideActionSelect(el) {	
	el.setAttribute("class", "actionmenuitem");
	el.setAttribute("className", "actionmenuitem");
}


function selectLink(el) {	
	el.setAttribute("class", "address_email_link_select");
	el.setAttribute("className", "address_email_link_select");
}
function deSelectLink(el) {
	el.setAttribute("class", "address_email_link");
	el.setAttribute("className", "address_email_link");
}


// Normal functions for handling HTML elements:
function ged(elemname) {
	var elem = document.getElementById(elemname);
	if (!elem) {
		return null;	
	}
	return elem;
}
function gedVal(elemname) {
	var elem = document.getElementById(elemname);
	if (!elem) {
		return "";	
	}
	return elem.value;
}
function gedIn(elemname) {
	var elem = document.getElementById(elemname);
	if (!elem) {
		return "";	
	}
	return elem.innerHTML;
}
function gedSelId(elemname) {
	var selname = "";
	var sel = document.getElementById(elemname);
	if (!sel) {
		return selname;	
	}
	if (sel.selectedIndex >= 0) {
		selname = sel.options[sel.selectedIndex].id;
	}
	
	return selname;
}
function gedSelVal(elemname) {
	var selname = "";
	var sel = document.getElementById(elemname);
	if (!sel) {
		return selname;	
	}
	if (sel.selectedIndex >= 0) {
		selname = sel.options[sel.selectedIndex].innerHTML;
	}
	
	return selname;
}
function gedSelIn(elemname) {
	var selname = "";
	var sel = document.getElementById(elemname);
	if (!sel) {
		return selname;	
	}
	if (sel.selectedIndex >= 0) {
		selname = sel.options[sel.selectedIndex].innerHTML;
	}
	
	return selname;
}
function setGedSelByVal(elemname, val) {
	var selEl = ged(elemname);
	var fndselindex = 0;	
	for (var i = 0; i < selEl.options.length; i++) {		
		//alert (selEl.options[i].innerHTML);
		if (selEl.options[i].innerHTML == val) {
			fndselindex = i;
			break;
		}	
	}	
	selEl.selectedIndex = fndselindex;	
}
function setGedSelById(elemname, id) {
	var selEl = ged(elemname);
	var fndselindex = 0;	
	for (var i = 0; i < selEl.options.length; i++) {			
		if (selEl.options[i].id == id) {
			fndselindex = i;
			break;
		}	
	}	
	selEl.selectedIndex = fndselindex;
}
function cFromTextArea(vals) {
	return vals.replace(/\n/g, "<br>");	
}
function cToTextArea(vals) {
	//alert (vals);
	return vals.replace(/<br>/g, "\n");	
}
// END.


function storeGet(params) {
	
	pararr = [];
	
	var tmparr = params.split("&");	
	
	var storepos = 0;
	for (var p = 0; p < tmparr.length; p++) {
		var str = tmparr[p];
		var tmparrval = str.split("=");
		//alert (tmparr[p]);
		if (tmparrval) {
			if (tmparrval.length == 2) {
				pararr[tmparrval[0]] = tmparrval[1];
				storepos++;
			}
		}
	}
}

function changeOpac(opacity, objEl) {      
	if (objEl) {	
		if (objEl.style.opacity) {
		objEl.style.opacity = opacity;
		}
		objEl.style.MozOpacity = opacity;
		objEl.style.KhtmlOpacity = opacity;		
		objEl.style.filter = "alpha(opacity=" + (opacity * 100) + ")";	
	}
}


function arrayMainImage() {
		
	arrMain[0] = "Greenwich Reach, London";
	arrMain[1] = "Accreditations";
	arrMain[2] = "Birmingham International";	
	arrMain[3] = "Ashford, Working Near Railways";
	arrMain[4] = "Tarling Estate, Shadwell";
	arrMain[5] = "Battersea Reach";
	arrMain[6] = "Greenwich Reach, London";
	arrMain[7] = "New England Quarter, Brighton";
	arrMain[8] = "Birmingham International";
	//arrMain[8] = "";
}

function changeHeaderText() {
	if (ged("main_image_2")) {
		clearInterval(fadehead);
		clearInterval(mimg_clock);	
		headopacity = 1.0;
		headopacity2 = 0.0;
		var fadstr = function(){headFade(-0.1);};	
		fadehead = setInterval(fadstr, 50);	
	}
}

function headFade(qty) {
	
	var cel = ged("main_image_" + img1);
	var cel2 = ged("main_image_" + img2);
		
	if (headopacity < 0) {
		//clearInterval(fadehead);		
			
		//cel = ged("main_image_div");
		if (cel.style.opacity) {
			cel.style.opacity = 0;
		}
		cel.style.MozOpacity = 0;
		cel.style.KhtmlOpacity = 0;
		cel.style.filter = "alpha(opacity=0)";	
				
	} 
	
	if (headopacity2 > 1) {
		clearInterval(fadehead);	
		
		if (cel2.style.opacity) {
			cel2.style.opacity = 1;
		}
		cel2.style.MozOpacity = 1;
		cel2.style.KhtmlOpacity = 1;
		cel2.style.filter = "alpha(opacity=100)";	
		
		curimg1++;
		if (curimg1 >= totalHeadItems) {
			curimg1 = 0;	
		}			
		cel.src = "img/photos/img_" + curimg1 + ".jpg?" + curimg1 + "=1";			
		
		if (img1 == 1) {
			img1 = 2;
			img2 = 1;
		} else {
			img1 = 1;
			img2 = 2;	
		}	
		
		var head_func = function(){changeHeaderText()};	
		mimg_clock = setInterval(head_func, headSwapTime);	 //make 10000
		
		return;
	}	
	
	changeOpac(headopacity, cel);
	headopacity = headopacity + qty;
	
	if (headopacity < 0.1) {
		ged("main_image_txt").innerHTML = arrMain[curimg1];
	}
	
	if (headopacity < 0.7) {		
		changeOpac(headopacity2, cel2);
		headopacity2 = headopacity2 - (qty);	
	}	
		
}


function changePilingNews() {
	if (ged("newsitem_1")) {
		clearInterval(fadenews);
		clearInterval(newsClock);	
		newsopacity = 1;
		var fadstr = function(){newsFade(-0.1);};	
		fadenews = setInterval(fadstr, 50);		
	}
}

function newsFade(qty) {
	
	var cel = ged("newsitem_" + currNewsItem);	
	
	if (qty < 0 && newsopacity < 0.2) {
		clearInterval(fadenews);		
		newsopacity = 0.2;	
		
		//cel.style.display = "none";	
		//Move DIV for news item to bottom of list instead of hiding it
		var pnode = cel.parentNode;
		pnode.removeChild(cel);
		
		cel.style.MozOpacity = 1;
		cel.style.KhtmlOpacity = 1;
		cel.style.filter = "alpha(opacity=100)";
		pnode.appendChild(cel);
		
		currNewsItem ++;
		if (currNewsItem >= totalNewsItems) {
			currNewsItem = 0;	
		}
		
		cel = ged("newsitem_" + currNewsItem);
		if (cel.style.opacity) {
			cel.style.opacity = 0.2;
		}
		cel.style.MozOpacity = 0.2;
		cel.style.KhtmlOpacity = 0.2;
		cel.style.filter = "alpha(opacity=20)";	
		
		//cel.style.display = "";	
		
		var fadstr = function(){newsFade(0.1);};	
		fadenews = setInterval(fadstr, 50);	
		return;
		
	} else if (qty > 0 && newsopacity > 1) {
		clearInterval(fadenews);	
		
		if (cel.style.opacity) {
			cel.style.opacity = 1;
		}
		cel.style.MozOpacity = 1;
		cel.style.KhtmlOpacity = 1;
		cel.style.filter = "alpha(opacity=100)";	
		
		var news_func = function(){changePilingNews()};	
		newsClock = setInterval(news_func, newsSwapTime);	 //make 10000
		return;
	}	
	
	changeOpac(newsopacity, cel);	
	newsopacity = newsopacity + qty;	
}


function resetPins() {
	var elem = ged("map_ho");	
	elem.src = "img/pinlocate.png";	
	elem = ged("map_ne");	
	elem.src = "img/pinlocate.png";	
	elem = ged("map_nw");	
	elem.src = "img/pinlocate.png";	
}


function showMapLocation(elem) {
	
	resetPins();
	
	elem.src = "img/pinlocate_sel.png";
	
	ged("help_map").style.display = "none";
	
	ged("map_ho_text").style.display = "none";
	ged("map_ne_text").style.display = "none";
	ged("map_nw_text").style.display = "none";
	
	//alert (elem.id);
	
	var type = elem.id; //eg map_ho	
	var txtel = ged(type + "_text");
	txtel.style.display = "";
}

function hideMapLocation(elem) {
	//var type = elem.id; //eg map_ho	
	//var txtel = ged(type + "_text");
	elem.style.display = "none";
}

var curOff = 1;
function changeOfficeImg() {	
	
	var el = ged("office_image");
	
	if (curOff > 3) {
		curOff = 1;	
	}
	
	ged("headoffice").style.fontWeight = "normal";
	ged("neoffice").style.fontWeight = "normal";
	ged("nwoffice").style.fontWeight = "normal";
	ged("map_ho").src = "img/pinlocate.png";
	ged("map_ne").src = "img/pinlocate.png";
	ged("map_nw").src = "img/pinlocate.png";
	
	if (curOff == 1) {
		el.src = "img/office_grimsby.jpg";
		ged("neoffice").style.fontWeight = "bold";
		ged("map_ne").src = "img/yellowpin.png";
	}
	
	if (curOff == 2) {
		el.src = "img/office_warrington.jpg";
		ged("nwoffice").style.fontWeight = "bold";	
		ged("map_nw").src = "img/yellowpin.png";
	}
	
	if (curOff == 3) {
		el.src = "img/office_leatherhead.jpg";
		ged("headoffice").style.fontWeight = "bold";
		ged("map_ho").src = "img/yellowpin.png";
	}
	
	curOff++;	
}
