window.addEvent('domready',function() {
	
	console.log($$('#tellFriend, #tellFriend-j'));
	
	$('taf-modal').setStyles({
		opacity:0,
		display:'block'
	});

	$('taf-close').addEvent('click',function(e) { $('taf-modal').fade('out'); });
	
	window.addEvent('keypress',function(e) { if(e.key == 'esc') { $('taf-modal').fade('out'); } });	
	
	$('taf-table').addEvent('click', function(e) { 
		console.log('event stopped');
		e.stop();
	});
	
	$(document.body).addEvent('click',function(e) { 
		if($('taf-modal').get('opacity') == 1 && !e.target.getParent('.generic_dialog')) { 
			$('taf-modal').fade('out'); 
		} 
	});

	$$('#tellFriend, #tellFriend-j').each(function(item){
		console.log('jdhfgskjdfhkdsjkh');
		item.addEvent('click',function() {
			tafTable = $('taf-table').retrieve('form-body');		
			if( $defined(tafTable) ) {
				tafTable.replaces($('form-body'));
			}
			$('taf-modal').fade('in');			
		});
	}); 
	
	var req = new Request({
		url: "/taf/index.php",
		//url: "http://genkirobot.com/taf/index.php",
		onSuccess: function(text){
			
			var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
			text = (match) ? match[1] : text;

			var container = new Element('div');

			var html = $try(function(){
				var root = '<root>' + text + '</root>', doc;
				if (Browser.Engine.trident){
					doc = new ActiveXObject('Microsoft.XMLDOM');
					doc.async = false;
					doc.loadXML(root);
				} else {
					doc = new DOMParser().parseFromString(root, 'text/xml');
				}
				root = doc.getElementsByTagName('root')[0];
				for (var i = 0, k = root.childNodes.length; i < k; i++){
					var child = Element.clone(root.childNodes[i], true, true);
					if (child) container.grab(child);
				}
				return container;
			}) || container.set('html', text);
			
			$$('#form-body td').fade('show');
			$('formsubmit').removeClass('formprocessing');
			tafTable = $('taf-table');
			formBody = $('form-body').dispose();
			tafTable.store('form-body', formBody);
			tafTable.adopt(html.getChildren());
			$('taf-close').addEvent('click',function(e) { $('taf-modal').fade('out'); });
			if (html.hasChild('ok_errors_button')) {
				console.log('ok, an error happened');
				$('ok_errors_button').addEvent('click',function(e) { 
					//$('taf-modal').fade('out');
					$('message-body').dispose();
					tafTable.adopt(tafTable.retrieve('form-body'));
					});
			} else {
				$('okbutton').addEvent('click',function(e) { $('taf-modal').fade('out'); });				
			}
		},
		onFailure: function(){
			console.log('form post failed');
		}
	});

	$('send').addEvent('click', function(e){
		e.stop();
		$$('#form-body td').fade(.3);
		$('formsubmit').addClass('formprocessing');
		(function(){ 
			req.send("actionFlow=sendFeedback&senderName="+$('name').get('value')+"&senderMailAddress="+$('from').get('value')+"&toEmail="+$('to').get('value')+"&senderMessage="+$('message').get('value')+"&langPref="+$('langPref').get('value'));			
			}).delay(1000);
	});
		
	$$('#tellFriend, #tellFriend-j').each(function(item){
		console.log('jdhfgskjdfhkdsjkh');
		item.addEvent('click',function() {
			tafTable = $('taf-table').retrieve('form-body');		
			if( $defined(tafTable) ) {
				tafTable.replaces($('form-body'));
			}
			$('taf-modal').fade('in');			
		});
	}); 
	
		
});
