/*
 * Animation functions for the Red boxes at the bottom.
 * 
 */

//prefix for pages
var pagePrefix = "page_";
//prefix for menu buttons
var menuPrefix = "menu_";
//prefix for other elements that control content
var specialPrefix = "special_";
var redboxPrefix = "redbox_";
var guestbookURL = "http://www.limburgsewallen.nl/guestbook/";

//array with special tags
var  arrSpecialTags= new Array("movie", "gastenboek", "contact", "route", "dedames","depersoverons");

function initPages() {
	var pagesDivs = getPages();
	var menuItems = getMenuItems();
	var redboxItems = getRedboxItems();
	
	initPageVisibility(pagesDivs, true);
	linkPages(pagesDivs, menuItems);
	linkRedBoxes(pagesDivs, redboxItems);
	
}

function getPages() {
	//select both normal and special pages
	var items = $("div[id^='" + pagePrefix + "']").add("div[id^='" + specialPrefix + "']"); 
	return items;	
}

function getMenuItems() {
	var items = $("li[id^='" + menuPrefix + "']").add("a[id^='" + menuPrefix + "']"); 
	return items;
}

function getRedboxItems() {
	var items = $("li[id^='" + redboxPrefix + "']"); 
	return items;
}

//hides all page divs except the first
function initPageVisibility(pagedivs, blnShowHome) {
	pagedivs.each(		 
		// loop iteration index on the current element.
		function( intIndex ){
			var strHomeID = pagePrefix+"home";
			if ((blnShowHome == true) && ($(this).attr('id') == strHomeID) ) {	
				//hide all layers except the first one
  				$(this).hide();
  				resizePage(strHomeID);
				$(this).fadeIn("slow");
			} else {
				$(this).hide();
			}
  		});
	return true;
}

//checks if an ID is a known special tag
function checkIfSpecialTag(theID) {
	return arrSpecialTags.contains(theID);
}

//hook the menu items up with a function to show the right page
function linkPages(pageDivs, menulistItems) {
	//link menu (and button) with pages
	pageDivs.each(
		// loop iteration index on the current element.
		function( intIndex ) {
			
			//get ID of menu item - add one because of home button
			var arrSplit = $(this).attr("id").split("_"); //match(/_[\d]+$/);
			var pageID		= arrSplit[1];
			//var pageID	= $(this).attr("id");
			var isSpecial = 'false';
			var currentMenuLink = null;
			var targetID = null;
			
			if ( checkIfSpecialTag(pageID) ) {
				//set isSpecial to true and use specialPrefix
				isSpecial = 'true';
				//check if rondleiding button
				if ( $("#" + menuPrefix + pageID).is("a") ) {
					currentMenuLink = $("#" + menuPrefix + pageID);
				} else {
					currentMenuLink = $("#" + menuPrefix + pageID).find('a');
				}
				targetID = specialPrefix + pageID;
			} else {
				//isSpecial stays false  and use pagePrefix 
				//select anchortag element of the (non-special) menu item
				//currentLink = $("#" + menuPrefix + pageID).find('a');
				currentMenuLink = $("#" + menuPrefix + pageID).find('a');
				targetID = pagePrefix + pageID;
				
			}
						
			currentMenuLink.click(
				function() {
					initPageVisibility(pageDivs, false);
					resizePage(targetID);
					showPage(targetID, pageDivs, isSpecial);
					return false;
				}
			);			
	});		
}

