/*GLobal Variables:*/
var mailto ="m"+"a"+"i"+"l"+"t"+"o"+":";
var theLocation = "@"+"maytul"+"."+"co"+"."+"il";
var isIE6;
var isIE;
var fadeInOut;
/*Functions*/

function doar(correspondent){
   location = mailto + correspondent + theLocation + "?subject=From [Please add your name here]" ;
};

function moveElement (sElement, direction, distance) {
	var oElement = $(sElement);
	var clip = oElement.parentNode.offsetWidth;
	var strip  = oElement.offsetWidth;
	var currentDistance;
	if (!oElement.style.right) {
 		currentDistance = 0;
 	}
	else {
		currentDistance = oElement.style.right.replace(/px/i,'')*1;
	}
 	distance += currentDistance;
 	/*$('debug').innerHTML = clip + Math.abs(distance);*/
 	
	if ('next' == direction && (clip + Math.abs(distance)) < strip) { /* if offset + container's width equals element width then stop */
		oElement.tween('right', '' + distance);
	}
	else if ('prev' == direction && (distance <= 0)) {
		oElement.tween('right', '' + distance);
	}
}

function scrollElement (sElement, direction) {
	var oElement = $(sElement);
	var total_items = oElement.getElements('li').length;
	var clip = oElement.parentNode.offsetHeight;
	var strip  = oElement.offsetHeight;
	var remainder = strip - clip; 
	var step;
	var currentDistance;
	if (!oElement.style.top) {
 		currentDistance = 0;
 	}
	else {
		currentDistance = oElement.style.top.replace(/px/i,'')*1;
	}
	if ('down' == direction) {
		step = (remainder > clip)?clip:remainder;
	}
	else {
		step = (currentDistance > clip)?clip:currentDistance;
	}
 	step += Math.abs(currentDistance);
 	/*$('debug').innerHTML = clip + Math.abs(distance);*/
 	
	if ('down' == direction && remainder >= step) { /* if offset + container's width equals element width then stop */
		oElement.tween('top', '-' + step + 'px');
	}
	else if ('up' == direction && currentDistance < 0) {
		oElement.tween('top', step + 'px');
	}
}

//The right way, but then, explorer acts weird, of-cours.
function displayImg (sImg, num) {
	if ($('mainImg').src.indexOf(sImg) > -1) {
		return false;
	}
	fadeInOut = new Fx.Tween('mainImg');
	fadeInOut.start("opacity",0).chain(function() {
		$('mainImg').style.borderColor = '#9A9F90';
		$('mainImg').setProperty('src', 'Images/ajax-loader.gif').fade('in');
		newSrc = 'Projects/Fullsize/' + sImg;
		var currentImg = new Image();
		currentImg.onload = function () {

			fadeInOut.start("opacity",0).chain(function() {
				$('mainImg').style.borderColor = '#777777';
				$('mainImg').setProperty('src', currentImg.src).fade('in');
			});
			var totalThumbs = $('nav').getElements('li').each (function (item) {
				if ('imgBox' + num == item.id) {
					item.className = 'current';
				}
				else {
					item.className = '';
				}
			});
		}
		currentImg.src = newSrc;
		
	});
}


function validate(oForm) {
	try {
		if (oForm.mib.value != '') {
			return false;
		}
		var validator = new FormValidator(oForm.id);
		var errBuffer = new StringBuffer();
	}
	catch (e) {
		alert(e);
		return false;
	}
	if (validator.isEmpty('name')) {
		errBuffer.append(messages.name_empty + '\n')
	}
	if (!validator.validateMail('email')) {
		errBuffer.append(messages.email_empty + '\n')
	}
	if(!validator.validatePhone('phone')) {
 		errBuffer.append(messages.invalid_phone + '\n');
 	}
	if (errBuffer.toString() != '') {
		alert(errBuffer.toString());
		return false;
	}
	else {
		return true;
	}
}


function init () {
	detectIe();
	/* Gallery functions */
	if ($('prev')) {
		$('prev').onclick = function() {
			moveElement('nav','prev',73, 10);
		}
		$('next').onclick = function() {
			moveElement('nav','next',-73, -10);
		}
	}
	if ($('scroll_up')) {
		$('scroll_up').onclick = function() {
			scrollElement('galleries','up');
		}
		$('scroll_down').onclick = function() {
			scrollElement('galleries','down');
		}
	}
};


window.addEvent('domready', init);
