/*  
Project:  tell a friend lightbox
Autor:    Sindre Wimberger / wimberger@echonet.at
Created:  23.07.2007
Modified: 

 */

var taf = {
	stat : false,
	init: function(){		
		taf.starter = $('recommendword');
		if (taf.starter) {
			var objBody = document.getElementsByTagName("body").item(0);	
				var objOverlay = document.createElement("div");
				objOverlay.setAttribute('id','tafoverlay');
				objOverlay.style.display = 'none';
				Element.setOpacity(objOverlay, 0.8);
				objOverlay.onclick = function() { taf.end(); }
				objBody.appendChild(objOverlay);
				var objTaf = document.createElement("div");
				objTaf.setAttribute('id','taf');
				objTaf.style.display = 'none';
//				objTaf.onclick = function(e) {	// close Lightbox is user clicks shadow overlay
//					if (!e) var e = window.event;
//					var clickObj = Event.element(e).id;
//					if ( clickObj == 'lightbox') {
//						taf.end();
//					}
//				};
				objBody.appendChild(objTaf);
			Event.observe(taf.starter, 'click', taf.start);
//			$('mailto').show();
		}
	},
	start: function(event){
		taf.url = taf.starter.href;
        Event.stop(event);
		myLightbox.end();
		hideSelectBoxes();
		hideFlash();
		var arrayPageSize = getPageSize();
		Element.setHeight('tafoverlay', arrayPageSize[1]);
		new Effect.Appear('tafoverlay', { duration: 1, from: 0.0, to: 0.8 });
		var arrayPageScroll = getPageScroll();
		var boxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		Element.setTop('taf', boxTop);
		new Ajax.Request(taf.url, {
			method: 'get',
			onException: function(r,e) {
			  taf.message(e);
			},
			onComplete: function(resp) {
					$('taf').update(resp.responseText);
						Element.show('taf');
						Event.observe('recommend', 'submit', taf.send);
						Event.observe('sendbutton', 'click', taf.send);
						Event.observe('closebutton', 'click', taf.end);					
			},
			onFailure: function() {
			  taf.message("Übertragung fehlgeschlagen. Bitte versuchen Sie es noch einmal.");
			}
		});
	},
	send: function(event){
        Event.stop(event);
		if(taf.stat) return;
		taf.stat = true;	
        f = $('recommend');
//		var params = Form.serialize(f);
		var params = "fromname="+Form.Element.getValue('fromname')+ "&fromemail="+Form.Element.getValue('fromemail')+"&toname="+Form.Element.getValue('toname')+"&toemail="+Form.Element.getValue('toemail')+"&text="+Form.Element.getValue('text')+"&sent=1";
		Element.hide('taf');
		new Ajax.Request(taf.url, {
			method: 'post',
			parameters: params,
			onComplete: function(resp) {
					$('taf').update(resp.responseText);
						Element.show('taf');
						if($('recommend')) {Event.observe('recommend', 'submit', taf.send);}
						if($('sendbutton')) {Event.observe('sendbutton', 'click', taf.send);}
						Event.observe('closebutton', 'click', taf.end);					
						taf.stat = false;	
			},
			onFailure: function() {
			  taf.message("Übertragung fehlgeschlagen. Bitte versuchen Sie es noch einmal.");
 			  taf.stat = false;	
			}
		});
	},
	end: function(){
		Element.hide('taf');
		new Effect.Fade('tafoverlay', { duration: 0.5});
		showSelectBoxes();
		showFlash();
		$('taf').update('');
		taf.stat = false;	
	},
	// Error Klasse
	message: function(msg){
		alert(msg);
	}	
}

addEvent(window,'load',function(){taf.init();});