function linkRedBoxes(pageDivs, redBoxes) {
	redBoxes.each(
			// loop iteration index on the current element.
			function( intIndex ) {
				
				//get ID of menu item - add one because of home button
				var arrSplit = $(this).attr("id").split("_"); //match(/_[\d]+$/);
				var redboxID = arrSplit[1];
				var isSpecial = 'false';
				var currentRedBoxDiv = null;
				var targetID = null;
				
				//get the div
				currentRedBoxDiv = $("#" + redboxPrefix + redboxID).find('div');
				var pageID = currentRedBoxDiv.attr("id");
				
				//only proceed if redbox has a pageID to link too 	
				if ((pageID.length > 0) && (redboxID.length > 0) ) {
					if ( checkIfSpecialTag(pageID) ) {
						//set isSpecial to true and use specialPrefix
						isSpecial = 'true';	
						targetID = specialPrefix + pageID;
					} else {
						//isSpecial stays false  and use pagePrefix 
						targetID = pagePrefix + pageID;
						
					}
				
					currentRedBoxDiv.click(
						function() {
							initPageVisibility(pageDivs, false);
							resizePage(targetID);
							showPage(targetID, pageDivs, isSpecial);
							return false;
						}
					);	
				} else {
					currentRedBoxDiv.click(
							function() {
								//do nothing
							});
				}
		});		
}

function resizePage(targetID) {
	//event.preventDefault(); 
	var pageDiv = $('#' + targetID);
	var pageDivPadding = parseInt(pageDiv.css("padding-bottom").replace("px",""));
	var pageDivHeight = pageDiv.height()+pageDivPadding;
	//IE hack: no minimum height so set it here!
	//var leftDiv = $('.middlebar_left');
	/*if (pageDivHeight < leftDiv.height()) {
		pageDivHeight = leftDiv.height();
	}*/
	if (pageDivHeight < 300) {
		pageDivHeight = 300;
	}
	$("#pagecontent").animate({height:pageDivHeight});	
}

//this function performs the "flipping" actually the fading and showing of pages				
function showPage(targetID, pageDivs, special) {
	var pageDiv = null;

	//first hide - both special and normal - pages
	$("div[id^='page_']").fadeOut('fast', function() {
		//animation complete
	});
	$("div[id^='special_']").fadeOut('fast', function() {
		//animation complete
	});

	pageDiv = $("#" + targetID);	
	
	if (pageDiv) {
		var targetPage = "#" + targetID;
		var moviePage = "#" + specialPrefix + "movie";
		var guestbookPage = "#" + specialPrefix + "gastenboek"; 
		if (targetPage == moviePage) {
			switchMovieContent(moviePage,'true');
		} else if (targetPage == guestbookPage) {
//			var guestbookLayer = "#gastenboek_div";
//			var guestbookFrame = "#gastenboek_frame";
//			switchGuestbookContent(guestbookLayer, guestbookFrame, 'true');
		}else {
			switchMovieContent(moviePage,'false');
		}			
		
		pageDiv.fadeIn('slow', function() {
		//animation complete
		});	
	}
}

function switchGuestbookContent(guestbookLayer, guestbookFrame, fill) {	
	if (fill === 'true') {
		$(guestbookFrame).src(guestbookURL).appendTo(guestbookLayer);
	} else {
		$(guestbookFrame).html('');
	}
	
}

function switchMovieContent(pageID, fill) {
	if (fill ==='true') {
		$(pageID).html(
			'<div style="align:center;width:550px;height:328px;overflow:hidden;">'+
			'<!--[if !IE]> <-->' +
			'<object type="application/x-shockwave-flash" width="550" height="328"' +
			'data="http://www.youtube.com/v/UfIE3CANFrA?rel=0&loop=1&autoplay=1&fs=1&amp;hl=nl_NL">' +
			'<!--> <![endif]-->' +
			'<!--[if IE]>' +
			'<object type="application/x-shockwave-flash" width="550" height="328"' +
			'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
			'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">' +
			'<![endif]-->' +
			'<param name="movie" value="http://www.youtube.com/v/UfIE3CANFrA?rel=0&loop=1&autoplay=1&fs=1&amp;hl=nl_NL" />' +
			'<p>Your browser is not able to display this multimedia content.</p>' +
			'</object>' +
			'</div>'
		);	
	} else {
		$(pageID).html('');
	}
		
	
}
