// JavaScript Document

function hidelightbox(){
	removeElement('overlay');
	removeElement('alert');
}
function removeElement(id) {
	document.getElementById(id).parentNode.removeChild(document.getElementById(id));
}

function popupvideo(src,width,height) {
	
	src = src.replace('&amp;', '%26');
	src = src.replace('&', '%26');
	var changeinnerhtml = true;
	var closeheight = 29;

	htmlwidth = width;
	htmlheight = closeheight + height;
	htmlmargintop = htmlheight;
	htmlmarginleft = '-'+eval(htmlwidth/2);
	alttext = '';
	htmltext = "<a href='"+src+"' id='player' title='"+alttext+"' style='padding:0px;'></a>";

	bodytext = document.body; //document.documentElement?
	
	//get overall height of page
	headerwrapperdiv = document.getElementById('headerwrapper');
	contentfooterdiv = document.getElementById('contentfooter');
	pageheight = headerwrapperdiv.scrollHeight + contentfooterdiv.scrollHeight;
	
	// has this already been instantiated
	if(document.getElementById('overlay') == null) {
		//blue background
		overlaydiv = document.createElement("div");
		overlaydiv.setAttribute('id', 'overlay');
		overlaydiv.setAttribute('style', 'height:'+pageheight+'px');
		overlaydiv.onclick = function(){hidelightbox();return false;};
		overlaydiv.style.cssText = 'height:'+pageheight+'px';
		
		//box that shows the content
		alertdiv = document.createElement("div");
		alertdiv.setAttribute('id', 'alert');
				
		//append the new layers to the DOM
		bodytext.appendChild(overlaydiv); 
		bodytext.appendChild(alertdiv);
	}

	//has the user scrolled? if yes then change the position of the box
	if(typeof(window.pageYOffset) == "undefined") {
		scrollpos = document.documentElement.scrollTop;
	} else {
		scrollpos = window.pageYOffset;
	}
	scrollpos = scrollpos - eval(htmlheight/2);

	//Position the new layers
	alertdiv1 = document.getElementById('alert');
	alertdiv1.style.height = htmlheight+'px';
	alertdiv1.style.width = htmlwidth+'px';
	alertdiv1.style.margin = scrollpos+'px 0px 0px '+htmlmarginleft+'px';

	//create the html
	alertdiv.innerHTML = '<iframe id="alertiframe" frameborder="0" width="'+htmlwidth+'" height="'+htmlheight+'"></iframe><div id="alertoveriframe"><div id="alerttext"></div><div id="closebar"><a href="javascript:hidelightbox();"><img src="../../images/lightbox/lightbox-btn-close.gif" /></a></div></div>';

	alertoveriframediv = document.getElementById('alertoveriframe');
	alertoveriframediv.style.width = htmlwidth+"px";
	alertoveriframediv.style.height = height+"px";
	
	alerttextdiv = document.getElementById('alerttext');
	alerttextdiv.innerHTML = htmltext;
	
	// trigger flowplayer
	var player1 = flowplayer("player", {src: "../../images/flash/flowplayer-3.1.5.swf", wmode: 'opaque'}).ipad();
	document.getElementById('player').style.height = height + "px";
	document.getElementById('player').style.width = width + "px";
	document.getElementById('player').style.position = "absolute";
	document.getElementById('player').style.top = "0px";
	document.getElementById('player').style.left = "0px";
	player1.play();
}

var currentlySelectedbtn = "";

function flowplayerTabs(parentid, layer, indicator) {
	toggleparent = document.getElementById(parentid);
	for(var i=0; i<toggleparent.childNodes.length; i++) {
		if(toggleparent.childNodes[i].className && toggleparent.childNodes[i].className.search(/toggleitem/i) != -1) {
			if(toggleparent.childNodes[i].id == layer) {
				if(toggleparent.childNodes[i].style.display != "block")
				{
					toggleparent.childNodes[i].style.display = "block";	
				}
				toggleparent.childNodes[i].style.display = "block";
				thisbtn = document.getElementById(layer + "btn");
				currentlySelectedbtn = layer + "btn";
				switch(indicator) {
                    case 'src':
                        thisbtn.src = thisbtn.src.replace(/_off./gi, '_on.');
                        break;
                    case 'display':
                        thisbtn.style.display = "none";
                        break;
                    case 'class':
                        thisbtn.className = "on";
                        break;
                    default:
                        // No indicator required
                }
			} else {
				if(toggleparent.childNodes[i].style.display != "block")
				{
					toggleparent.childNodes[i].style.display = "block";	
				}
				toggleparent.childNodes[i].style.display = "none";
				thisbtn = document.getElementById(toggleparent.childNodes[i].id + "btn");
				switch(indicator) {
                    case 'src':
                        thisbtn.src = thisbtn.src.replace(/_on./gi, '_off.');
                        break;
                    case 'display':
                        thisbtn.style.display = "block";
                        break;
                    case 'class':
                        thisbtn.className = "";
                        break;
                    default:
                        // No indicator required
                }
			}
		}
	}
}

