/* code for scrolling and selecting the campaign list */

/* determine current mouse position */

function getMousePosition(objEvent)
{
	if (objEvent.pageX || objEvent.pageY)
	{
		return { x:objEvent.pageX, y:objEvent.pageY };
	}

	return {	x:objEvent.clientX + document.body.scrollLeft - document.body.clientLeft,
				y:objEvent.clientY + document.body.scrollTop  - document.body.clientTop };
}

var gintMousePosBeg = null;
var gintMousePosCur = null;
var gblnDragin = false;
var gintScrollOnStart = null;
var gstrLastMoveDirection = "up";

function dragStart(objThis, objEvent)
{
	gintMousePosBeg = getMousePosition(objEvent)
	gblnDragin = true;

	if (objThis.style.top)
			{ gintScrollOnStart = parseInt(objThis.style.top, 10);	}
	else	{ gintScrollOnStart = 0; }

	return true;
}

function dragStop(objThis, objEvent)
{
	if (gblnDragin)
	{
		gintMousePosCur = getMousePosition(objEvent)
		posFrom = (gintScrollOnStart - (gintMousePosBeg.y - gintMousePosCur.y));
		
		posDiff = posFrom % 75;

		if (posDiff == 0)
		{
			gblnDragin = false;
			return;
		}

		if (gstrLastMoveDirection == "down")
		{
			posTo = (posFrom - posDiff);
		}
		else
		{
			posTo = (posFrom - posDiff) - 75;
		}
		
		gblnDragin = false;

		animateMe_objThis = objThis;
		animateMe_posFrom = posFrom;
		animateMe_posTo = posTo;
		animateMe();
	}

	gblnDragin = false;
}

function dragGo(objThis, objEvent)
{
	if (gblnDragin)
	{
		var intTmp = getMousePosition(objEvent)
		var posDiff = gintMousePosBeg.y - intTmp.y;

		objThis.style.top = (gintScrollOnStart - (posDiff)) + 'px';

		if (posDiff < 0)
		{
			gstrLastMoveDirection = "down";
		}
		else
		{
			gstrLastMoveDirection = "up";
		}
	}

	return;
}

/* this function and variables scroll the objects to some steps - predefined values */

var animateMe_objThis = null;
var animateMe_posFrom = null;
var animateMe_posTo = null;

function animateMe()
{
	if (animateMe_posFrom == animateMe_posTo)
	{
		// not in animation
		return;
	}

	var newPos = null;

	if (animateMe_posFrom > animateMe_posTo)
	{
		newPos = animateMe_posFrom - 1;
	}
	else if (animateMe_posFrom < animateMe_posTo)
	{
		newPos = animateMe_posFrom + 1;
	}

	animateMe_objThis.style.top = newPos + 'px';
	animateMe_posFrom = newPos;
	
	setTimeout("animateMe()", 10);
	return;
}

/* code for showing the data about the selected campaign */

var gobjLastCampaignViewed = null;

function showCampaign(objThis)
{
	var objTemp = null;
	gobjLastCampaignViewed = objThis;

	objTemp = document.getElementById("campaign_title");
	strTitle = objThis.getAttribute('_title');

	objTemp.setAttribute('alt', strTitle);
	objTemp.src = 'text2image.php/text/' + strTitle +
					'/font/impact/size/12/txcol/224,223,227/bgcol/51,51,51/title.png';
	objTemp.setAttribute('title', strTitle);

	objTemp = document.getElementById("campaign_text");
	objTemp.innerHTML = objThis.getAttribute('_text');

	objTemp = document.getElementById("campaign_client");
	objTemp.innerHTML = objThis.getAttribute('_client');

	objThumbnails = document.getElementById("campaign_thumbnails");

	var intCounter = 0;
	var strFile = objThis.getAttribute('_thumbnail_' + intCounter);

	while (strFile != null)
	{
		objThumbnails.innerHTML = ((intCounter == 0) ? "" : objThumbnails.innerHTML) +
			"<img class='thumbimg' src='" + strFile + "' alt='more' border='0' " +
			"onclick='showSample(" + intCounter + ");'/>";

		strFile = objThis.getAttribute('_thumbnail_' + ++intCounter);

		if (strFile != null)
		{
			/* dodaj prazninu */
			objThumbnails.innerHTML = objThumbnails.innerHTML + 
				"<span>&nbsp; <span>";
		}
	}

	return true;
}

function ShowFirstCampaignOnIdle()
{
	showCampaign(document.getElementById("first_campaign"));
	return;
}

function showSample(pintIdx)
{
	var objCampaign = gobjLastCampaignViewed;
	var intSampleIdx = pintIdx;

	var intCounter = 0;
	var strFile = objCampaign.getAttribute('_sample_' + intCounter);

	/*
	while (strFile != null)
	{
		strFile = objCampaign.getAttribute('_sample_' + ++intCounter);
	}

	var intTotalCount = intCounter;
	*/

	var strCurFile = objCampaign.getAttribute('_sample_' + intSampleIdx);
	var strCurTemp = objCampaign.getAttribute('_sample_' + intSampleIdx + '_template');
	var strCurLoct = objCampaign.getAttribute('_sample_' + intSampleIdx + '_location');
	var strCurTitl = objCampaign.getAttribute('_title');

	window.open(strCurTemp + '?banner=' + strCurFile + '&loc=' + strCurLoct + '&ttle=' + strCurTitl + '&pos=' + intSampleIdx, 'samplewindow');

	// _sample_0='banners/animation_120x60_uk_dev.swf'
	// _sample_0_template='templates/nytimes/index.html'
	// _sample_0_location='A1'

	return;
}