function toggleFeature(obj,singleFeature) {
	var list = obj;
	var tag = '';
	while (tag != 'LI') {
		list = list.parentNode;
		tag = list.tagName;
	}

	if (singleFeature == 1) {

		if (obj.parentNode.parentNode.className.indexOf('open') != -1) { return false; }

		divs = $$('.content');
		for (c=0; c<divs.length; c++) {
			if (divs[c].parentNode.className.indexOf('open') != -1) {
				cdiv = divs[c];
				new Effect.BlindUp(cdiv, {
					duration: 0.5,
					scaleX: false,
					afterFinish: function() {
						cdiv.parentNode.className = cdiv.parentNode.className.replace("open", "closed");

				divs = list.getElementsByTagName('div');
				for (c = 0; c < divs.length; c++) {
					if (divs[c].className.indexOf('content') != -1) {
						cdiv = divs[c];
					}
				}

				new Effect.BlindDown(cdiv, {
					duration: 0.5,
					scaleFrom: 0,
					scaleTo: 100,
					scaleX: false,
					beforeStart: function() {
						list.className = list.className.replace("closed", "open");
						cdiv.style.display = 'none';
					}
				});

					}
				});
			}
		}
	} else {

		divs = list.getElementsByTagName('div');
		for (c = 0; c < divs.length; c++) {
			if (divs[c].className.indexOf('content') != -1) {
				cdiv = divs[c];
			}
		}

		if (!cdiv) { return false; }

		if (list.className.indexOf('closed') != -1) {
			new Effect.BlindDown(cdiv, {
				duration: 1,
				scaleFrom: 0,
				scaleTo: 100,
				scaleX: false,
				beforeStart: function() {
					list.className = list.className.replace("closed", "open");
					cdiv.style.display = 'none';
				}
			});
		} else {
			new Effect.BlindUp(cdiv, {
				duration: 1,
				scaleX: false,
				afterFinish: function() {
					list.className = list.className.replace("open", "closed");
				}
			});
		}
	}
}

var switchProductPicture = function(url) {
	// preload the image
	img = new Image();
	img.src = url;
	img.alt = "";

	$('mainPicturePlaceholder').innerHTML = '';
	$('mainPicturePlaceholder').appendChild(img);
}

var Alf = window.Alf || {};

Alf.previousProductPictures = function() {
	var thumbsList = $('productThumbnails');
	thumbsList.hide();
	var mainContainer = $$('div.thumbsContainer')[0];
	var thumbsListBottomDistance = parseInt(thumbsList.style.bottom);
	var thumbsListHeight = thumbsList.getHeight();
	var incrementWith = 165;
	var thumbsListRemainingDistance = (thumbsListBottomDistance + incrementWith);

	if(thumbsListBottomDistance > 0)
	{
		thumbsList.style.bottom = (thumbsListBottomDistance - incrementWith) + 'px';
	}

	Effect.toggle('productThumbnails', 'appear');
}

Alf.nextProductPictures = function() {
	var thumbsList = $('productThumbnails');
	thumbsList.hide();
	var mainContainer = $$('div.thumbsContainer')[0];
	var thumbsListBottomDistance = parseInt(thumbsList.style.bottom);
	var thumbsListHeight = thumbsList.getHeight();
	var incrementWith = 165;
	var thumbsListRemainingDistance = thumbsListHeight - thumbsListBottomDistance;

	if(thumbsListRemainingDistance >= incrementWith)
	{
		thumbsList.style.bottom = (thumbsListBottomDistance + incrementWith) + 'px';
	}

	Effect.toggle('productThumbnails', 'appear');
}

Alf.openPlayerWindow = function(flvURL, containerID, sizeWidth, sizeHeight) {
    $('recommendToFriend').hide();
    $('homepageVideo').show();
	$('overlayer').show();
	Utils.embedSWF({url:flvURL, container:containerID, width:sizeWidth, height:sizeHeight});
}

Alf.recommendToFriend = function() {
    $('homepageVideo').hide();
    $('recommendToFriend').show();
    $('yourName').value = "";
	$('friendEmail').value = "";
	$('yourName').removeClassName('highlighted');
	$('friendEmail').removeClassName('highlighted');
	$('overlayer').show();
}

Alf.closePlayerWindow = function() {
	$('overlayer').hide();
	$('yourName').value = "";
	$('friendEmail').value = "";
	$('yourName').removeClassName('highlighted');
	$('friendEmail').removeClassName('highlighted');
}

Alf.sendWebsiteLink = function() {
	var yourName = trim($('yourName').value);
	var friendEmail = trim($('friendEmail').value);
	var _hasErrors = false;
	
	if(yourName == "") {
		$('yourName').addClassName('highlighted');
		_hasErrors = true;
	}
	else {
		$('yourName').removeClassName('highlighted');
	}
	
	if(friendEmail == "" || isValidEmail(friendEmail) == false) {
		$('friendEmail').addClassName('highlighted');
		_hasErrors = true;
	}
	else {
		$('friendEmail').removeClassName('highlighted');
	}
	
	if(_hasErrors == false) {
		new Ajax.Request(WEBSITE_URL + 'Helpers/SendWebsiteLinkToFriend/', {
			method:'post',
			parameters: {
				name:yourName,
				email:friendEmail
			},
			onComplete:function(transport) {
				var _response = transport.responseJSON;
				
				if(_response.okMessage) {
					Alf.closePlayerWindow();
					alert(_response.okMessage);
				} else if(_response.errors) {
					var errors = '';
					
					if(_response.errors.name) {
						errors += _response.errors.name;
						$('yourName').value = '';
						$('yourName').addClassName('highlighted');
					}
					
					if(_response.errors.email) {
						errors += _response.errors.email;
						$('friendEmail').value = '';
						$('friendEmail').addClassName('highlighted');
					}
					
					alert('Va rugam corectati erorile de mai jos si incercati din nou:' + "\n\n" + errors);
				}
				else if(_response.other) {
					alert(_response.other);
				}
			}
		});
	}
}

var trim = function(string) {
	return string.replace(/^[ ]+/, "").replace(/[ ]+$/, "");
}

var isValidEmail = function(addr) {
	if(addr == '') {
		return false;
	}
	else {
		if(!addr.match(/^(((?:[a-zA-Z0-9]+)(?:[\.\-_])?(?:[a-zA-Z0-9]+))@((?:[a-zA-Z0-9\-]+)(?:(?:\.(?:[a-zA-Z0-9\-]+)+)?\.(?:[a-zA-Z0-9]{2,4}\.)?(?:[a-zA-Z0-9]{2,4}))))$/i)) {
			return false;
		}
	}
	
	return true;
}

