/* 
    TipAFriend extension for eZ publish 3.x
    Version 0.1 ( 2006-07-24 08:32 )
    
    Copyright (C) 2006  sam24.pl, Konrad Mazurkiewicz <mazkon@sam24.com.pl>, http://www.sam24.com.pl
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    
    Requirements:
        prototype.js ( http://prototype.conio.net/ )
*/

/* attach event to onload */
Event.observe(window, 'load', init, false);

/* Initialize hooks for element with id='tipafriend', so this id must be added to div with calling element */
 function init(){
    makeHook('tipafriend');
}

function makeHook(id){
        Event.observe(id, 'click', function(){showTAF($(id))}, false);  
        Event.observe(id, 'mouseover', function(){showAsHooked($(id))}, false);
        Event.observe(id, 'mouseout', function(){showAsHooked($(id), true)}, false);}

/* 'editable' is a css class for distinguish hooked element */
function showAsHooked(obj, clear){
    if (!clear){Element.addClassName(obj,'editable');}else{Element.removeClassName(obj,'editable');}
}

/* this function retrievies form content for fill */
function showTAF(obj){
    Element.hide(obj);
    if ( $(obj.id+'_form') != null ) Element.remove(obj.id+'_form');
    var tipafriend_form ='<div id="' + obj.id + '_form"></div>';
    new Insertion.After(obj, tipafriend_form);
    $(obj.id+'_form').innerHTML = 'Loading ... <img src="/extension/tipafriend/design/standard/images/progerssbar.gif" width="100" height="10" alt="" />';
    var url = '/tipafriend/tipafriend';
    var pars = 'NodeID=' + $('nodeID').innerHTML;
    var myAjax = new Ajax.Request( url, { method: 'post', parameters: pars, onComplete: showResponse });
    function showResponse(r)
	{
		//put returned code in the tipafriend_form div
		$(obj.id+'_form').innerHTML = r.responseText;
		/* In template tipafriend form threre are added id's to buttons 'tipafriend_send' and 'tipafriend_cancel' */
		Event.observe(obj.id+'_send', 'click', function(){sendTAF(obj)}, false);
        Event.observe(obj.id+'_cancel', 'click', function(){cleanUp(obj)}, false);
	}
}

/* if user clicks cancel button do some clean up 
    remove tipafriend_form div with loaded content and next add clean
*/
function cleanUp(obj, keepEditable){
    Element.remove(obj.id+'_form');
    Element.show(obj);
    if (!keepEditable) showAsHooked(obj, true);
}

/* if user fills form and clicks send button */
function sendTAF(obj){
    var success = function(t){sendComplete(t, obj);}
    var failure = function(t){sendFailed(t, obj);}
    /* prepare post variables from form fields */
    var node = $('nodeID');
    var url = '/tipafriend/tipafriend';
    var pars = 'NodeID=' + $('nodeID').innerHTML+'&SendButton='+'send'+'&YourName='+$F('YourName');
    pars += '&YourEmail='+escape($F('YourEmail'));
    pars += '&ReceiversEmail='+escape($F('ReceiversEmail'));
    pars += '&Subject='+$F('Subject');
    pars += '&Comment='+$F('Comment');
    $(obj.id+'_form').innerHTML = 'Sending ... <img src="/extension/tipafriend/design/standard/images/progerssbar.gif" width="100" height="10" alt="" />';
    var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
}

/*
    this returns information after sending mail or
    if user not fills every fields, shows warning plus form
    if warning will be returned, in template tipafriend.tpl to div with class="warning" must be added id="warning"
    for proper add hooks to buttons
*/
 function sendComplete(t, obj){
    $(obj.id+'_form').innerHTML = t.responseText;
    // if response contains element of id="warning" add hooks
    if ( $('warning') != null )
    {
        Event.observe(obj.id+'_send', 'click', function(){sendTAF(obj)}, false);
        Event.observe(obj.id+'_cancel', 'click', function(){cleanUp(obj)}, false);
    }
    else
    {
        Element.show(obj);
        showAsHooked(obj, true);
    }
}

/* this probably will happend only when ez do stupid thing :) */
 function sendFailed(t, obj){
    $(obj.id+'_form').innerHTML = 'Internal server error, please try again or inform administrator about that.';
}
