d = document;

/**
 * getElementById
 **/
function $(id){
	return d.getElementById(id);
}


function getStyle(element, style){
	try{
		if(document.defaultView && document.defaultView.getComputedStyle)
			return eval("d.defaultView.getComputedStyle(element, '')." + style);
		else if(element.currentStyle)
			return eval("element.currentStyle." + style);
	}catch(e){
		alert("אירעה שגיאה\n" + e.message);
	}
}

/***
 * Based on: getStyle()
 ***/
function showHideElement(element){
	element.style.display = getStyle(element, 'display')=='block' ? 'none' : 'block';
}


function addEventsAction(element, event, action, useCapture){
	if(!action)
		action = function(){};

	if(element.addEventListener)
		element.addEventListener(event, action, useCapture);
	else if(element.attachEvent)
		element.attachEvent('on' + event, action);
	else
		alert('Error Occur...');
}


/***
HTML Code example:
	<div>
		<div id="sideFlashesMarquee">
			Here is the scroller content
		</div>
	</div>

JavaScript Code example:
	$("sideFlashesMarquee").parentNode height must be set.
	$("sideFlashesMarquee") float (right or left) must be set, for right/left direction.

	var sideFlashes = new Scroller($("sideFlashesMarquee"), 'u', 100);
	sideFlashes.start()
	sideFlashes.stop()
	sideFlashes.changeDir('l')

@object: the object contained thw moving content
@direction: u=up, d=down, r-right, l=left
@tempo: the time in mili-seconds

Technical Information:
	Based on:
		addEventsAction()
***/
function Scroller(object, direction, tempo){
	var thisObj 		= this;
	this.xCoordinate 	= 0;
	this.yCoordinate 	= 0;
	this.offset			= 0;
	this.tempo			= tempo;
	this.strDirection	= direction;
	this.scrollerStatus	= true;
	this.htmlObj		= object;
	this.htmlObjWrapper	= object.parentNode;
	this.interval;

	this.htmlObj.style.position = "relative";
	this.htmlObjWrapper.style.position = "relative"; // needed to fix an overflow bug in IE7
	this.htmlObjWrapper.style.overflow = "hidden";

	this.move = function(){
		if(this.scrollerStatus){
			switch(this.strDirection){
				case 'u':	// moving up
					this.htmlObj.style.top = --this.yCoordinate + "px";

					if ((0-this.yCoordinate) > this.offset)
						this.yCoordinate = this.htmlObjWrapper.offsetHeight;

					break;
				case 'd':	// moving down
					this.yCoordinate++;
					this.htmlObj.style.top = this.yCoordinate+"px";

					if (this.yCoordinate > this.offset)
						this.yCoordinate = -this.htmlObjWrapper.offsetHeight;

					break;
				case 'r':	// moving right
					this.xCoordinate++;
					this.htmlObj.style.left = this.xCoordinate+"px";

					if (this.xCoordinate > this.offset)
						this.xCoordinate = -this.htmlObjWrapper.offsetWidth;

					break;
				case 'l':	// moving left
					this.xCoordinate--;
					this.htmlObj.style.left = this.xCoordinate+"px";

					if ((0-this.xCoordinate) > this.offset)
						this.xCoordinate = this.htmlObjWrapper.offsetWidth;
			}
		}
	}

	this.changeDir = function(dir){
		thisObj.strDirection = dir;

		if(thisObj.scrollerStatus){
			thisObj.stopScroll();
			thisObj.startScroll();
		}else{
			thisObj.startScroll();
			thisObj.stopScroll();
		}
	}

	this.start = function(){
		thisObj.offset = (thisObj.strDirection=='u' || thisObj.strDirection=='d')
			? thisObj.htmlObj.offsetHeight
				: thisObj.htmlObj.offsetWidth; // need to check this line code, maybe it's not "width"

		addEventsAction(thisObj.htmlObj, 'mouseover', function(){
			thisObj.interval = clearInterval(thisObj.interval);
		});

		addEventsAction(thisObj.htmlObj, 'mouseout', function(){
			thisObj.interval = setInterval(function() { thisObj.move(); }, thisObj.tempo);
		});

		thisObj.interval = setInterval(function() { thisObj.move(); }, thisObj.tempo);
	}

	this.stop = function(){
		thisObj.interval = clearInterval(thisObj.interval);
		//addEventsAction(thisObj.htmlObj, 'mouseout', function(){});
		//addEventsAction(thisObj.htmlObj, 'mouseover', function(){});
		thisObj.htmlObj.onmouseout = function(){};
		thisObj.htmlObj.onmouseover = function(){};
	}
}




/****************
 * Dedicated code
 ****************/

function story_commentForm_checkForm(){
	try{
		if(!$("story_commentForm_author").value){
			alert("חובה לציין שם");
			$("story_commentForm_author").focus();
			return false;
		}

		if(!$("story_commentForm_comment").value){
			alert("נא ציין את הערתך");
			$("story_commentForm_comment").focus();
			return false;
		}

		return true;
	}catch(e){
		alert("אירעה שגיאה\n" + e.message);
		return false;
	}
}

function setLayout(){
	if($('left')){
		var left_height = $('left').clientHeight;
		var maxHeight = Math.max($('right').clientHeight, $('menu').clientHeight);

		if(maxHeight > left_height)
			$('left').style.height = maxHeight + "px";
	}
}
addEventsAction(window, "load", setLayout);
window.setTimeout(setLayout, 5000);