function mediaboxTabHover (stateIndicator, btnID)
{
	var thisbtn = document.getElementById(btnID);
	
	if (stateIndicator == "hoveron")
	{
		if (thisbtn.src.indexOf("_on") != -1)
		{
			currentlySelectedbtn = btnID;
		}
		else
		{
			thisbtn.src = thisbtn.src.replace(/_off./gi, '_on.');
		}
	}
	else
	{
		if (!(currentlySelectedbtn == btnID))
		{
			thisbtn.src = thisbtn.src.replace(/_on./gi, '_off.');
		}
	}
}

function toggleRegionalSubSubNav (sectionid, navelementid, parentelementid)
{
	navparent = document.getElementById('subnav');
	for (var i=0; i<navparent.childNodes.length; i++)
	{
		if (navparent.childNodes[i].id == navelementid)
		{
			navparent.childNodes[i].className = "subitemon";
			document.getElementById(parentelementid).className = "on";
		}
		else
		{
			if (navparent.childNodes[i].id != parentelementid)
			{
				navparent.childNodes[i].className = "";
			}
		}
	}
	
	toggleparent = document.getElementById('togglewrap');
	for (var i=0; i<toggleparent.childNodes.length; i++)
	{
		if (toggleparent.childNodes[i].className && toggleparent.childNodes[i].className.search(/toggleitem/i) != -1)
		{
			if (toggleparent.childNodes[i].id == sectionid)
			{
				toggleparent.childNodes[i].style.display = "block";
			}
			else
			{
				toggleparent.childNodes[i].style.display = "none";
			}
		}
	}
}

function showsinglelayer(layer, parentgroup) {
	if(document.getElementById(layer).style.display == 'block') {
		hidesinglelayer(layer, parentgroup);
	}	else {
		//$('#' + layer).fadeIn('slow');
		document.getElementById(layer).style.display = 'block';
	}
	
	var allShown = 1;
	
	$('#' + parentgroup + ' .btnOrange').each(function() {
		if($(this).is(":contains('SHOW')")) {
			$('#' + parentgroup + ' .toggleitem').each(function() {
				if($(this).css('display') != 'block')
				{
					allShown = 0;
				}
			});
			if (allShown == 1)
			{
				$(this).html('<span>HIDE ALL</span>');
			}
		} 
	});
}

function hidesinglelayer(layer, parentgroup) {
	//$('#' + layer).fadeOut('slow');
	document.getElementById(layer).style.display = 'none';
	$('#' + parentgroup + ' .btnOrange').each(function() {
		$(this).html('<span>SHOW ALL</span>');
	});
}

function showAllAnswers(parentid) {
		$('#' + parentid + ' .btnOrange').each(function() {
		if($(this).hasClass("faqAlternate")) {
			$('#' + parentid + ' .toggleitem').each(function() {$(this).fadeIn('slow');});
			$(this).removeClass("faqAlternate");
		} else {
			$('#' + parentid + ' .toggleitem').each(function() {$(this).fadeOut('slow');});
			$(this).addClass("faqAlternate");
		}
	});
}

function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

function showhide(tab) {
	for (i=1;i<=3;i++) {
		if (i==tab) {
			document.getElementById('c4lblocktab'+i).style.display = 'block';
			active(i);
		} else {
			document.getElementById('c4lblocktab'+i).style.display = 'none';
			inactive(i);
		}
	}
}

function showShortIcons()
{
	$('#shortIcons').css('display','block');
	$('#fullIcons').css('margin-bottom','-125px');
	
	$('#fullIcons').fadeOut('slow', function() {
		$('#fullIcons').css('margin-bottom','10px');
	});
	
	$('#fullIcons').css('top','-135px');
	
	$('#shortIconButton').fadeIn('slow', function() {
		// Animation complete
    });
	
	//document.getElementById("fullIcons").className = "hiddenContainer";
	//document.getElementById("shortIcons").className = "";
}

