/**

POPIN FUNCTIONALITY

*/

var myActivity = null,
    MaxScaleValue = 4000,
    lastMove = 0;

window.addEvent("resize", function() {
	rePositionActivityPopin();
});

function rePositionActivityPopin() {
	if ($("popin-wrapper"))
		$("popin-wrapper").setStyle("height", window.getScrollSize().y.toInt());
	if ($("activity-popin"))
		$("activity-popin").setStyle("left", window.getSize().x/2 - $("activity-popin").getStyle("width").toInt()/2);
}

function GetActivityContent(actID, steps, customName)
{
	var winHeight = window.getScrollSize().y.toInt();
	
	var URL = "/GI/Activities/util_/ActivityPopin/ContentHandler.aspx?element=editactivity";
	
	return doAjaxJSON(
		URL,
		"activityID=" + actID + "&steps=" + steps + "&customName=" + customName,
		SetContent, 
		"act-content");
}

function createActivityPopin(actID, steps, customName)
{
	$(document.body).adopt(
		$$(
			new Element("div", 
				{
					id:"popin-wrapper",
					styles: { "height": window.getScrollSize().y.toInt() }
				}
			)
		),
		popin = 
			new Element("div", { id:"activity-popin" }).adopt(
				$$(	new Element("div", { id:"act-header" }) ).adopt(
					$$(	new Element("h1", { html:"L&auml;gg till nytt motionspass", "style":"float:left;margin-left:140px;" }) )
					,
					$$(	new Element("a", { id:"navclose", href:"javascript:;", "onclick":"DisposeActivityPopin();" }) ).adopt(
						$$(	new Element("div", { "class":"close" }) )
					)					
				)
				,
				$$(	new Element("div", { id:"act-content" }) )
				,
				$$(	new Element("div", { id:"act-options" }) ).adopt( 
					$$(	new Element("a", { id:"act-cancel", "href":"#" }) ).adopt(
						$$(	new Element("div", { "class":"intro-button", html:"Avbryt" }) )
					)
					,
					$$(	new Element("a", { id:"act-save", "href":"#" }) ).adopt(
						$$(	new Element("div", { "class":"intro-button" }) ).adopt(
							$$(	new Element("a", { "href":"#", html:"Spara &raquo;" }) )
						)
					)
				)
			
		)
	);
	
	
	$('act-save').addEvent("click", function(e) {
		if($('activity-popin')){
			$('activity-popin').fade(0);
			SaveActivity();
		}
	});
	$('act-cancel').addEvent("click", function(e) {
		e.stop();
		if($('activity-popin')){
			$('activity-popin').fade(0);
			DisposeActivityPopin();
		}
	});
	
	rePositionActivityPopin();
	GetActivityContent(actID, steps, customName);
}

function SaveActivity()
{
	var URL = "/GI/Activities/util_/ActivityPopin/DataInterface.aspx?action=saveActivity";
	
	var name = "";
	if($('act-name').value != null && $('act-name').value != "")
		name = $('act-name').value;
	else
		name = $('act-type').value;
	
	doAjaxJSON(
		URL,
		"name="			+ name
		+ "&actid="		+ $('act-id').value
		+ "&steps="		+ $('act-steps').value
		+ "&level="		+ $('act-level').selectedIndex 
		+ "&minutes="	+ $('act-time').value
		+ "&mood="		+ $('act-mood').selectedIndex
		+ "&comment="	+ $('act-notes').value
		+ "&date="		+ $('act-date').value ,
		DisposeActivityPopin, 
		null);
}

function deleteActivity(exerciseID)
{
	var URL = "/GI/Activities/util_/ActivityPopin/DataInterface.aspx?action=deleteActivity";
		
	doAjaxJSON(
		URL,
		"exerciseID=" + exerciseID,
		function() { window.location.reload(); }, 
		null);
}

function DisposeActivityPopin()
{
	DisposeActivityPopin(null);
}
function DisposeActivityPopin(response)
{
	$('activity-popin').dispose();
	$('popin-wrapper').dispose();
	
	if(response != null)
		window.location.reload();
}

function CalculateSteps(level, time)
{
	if(level == 0)
		return $('constant-steps-per-min-low').value * time;
	else if(level == 1)
		return $('constant-steps-per-min-medium').value * time;
	else if(level == 2)
		return $('constant-steps-per-min-high').value * time;
}

function CalculateTime(steps)
{
	return Math.round(steps / $('constant-steps-per-min-medium').value);
}

function InitActivityPopin()
{
	GetActivityContent();
}