var http_request = false;
var a = null;
var b = null;
subNavSlider = null;
subNav2Slider = null;
subNavContainer = null;
subNav2Container = null;
var navWidth = 0;
var subWidth = 0;

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') { 
			c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function menuSwapIn(e) {
	e.className = e.id + "Image_over";
}
function menuSwapOut(e) {
	e.className = e.id + "Image";
}

function menuOver(e) {
	e.style.backgroundPosition = "left bottom";
}
function menuOut(e) {
	e.style.backgroundPosition = "";
}

function rssXOver() {
	document.getElementById("rssClose").className = "rssCloseOver";
}
function rssXOut() {
	document.getElementById("rssClose").className = "rssCloseUp";
}




function changePic(i) {
	document.body.className = "bg" + i;
//	document.cookie="beanersBG=bg" + i + "; path=/";
	createCookie("beanersBG","bg" + i,365);
}

function setName(sName) {
	if(sName != "" && sName != "null") {
//		alert("name: " + sName);
		createCookie("beanersName",sName,365);
	} 
}

function getName() {
	sN = readCookie("beanersName");
	if (sN == null) {
		return "null"; 	
	} else {
		return sN;
	}
}

function setBoardBg() {
	bc = document.body.className;
//	document.getElementById("board").changeMarkExternal(bc.substr(2,1));
	return bc.substr(2,1);
}

function hideEmail(name, domain, display) {
	displayed=(typeof(display)=="undefined") ? name+"@"+domain : display 
	document.write('<a href=mailto:' + name + '@' + domain + '>' + displayed + '</a>');
}

function init() {
	
	subNavSlider = (document.getElementById("menu_subNav")) ? document.getElementById("menu_subNav") : document.getElementById("subNav");  // small hack since the orange from the original artwork was technically the wrong color
	subNavContainer = document.getElementById("subNavContainer");
	subNav2Slider = document.getElementById("subNav2");
	subNav2Container = document.getElementById("subNav2Container");
	
	aImages = null;
	aSubs = null;

	switch(pageSection) {
		case "menu":
			navWidth = 460;
			aImages = new Array();
			break;
			
		case "b-heard":
			navWidth = 333;
			aImages = new Array();
			break;
			 
		case "franchise":
			aImages = new Array();
			break;

		case "shop":
			navWidth = 498;
			aImages = new Array("/images/subLinkCoffee_over.png","/images/subLinkGiftCards_over.png","/images/subLinkMerchandise_over.png","/images/subLinkTea_over.png");
			break;
	}
	
	switch (subSection) {
		case "b-creative":
			subWidth = 296;
			aSubs = new Array();
			aImages = aImages.concat(aSubs);
			break;
			
		case "about":
			subWidth = 470;
			aSubs = new Array();
			aImages = aImages.concat(aSubs);
			break;
			
		case "theMenu":
			subWidth = 678;
			aSubs = new Array();
			aImages = aImages.concat(aSubs);
			break;
	}

//	if(navWidth > 0) {
	if(subNavSlider) {
		slideSubNav();
	}
	
	if(subNav2Slider) {
		slideSubNav2();	
	}

//	imagePreloadin(aImages);
	
	setTimeout('setBoardBg()',1000);
}

function imagePreloadin(aImgs) {
	images = new Array("/images/linkBHeard_over.png","/images/linkMenu_over.png","/images/linkShop_over.png","/images/linkLocations_over.png","/images/linkFranchise_over.png");

	if(aImgs != null) {
		images = images.concat(aImgs);
	}
	
	imageObj = new Array();
	for(i in images) {
		imageObj[i] = new Image();
		imageObj[i].src = images[i];
	}

}

function slideSubNav() {
	if (a != null) {
		a.stop();
		a = null;
	}

	navWidth = parseInt(subNavSlider.style.width);
	
	a = new Accelimation(subNavContainer.style, "width", navWidth, 600, .8, "px");
	a.onend = onSlideEnd;
	a.onframe = slideSub;
	a.start();
}