function showFullIcons()
{
	$('#shortIconButton').fadeOut('slow', function() {
    	// Animation complete.
	});
	
	$('#fullIcons').css('margin-bottom','-125px');
	
	$('#fullIcons').fadeIn('slow', function() {
		$('#shortIcons').css('display','none');
		$('#fullIcons').css('top','0px');
		$('#fullIcons').css('margin-bottom','10px');
		//$('#fullIcons').css('top','-110px');
		//$('#fullIcons').css('margin-bottom','-100px');
    });

	//document.getElementById("shortIcons").className = "hiddenContainer";
	//document.getElementById("fullIcons").className = "";
}

// Show and/or hide tabbed articles

function toggleArticleTab(selectedID, tabID)
{
	var i = 0, keepGoing = 1;
	
	while (keepGoing == 1)
	{
		var toTestId = "tabbedArticle" + i;
		
		if (document.getElementById(toTestId))
		{
			if (document.getElementById(toTestId).className == "")
			{
				document.getElementById(toTestId).className = "hiddenArticle";
			}
		}
		else
		{
			keepGoing = 0;	
		}
		
		i = i + 1;
	}
	
	keepGoing = 1;
	i = 0;
	
	while (keepGoing == 1)
	{
		var toTestId = "articleTab" + i;
		
		if (document.getElementById(toTestId))
		{
			if (document.getElementById(toTestId).className == "selectedArticleTab")
			{
				document.getElementById(toTestId).className = "unselectedArticleTab";
			}
		}
		else
		{
			keepGoing = 0;	
		}
		
		i = i + 1;
	}
	
	document.getElementById(selectedID).className = "";
	document.getElementById(tabID).className = "selectedArticleTab";
}

// If a Subnav is included on the page, set the 'current page'

function fixSubNavHighlight() {
	var subnav = document.getElementById("subnav");

	if (subnav != null)
	{
		var twoColTitle = document.getElementById("twoColTitle");
		if (twoColTitle != null)
		{
			var pageTitle = twoColTitle.innerHTML;
			pageTitle = pageTitle.replace(/^\s+|\s+$/g,'');
			pageTitle = pageTitle + "</a>";
			pageTitle = pageTitle.toLowerCase();
			
			if (pageTitle != null)
			{
				var navElements = subnav.getElementsByTagName("li");
				for (var i = 0; i < navElements.length; i++) {
					var itemContents = navElements[i].innerHTML;
					itemContents = itemContents.toLowerCase();
					
					if ((itemContents.indexOf(pageTitle)) > -1)
					{
						navElements[i].className = "on";
						navElements[i].setAttribute("class", "on");
					}
				}
			}
		}
	}
}

function showPlacesSubNav()
{
	if (document.getElementById("sightseeingbtn"))
	{
		document.getElementById("sightseeingbtn").className = "";
	}
	if (document.getElementById("attractionsbtn"))
	{
		document.getElementById("attractionsbtn").className = "";
	}
	if (document.getElementById("diningoutbtn"))
	{
		document.getElementById("diningoutbtn").className = "";
	}
}

function hidePlacesSubNav()
{
	if (document.getElementById("sightseeingbtn"))
	{
		document.getElementById("sightseeingbtn").className = "hiddenContainer";
	}
	if (document.getElementById("attractionsbtn"))
	{
		document.getElementById("attractionsbtn").className = "hiddenContainer";
	}
	if (document.getElementById("diningoutbtn"))
	{
		document.getElementById("diningoutbtn").className = "hiddenContainer";
	}
	
	if (!(document.getElementById("sightseeingbtn")) && !(document.getElementById("attractionsbtn")) && !(document.getElementById("diningoutbtn")))
	{
		document.getElementById("placesbtn").className = "hiddenContainer";
	}
}

if (window.attachEvent) {window.attachEvent('onload', fixSubNavHighlight);}
else if (window.addEventListener) {window.addEventListener('load', fixSubNavHighlight, false);}
else {document.addEventListener('load', fixSubNavHighlight, false);} 

/*Javascript functions for the EMail newsletter signup on the Home page and  overlay - 6 Dec 11 VA */

