function loadLightboxEvent(oEvent)
{
	var sHref = null;
	oEvent.target = $(oEvent.target);
	
	if (oEvent.target.get('tag') == 'a')
	{
		sHref = oEvent.target.href;
	}
	if (oEvent.target.get('tag') == 'img')
	{
		sHref = $(oEvent.target).getParent().get('href');
	}
	
	loadLightbox(sHref);
	return false;
}

function sendFormToLightboxEvent(oEvent)
{
	sendFormToLightbox(oEvent.target);
	return false;
}

function loadLightbox(sUrl)
{
	var oRequest = new Request({
		method		: 'get',
		url			: sUrl,
		onSuccess	: showLightbox
	});
	oRequest.send('ajax=1');
}

function sendFormToLightbox(oForm)
{
	oForm.adopt (new Element( 'input', {name : 'ajax', value : 1, type : 'hidden'}));
	oForm.set('send', {onSuccess : showLightbox});
	oForm.send();
}

function showLightbox(sHtml)
{
	$('lightbox_content').innerHTML = sHtml;
	$('lightbox').removeClass('hidden');
	$('lightbox_overlay').removeClass('hidden');	
	initLightboxes();
	if ( typeof(initProductOrder) != 'undefined' )
	{
		initProductOrder();
	}
}

function hideLightbox()
{
	$('lightbox').addClass('hidden');
	$('lightbox_overlay').addClass('hidden');
	$('lightbox_content').innerHTML = '';
	return false;
}

function showDivForEmail ()
{
	$('lightbox_email').removeClass('hidden');
	$('lightbox_overlay').removeClass('hidden');
}
function hideDivForEmail ()
{
	$('lightbox_email').addClass('hidden');
	$('lightbox_overlay').addClass('hidden');
}

var sendmailClass = new Class ({

	initialize: function()
	{
		this.setEvents();
	},

	setEvents: function()
	{
		var oBtnSendEmail = $('sendEmail');
		
		if(oBtnSendEmail) {
			oBtnSendEmail.removeEvents();
			oBtnSendEmail.addEvent('click', this.sendEmail.create({bind : this}));
		}
	},
	
	sendEmail: function(oEvent)
	{
		var oBtn = $(oEvent.target);
		var oForm = $(oBtn.get('rel'));

		var validator = oForm.retrieve('validation');			
		if( validator!=null && !validator.doValidate() ){
			
		} else {		
			oForm.send();
			var oDiv = $('emailForm');
			oDiv.set('style', 'display:none');
			var oDiv = $('emailFormThanx');
			oDiv.set('style', 'display:block');
		}
		
		return false;
	}
	
});

// -------------------------------------------------------------------------- //

function initEmails()
{
	sendmailObj = new sendmailClass();
}

function initDiscussions()
{
	oDiscussion = $('discussion');
	if(oDiscussion){
		oDiscussion.viewport = $('discussion').getElement('.viewport')
		oScrollbar = new scrollbarClass( oDiscussion.viewport );
	}
}

function initLightboxes()
{
	$$('a.lightbox').removeEvent('click');
	$$('a.lightbox').addEvent('click', loadLightboxEvent);
	$$('form.lightbox').removeEvent('submit');
	$$('form.lightbox').addEvent('submit', sendFormToLightboxEvent);
	$('lightbox_overlay').removeEvent('click');
	$('lightbox_overlay').addEvent('click', hideLightbox);
}

initVoo = function()
{
	initEmails();
	initDiscussions();
	initLightboxes();
};

var sendmailObj, oScrollbar;

window.addEvent('domready', initVoo);
