var regex = /(.*)\/([a-zA-Z0-9-]+)/;
var url = window.location.hostname + window.location.pathname + window.location.search + window.location.hash;

var match = regex.exec(url);
if (match != null) {
	window.location = "http://" + match[1] + "/" + "#" + match[2];
}

var ref = window.location.hash.substring(1);
if (ref) {
	setPageContent(ref);
}

function setPageContent(ref) {

	// validate input
	if (typeof(ref)!='string') {  //input must be a string
		return false;
	}
	if (!is_Page(ref) && !is_HideBox(ref)) { // input must be a valid Page ref or HideBox ref
		return false;
	}
	
	// Create an XMLHttpRequest Object
	var xmlhttp;
	if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else { // code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	// get current music state
	var musicState = document.getElementById('play-state').innerHTML;

	// fill the page content
	xmlhttp.open("GET","?ref="+ref+"&piece=page",false);
	xmlhttp.send();
	document.getElementById('page').innerHTML=xmlhttp.responseText;

	// set page's title
	set_title(ref);
	
	// reset music state
	document.getElementById('play-state').innerHTML = musicState;
	
	// content was updated, set url hash to identify current page
	window.location.hash=ref;
	if (ref != 'see-more') {
		window.scrollTo(0,0);
	}
}

function is_Page(ref) {

	var allowable_refs = 	[	
				'contact-us',
				'see-more',
			 	'home',
				'maffe',
				'marinades',
				'our-founder',
				'raves',
				'recipes',
				'sitemap',
				'terms-and-conditions',
				'the-company',
				'where-to-buy',
				'yassa'
				];

	for (i=0; i<allowable_refs.length; i++) {
	
		if (ref === allowable_refs[i]) {
			return true;
		}
		
	}
	
	return false;

}

function set_title(ref) {
	if ( ref == 'home' || ref == 'see-more' ) {
		document.title = 'Savor the Flavors of Africa at Your Own Kitchen Table, African Cooking Made Easy';
	}
	else if ( ref == 'the-company') {
		document.title =  'Make Delicious African Meals with Our Authentic Simmer Sauces & Jerk Pastes';
	}
	else if ( ref == 'our-founder') {
		document.title =  'Jainaba Jeng, Founder of Kitchens of Africa, Shares Her Love of African Cooking';
	}
	else if ( ref == 'yassa') {
		document.title =  'Yassa for Chicken, Meat, Seafood, Tofu & Vegetables, Proprietary Spice Blend from Kitchens of Africa';
	}
	else if ( ref == 'maffe') {
		document.title =  'Maffé Sweet, Spicy, Tangy, Peanut Sauce from West Africa, Simmer with Meat, Seafood, Tofu, Veggies, etc.';
	}
	else if ( ref == 'marinades') {
		document.title =  'African Jerk Pastes for Meat, Poultry, Seafood or Tofu. Mild, Spicy or Fiery. Grill, BBQ, Bake or Roast';
	}
	else {
		document.title =  'Kitchens of Africa';
	}
}

function displayHideBox(ref) { 
	var e = document.getElementById('HideBox_content');
			
    if(e.style.display=="none") { // if the HideBox is currently hidden

		// validate input
		if (typeof(ref)!='string') {  //input must be a string
			return false;
		}
		if (!is_HideBox(ref)) { // input must be a valid hidebox ref
			return false;
		}
		
		// Create an XMLHttpRequest Object
		var xmlhttp;
		if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else { // code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}

		// fill the HideBox content
		xmlhttp.open("GET","?ref="+ref+"&piece=ref",false);
		xmlhttp.send();
		e.innerHTML=xmlhttp.responseText;
				
		// show the HideBox
		e.style.display="block"; 
		document.getElementById("HideBox_background").style.display="block";

		//HideBox is shown, set url hash to identify current HideBox
		window.location.hash= ref;
		document.getElementById("body").style.overflow="hidden";

		centerHidebox();

    } else { // if the HideBox is currently shown

		// hide the HideBox
        e.style.display="none"; 
        document.getElementById("HideBox_background").style.display="none";

		//HideBox is hidden, set url hash to back to the calling page
		window.location.hash= ref;
		document.getElementById("body").style.overflow="visible";
    }
}

function centerHidebox() {
	var e = document.getElementById('HideBox_content').firstChild;
	var winHeight = document.body.clientHeight;
	var picHeight = e.clientHeight;
	if (picHeight < winHeight) {
		e.style.marginTop = Math.round((winHeight-picHeight)/2) + "px";
	}
}

function is_HideBox(ref) {

	var allowable_refs = 	[	
				'thai-inspired-peanut-dipping-sauce',
				'maffe-as-a-marinade',
				'slow-cooker-peanut-meatballs',
				'chicken-and-peas-in-orange-peanut-cream-sauce',
				'lamb-chops-in-spicy-pineapple-coconut-peanut-sauce',
				'oven-baked-peanut-coconut-chicken',
				'shrimp-in-spicy-peanut-cilantro-coconut-sauce',
				'pan-seared-tofu-in-orange-peanut-cream-sauce',
				'vegetarian-peanut-coconut-curry-sauce',
				'penne-in-spicy-sausage-peanut-cream-sauce',
				'french-inspired-mustard-cream-sauce',
				'yassa-as-a-marinade',
				'weekend-special-authentic-chicken-yassa',
				'african-salmon-baked-in-foil-packets',
				'chicken-in-tarragon-mushroom-mustard-cream-sauce',
				'grilled-lamb-chops-with-caramelized-onion-sauce',
				'baked-honey-mustard-chicken-with-peppers-and-olives',
				'shrimp-in-spicy-coconut-mustard-onion-sauce',
				'slow-cooker-sweet-potato-orange-chicken-yassa',
				'chicken-thighs-in-dijon-mustard-orange-sauce',
				'yassa-cooking-tips',
				'maffe-cooking-tips',
				'marinades-cooking-tips',
				'yassa-ingredients',
				'maffe-ingredients',
				'marinades-ingredients'
				];

	for (i=0; i<allowable_refs.length; i++) {
	
		if (ref === allowable_refs[i]) {
			return true;
		}
		
	}

	return false;

}

function switchPlayState() {
	if (document.getElementById('play-state').innerHTML === 'Music Off') {
		musicOff();
		setCookie('music', 'off', 30);
	document.getElementById('play-state').innerHTML = 'Music On';
	}
	else {
		musicOn();
		setCookie('music', 'on', 30);
		document.getElementById('play-state').innerHTML = 'Music Off';
	}
}

function musicOn() {
	getPlayer('player').playMusic();
}
	
function musicOff() {
	getPlayer('player').stopMusic();
}

function getPlayer(movieName) {
	if (Math.floor(ie_version()) == 7) { // if ie7
		return window[movieName];
	} 
	else {
		return document[movieName];
	}
}

function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name) {
			return unescape(y);
		}
	}
}

function ie_version() {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 		var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 		return ieversion;
 	}
 	else
 		return 0;
}

function pageLoaded() {
	setPageContent(window.location.hash.substring(1));
	if (document.getElementById('play-state').innerHTML === 'Music On') {
		musicOff();
	}
}

function validateEmailForm() {
	if (!isEmail(document.forms["contactUs"]["email"].value)) {
		alert('Please input a valid e-mail address');
		return false;
	}
	else {
		alert('Thank you for your comment! Please allow 24-48 hours for a response.');
		return true;
	}
	 
}

function isEmail(str) {
	var atpos=str.indexOf("@");
	var dotpos=str.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=str.length) {
  		return false;
  	}
  	else {
		return true;
  	}
}

