
// helper function to do the url loading - do not edit
function mb_launchUrl(url) {
	var mb_frame = document.getElementById('mealbox_iframe');
	if(!mb_frame) mb_frame = document.getElementById(clearspring_iframe);
	if (mb_frame) {
		mb_frame.src = mb_url + 'widget.jsp?' + url;
	} else {
		var w = document.getElementById('widgetbox_widget_div_0');
		if (!w) w = document.getElementById('widgetbox_widget_div_1');
		if(w) w.innerHTML = '<iframe id="mealbox_iframe" src="' + mb_url + 'widget.jsp?' + url + '" width=300 height=500 scrolling="no" frameborder=0 style="margin:0px; padding:0px; border:0px; overflow:hidden;"> </iframe>';
	}

	if (document.all) {
		setTimeout(function(){var e = document.getElementById('mealbox_iframe'); if(e) e.src = mb_url + 'widget.jsp?' + url;},100);
	}
}

//loads the widget with a recipe detail showing
function mb_recipeDetail(id) {
	if(id) mb_launchUrl('recipe=' + id);
}

// loads the widget with a recipe search showing
function mb_recipeSearch(str) {
	if(str && str.length > 0) mb_launchUrl('search=' + escape(str));
}

// loads the widget with the coupon landing page showing
function mb_showCoupons() {
	mb_launchUrl('show=coupons');
}

// fires the video window by video id. Can specify window title.
function mb_videoWindow(id, title) {
	if(!title || title == "") title = "Featured Video";
	window.open(mb_url + '/videoLaunch.jsp?id=' + id + '&title=' + title,'video','status=yes,scrollbars=no,toolbar=no,menubar=yes,resizable=yes,width=450,height=420');
	var w = window.open(mb_url + '/videoLaunch.jsp?id=' + id + '&title=' + title,'video','status=yes,scrollbars=no,toolbar=no,menubar=yes,resizable=yes,width=450,height=420');
}

// loads the widget with a coupon detail showing
function mb_couponDetail(id) {
	if(id) mb_launchUrl('coupon=' + id);
}

//loads the widget with results from recipe landing search
function mb_recipeLanding(name) {
	if(id) mb_launchUrl('recipeGroup=' + name);
}

//loads the widget with results of recipes for promotion
function mb_recipesForPromotion(id) {
	if(id) mb_launchUrl('recipePromotion=' + id);
}

// this function will create an iframe with the widget using any "deep link" params on the current url.
// allows the loading of deep links into the context page. see local-deep.html for an example usage.
function mealboxWidgetIFrame() {
	var urlPg = '' + document.location;
	var endFile = urlPg.indexOf("?");
	var url = 'widget.jsp' + (endFile > 0? urlPg.substr(endFile) : '');
	try {url = mb_url + '/' + url;} catch(x) {}

	if (endFile > 0) {
		document.write('<iframe src="' + url + '" width=300 height=500 scrolling="no" frameborder=0 style="margin:0px; padding:0px; border:0px; overflow:hidden;"> </iframe>');
	} else {
		document.write('<script type="text/javascript" src="http://widgetserver.com/syndication/subscriber/InsertWidget.js?appId=3c469986-cc9a-4623-a0f0-43efb59a477f"></script>'
			+ '<noscript>Get the <a href="http://www.widgetbox.com/widget/mealbox">Meijer MealBox</a> widget and many other <a href="http://www.widgetbox.com/galleryhome/">great free widgets</a> at <a href="http://www.widgetbox.com">Widgetbox</a>!</noscript>');
	}
}

//this function will embed the widget from clearspring using any "deep link" params on the current url.
//allows the loading of deep links into the context page with clearspring widget.
function widgetClearspringEmbed() {
	var urlPg = '' + document.location;
	var endFile = urlPg.indexOf("?");
	var params = endFile > 0? urlPg.substr(endFile+1) : "";
	var csStr = "";

	var nvPairs = params.split("&");
	for(i=0; i < nvPairs.length; i++) {
		var nvPair = nvPairs[i]? nvPairs[i].split("=") : null;
		if(nvPair != null && nvPair[0].length > 0) csStr += nvPair[0] + "/" + nvPair[1] + "/";
	}

	var url = null;
	try {url = ''+clearspring_url;} catch(x) {};
	if(url != null && url.length > 0) {
		url = url.replace("widget.js", csStr+"widget.js");
		var s = "<div><script type='text/javascript' src='" + url + "'></script></div>";
		document.write(s);
	}
}
