
window.onload=template_init;
function template_init(){
    channel_init();
}

var tf_http_req = false;

function makeReq(url,outputHere) {
	tf_http_req = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		tf_http_req = new XMLHttpRequest();
		if (tf_http_req.overrideMimeType) {
			tf_http_req.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			tf_http_req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			tf_http_req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!tf_http_req) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	tf_http_req.onreadystatechange = outputReq;
	tf_http_req.open('GET', url, true);
	tf_http_req.send(null);

	function outputReq() {
		try {
			if (tf_http_req.readyState == 4) {
				if (tf_http_req.status == 200) {
					var responce = tf_http_req.responseText;
					var outputDiv = document.getElementById(outputHere);
					outputDiv.innerHTML = responce;
				} else {
					alert('There was a problem with the request.');
				}
			}
		}
		catch( e ) {
			alert('Caught Exception: ' + e.description);
		}
	}
}


var thisDay=new Date().getDay();
var myDays= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var thisDayTxt=myDays[thisDay];

function mpNav(){
	var anchors = document.getElementById("menuPlanner").getElementsByTagName("a"); 
	thisDayTxt=myDays[thisDay];
	document.getElementById("theDay").innerHTML= thisDayTxt;
	
	for(i=0;i< anchors.length;i++){
		if (thisDay == 0){
			anchors[0].href = mpArray[6];
			anchors[0].onclick = function(){
				makeReq(mpArray[6],'mpOutput');
				mpNavFirst();
				return false;
			}
		} else {
			anchors[0].href = mpArray[thisDay - 1];
			anchors[0].onclick = function(){
				makeReq(mpArray[thisDay - 1],'mpOutput');
				mpNavDown();
				return false;
			}
		}	
		if (thisDay == 6){
			anchors[1].href = mpArray[0];
			anchors[1].onclick = function(){
				makeReq(mpArray[0],'mpOutput');
				mpNavLast();
				return false;
			}
		} else {
			anchors[1].href = mpArray[thisDay + 1];
			anchors[1].onclick = function(){
				makeReq(mpArray[thisDay + 1],'mpOutput');
				mpNavUp();
				return false;
			}
		}
	}
}

function mpNavUp(){
	thisDay = thisDay + 1;
	mpNav();
}
function mpNavDown(){
	thisDay = thisDay - 1;
	mpNav();
}
function mpNavFirst(){
	thisDay = thisDay + 6;
	mpNav();
}
function mpNavLast(){
	thisDay = thisDay - 6;
	mpNav();
}

function tfPrintRecipe(){
	var head = document.getElementsByTagName('head').item(0)
	style = document.createElement('link');   
	style.type = 'text/css';
	style.rel = 'stylesheet';
	style.media = 'print';
	style.href = 'http://cdn.channel.aol.com/_media/channels/us.cooking/cooking-recipe-print.css';
	head.appendChild(style);
	window.print();
}
