/**

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("a", { id:"navclose", href:"javascript:;", "onclick":"DisposeActivityPopin();" }) ).adopt(
						$$(	new Element("div", { "class":"close" }) )
					)					
				)
				,
				$$(	new Element("h1", { html:"L&auml;gg till nytt motionspass" }) )
				,
				$$(	new Element("div", { id:"act-content" }) )
				,
				$$(	new Element("div", { id:"act-options" }) ).adopt( 
					$$(	new Element("a", { id:"act-cancel", "href":"#", "class":"mbutton custom", "style":"margin-right: 196px;" }) ).adopt(
						$$(	new Element("span", { html:"Avbryt" }) )
					)
					,
					$$(	new Element("a", { id:"act-save", "href":"#", "class":"mbutton custom" }) ).adopt(
						$$(	new Element("span", { html:"Spara" }) )
					)
				)
			
		)
	);
		
	$('act-save').addEvent('click', function(e) {
		e.stop();
		SaveActivity();
	});
					
	$('act-cancel').addEvent("click", function(e) {
		e.stop();
		if($('activity-popin')){
			$('activity-popin').fade(0);
			DisposeActivityPopin();
		}
	});
	
	Cufon.replace('h1, .mbutton.custom');
	
	rePositionActivityPopin();
	GetActivityContent(actID, steps, customName);
}

function SaveActivity()
{
	$('act-err').set('html','');

	var isInvalid = 0;
	/**must be alphanumerical if( !CheckIfValidCustom('act-name','alphabetical','act-err','N&aring;got gick fel n&auml;r du valde motionspass.', false) ) isInvalid++;*/
	if( !CheckIfValidCustom('act-time','numerical','act-err','Du har inte skrivit in en korrekt l&auml;ngd f&ouml;r motionspasset.', false) ) isInvalid++;
	if( !CheckIfValidCustom('act-weight','numerical','act-err','Din vikt &auml;r inte korrekt.', false) ) isInvalid++;
	if( !CheckIfValidCustom('act-energy','numerical','act-err','Energiv&auml;rdet &auml;r felaktigt.', false) ) isInvalid++;
	if( !CheckIfValidCustom('act-date','date','act-err','Du har fyllt i ett felaktigt datum.', false) ) isInvalid++;
	if( !CheckIfValidCustom('act-notes','alphabetical','act-err','Du har anv&auml;nt ogiltiga tecken i kommentaren.', true) ) isInvalid++;

	if(isInvalid > 0) return;
	
	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;*/
	
	if($('activity-popin'))
		$('activity-popin').fade(0);
							
	doAjaxJSON(
		URL,
		"name="			+ $('act-name').value
		+ "&refactid="	+ $('ref-act-id').value
		+ "&minutes="	+ $('act-time').value
		+ "&weight="	+ $('act-weight').value
		+ "&energy="	+ $('act-energy').value
		+ "&date="		+ $('act-date').value 
		+ "&comment="	+ $('act-notes').value,
		/**+ "&mood="		+ $('act-mood').selectedIndex*/
		/**+ "&steps="		+ $('act-steps').value*/
		/**+ "&actid="		+ $('act-id').value*/
		/**+ "&level="		+ $('act-level').selectedIndex */
		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();
}

function SelectActivity(actRow)
{
	var containers = $(actRow).getChildren('span');
	var nameArr = containers[0].getChildren('span.row-name');
	var energyArr = containers[1].getChildren('span.row-energy');
	var id = containers[2];
	
	$('act-name').set('value', nameArr[0].get('html'));
	$('ref-energy').set('value', energyArr[0].get('html'));
	$('ref-act-id').set('value', id.get('html'));
	
	ToggleActInfo();
}

function CalculateEnergy()
{
	var time = $('act-time').get('value').replace(",",".");
	var weight = $('act-weight').get('value').replace(",",".");
	var refEnergy = $('ref-energy').get('value').replace(",",".");
	
	if(time == '' || weight == '' || refEnergy == '') return;
		
	if(weight < 60) weight = 60;
	else if(weight > 110) weight = 110;
	
	var weightFactor = weight / 90;
	
	$('act-energy').set('value', Math.floor(refEnergy / 60 * time * weightFactor));
}

function ToggleActInfo(showId)
{	
	if(showId)
	{
		$('info-container').setStyle('display','block');
		
		$('info-container').getChildren('div').setStyle('display','none');
	
		$(showId).setStyle('display','block');
		
		var margin;
		
		if(showId == 'info-time')
			margin = '208px';
		else if(showId == 'info-weight')
			margin = '272px';
		else if(showId == 'info-energy')
			margin = '337px';
		else
			margin = '0';
		
		$('ico-pointer').setStyle('margin-left',margin);	
	}
	else
		$('info-container').setStyle('display','none');
}
/*
function ToggleActEmoticons(setSelectedEl)
{
	$('act-emotions').getChildren('a').removeClass('selected');
	
	$(setSelectedEl).addClass('selected');
}*/
