/**
 * @author Wrzasq <wrzasq@gmail.com>
 * @copyright 2008 - 2009 (C) by Wrzasq
 * @package WrzasqCMF
 * @version 0.0.1
 */

// pagination creation
Element.addMethods( {
    paginate: function(element, count, current, href, closure) {
        element = $(element);
        
        // first page link
        if(current > 1)
        {
            element.insert( new Element("a", {
                href: href + "1",
                rel: "start"
            } ).update("«").observe("click", closure(1) ) ).insert(" ");
        }

        // previous page link
        if(current > 2)
        {
            element.insert( new Element("a", {
                href: href + (current - 1),
                rel: "prev",
                rev: "next"
            } ).update("‹").observe("click", closure(current - 1) ) ).insert(" ");
        }

        var stop = current + 5;
        stop = stop > count ? count : stop;

        var start = current - 5;
        start = start < 1 ? 1 : start;
        start = start + 5 > stop ? start - (start + 5 - stop) : start;

        // page links
        for(var i = start; i <= stop; ++i)
        {
            // skips pre-first pages
            if(i < 1)
            {
                continue;
            }

            // no more pages
            if(i > count)
            {
                break;
            }

            // current page
            if(i == current)
            {
                element.insert( new Element("span").update(i) );
            }
            // pager link
            else
            {
                element.insert( new Element("a", {
                    href: href + i
                } ).update(i).observe("click", closure(i) ) );
            }

            element.insert(" ");
        }

        // next page link
        if(current < count - 1)
        {
            element.insert( new Element("a", {
                href: href + (current + 1),
                rel: "next",
                rev: "prev"
            } ).update("›").observe("click", closure(current + 1) ) ).insert(" ");
        }

        // last page link
        if(current < count)
        {
            element.insert( new Element("a", {
                href: href + count
            } ).update("»").observe("click", closure(count) ) );
        }

        return element;
    }
} );