function slideSub(x) {
	subNavContainer.style.width = x + "px";
//	subNavSlider.style.left =  x + "px";
}

function onSlideEnd() {
	a.stop();
	a = null;
}

function slideSubNav2() {
	if (b != null) {
		b.stop();
		b = null;
	}

	subWidth = parseInt(subNav2Slider.style.width);
	
	b = new Accelimation(subNav2Container.style, "width", subWidth, 600, .8, "px");
	b.onend = onSlide2End;
	b.onframe = slideSub2;
	b.start();
}

function slideSub2(x) {
	subNav2Container.style.width = x + "px";
}

function onSlide2End() {
	b.stop();
	b = null;
}

function xmlRequest() {

	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Cannot create an XMLHTTP instance');
		return false;
	} else {
		return true;	
	}
}

function locationLookup(list) {
	if(list == 2) return;
	if(document.f.location_state.selectedIndex == 0) {
		cities = document.f.location_city;
		for(i=0; i<cities.length; i++) cities.options[i] = null;
		cities.length = 0;
		
		addresses = document.f.location_address;
		for(i=0; i<addresses.length; i++) addresses.options[i] = null;
		addresses.length = 0;

		return;
	}

	if(xmlRequest()) {
		http_request.onreadystatechange = updateLocationLists;
		http_request.open('POST', "/locationLookup.php", true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		var pInfo = "state=" + document.f.location_state.options[document.f.location_state.selectedIndex].value;
		if(list == 1)
			pInfo += "&city=" + document.f.location_city.options[document.f.location_city.selectedIndex].value;
		
		http_request.send(pInfo);
	}
}

function updateLocationLists() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			try {
				x = new ActiveXObject("Microsoft.XMLDOM");
				x.async = false;
				x.loadXML(http_request.responseText);
			} catch (e) {
				x = http_request.responseXML;
			}

			var opts = x.getElementsByTagName("city");
            
			if(opts.length > 0) {
				cities = document.f.location_city;
				for(i=0; i<cities.length; i++) cities.options[i] = null;
				cities.length = 0;
	
				i = 0;
				while(i<opts.length) {
					opt = opts.item(i);
					cities.options[i] = new Option(opt.firstChild.data,opt.attributes[0].value,false,false);
					i++;
				}
			}

			opts = x.getElementsByTagName("address");
            
			addresses = document.f.location_address;
			for(i=0; i<addresses.length; i++) addresses.options[i] = null;
			addresses.length = 0;

			i = 0;
			while(i<opts.length) {
				opt = opts.item(i);
				addresses.options[i] = new Option(opt.firstChild.data,opt.attributes[0].value,false,false);
				i++;
			}

		} else {
			alert('There was a problem with the request.');
		}
	}
}

function sendBStatement(str) {
	
	//alert("B - " + str);
}

function detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}


function showFeed() {
//	$("rssContainer").className = "rssVis";			
//	new Ajax.Request("/rssLoader.php", {
//		method: "post",
//		onSuccess: function(transport) {
//			$("rssContent").innerHTML = transport.responseText;
//		}
//	});

	if(xmlRequest()) {
		http_request.onreadystatechange = feedsLoaded;
		http_request.open('POST', "/rssLoader.php", true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send();
	}

}

function feedsLoaded() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById("rssContent").innerHTML = http_request.responseText;
			document.getElementById("rssContainer").className = "rssVis";
		} else {
			alert('RSS Feed is unavailable at this time.');

		}
	}
	
}

function hideFeed() {
//	$("rssContainer").className = "rssHidden";	
	document.getElementById("rssContainer").className = "rssHidden";
//	$("rssContainer").hide();

/*
	 var d = detectMacXFF();
	 if (d) {
		document.getElementById("rssContainer").className = "rssHidden";
		}else{
		new Fx.Style(div, 'opacity', {duration: 500} ).start(0);
		}
*/
}


function beanersLog (vars) {
	console.log(vars);
}


