/**
 * Yafim JS scripts
 * 
 * @version 1.1.1 20/02/2008 - Fixed reportPic bug
 */

/** Some global vars */
var mosaic,commentNavigator;
var complaintXHR = null;
var canComplain = true;

function initYafim(initNavigator,pageNum,numPerPage)
{
	if ($('mosaic') != null)
	{
		mosaic = new Mosaic('mosaic', 160, 320, 2);
		mosaic.setItems(mosaicItems);
		mosaicItems = null;
	}

	if (initNavigator)
	{
		CommentNavigator.prototype = new PageNavigator;
		commentNavigator = new CommentNavigator('comments', numPerPage, 2);

		commentNavigator.secondNavigator = $('topNavigator');
		commentNavigator.jumpElement = $('comments_numReport');

		var commentsCountElement = $('comments_count');
		if (commentsCountElement == null) return;
 
		var numComments = parseInt(commentsCountElement.innerHTML);
		if (isNaN(numComments) || numComments == 0)	return;
	
		commentNavigator.setNumItems(numComments);
		commentNavigator.setPage(pageNum);

		// If we're getting the lastComment - scroll to it!
		var url = new String(window.location);
		if (url.indexOf('lastComment') != -1)
		{
			// get last comment
			var comments = document.getElementsByName('yafimComm');
			var lastComment = comments[comments.length - 1];
			var pos = getRealOffset(lastComment);
			window.scrollTo(0, pos.y);
		}
	}
}

function delPic(id)
{
	if (confirm('האם למחוק תמונה זו?'))
	{
		doPostBack('delPic=1&id='+id);
	}
}

function reportPic(id)
{
	if (!canComplain)
	{
		alert('כבר התלוננתם');
		return;
	}
	if (confirm('?האם להודיע להנהלת האתר על תמונה זאת'))
	{
		if (complaintXHR == null) complaintXHR = new XHR();
		var postInfo = 'type=yafim&sid='+id;
		complaintXHR.Post('complaint.php',postInfo,reportPicResult);
		$('complaintSpan').innerHTML = 'התלונה נשלחת...';
	}
}
function reportPicError(errorText)
{
	alert(errorText);
	$('complaintSpan').innerHTML = "מרגיש שתוכן זה פוגע בך? דווח לנו!";
}
function reportPicResult(responseText)
{
	if (responseText != 'ok')
	{
		reportPicError(responseText);
		return;	
	}

	$('complaintSpan').innerHTML = 'התלונה נשלחה בהצלחה';
	canComplain = false;
}

// Main page - highlighting rows
function getCell(evt)
{
	var target = getEventTarget(evt);
	var tagName;
	while (target.tagName) 
	{
		tagName = target.tagName.toUpperCase();
		if (tagName == 'TD') return target;
		if (tagName == 'TR') return null;
		target = target.parentNode;
	}
	return null;
}
function getParentRow(cell)
{
 	var parentRow = cell;
 	do
 	{
 		parentRow = parentRow.parentNode;
 	}
 	while (!parentRow.tagName || parentRow.tagName != 'TR');
 	return parentRow;
}
function setColor(parentRow,color)
{
 	// Get all cells of this row
 	var children = parentRow.childNodes;
 	for (var i=0;i<children.length;i++)
 	{
 		if (children[i].tagName && children[i].tagName == 'TD')
 			children[i].style.backgroundColor = color;
	}
}
function highlight(evt)
{
	var cell = getCell(evt);
	if (!cell) return;
	var parentRow = getParentRow(cell);
 	if (parentRow.id && parentRow.id == 'yafimHeader') return; 
	setColor(parentRow,'#dce0e2');
}
function dim(evt)
{
	var cell = getCell(evt);
	if (!cell) return;
	var parentRow = getParentRow(cell);
 	if (parentRow.id && parentRow.id == 'yafimHeader') return;
	if (parentRow.id == 'yafimRow1') setColor(parentRow,'transparent');
	else setColor(parentRow,'#FFFFFF');
}

/** Gallery comment navigator object */
function CommentNavigator(elementIdPrefix,numPerPage,pageSpan)
{
	var myself = this;
	PageNavigator.apply(this,arguments);
	/** Throttles refresh requests */
	this.timer = null;
	this.canRefresh = true;
	
	this.request = new XHR(15000);
	this.serverScript = window.location;
	
	this.request.SetErrorFunc(setWindowCursorDefault)

	this.onPageRequest = function(pageNum)
	{
		var postInfo = 'getComm=1&page='+(pageNum+1);
		this.request.Post(this.serverScript,postInfo,this.onRequestResult,pageNum);
		setWindowCursorWait();
	}

	this.onRequestResult = function(responseText,pageNum)
	{
		PageNavigator.prototype.onRequestResult.call(myself,responseText,pageNum,'comments_count');
		$('comments_numReport').innerHTML = (myself.numItems > 0) ? myself.numItems : '&nbsp;';
		setWindowCursorDefault();
		pageTracker._trackPageview('/yafim/commentPaging');
	}
	
	this.refresh = function()
	{
		if (!this.canRefresh) return;
		this.canRefresh = false;

		if (this.curPage == -1)	this.requestPage(0);
		else this.requestPage(this.curPage);

		$('mentos').style.color = '#5E5E5E';
		this.timer = setTimeout(this.allowRefresh,5000);
	}
	
	this.allowRefresh = function()
	{
		myself.canRefresh = true;
		$('mentos').style.color = '#E69B0B';
	}

	this.delItem = function(id)
	{
		if (confirm('האם למחוק הערה זו?'))
		{
			var postInfo = 'delComm=1&id='+id+'&page='+(this.curPage+1);
			this.request.Post(this.serverScript,postInfo,this.onRequestResult,this.curPage);
		}
	}
}