/*
 * $Id: index.php,v 1.10.2.2 2004/10/13 13:07:41 mithyt2 Exp $
 *
 * WeembleCMS - PHP Content Managment System
 * Copyright ( C ) 2005-2007 Ant-Shtain
 *
 * For further information visit:
 *     http://www.weemble.com/
 *
 * File Name: index.php
 *   Index file.
 *   See the documentation for more info.
 *
 * File Author:
 *     Dmitry Semenov ( developer@ant-shtain.com )
 */

var DEBUG = false;

    // {{{ WeembleActions
    function WeembleActions( instanceName )
    {
	      this.InstanceName	= instanceName ;
        this.wm_message   = 0;
        this.class_id     = 0;
        this.object_id    = null;
        this.parent_id    = null;
        this.canSubmit    = false;
        this.Instance     = null;
      	// Events
      	this.OnError		  = null ;	// function( source, errorNumber, errorDescription )
    }
    // }}} WeembleActions

    // {{{ Initialization
    WeembleActions.prototype.Initialization = function (  )
    {
        this.Instance = document.weemble_frm;
    		if ( ! this.Instance )
    		{
    			  alert( 'Error: The FORM with id or name set to "' + this.InstanceName + '" was not found (forms count: ' + n + ')' ) ;
    		} // if
			  return false;
    }
    // }}} Initialization

    // {{{ TestField
    /**
     * Выполняем действие
     */
    WeembleActions.prototype.TestField = function ( )
    {
        var len = document.weemble_frm.elements.length;
        for ( var i = 0 ; i < len ; i ++ ) {
            if ( document.weemble_frm.elements[ i ].id == 1 ) {
                var s = WeembleJSUtils.trim(document.weemble_frm.elements[ i ].value);
                if ( s == '' || s == null ) {
                    document.weemble_frm.elements[ i ].focus();
//                    alert('Поле "' + document.weemble_frm.elements[ i ].name + '" является обязательным для заполненения');
                    alert('Пропущено поле, обязательное для заполнения');
                    return false;
                } // if
            } // if
        } // for

        return false;
    }
    // }}} TestField

    // {{{ setMessageId
    WeembleActions.prototype.setMessageId = function ( value )
    {
        this.Instance.wm_message.value = value;
    }
    // }}} setMessageId

    // {{{ setClassId
    WeembleActions.prototype.setClassId = function ( value )
    {
        this.Instance.class_id.value = value;
    }
    // }}} setClassId

    // {{{ setObjId
    WeembleActions.prototype.setObjId = function ( value )
    {
        this.Instance.object_id.value = value;
    }
    // }}} setObjId

    // {{{ setParentId
    WeembleActions.prototype.setParentId = function ( value )
    {
        this.Instance.parent_id.value = value;
    }
    // }}} setParentId

    // {{{ UpdateSubmit
    WeembleActions.prototype.UpdateSubmit = function ( )
    {
        if ( DEBUG ) {
            alert( 'WM_MESSAGE := ' + this.Instance.wm_message.value + "\n" +
                   'CLASS_ID   := ' + this.Instance.class_id.value + "\n" +
                   'OBJECT_ID  := ' + this.Instance.object_id.value + "\n" +
                   'PARENT_ID  := ' + this.Instance.parent_id.value );
        } // if
        return this.canSubmit;
    }
    // }}} UpdateSubmit

    // {{{ ExecuteTest
    WeembleActions.prototype.ExecuteTest = function ( wm_message, class_id, names )
    {
        this.canSubmit = verifyForm( names );
        if ( this.canSubmit ) {
            this.setMessageId( wm_message );
            this.setClassId( class_id );
        } // if
    }
    // }}} ExecuteTest

    // {{{ Execute
    WeembleActions.prototype.Execute = function ( wm_message, class_id, object_id, parent_id )
    {
        this.setMessageId( wm_message );
        this.setClassId( class_id );
        this.setObjId( object_id );
        this.setParentId( parent_id );
        this.canSubmit = true;
        this.Submit();
    }
    // }}} Execute

    // {{{ Submit
    WeembleActions.prototype.Submit = function ( )
    {
        if ( this.canSubmit ) {
            this.Instance.submit( );
            return true;
        }
        return false;
    }
    // }}} Submit

    // {{{ ExecuteDel
    /**
     * Выполняем действие перед удалением
     */
    WeembleActions.prototype.ExecuteDel = function ( wm_message, object_id, class_id, is_folder, f_name, count )
    {
        var delete_str = '';
        var count = 1;

        // TODO ...
        // Mazilla bug...
        var actual = true; // window.event.shiftKey;
//        var actual = window.event.shiftKey;

        if ( count == 1 ) {
            if ( is_folder ) {
                if ( actual ) {
                    delete_str = WeembleLang.RemoveFolder.replace( /%1/g, f_name );
                }
                else {
                    delete_str = WeembleLang.RemoveFolderRecycleBin.replace( /%1/g, f_name );
                } // if
            }
            else {
                if ( actual ) {
                    delete_str = WeembleLang.RemoveFile.replace( /%1/g, f_name );
                }
                else {
                    delete_str = WeembleLang.RemoveFileRecycleBin.replace( /%1/g, f_name );
                } // if
            } // if
        }
        else {
            if ( actual ) {
                delete_str = WeembleLang.RemoveObject.replace( /%1/g, count );
            }
            else {
                delete_str = WeembleLang.RemoveObjectRecycleBin.replace( /%1/g, count );
            } // if
        } // if

        /// спрашиваем подтверждение
        if ( WeembleJSUtils.confirm( delete_str ) ) {
            this.setMessageId( wm_message );
            this.setClassId( class_id );
            this.setObjId( object_id );
            this.canSubmit = true;
            this.Submit();
            return true;
        }
        else {
            this.canSubmit = false;
            return false;
        } // if
    }
    // }}} ExecuteDel

    // Инициализация обработчика
    var WActions = new WeembleActions( 'weemble_frm' );
    window.onload = function()
    {
    	  WActions.Initialization()
    }

    var down = new Image; down.src = '/weemble/img/panel/down.png';
    var up   = new Image; up.src   = '/weemble/img/panel/up.png';

    function togglePanel( _id )
    {
        var obj = WeembleJSUtils.getItem( 'panel_' + _id );
        if ( obj ) {
            obj.style.display = obj.style.display == '' ? 'none' : '';
            var obj_img = WeembleJSUtils.getItem( 'panel_img_' + _id );
            if ( obj_img ) {

                var cookieString = new Array();
                var add;
                if ( obj.style.display == '' ) {
                    obj_img.src = up.src;
                    add = false;
                }
                else {
                    obj_img.src = down.src;
                    add = true;
                } // if

                var cookieString = new Array();
                var cookie = document.cookie.split('; ');
                for (var i=0; i < cookie.length; i++) {
                    var crumb = cookie[i].split('=');
                    if ('WeemblePanelStatus' == crumb[0] && crumb[1]) {
                        var items = crumb[1].split(':');
                        for (var j=0; j <items.length; j++) {
                            if ( ! (items[ j ] == 'panel_' + _id && add == false) ) {
                                cookieString[  cookieString.length ] = items[ j ];
                            } // if
                        } // for
                    } // if
                } // for
                if ( add ) {
                    cookieString[ cookieString.length ] = 'panel_' + _id ;
                } // if
                document.cookie = 'WeemblePanelStatus=' + cookieString.join(':');
            }
        } // if
        return false;
    }

    function verifyForm( names )
    {
        for( var i = 0 ; i < names.length ; i++ ) {
            var obj = WeembleJSUtils.getItem( names[ i ] );
            if( obj ) {
                if( WeembleJSUtils.trim( obj.value ) == "" ) {

                    var point = WeembleJSUtils.getRealPos( obj );

                    var tooltip = new WeembleToolTip( 'wtooltip_tp', '/weemble/img/tooltip/' );

                    tooltip.show( point,'Пропущено поле', 'Поле <b>' + names[ i ] + '</b> является обязатльным для заполенения'  );

                    obj.focus();

                    return false;
                }
            }
            else {
                alert( "Объекта " + names[ i ] + " нет, обратитесь к администратору" );
                return false;
            }
        } // for
        return true;
    }

    // {{{
    function toggle()
    {
        var obj = WeembleJSUtils.getItem( 'extra_panel' );
        if ( obj ) {
            var btn = WeembleJSUtils.getItem( 'extra_btn' );
            if ( obj.style.display == 'none' ) {
                obj.style.display = '';
                btn.value = 'Дополнительно <<';
            }
            else {
                obj.style.display = 'none';
                btn.value = '';
                btn.value = 'Дополнительно >>';
            } // if
        } //if
    }
    // }}}

    function openUser( _id )
    {
        var url = '?popup=1&gid=' + _id + '&user=1';
        var oWindow = window.open( url, 'groupUser', 'menubar=no,scrollbars=yes,resizable=yes,location=no,toolbar=no,width=500,height=400' ) ;
        return false;
    }