// dynamic forms
Element.addMethods("form", {
    render: function(element, source) {
        element = $(element);
    
        // form elements container
        var table = new Element("table");
        var tfoot = $( table.createTFoot() );
        var tbody = new Element("tbody");

        // temporary variables
        var field;
        var input;
        var cell;
        var row;
        var label;
        var j;
    
        // common settings
        element.action = source.action;
        element.method = source.method;

        // form title
        if(source.title)
        {
            $( table.createCaption() ).update(source.title);
        }

        // submit button
        input = new Element("input", {
            type: "submit",
            value: source.submit
        } );

        cell = $( tfoot.insertRow(-1).insertCell(-1) ).insert(input);
        cell.colSpan = 2;
    
        // elements ID
        if(source.id)
        {
            element.id = source.id;
        }
    
        // CSS class
        if(source.class)
        {
            element.addClassName(source.class);
        }
    
        // form fields
        for(var i in source.fields)
        {
            // current field
            field = source.fields[i];
            
            // new table row for field
            row = $( tbody.insertRow(-1) );
            
            if(field.label)
            {
                field.label += ":";
            }
            
            switch(field.component)
            {
                // form break
                case "FormSpacer":
                    // message text
                    label = new Element("p").update(field.message);

                    // elements ID
                    if(field.id)
                    {
                        label.id = field.id;
                    }
                    
                    // CSS class
                    if(field.class)
                    {
                        label.addClassName(field.class);
                    }
                    
                    cell = $( row.insertCell(-1) ).update(label);
                    cell.colSpan = 2;
    
                    break;
                    
                case "TextField":
                    // label cell
                    if(field.label)
                    {
                        cell = $( row.insertCell(-1) );
                        
                        // label for field
                        if(field.id)
                        {
                            cell.update( new Element("label", {
                                "for": field.id
                            } ).update(field.label) );
                        }
                        // just a text
                        else
                        {
                            cell.update(field.label);
                        }
                    }
                    
                    // creates value cell
                    cell = $( row.insertCell(-1) );
                    
                    // without label cell is spaned to entire table
                    if(!field.label)
                    {
                        cell.colSpan = 2;
                    }
                    
                    input = new Element("input", {
                        type: "text",
                        name: field.name
                    } );
                    
                    if(field.value)
                    {
                        input.value = field.value;
                    }
                    
                    // element ID
                    if(field.id)
                    {
                        input.id = field.id;
                    }
                    
                    // CSS class
                    if(field.class)
                    {
                        input.addClassName(field.class);
                    }
                    
                    cell.update(input);
                    
                    break;
                    
                case "PasswordField":
                    // label cell
                    if(field.label)
                    {
                        cell = $( row.insertCell(-1) );
                        
                        // label for field
                        if(field.id)
                        {
                            cell.update( new Element("label", {
                                "for": field.id
                            } ).update(field.label) );
                        }
                        // just a text
                        else
                        {
                            cell.update(field.label);
                        }
                    }
                    
                    // creates value cell
                    cell = $( row.insertCell(-1) );
                    
                    // without label cell is spaned to entire table
                    if(!field.label)
                    {
                        cell.colSpan = 2;
                    }
                    
                    input = new Element("input", {
                        type: "password",
                        name: field.name
                    } );
                    
                    if(field.value)
                    {
                        input.value = field.value;
                    }
                    
                    // element ID
                    if(field.id)
                    {
                        input.id = field.id;
                    }
                    
                    // CSS class
                    if(field.class)
                    {
                        input.addClassName(field.class);
                    }

                    cell.update(input);
                    
                    break;
                    
                case "MemoField":
                    // label cell
                    if(field.label)
                    {
                        cell = $( row.insertCell(-1) );
                        
                        // label for field
                        if(field.id)
                        {
                            cell.update( new Element("label", {
                                "for": field.id
                            } ).update(field.label) );
                        }
                        // just a text
                        else
                        {
                            cell.update(field.label);
                        }
                    }
                    
                    // creates value cell
                    cell = $( row.insertCell(-1) );
                    
                    // without label cell is spaned to entire table
                    if(!field.label)
                    {
                        cell.colSpan = 2;
                    }
                    
                    input = new Element("textarea", {
                        name: field.name
                    } );
                    
                    if(field.value)
                    {
                        input.value = field.value;
                    }
                    
                    // element ID
                    if(field.id)
                    {
                        input.id = field.id;
                    }
                    
                    // CSS class
                    if(field.class)
                    {
                        input.addClassName(field.class);
                    }

                    cell.update(input);
                    
                    break;
                    
                case "SelectField":
                    // label cell
                    if(field.label)
                    {
                        cell = $( row.insertCell(-1) );
                        
                        // label for field
                        if(field.id)
                        {
                            cell.update( new Element("label", {
                                "for": field.id
                            } ).update(field.label) );
                        }
                        // just a text
                        else
                        {
                            cell.update(field.label);
                        }
                    }
                    
                    // creates value cell
                    cell = $( row.insertCell(-1) );
                    
                    // without label cell is spaned to entire table
                    if(!field.label)
                    {
                        cell.colSpan = 2;
                    }
                    
                    input = new Element("select", {
                        name: field.name
                    } );

                    // available options
                    for(j in field.options)
                    {
                        var option = new Element("option", {
                            value: j
                        } );
                        option.text = field.options[j];
                        input.add(option, null);
                    }

                    // selects current value
                    if(field.value)
                    {
                        if( typeof(field.options[j]) == "function")
                        {
                            continue;
                        }

                        for(j = 0; j < input.options.length; ++j)
                        {
                            if(input.options[j].value == field.value)
                            {
                                input.selectedIndex = j;
                            }
                        }
                    }
                    
                    // element ID
                    if(field.id)
                    {
                        input.id = field.id;
                    }
                    
                    // CSS class
                    if(field.class)
                    {
                        input.addClassName(field.class);
                    }

                    cell.update(input);
                    
                    break;
                    
                case "RadioField":
                    // label cell
                    if(field.label)
                    {
                        $( row.insertCell(-1) ).update(field.label);
                    }
                    
                    // creates value cell
                    cell = $( row.insertCell(-1) );
                    
                    // without label cell is spaned to entire table
                    if(!field.label)
                    {
                        cell.colSpan = 2;
                    }

                    // radio options
                    for(j in field.options)
                    {
                        if( typeof(field.options[j]) == "function")
                        {
                            continue;
                        }

                        input = new Element("input", {
                            type: "radio",
                            name: field.name,
                            value: j
                        } );
                        
                        if( typeof(field.value) != "undefined" && field.value == j)
                        {
                            input.checked = true;
                        }
                        
                        // element ID
                        if(field.id)
                        {
                            input.id = field.id + j;
                        }
                        
                        // CSS class
                        if(field.class)
                        {
                            input.addClassName(field.class);
                        }

                        cell.insert(input);
                        
                        // option label
                        if(field.id)
                        {
                            cell.insert( new Element("label", {
                                "for": field.id + j
                            } ).update(field.options[j]) );
                        }
                        else
                        {
                            cell.insert(field.options[j]);
                        }
                    }
                    
                    break;
                    
                case "CheckboxField":
                    // label cell
                    if(field.label)
                    {
                        $( row.insertCell(-1) ).update(field.label);
                    }
                    
                    // creates value cell
                    cell = $( row.insertCell(-1) );
                    
                    // without label cell is spaned to entire table
                    if(!field.label)
                    {
                        cell.colSpan = 2;
                    }

                    // radio options
                    for(j in field.options)
                    {
                        if( typeof(field.options[j]) == "function")
                        {
                            continue;
                        }

                        input = new Element("input", {
                            type: "checkbox",
                            name: field.name,
                            value: j
                        } );

                        // checks if option is set
                        for(var k in field.values)
                        {
                            if(j == field.values[k])
                            {
                                input.checked = true;
                            }
                        }
                        
                        // element ID
                        if(field.id)
                        {
                            input.id = field.id + j;
                        }
                        
                        // CSS class
                        if(field.class)
                        {
                            input.addClassName(field.class);
                        }

                        cell.insert(input);
                        
                        // option label
                        if(field.id)
                        {
                            cell.insert( new Element("label", {
                                "for": field.id + j
                            } ).update(field.options[j]) );
                        }
                        else
                        {
                            cell.insert(field.options[j]);
                        }
                    }
                    
                    break;
                    
                case "FileField":
                    // label cell
                    if(field.label)
                    {
                        cell = $( row.insertCell(-1) );
                        
                        // label for field
                        if(field.id)
                        {
                            cell.update( new Element("label", {
                                "for": field.id
                            } ).update(field.label) );
                        }
                        // just a text
                        else
                        {
                            cell.update(field.label);
                        }
                    }
                    
                    // creates value cell
                    cell = $( row.insertCell(-1) );
                    
                    // without label cell is spaned to entire table
                    if(!field.label)
                    {
                        cell.colSpan = 2;
                    }
                    
                    input = new Element("input", {
                        type: "file",
                        name: field.name
                    } );
                    
                    // element ID
                    if(field.id)
                    {
                        input.id = field.id;
                    }
                    
                    // CSS class
                    if(field.class)
                    {
                        input.addClassName(field.class);
                    }

                    cell.insert(input);
                    
                    break;
            }
        }
    
        // puts fields on form
        return element.insert( table.insert(tbody) );
    }
} );
    
