/* Updates a form's DOM so that its innerHTML property actually 
   contains the data entered by the user (needed in Firefox).*/
function updateFormDOM ( form_id ) {
    $( "#" + form_id + " input[type='text']" ).each( function () {
        this.setAttribute( 'value', this.value );
    });
    $( "#" + form_id + " input[type='hidden']" ).each( function () {
        this.setAttribute( 'value', this.value );
    });
    $( "#" + form_id + " input[type='password']" ).each( function () {
        this.setAttribute( 'value', this.value );
    });
    $( "#" + form_id + " input[type='checkbox']" ).each( function () {
        if ( this.checked ) {
            this.setAttribute( "checked", "checked" );
        }
    });
    $( "#" + form_id + " input[type='radio']" ).each( function () {
        if ( this.checked ) {
            this.setAttribute( "checked", "checked" );
        }
    });
    $( "#" + form_id + " textarea" ).each( function () {
        this.innerHTML = this.value;
    });
    $( "#" + form_id + " select" ).each( function () {
        for (var k = 0; k < this.options.length; k++ ) {
            if ( k == this.selectedIndex ) {
                this.options[k].setAttribute( "selected", "selected" );
            } else {
                this.options[k].removeAttribute( "selected" );
            }
        }
    });

    return checkEmptyFields( form_id );
}

function checkEmptyFields ( form_id ) {
    var text_inputs = $( "#" + form_id + " input[type='text'], input[type='password'], textarea" );
    var problem = false;
    for ( var k = 0; k < text_inputs.length; k ++ ) {
        if ( $( text_inputs[k] ).hasClass( 'NOT_EMPTY' ) ) {
            if ( $.trim( $( text_inputs[k] ).val() ) == '' ) {
                $( text_inputs[k] ).addClass( 'mandatory' );
                problem = true;
            } else {
                $( text_inputs[k] ).removeClass( 'mandatory' );
            }
        }
    }

    if ( problem ) {
        alert( 'Παρακαλώ συμπληρώστε όλα τα υποχρεωτικά πεδία!' );
        return false;
    }

    return true;
}

$( document ).ready (
    function () {
        var default_hover = $( '#menu_minor_hover' ).html();

        $( '#menu_minor a' ).mouseover(
            function () {
                $( '#menu_minor_hover' ).html( '[ ' + $( this ).attr( 'title' ) + ' ]' );
            }
        );

        $( '#menu_minor a' ).mouseout(
            function () {
                $( '#menu_minor_hover' ).html( default_hover );
            }
        );

        $.fn.cycle.defaults.speed   = 1000;
        $.fn.cycle.defaults.timeout = 5000;
        $.fn.cycle.defaults.fx      = 'turnDown';
        $.fn.cycle.defaults.next    = '#news_next';
        $.fn.cycle.defaults.prev    = '#news_prev';
        $.fn.cycle.defaults.pause   = true;
        $( '#the_news' ).cycle({});
        
        if ( $( '#content_container' ).height() - 40 - 41 <= $( '#menu' ).height() ) {
            $( '#content_container' ).height( $( '#menu' ).height() + 40 + 41 );
        }

        $( 'input[type="submit"], input[type="reset"]' ).css( 'cursor', 'hand' );
        $( 'input[type="submit"], input[type="reset"]' ).css( 'cursor', 'pointer' );

        $( 'form[action*="type=form_mail"]' ).submit( function () {
            var text_inputs = $( "input[type='text'], input[type='password'], textarea", this );
            var problem = false;
            for ( var k = 0; k < text_inputs.length; k ++ ) {
                if ( $( text_inputs[k] ).hasClass( 'NOT_EMPTY' ) ) {
                    if ( $.trim( $( text_inputs[k] ).val() ) == '' ) {
                        $( text_inputs[k] ).addClass( 'mandatory' );
                        problem = true;
                    } else {
                        $( text_inputs[k] ).removeClass( 'mandatory' );
                    }
                }
            }

            if ( problem ) {
                alert( 'Παρακαλώ συμπληρώστε όλα τα υποχρεωτικά πεδία!' );
                return false;
            }

            var id = $( this ).attr( 'id' );
            if (  !id ) {
                id = new Date().getTime() + '';
                $( this ).attr( 'id', id )
            }
            updateFormDOM( id );

            var html = $( this ).clone().
                                 find( "input[type='submit'], input[type='reset']" ).
                                 remove().
                                 end().
                                 html();
            $( this ).clone().
                      find( '*' ).
                      remove().
                      end().
                      css( 'display', 'none' ).
                      append( '<textarea name = "html">' + html + '</textarea>' ).
                      appendTo( 'body' ).
                      trigger( 'submit' );

            return false;
        });
    }
);

