/*---[ Details ]---------------------------------------
Javascipt Custom Functions
Site: www.innocentdrinks.co.uk
Author: Lee Powell
Contact: lee.powell@soup.co.uk
Date Created: 17.05.2007
Date Modified: 17.05.2007
-------------------------------------------------------*/

/*-----------------------------------------------------
Contents:
[01] Home
[02] Fete Finder
[03] Fete Details
[04] Add Your Fete
-------------------------------------------------------*/

/* [01] Home
-------------------------------------------------------*/
function homeInit() {
	var container = container || document;
    var column = container.getElementById("col_2");
	var listItems = column.getElementsByTagName("li");
	var targets = new Array;
	for (i=0; i < listItems.length; i++) {
		var target = listItems[i].getElementsByTagName("p");
		targets.push(target[0]);
		var links = listItems[i].getElementsByTagName("a");
		links[0].onmouseover = function() {
			showOverlay(this);	
		}
		links[0].onmouseout = function() {
			hideAll(targets);
		}
		target[0].onmouseover = function() {
			showOverlay(this);	
		}
		target[0].onmouseout = function() {
			hideAll(targets);
		}
	}
	hideAll(targets);
}

function showOverlay(obj) {
	var target = obj.parentNode.getElementsByTagName("p")[0];
	var tempclass = target.className.replace("hide", "show");
	target.className = tempclass;
}

/* [02] Fete Finder
-------------------------------------------------------*/
function mapInit() {
    var container = container || document;
    var column = container.getElementById("col_2");
    
    /* we set the column height here as if we did it in the
    style sheet it'd truncate the non-js list */
    column.className = "setheight";
    
    /* get all dl's and hide them */
    var targets = column.getElementsByTagName("dl");
    for (i=0; i < targets.length; i++) {
        targets[i].className = targets[i].className+" "+"hide";
    }
    
    var maps = column.getElementsByTagName("area");
	var overlays = column.getElementsByTagName("dl");
	
    for (x=1; x < maps.length; x++) {
    
        /* removes title and alt tags from image to stop firefox/IE flicker bug */
        maps[x].setAttribute("title", "");
        maps[x].setAttribute("alt", "");
       
        /* adds mouseover event */
        maps[x].onmouseover = function() {      
        	if (typeof hideAll != "undefined") {     
            	hideAll(targets);
            }
            if (typeof showFeteDetails != "undefined") {
            	showFeteDetails(this); 
            } 
        }
        	
    }
	
	/* stops strange ie flicker on rolling over shown popup */
	for (z=0; z < overlays.length; z++) {
		overlays[z].onmouseover = function() {
			if (this.className == "hide") {
				this.className = "show";	
			}
		}
	}
    
    /* on mouseover of a non-region hide all pop-ups */
    var mapunderlay = document.getElementById("img_fetemap");
    mapunderlay.onmouseout = function() {
    	if (typeof hideAll != "undefined") {     
    		hideAll(targets);
    	}
    }
}	

function hideAll(targets) {
	if (targets != null) {
		var container = container || document;
		if(targets.length) {
			for (i=0; i < targets.length; i++) {
				if(targets[i].className.indexOf("show").length > 0) {
					var tempclass = targets[i].className.replace("show", "hide");
				} else {
					var tempclass = "hide";
				}
				targets[i].className = tempclass;
			}
		} else {
			if(targets.className.indexOf("show").length > 0) {
				var tempclass = targets.className.replace("show", "hide");
			} else {
				var tempclass = "hide";
			}
			targets.className = tempclass;
		}
	}
}

function showFeteDetails(target) {
    mapLink = target.getAttribute("href");
    stripLink = mapLink.split("=");
    target = "region"+stripLink[1];
    var targetDetail = document.getElementById(target);
	var tempclass = targetDetail.className.replace("hide", "show");
    targetDetail.className = tempclass;
}


/* [03] Fete Details
-------------------------------------------------------*/
function attractionsInit() {
    var container = container || document;
    var attractions = document.getElementById("attractions").getElementsByTagName("li");
    var targets = new Array;
    for (i=0; i < attractions.length; i++){
        var image = attractions[i].getElementsByTagName("img");
       
        /* creates new paragraph element, and populates it with text from the images alt tag */
        var newElement = document.createElement("p")
        var newTextNode = document.createTextNode(image[0].getAttribute("alt"));
		var newSpan = document.createElement("span");
		var newInnerSpan = document.createElement("span");
        newInnerSpan.appendChild(newTextNode);
		newSpan.appendChild(newInnerSpan); 
		newElement.appendChild(newSpan);
        attractions[i].appendChild(newElement);
       
        /* get all 'p' elements within the list item,
        there should only be the one we just created */
        var target = attractions[i].getElementsByTagName("p");
       
        /* removes title and alt tags from image to stop firefox/IE flicker bug */
        image[0].setAttribute("title", "");
        image[0].setAttribute("alt", "");
       
        /* assign mouseover and mouseout events */
		image[0].onmouseover = function() {
			if (typeof calculateTop != "undefined") {
				calculateTop(this);
			}
		}
		target[0].onmouseout = function() {
			if (typeof hideAll != "undefined") {
				hideAll(targets);
			}
		}
		target[0].onmouseover = function() {
			if (typeof calculateTop != "undefined") {
				calculateTop(this);
			}
		}
		image[0].onmouseout = function() {
			if (typeof hideAll != "undefined") {
				hideAll(targets);
			}
		}
        targets.push(target[0]);
    }
	targets.length > 0 ? hideAll(targets) : false;
} 

function calculateTop(obj) {
	var show = obj.parentNode.getElementsByTagName("p");
    var str = show[0].firstChild.firstChild.firstChild.nodeValue.toLowerCase();
	var lines = Math.ceil(str.length/11);
	
	/* settings for tricky icons */
	if (str == "coconut shy") { lines = 2; }
	if (str == "white elephant stall") { lines = 3; }
	
	
	var lineHeight = 14;
	var genericTop = 45;
	if (lines == 1) { var num = genericTop; }
	if (lines == 2) { var num = genericTop+lineHeight; }
	if (lines == 3) { var num = genericTop+(lineHeight*2); }
	show[0].style.top = "-"+num+"px";
	show[0].className = "";
}

/* [04] Add Your Fete
-------------------------------------------------------*/
function initPopups() {
	var container = container || document;
	var templinks = container.getElementsByTagName("a");
	for (i=0; i < templinks.length; i++) {
		if (templinks[i].className == "popup") {
			templinks[i].onclick = function() {
				return showPopUp(this);
			}
		}
	}
}

function showPopUp(obj) {
	var target = obj.getAttribute("href");
	var pagetitle = obj.getAttribute("title");
	window.open( target, "", "status=1, height=610, width=536, resizable=0" );
	return false;
}
