
var URLHistoryHash = new Hash();
var cal = null;

window.addEvent("resize", function() {
	rePositionIntroPopin();
});

function rePositionIntroPopin() {
	if ($("popin-wrapper"))
		$("popin-wrapper").setStyle("height", window.getScrollSize().y.toInt());
	if ($("intro-container"))
		$("intro-container").setStyle("left", window.getSize().x/2 - $("intro-container").getStyle("width").toInt()/2);
}

function addToIntroHistory(url, target) {
	if (typeof url !== "string" && typeof target !== "string")
		return;

	URLHistoryHash.set(url, target);
}

function goToIntroHistory() {
	var url = lurl = target = ltarget = null;
	URLHistoryHash.each(function(key, value) {
		if (url) {
			lurl = url;
			ltarget = target;
		}
		url = value;
		target = key;
	});
	if (lurl !== null) {
		URLHistoryHash.erase(url);
		GetContent(ltarget);
		/**doAjaxJSON(
			"/util_/IntroPopin/ContentHandler.aspx?element=" + ltarget,
			"",
			SetContent, 
			"intro-content");*/
	}
}

function ToggleCalendar(targetField)
{
	cal = new CalendarPopup('cal-field')
	cal.setTodayText("");
	
	if($('cal-container').getStyle('display') == 'none')
	{
		$('cal-container').setStyle('display','block');
		cal.select(
			$(targetField),
			'cal-link',
			'yyyy-MM-dd',
			new Date()
			);
			
		$('cal-container').getElements('a.cpCurrentMonthDate').addEvent('click', function(){ $('cal-container').setStyle('display','none'); });
		$('cal-container').getElements('a.cpOtherMonthDate').addEvent('click', function(){ $('cal-container').setStyle('display','none'); });
		$('cal-container').getElements('a.cpTodayText').addEvent('click', function(){ $('cal-container').setStyle('display','none'); });

		return false;
	}
	else
	{
		$('cal-container').setStyle('display','none');
	}
}
function InitiateTutorialPopin()
{
	var winHeight = window.getScrollSize().y.toInt();
	
	if($('popin-container'))
		$('popin-container').set('html',
						"<div id=\"popin-wrapper\" style=\"height:" + winHeight + "px;\"></div>"
						+ "<div id=\"intro-container\">"
							+ "<div id=\"intro-header\">"
								+ "<div id=\"intro-back\">"
								+ "</div>"
								+ "<div class='title'>V&auml;lkommen!</div>"
								+ "<a id='navclose' href='javascript:;' onclick='DisposeTutorialPopin();'>"
									+ "<div class='close'>St&auml;ng f&ouml;nster</div>"
								+ "</a>"
							+ "</div>"
							+ "<div id=\"intro-content\"></div>"
						+ "</div>");
					
	rePositionIntroPopin();
	GetContent('welcome');
}

function DisposeTutorialPopin()
{
	$('popin-container').set('html','');
}

function DisposeTutorialPopinStart(r, CallbackArgs)
{
	$('popin-container').set('html','');
	
	if(CallbackArgs)
		window.location = CallbackArgs;
	
}

function GetGoals(r, CallbackArgs)
{
	GetContent('goals');
}

function GetConfirmationOrRecommendation(r, CallbackArgs)
{
	GetContent('conformationOrRecommendation');
}

