// JavaScript Document

function showAdvertisement() {
     var theAd = document.getElementById('Advertisement');
     theAd.style.display='block';
}

function hideAdvertisement() {
     var theAd = document.getElementById('Advertisement');
     theAd.style.display='none';
}

function init () {
	loadContent();
	addDate();
	showTab();
//    showAdvertisement();
}

function loadContent() {
	if (document.getElementById('testimonialsContent')) {
		var theContainer = document.getElementById('testimonialsContent');
		var divArray = theContainer.getElementsByTagName('DIV');
		var randomNumber = Math.floor(Math.random()*divArray.length);
		divArray[randomNumber].className = "open";
	}
}


// if document had an id "dateContent" then it will replace the innerhtml with todays date
function addDate() {
	if (document.getElementById('dateContent')) {
		var dateContainer = document.getElementById('dateContent');
		var arMonths = new Array ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
		var d = new Date();
		var curDate = d.getDate();
		var curMonth = d.getMonth();
		var curYear = d.getFullYear();
		var todaysDate = curDate + " " + arMonths[curMonth] + " " + curYear;
		dateContainer.innerHTML = todaysDate;
	}
	
	if (document.getElementById('printDate')) {	
		var printDate = document.getElementById('printDate');
		var headerDate = document.getElementById('dateContent').innerHTML;
		printDate.innerHTML = headerDate;
	}
}

function showTab(tabNo) {
	if (document.getElementById('domTabs')) {
		var domTabs = document.getElementById('domTabs');
		var divArray = domTabs.getElementsByTagName('div');
		for (i = 0; i < divArray.length; i++) {
			if (divArray[i].id.indexOf('tab') >= 0) {
				divArray[i].style.display = "none";
			}
		}
		if (tabNo) {
			var tabToOpen = document.getElementById(tabNo);
			tabToOpen.style.display = "block";			
		}
		else {
	/*			document.getElementById('tab1').style.display = "block"; */
		}
	}
}


function getFirstChild(f) { 
	f = f.firstChild;
	while (f && !f.tagName) {
		f = f.nextSibling;
	} 
	return f;
}

function getNextSibling(ns) { 
	ns = ns.nextSibling;
	while (ns && !ns.tagName) {
		ns = ns.nextSibling;
	} 
	return ns;
}

/* BELOW FUNCTIONS FOR IMAGE GALLERY SCROLL AND IMAGE SWITCH */

function switchImage(galleryNo, theEl) {
	var theGallery = document.getElementById(galleryNo);
	var thumbClicked = theEl;
	
	theGallery.innerHTML = thumbClicked.innerHTML;
	
	var galleryImage = getFirstChild(theGallery);
	var galleryText = getFirstChild(getNextSibling(galleryImage));
	
	galleryImage.removeAttribute('width');
	galleryText.style.display = 'block';
}


function moveRight(theEl) {
	theDiv = document.getElementById(theEl);
	var numberImages = theDiv.getElementsByTagName('a');
	if (numberImages.length >= 4) {
		furthestMargin = parseInt(numberImages.length -4) * 66; // -4 was -1
		minusFurthest = furthestMargin - furthestMargin - furthestMargin;
		curMargin = theDiv.style.marginLeft;
		splitMargin = curMargin.split('px');
		intMargin = parseInt(splitMargin[0]);
		if (intMargin != minusFurthest) {
			newMargin = intMargin - 66;
			strMargin = newMargin + 'px';
			theDiv.style.marginLeft = strMargin;
		}
	}
}

function moveLeft(theEl) {
	theDiv = document.getElementById(theEl);
	curMargin = theDiv.style.marginLeft;
		splitMargin = curMargin.split('px');
		intMargin = parseInt(splitMargin[0]);
		if (intMargin <= -66) {
			newMargin = intMargin + 66;
			strMargin = newMargin + 'px';
			theDiv.style.marginLeft = strMargin;
		}
}

function sendEmail(theCheckBox) {
	if (theCheckBox.checked) {
		document.location='mailto:info@passmores.co.uk';
	}
	else {
		return false;
	}
}

function openForm(theEl,containerId) {
		theObj = document.getElementById(containerId);
		if (theEl.checked) {
			theObj.style.display = 'block';
		}
		else {
			theObj.style.display = 'none';
		}
}