// display layer
var ajaxDialog;

//AJAX dialog initialisation
document.observe("dom:loaded", function() {
    ajaxDialog = new Element("div", {
        id: "ajaxDisplay"
    } ).hide();
    
    // cancel link
    ajaxDialog.closeLink = new Element("a", {
        href: document.location
    } ).update("[X]").observe("click", function(event) {
        ajaxDialog.remove();
        event.stop();
    } );
    
    // displays AJAX layer
    ajaxDialog.display = function(content) {
        // cleans current display
        this.update(this.closeLink);

        // displays given content
        this.insert(content);
        document.body.appendChild(this);
        this.show();
    };
} );

// displays first message in stack
function displayFirstMessage(stack)
{
    // temporary variable
    var component;
    
    for(var i in stack)
    {
        component = stack[i];
        
        switch(component.type)
        {
            case "Message":
                alert(component.data.message);
                return true;
                
            case "ErrorMessage":
                alert(component.data.message);
                return false;
        }
    }
    
    return true;
}

var lastHeader;
var lastName;
var lastImage;

// switches current project
function makeActive()
{
    lastHeader.update("\"" + this.projectName + "\"");
    lastName.update(this.projectText);
    lastImage.src = this.projectImage;
}

// chilldev.pl specific initialisation
document.observe("dom:loaded", function() {
    lastHeader = $("lastHeader");
    lastName = $("lastName");
    lastImage = $("lastImage");

    if(lastHeader)
    {
        var project = $("s1").observe("click", makeActive);
        project.projectName = "Czestochowa.cz";
        project.projectText = "Portal regionalny";
        project.projectImage = "/images/portfolio/czestochowa_mini.png";

        project = $("s2").observe("click", makeActive);
        project.projectName = "Sailbook";
        project.projectText = "Portal żeglarski";
        project.projectImage = "/images/portfolio/sailbook_mini.png";

        project = $("s3").observe("click", makeActive);
        project.projectName = "Pies.TV";
        project.projectText = "Psia telewizja";
        project.projectImage = "/images/portfolio/piestv_mini.png";

        project = $("s4").observe("click", makeActive);
        project.projectName = "Seks Fotki";
        project.projectText = "Portal erotyczny";
        project.projectImage = "/images/portfolio/seksfotki_mini.png";

        project = $("s5").observe("click", makeActive);
        project.projectName = "Native Speaker";
        project.projectText = "Kursy językowe";
        project.projectImage = "/images/portfolio/getanativespeaker_mini.png";
    }
} );