function GetContentStart(response, contentName)
{
	GetContent(contentName, -1);
}
function GetContent(contentName, phaseNumber)
{
	if(contentName == 'welcome')
	{
		$('intro-back').set('html','');
		$('intro-back').setStyle('background','');
	}
	else
	{
		$('intro-back').set('html', "<a id='navback' href='javascript:;' onclick='goToIntroHistory();'>Tillbaka</a>");
		$('intro-back').setStyle('background',"transparent url('/images/intropopin/back.gif') no-repeat left");
	}
	
	var URL = "/util_/IntroPopin/ContentHandler.aspx?element=" + contentName;
	var args = null;
	
	if(contentName == 'overview' && $('recipeURL') && $('hallkollURL') && $('healthURL') && $('programURL') && $('activitiesURL') && $('chatURL'))
		URL += "&recipeURL=" + $('recipeURL').get('value')
			+ "&hallkollURL=" + $('hallkollURL').get('value')
			+ "&healthURL=" + $('healthURL').get('value')
			+ "&programURL=" + $('programURL').get('value')
			+ "&activitiesURL=" + $('activitiesURL').get('value')
			+ "&chatURL=" + $('chatURL').get('value');
	
	if(contentName == 'recommendations' && $('phaseOneStartURL') && $('phaseTwoStartURL') && $('phaseThreeStartURL'))
		URL += "&phaseOneStartURL=" + $('phaseOneStartURL').get('value')
			+ "&phaseTwoStartURL=" + $('phaseTwoStartURL').get('value')
			+ "&phaseThreeStartURL=" + $('phaseThreeStartURL').get('value');

	if(phaseNumber > 0)
		URL += "&phase=" + phaseNumber;

	doAjaxJSON(
		URL,
		null,
		SetContent, 
		"intro-content");
		
	rePositionPopin();
	addToIntroHistory(contentName,contentName);
}

function ToggleVideo(chapter)
{
	var URL = "/util_/IntroPopin/ContentHandler.aspx?element=video&chapter=" + chapter;
	
	doAjaxJSON(
		URL,
		null,
		SetContent, 
		"welcome-screencast");
}

function StartProgram(phaseNumber, phaseURL)
{	
		doAjaxJSON(
			"/util_/IntroPopin/DataInterface.aspx?form=saveFinalize&phase=" + phaseNumber, 
			null, 
			DisposeTutorialPopinStart, 
			phaseURL);
}

function SubmitMeasurements()
{
	doAjaxJSON(
		"/util_/IntroPopin/DataInterface.aspx?form=saveMeasurements", 
		"weight=" + $('intro-weight').value 
		+ "&waist=" + $('intro-waist').value 
		+ "&length=" + $('intro-length').value, 
		GetGoals, 
		null);
}
function SubmitGoals()
{
	return doAjaxJSON(
		"/util_/IntroPopin/DataInterface.aspx?form=saveGoals", 
		"targetweight=" + $('intro-targetweight').value 
		+ "&targetwaist=" + $('intro-targetwaist').value 
		+ "&targetdate=" + $('intro-targetdate').value, 
		GetConfirmationOrRecommendation, 
		null);
}

function TogglePopinIsActivated()
{
	TogglePopinIsActivated(null);
}
function TogglePopinIsActivated(target)
{
		doAjaxJSON(
		"/util_/IntroPopin/DataInterface.aspx?form=tooglePopin",
		"isActivated=" + $('popin-is-activated').checked,
		new function() { window.location = target }, 
		null);
}

function rePositionPopin() {
	if ($("popin-wrapper"))
		$("popin-wrapper").setStyle("height", window.getScrollSize().y.toInt());
	if ($("intro-container"))
		$("intro-container").setStyle("left", window.getSize().x/2 - $("intro-container").getStyle("width").toInt()/2);
}

function printIntroPopinContent()
{
	$("page-container").set("class", "NoPrint");
	$("popin-wrapper").set("class", "NoPrint");
	$("intro-header").set("class", "NoPrint");
	
	window.print();	
}

function addExpandableBoxes(container)
{
	/**$(container).each('input').addEvent('onfocus',\"this.setStyle('height','100px');\");*/
}

function SuggestTargetDate(startWeight)
{
	var targetWeight = $('intro-targetweight').value;
	var weightDiff = startWeight - targetWeight;
	
	var kilosPerDay = 0.7/7;
	
	var daysNeeded = weightDiff / kilosPerDay;
	
	var currentTime = new Date()
	var year	= currentTime.getFullYear();
	var month	= currentTime.getMonth();
	var cDay	= currentTime.getDate();
	
	var day = cDay + daysNeeded;
	
	while(day > 28)
	{
		if(month < 12)
			month = month + 1;
		else
		{
			month = 1;
			year  = year + 1;
		}
		
		day = Math.round(day - 28);
	}
	
	$('intro-targetdate').value = year + "-" + month + "-" + day;
}