function popupForm(titleText, descrText, successText, duplicateText, failedText, urlText, urlLinkText) {
	bodytext = document.body;
	// has this already been instantiated
	if(document.getElementById('overlay') == null) {
		//blue background
		overlaydiv = document.createElement("div");
		overlaydiv.setAttribute('id', 'overlaySignUp');
		overlaydiv.setAttribute('class', 'overlaySignUp');
	    //append the new layers to the DOM
	    bodytext.appendChild(overlaydiv); 
	 }
	var htmlDiv = '<div id="continueblock" class="continueBlock" style="display: block;">';
	htmlDiv += '<div class="closebar"><a href="javascript:hideOverlayForm();"><img src="'+ ('https:' == document.location.protocol ? '../' : '/') + 'images/headers/ec_close_butn.gif" alt="Close signup" /></a></div>';
	htmlDiv += '<h1>'+ titleText +'</h1>'
	htmlDiv += '<div id="signupText" class="wrapPg"><p>' + descrText 
	htmlDiv += '  <a href="'+urlText+'" >'+urlLinkText+'</a>';
	htmlDiv += '</p></div>'
	htmlDiv += '<div class="signUpForm"><input type="text" id="txtEmail" class="txtSignUp" /><a href="javascript:parseSignUp(\''+successText+'\',\''+duplicateText+'\',\''+failedText+'\');"><img src="'+ ('https:' == document.location.protocol ? '../' : '/') + 'images/headers/ec_signup_butn.png" alt="Sign up to newsletter" id="btnSubmit" /></a></div></div>';
	overlaydiv.innerHTML = htmlDiv;		
	

}
        
function hideOverlayForm(){
	$('#continueblock').empty();            
    $('#overlaySignUp').empty();
    $('#continueblock').remove();            
    $('#overlaySignUp').remove();
}

function parseSignUp(successText, duplicateText, failedText){
	var email = '';
    email = $("#txtEmail").val();
    var theMessage = ""; 
    if (!IsValidEmail(email)){
    	alert("Invalid email address, please try again");
    } else{
	    $("#txtEmail").remove(); 
        $("#btnSubmit").remove(); 
		 $.getJSON("https://www.secure-hb.com/eurocamp-uk/forms/WebSignUp.ashx?var="+email+"&callback=?",function(data){
         	theData = data.returns.toString();
            if (theData == '1'){ messageLabel = duplicateText; }
		    if (theData == '0'){ messageLabel = successText; }
		    if (theData == '2'){ messageLabel = failedText; }                     
            $("#signupText").html("<p>"+messageLabel+"</p>");            
	 	});
    }
}
function parseSignUpHome(successText, duplicateText, failedText){
	var emailHome = '';
    emailHome = $("#txtEmailHome").val();
    if (!IsValidEmail(emailHome)){
    	alert("Invalid email address, please try again");
    } else{
	    $("#txtEmailHome").remove(); 
        $("#btnSubmitHome").remove(); 
		 // var jsonURL = "https://staging-secure/eurocamp-uk/forms/WebSignUp.ashx?var="+emailHome+"&callback=?";
		 var jsonURL = "https://www.secure-hb.com/eurocamp-uk/forms/WebSignUp.ashx?var="+emailHome+"&callback=?";
		 $.getJSON(jsonURL,function(dataHome){
         	theHomeData = dataHome.returns.toString();
            if (theHomeData == '1'){ messageLabelHome = duplicateText; }
            if (theHomeData == '0'){ messageLabelHome = successText; }
            if (theHomeData == '2'){ messageLabelHome = failedText; }
			document.getElementById('signupTextHome').style.width = "60%";
            $("#signupTextHome").html("<p>"+messageLabelHome+"</p>");       
	 	});

    }
}

function IsValidEmail(email)
{
 	var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return filter.test(email);
}


function  SetTheOnClickValidators(theMessage, description,success, duplicate, failed, urlText, urlLinkText){
	
	$('#emailHeadFunction').attr('href', "javascript:popupForm('"+theMessage+"','"+description+"','"+success+"','"+duplicate+"','"+failed+"','"+urlText+"','"+urlLinkText+"');" );
	$('#emailHeadFunction').attr('onkeypress', "javascript:popupForm('"+theMessage+"','"+description+"','"+success+"','"+duplicate+"','"+failed+"','"+urlText+"','"+urlLinkText+"');return false;" );
	$('#emailHeadFunction').click(function() {
		popupForm(theMessage, description, success, duplicate, failed, urlText, urlLinkText);
		return false;
	});

}


/* End Email newsletter sign up */

function showSignup() {
	if($('#emailHeadFunction').length && $('.showSignup').length) {
		$('.showSignup').css('cursor','pointer');
		$('.showSignup').click(function(){
			$('a#emailHeadFunction').trigger('click');
			$('html, body').animate({scrollTop:0}, 'fast');
		});	
	}
}

$(document).ready(function() {
	showSignup();
});
