/**
 * Dialog Box jQuery
 * http://jqueryui.com/demos/dialog/
 */

/**
 * Send to Friend
 */
function sendToFriend(title, url)
{
    // Destroy all box of the page
    $("#sendToFriend").dialog('destroy');
    $("#newsletterRegistration").dialog('destroy');
    $("#homeNotConnected").dialog('destroy');

    var idDiv = 'sendToFriend';

    createElt(idDiv, '<img style="float:left; margin-right:5px;" img src="/src/icons/silk/email_open.png" /> '+title);

    $(function() {
        $("#"+idDiv).dialog({
            bgiframe: true,
            autoOpen: false,
            minHeight: 100,
            width: 500,
            modal: true,
            resizable: false,
            show: 'blind'
        });

        $("#"+idDiv).dialog('open');

        $.ajax({
            type: 'POST',
            url : '/ajax.php',
            //url : '/ajax.php',
            //url: "/includes/box-sendtofriend.inc.php",
            data: 
            {
                cmd: 'sendToFriend',
                url: url
            },
            //data: 'url='+url,
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r)
            {
                $("#"+idDiv).html(r);
            }
        });
    });
}


/**
 * Newsletter Registration
 */
function newsletterRegistration(title, url)
{
    // Destroy all box of the page
    $("#sendToFriend").dialog('destroy');
    $("#newsletterRegistration").dialog('destroy');
    $("#homeNotConnected").dialog('destroy');

    var idDiv = 'newsletterRegistration';

    createElt(idDiv, '<img style="float:left; margin-right:5px;" img src="/src/icons/silk/email_open.png" /> '+title);

    $(function() {
        $("#"+idDiv).dialog({
            bgiframe: true,
            autoOpen: false,
            minHeight: 100,
            width: 500,
            modal: true,
            resizable: false,
            show: 'blind'
        });

        $("#"+idDiv).dialog('open');

        $.ajax({
            type: 'POST',
            url : '/ajax.php',
            data: 
            {
                cmd: 'newsletterRegistration',
                url: url
            },
            //data: 'url='+url,
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r)
            {
                $("#"+idDiv).html(r);
            }
        });
    });
}


/**
 * Newsletter Registration
 */
function homeNotConnected(title, url)
{
    // Destroy all box of the page
    $("#sendToFriend").dialog('destroy');
    $("#newsletterRegistration").dialog('destroy');
    $("#homeNotConnected").dialog('destroy');

    var idDiv = 'homeNotConnected';

    createElt(idDiv, '<img style="float:left; margin-right:5px;" img src="/src/icons/silk/email_open.png" /> '+title);

    $(function() {
        $("#"+idDiv).dialog({
            bgiframe: true,
            autoOpen: false,
            minHeight: 100,
            width: 500,
            modal: true,
            resizable: false,
            show: 'blind'
        });

        $("#"+idDiv).dialog('open');

        $.ajax({
            type: 'POST',
            url : '/ajax.php',
            data: 
            {
                cmd: 'homeNotConnected',
                url: url
            },
            //data: 'url='+url,
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r)
            {
                $("#"+idDiv).html(r);
            }
        });
    });
}

/*###########################
#                           #
#   DIALOGBOX               #
#                           #
###########################*/

// Creation d'une div pour le titre de la dialogbox
function createElt(id, title)
{
    if(!document.getElementById(id))
    {
        var popup = document.createElement("div");
    
        popup.setAttribute('id', id);
        popup.setAttribute('title', title);
    
        document.body.appendChild(popup);
    }
}


function submitIn(form, div)
{
    var thisForm = document.forms[form];    

    var cache = document.createElement("div");
    
    cache.setAttribute('id','loadCache');
    cache.setAttribute('title', 'cache');
    

    thisForm.appendChild(cache);
    
    $('#loadCache').html("<img src='/src/icons/zak/loading.gif' />");
    
    
    var data = '';
            
    for (i=0 ; i<= thisForm.length-1 ; i++)
    {
        if(thisForm[i].name != '')
            data += thisForm[i].name+'='+thisForm[i].value+'&';
    }

    
    $.ajax({
        type: "POST",
        url: thisForm.action,
        data: 'income=popup&cmd='+div+'&'+data,
        contentType : "application/x-www-form-urlencoded; charset=utf-8",
        success: function(r) // retour de la page php
        {
            $("#"+div).html(r);
            
            // OLD
            //if(r == '1')
            //{
                //showRegisterBox();
                /*if($('#baseForm').val() == "reload")
                    location.reload(true);
                else
                    document.forms[$('#baseForm').val()].submit();*/
                
            //}
            //else if(r == '0')
            //{
                //showRegisterBox();
            //}
            //else if(r == 'formAdressReload')
            //{
                //location.reload(true);
            //}
            //else
            //{
                //$("#"+div).html(r);
            //}
            // Fin OLD
        }
     });   
}
