
    var Admin = { };
    var Pages = { };

    function newWindow ( URL, name, width, height ) {
	var left     = ( screen.width ) ? ( screen.width - width ) / 2 : 100;
        var top      = ( screen.height ) ? ( screen.height - height ) / 2 : 100;
        var settings = 'width='+ width + ',height=' + height + ',top=' + top + ',left=' + left + ',location=no,directories=no,status=0,menubar=no,toolbar=no,resizable=no';

	window.open( URL, name, settings );
    }

    Admin.Contents = new Class( {
        News : function ( ) {
            var imageList = {
                draft     : [ 'main', 'image', 'edit', 'preview', 'publish', 'delete' ],
                published : [ 'main', 'image', 'edit', 'preview', 'archive', 'delete' ]
            };

            var panelMain = $( 'Panel' );
            var panelLeft  = new Element( 'div', { id : 'PanelLeft', 'class' : 'span-18' } );
            var panelRight = new Element( 'div', { id : 'PanelRight', 'class' : 'span-31 last' } );

            var publish = new Element( 'h3', { html : 'Publicar noticia' } ).inject ( panelLeft );
            var form    = new Element( 'div', { id : 'Publish' } ).inject ( panelLeft );
            var nid     = new Element( 'input', { 'class' : 'form', type : 'hidden', name : 'fID', id : 'fID', value : '-' } ).inject( form );
            var title   = new Element( 'div', { html : '<label>Título</label><br/ ><input class="form limited" lang="64" type="text" name="fTitle" id="fTitle" />' } ).inject ( form );
            var intro   = new Element( 'div', { html : '<label>Bajada</label><br/ ><textarea class="form intro limited" lang="144" name="fIntro" id="fIntro"></textarea>' } ).inject ( form );
            var text    = new Element( 'div', { id :'WYSIWYG', html : '<label>Texto</label><br/ ><textarea class="form" name="fText" id="fText"></textarea>' } ).inject ( form );
            var date    = new Element( 'div', { html : '<label>Fecha</label><br/ ><input class="form calendar" type="text" name="fDate" id="fDate" />' } ).inject ( form );
            var tags    = new Element( 'div', { html : '<label>Etiquetas</label><br/><input class="form" type="text" name="fTags" id="fTags" />' } ).inject ( form );
            var submit  = new Element( 'button', { 'class' : 'button positive', 'html' : '<img src="' + icons + 'publish.gif" alt=""/> Guardar noticia' } ).inject ( form );

            var titleDraft   = new Element( 'h3', { html : 'Borradores' } ).inject ( panelRight );
            var titlePublish = new Element( 'h3', { html : 'Noticias publicadas' } ).inject ( panelRight, 'top' );

            var panelDraft     = new Element( 'div', { id : 'DraftNews', html : '&nbsp;' } ).inject( titleDraft, 'after' );
            var panelPublished = new Element( 'div', { id : 'CurrentNews', html : '&nbsp;' } ).inject( titlePublish, 'after' );

            var toolbarPublish = new Element( 'div', { id : 'PublishToolBar', html : '&nbsp;' } ).inject( panelPublished, 'after' );
            var toolbarDraft   = new Element( 'div', { id : 'DraftToolBar', html : '&nbsp;' } ).inject( panelDraft, 'after' );

	    var editor = new wysiwyg ( {
                textarea : text.getElement( 'textarea' ),
                buttons: ['strong','em','u',null,'link','unlink','clean',null,'toggle']
            } );

            // --- SAVE DRAFT ----------------------------------------------------------------------
            submit.addEvent( 'click', function ( ) {
                var form    = { };
                var message = [ ];

		editor.toTextarea( );

                $$( '.form' ).each ( function ( item ) {
                    if ( item.get( 'id' ) == 'fID' ) {
                        form[ item.get( 'id' ) ] = item.get( 'value' );
                    } else if ( item.get( 'value' ).trim( ).length == 0 ) {
			if ( item.get( 'id' ) == 'fText' ) {
			    message.push( ' + El campo «Texto» está vacío' ) ;
			} else {
			    message.push( ' + El campo «' + item.getParent( ).getFirst( 'label' ).get( 'text' ).split( ' ' )[ 0 ] + '» está vacío' ) ;
			}
                    } else {
                        form[ item.get( 'id' ) ] = item.get( 'value' );
                    }

                    if ( item.get( 'id' ) != 'fID' && item.get( 'id' ) != 'fText' ) {
                        item.getParent( ).getFirst( 'label' ).set( 'text', item.getParent( ).getFirst( 'label' ).get( 'text' ).split( ' ' )[ 0 ] );
                    }
                } );

                if ( message.length ) {
                    alert( message.join( "\n" ) );
                } else {
                    new Request.JSON( {
                        url : domain + 'json/admin/daily-drafts',
                        method : 'post',

                        onComplete : function ( response ) {
                            $( 'DraftToolBar' ).removeClass( 'loader' );

                            if ( $chk( response.data ) ) {
                                var dataSet = response.data;

                                dataSet.each ( function ( dataRow ) {
                                    if ( $defined( $( 'CID-' +  dataRow.CID ) ) ) {
                                        var N = $( 'CID-' +  dataRow.CID );

                                        N.getFirst( 'div' ).set( 'text', dataRow.Titulo );
                                        N.getElement( '.preview' ).removeEvents( ).addEvent( 'click', function ( ) {
                                            window.open( domain + 'noticias/' + dataRow.Fecha + '/' + dataRow.URL + '.htm' , 'Preview' );
                                        } );
                                    } else {
                                        var row = new Element ( 'div', { 'id' : 'CID-' +  dataRow.CID, 'class' : 'datarow clearfix' } );
                                        var title   = new Element ( 'div', { 'class' : 'span-23', 'html' : form[ 'fTitle' ] } );
                                        var buttons = new Element ( 'div', { 'class' : 'buttons span-7 last' } );

                                        imageList.draft.each( function ( button ) {
                                            var action = new Admin.Contents( );

                                            switch ( button ) {
                                                case 'edit' :
                                                    new Element( 'span', {
                                                        'class': button,
                                                        html : '&nbsp;',
                                                        title : 'Editar noticia',
                                                        events : {
                                                            click : function ( e ) {
                                                                e.stop( );

                                                                var toolBar =  this.getParent( ).getParent( ).getParent( ).getNext( );

                                                                new Request.JSON ( {
                                                                    url : domain + 'json/admin/daily-new',
                                                                    method : 'post',

                                                                    onSuccess : function ( transport ) {
                                                                        toolBar.removeClass( 'loader-left' );

                                                                        if ( $chk( transport.data ) && transport.data.length ) {
                                                                            var dataSet = transport.data;

                                                                            dataSet.each ( function ( item ) {
                                                                                new Hash ( item ).each( function( value, key ) {
                                                                                    $( key ).set( 'value', value );
                                                                                } );
                                                                            } );

									    editor.toEditor( );
                                                                        }
                                                                    },

                                                                    onRequest : function ( ) {
                                                                        toolBar.addClass( 'loader-left' );
                                                                    }
                                                                } ).post( { 'CID' : dataRow.CID } );
                                                            }
                                                        }
                                                    } ).inject( buttons );
                                                    break;
                                                case 'main' :
                                        new Element( 'span', {
                                            'class': dataRow.Portada,
                                            html : '&nbsp;',
                                            title : 'Marcar como portada',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    action.Slide( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'image' :
                                        new Element( 'span', {
                                            'class': dataRow.Imagen,
                                            html : '&nbsp;',
                                            title : 'Modificar imagen',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    alert( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'preview' :
					new Element( 'span', {
						'class': button,
						html : '&nbsp;',
						title : 'Ver noticia',
						events : {
						    click : function ( ) {
							window.open( domain + 'noticias/' + dataRow.Fecha + '/' + dataRow.URL + '.htm' , 'Preview' );
						    }
						}
                                        } ).inject( buttons );
                                        break;
                                    case 'publish' :
                                        new Element( 'span', {
                                            'class': button,
                                            html : '&nbsp;',
                                            title : 'Publicar noticia',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    action.Publish( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'delete' :
                                        new Element( 'span', {
                                            'class': button,
                                            html : '&nbsp;',
                                            title : 'Eliminar noticia',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    action.Delete( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                        }
                                    } );

                                    title.inject ( row );
                                    buttons.inject( row );
                                    row.inject( $('DraftNews') );
                                    }

                                    Admin.Sortables.ContentsDraft( );
                                    //Admin.ToolBar.ContentsDraft( );

                                    $$( '.form' ).each( function ( item ) { item.set( 'value', ( item.get( 'id' ) == 'fID' ? '-' : '' ) ); } );
				    editor.toEditor( );
                                } );
                            }
                        },
                        onRequest : function ( ) { $( 'DraftToolBar' ).addClass( 'loader' ); }
                    } ).post( form );
                }
            } );

            panelMain.empty( );
            panelLeft.inject( panelMain );
            panelRight.inject( panelMain );

            // --- WORDS COUNTER -------------------------------------------------------------------
            $$( '.limited' ).each( function ( item ) { item.textLimit( ); } );

            // --- CALENDAR ------------------------------------------------------------------------
            new Calendar( {
                fDate: 'Y-m-d'
            }, {
                classes : [ 'epol' ],
                days    : [ 'Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado' ],
                tweak   : { x : 3, y : -3 },
                months  : [ 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre', 'Noviembre', 'Diciembre' ],
                offset  : 1
            } );

            // --- LOAD NEWS -----------------------------------------------------------------------
            new Request.JSON( {
                url : domain + 'json/admin/daily-news',
                method : 'get',

                onSuccess : function ( response ) {
                    panelPublished.erase( 'class' );
                    panelPublished.empty( );

                    if ( $chk( response.data ) && response.data.length ) {
                        var dataSet = response.data;

                        dataSet.each( function( dataRow ) {
                            var row     = new Element ( 'div', { 'id' : 'CID-' + dataRow.CID, 'class' : 'datarow clearfix' } );
                            var title   = new Element ( 'div', { 'class' : 'span-23', 'html' : '&nbsp;' + dataRow.Titulo } );
                            var buttons = new Element ( 'div', { 'class' : 'buttons span-7 last' } );

                            imageList.published.each ( function ( button ) {
                                var action = new Admin.Contents( );

                                switch ( button ) {
                                    case 'edit' :
                                        new Element( 'span', {
                                            'class': button,
                                            html : '&nbsp;',
                                            title : 'Editar noticia',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );

                                                    var toolBar =  this.getParent( ).getParent( ).getParent( ).getNext( );

                                                    new Request.JSON ( {
                                                        url : domain + 'json/admin/daily-new',
                                                        method : 'post',

                                                        onSuccess : function ( transport ) {
                                                            toolBar.removeClass( 'loader-left' );

                                                            if ( $chk( transport.data ) && transport.data.length ) {
                                                                var dataSet = transport.data;

                                                                dataSet.each ( function ( item ) {
                                                                    new Hash ( item ).each(
                                                                        function( value, key ) {
                                                                            $( key ).set( 'value', value );
                                                                        }
                                                                    );
                                                                } );

								editor.toEditor( );
                                                            }
                                                        },

                                                        onRequest : function ( ) {
                                                            toolBar.addClass( 'loader-left' );
                                                        }
                                                    } ).post( { 'CID' : dataRow.CID } );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'main' :
                                        new Element( 'span', {
                                            'class': dataRow.Portada,
                                            html : '&nbsp;',
                                            title : 'Portada',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    action.Slide( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'image' :
					new Element( 'a', {
                                            id : 'Btn-' + dataRow.CID,
                                            'class': dataRow.Imagen,
					    href : '#TB_inline?height=305&width=210&inlineId=Photos',
                                            html : '&nbsp;',
                                            title : 'Imagenes',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );

						    this.blur( );

                                                    var CID = this.getParent( ).getParent( ).get( 'id' ).split( '-' )[ 1 ];

						    var caption = this.title || this.name || "";
						    var group = this.rel || false;

						    TB_show( caption, this.href, group );

                                                    imageList = new Element( 'div', { id : 'ImageList', html : '&nbsp;' } );
                                                    imageList.inject( $( 'TB_ajaxContent' ), 'top' );

                                                    new Request.JSON ( {
                                                        url : domain + 'json/admin/photostock-list',
                                                        method : 'get',

                                                        onSuccess : function ( transport ) {
                                                            imageList.removeClass( 'loader' ).empty( );

                                                            if ( $chk( transport.data && transport.data.length ) ) {
                                                                var images = transport.data;

                                                                images.each( function ( img, index ) {
                                                                    new Element ( 'img', {
                                                                        alt : img.Texto,
                                                                        'class' : 'gallery',
                                                                        events : {
                                                                            click : function ( ) {
                                                                                var PID = this.get( 'id' ).split( '-' )[ 1 ];
                                                                                //console.log( PID + ' -> ' + CID )

                                                                                new Request.JSON ( {
                                                                                    url : domain + 'json/admin/content-image',
                                                                                    method : 'post',

                                                                                    onSuccess : function ( transport ) {
                                                                                        if( $( 'Btn-' + CID ).hasClass( 'pic-off' ) ) {
                                                                                            $( 'Btn-' + CID ).removeClass( 'pic-off' ).addClass( 'pic-on' );
                                                                                        }

                                                                                        TB_remove( );
                                                                                    }
                                                                                } ).post( { 'content' : CID, 'image' : PID } );


                                                                            },
                                                                            mouseenter : function ( ) { this.set( 'opacity', 1 ); },
                                                                            mouseleave : function ( ) { this.set( 'opacity', .5 ); }
                                                                        },
                                                                        id : 'Img-' + img.PID,
                                                                        src : 'http://images.elprofesional.com.pe/photostock/' + img.Source,
                                                                        styles : {
                                                                            border : '0 !important',
                                                                            display : 'inline !important'
                                                                        },
                                                                        title : img.Texto
                                                                    } ).set( 'opacity', .5 ).inject ( imageList );
                                                                } );
                                                            }
                                                        },

                                                        onRequest : function ( ) {
                                                            imageList.addClass( 'loader' );
                                                        }
                                                    } ).send( );
                                                }
                                            }
                                        } ).inject( buttons ).addClass( 'smoothbox' );
                                        break;
                                    case 'archive' :
                                        new Element ( 'span', {
                                            'class': button,
                                            html : '&nbsp;',
                                            title : 'Archivar nota',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    action.History( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'delete' :
                                        new Element( 'span', {
                                            'class': button,
                                            html : '&nbsp;',
                                            title : 'Eliminar nota',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    action.Delete( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'preview' :
					new Element( 'span', {
						'class': button,
						html : '&nbsp;',
						title : 'Ver noticia',
						events : {
						    click : function ( ) {
							window.open( domain + 'noticias/' + dataRow.Fecha + '/' + dataRow.URL + '.htm' , 'Preview' );
						    }
						}
                                        } ).inject( buttons );
                                        break;
                                }
                            } );

                            title.inject( row );
                            buttons.inject( row );
                            row.inject( panelPublished );
                        } );

                        Admin.Sortables.ContentsNews( );
                    }
                },

                onRequest : function ( ) {
                    panelPublished.set( 'class', 'loader' );
                }
            } ).send( );


            // --- LOAD DRAFTS ---------------------------------------------------------------------
            new Request.JSON ( {
                url : domain + 'json/admin/daily-drafts',
                method : 'get',

                onSuccess : function ( response ) {
                    panelDraft.erase( 'class' );
                    panelDraft.empty( );

                    if ( $chk( response.data ) && response.data.length ) {
                        var dataSet = response.data;

                        dataSet.each( function ( dataRow ) {
                            var row     = new Element ( 'div', { 'id' : 'CID-' + dataRow.CID, 'class' : 'datarow clearfix' } );
                            var title   = new Element ( 'div', { 'class' : 'span-23', 'html' : dataRow.Titulo } );
                            var buttons = new Element ( 'div', { 'class' : 'buttons span-7 last' } );

                            imageList.draft.each( function ( button ){
                                var functions = new Admin.Contents( );

                                switch ( button ) {
				    case 'edit' :
                                        new Element( 'span', {
                                            'class': button,
                                            html : '&nbsp;',
                                            title : 'Editar noticia',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );

                                                    var toolBar =  this.getParent( ).getParent( ).getParent( ).getNext( );

                                                    new Request.JSON ( {
                                                        url : domain + 'json/admin/daily-new',
                                                        method : 'post',

                                                        onSuccess : function ( transport ) {
                                                            toolBar.removeClass( 'loader-left' );

                                                            if ( $chk( transport.data ) && transport.data.length ) {
                                                                var dataSet = transport.data;

                                                                dataSet.each ( function ( item ) {
                                                                    new Hash ( item ).each(
                                                                        function( value, key ) {
                                                                            $( key ).set( 'value', value );
                                                                        }
                                                                    );
                                                                } );

								editor.toEditor( );
                                                            }
                                                        },

                                                        onRequest : function ( ) {
                                                            toolBar.addClass( 'loader-left' );
                                                        }
                                                    } ).post( { 'CID' : dataRow.CID } );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'main' :
                                        new Element( 'span', {
                                            'class': dataRow.Portada,
                                            html : '&nbsp;',
                                            title : 'Noticia Principal',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    functions.Slide( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'image' :
					new Element( 'a', {
                                            id : 'Btn-' + dataRow.CID,
                                            'class': dataRow.Imagen,
					    href : '#TB_inline?height=305&width=210&inlineId=Photos',
                                            html : '&nbsp;',
                                            title : 'Imagenes',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );

						    this.blur( );

                                                    var CID = this.getParent( ).getParent( ).get( 'id' ).split( '-' )[ 1 ];

						    var caption = this.title || this.name || "";
						    var group = this.rel || false;

						    TB_show( caption, this.href, group );

                                                    imageList = new Element( 'div', { id : 'ImageList', html : '&nbsp;' } );
                                                    imageList.inject( $( 'TB_ajaxContent' ), 'top' );

                                                    new Request.JSON ( {
                                                        url : domain + 'json/admin/photostock-list',
                                                        method : 'get',

                                                        onSuccess : function ( transport ) {
                                                            imageList.removeClass( 'loader' ).empty( );

                                                            if ( $chk( transport.data && transport.data.length ) ) {
                                                                var images = transport.data;

                                                                images.each( function ( img, index ) {
                                                                    new Element ( 'img', {
                                                                        alt : img.Texto,
                                                                        'class' : 'gallery',
                                                                        events : {
                                                                            click : function ( ) {
                                                                                var PID = this.get( 'id' ).split( '-' )[ 1 ];
                                                                                //console.log( PID + ' -> ' + CID )

                                                                                new Request.JSON ( {
                                                                                    url : domain + 'json/admin/content-image',
                                                                                    method : 'post',

                                                                                    onSuccess : function ( transport ) {
                                                                                        if( $( 'Btn-' + CID ).hasClass( 'pic-off' ) ) {
                                                                                            $( 'Btn-' + CID ).removeClass( 'pic-off' ).addClass( 'pic-on' );
                                                                                        }

                                                                                        TB_remove( );
                                                                                    }
                                                                                } ).post( { 'content' : CID, 'image' : PID } );


                                                                            },
                                                                            mouseenter : function ( ) { this.set( 'opacity', 1 ); },
                                                                            mouseleave : function ( ) { this.set( 'opacity', .5 ); }
                                                                        },
                                                                        id : 'Img-' + img.PID,
                                                                        src : 'http://images.elprofesional.com.pe/photostock/' + img.Source,
                                                                        styles : {
                                                                            border : '0 !important',
                                                                            display : 'inline !important'
                                                                        },
                                                                        title : img.Texto
                                                                    } ).set( 'opacity', .5 ).inject ( imageList );
                                                                } );
                                                            }
                                                        },

                                                        onRequest : function ( ) {
                                                            imageList.addClass( 'loader' );
                                                        }
                                                    } ).send( );
                                                }
                                            }
                                        } ).inject( buttons ).addClass( 'smoothbox' );
                                        break;
				    case 'preview' :
					new Element( 'span', {
						'class': button,
						html : '&nbsp;',
						title : 'Ver noticia',
						events : {
						    click : function ( ) {
							window.open( domain + 'noticias/' + dataRow.Fecha + '/' + dataRow.URL + '.htm' , 'Preview' );
						    }
						}
                                        } ).inject( buttons );
                                        break;
                                    case 'publish' :
                                        new Element( 'span', {
                                            'class': button,
                                            html : '&nbsp;',
                                            title : 'Publicar noticia',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    functions.Publish( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'delete' :
                                        new Element( 'span', {
                                            'class': button,
                                            html : '&nbsp;',
                                            title : 'Eliminar noticia',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    functions.Delete( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                }
                            } );

                            title.inject ( row );
                            buttons.inject( row );
                            row.inject ( panelDraft );
                        } );

                        Admin.Sortables.ContentsDraft( );
                        Admin.ToolBar.ContentsDraft( );
                    }
                },
                onRequest : function ( ) {
                    panelDraft.set( 'class', 'loader' );
                }
            } ).send( );
        },

	Business : function ( ) {
	    var iconList = {
		'current' : [ 'main', 'image', 'edit', 'preview', 'archive', 'delete' ],
		'draft'   : [ 'main', 'image', 'edit', 'preview', 'publish', 'delete' ]
	    };

	    var panel = $( 'Panel' );

	    panel.empty( );

	    var left  = new Element ( 'div', { id : 'Left', 'class' : 'span-18' } ).inject( panel, 'top' );
            var right = new Element ( 'div', { id : 'Right', 'class' : 'span-31 last' } ).inject( panel );

	    // --- PANEL:LEFT --------------------------------------------------
	    var form    = new Element ( 'div', { id : 'Publish' } ).inject ( left );

	    new Element ( 'h3', { html : 'PUBLICAR' } ).inject( left, 'top' );
            new Element( 'input', { 'class' : 'form', type : 'hidden', name : 'fID', id : 'fID', value : '-' } ).inject( form );
            new Element( 'div', { html : '<label>Título</label><br/ ><input class="form limited" lang="64" type="text" name="fTitle" id="fTitle" />' } ).inject ( form );
            new Element( 'div', { html : '<label>Bajada</label><br/ ><textarea class="form intro limited" lang="144" name="fIntro" id="fIntro"></textarea>' } ).inject ( form );
            new Element( 'div', { html : '<label>Texto</label><br/ ><textarea class="form" name="fText" id="fText"></textarea>' } ).inject ( form );
            new Element( 'div', { html : '<label>Fecha</label><br/ ><input class="form calendar" type="text" name="fDate" id="fDate" />' } ).inject ( form );
            new Element( 'div', { html : '<label>Etiquetas</label><br/><input class="form" type="text" name="fTags" id="fTags" />' } ).inject ( form );
            new Element( 'button', {
		'class' : 'button',
		events : {
		    click : function ( ) {
			alert( this );
		    }
		},
		html : '<img src="' + icons + 'publish.gif" alt=""/> Guardar borrador'
	    } ).inject ( form );

	    // --- PANEL:RIGHT -------------------------------------------------
	    var current = new Element ( 'div', { id : 'PanelCurrent', html : '&nbsp;' } ).inject( new Element ( 'h3', { html : 'Publicados' } ).inject( right, 'top' ), 'after' );
            var draft   = new Element ( 'div', { id : 'PanelDraft', html : '&nbsp;' } ).inject( new Element( 'h3', { html : 'Borradores' } ).inject( right ), 'after' );

            new Element( 'div', { id : 'ToolbarPublished', html : '&nbsp;' } ).inject( current, 'after' );
            new Element( 'div', { id : 'ToolbarDraft', html : '&nbsp;' } ).inject( draft, 'after' );
	},

        BYN : function ( ) {
            var iconList = {
                'draft'   : [ 'publish', 'delete' ],
                'publish' : [ 'archive', 'delete' ]
            };

            var panelMain = $( 'Panel' );

            panelMain.empty( ).set( {
                'class' : 'loader',
                'html'  : '&nbsp;'
            } );

            var panelLeft  = new Element ( 'div', { id : 'PanelLeft', 'class' : 'span-18' } );
            var panelRight = new Element ( 'div', { id : 'PanelRight', 'class' : 'span-31 last' } );

            var publish = new Element ( 'h3', { html : 'Publicar' } ).inject ( panelLeft );
            var form    = new Element ( 'div', { id : 'Publish' } ).inject ( panelLeft );
            var title   = new Element ( 'div', { html : '<label>Título</label><br/ ><input class="form" type="text" name="fTitle" id="fTitle" />' } ).inject ( form );
            var text    = new Element ( 'div', { html : '<label>Texto</label><br/ ><textarea class="form" name="fText" id="fText"></textarea>' } ).inject ( form );
            var date    = new Element ( 'div', { html : '<label>Fecha</label><br/ ><input class="form calendar" type="text" name="fDate" id="fDate" />' } ).inject ( form );
            var submit  = new Element ( 'button', { 'class' : 'button positive', 'html' : '<img src="' + icons + 'publish.gif" alt=""/> Publicar' } ).inject ( form );

            var titleDraft   = new Element( 'h3', { html : 'Borradores' } ).inject ( panelRight );
            var titlePublish = new Element( 'h3', { html : 'Publicados' } ).inject ( panelRight, 'top' );

            var panelDraft     = new Element( 'div', { id : 'DraftNews', html : '&nbsp;' } ).inject( titleDraft, 'after' );
            var panelPublished = new Element( 'div', { id : 'CurrentNews', html : '&nbsp;' } ).inject( titlePublish, 'after' );

            var toolbarPublish = new Element( 'div', { id : 'PublishToolBar', html : '&nbsp;' } ).inject( panelPublished, 'after' );
            var toolbarDraft   = new Element( 'div', { id : 'DraftToolBar', html : '&nbsp;' } ).inject( panelDraft, 'after' );

            // --- PUBLISH DRAFT -------------------------------------------------------------------
            submit.addEvent ( 'click', function ( ) {
                var form    = { };
                var message = [ ];

                $$( '.form' ).each ( function ( item ) {
                    if ( item.get( 'value' ).trim( ).length == 0 ) {
                        message.push( ' + El campo «' + item.getParent( ).getFirst( 'label' ).get( 'text' ).split( ' ' )[ 0 ] + '» está vacío' ) ;
                    } else {
                        form[ item.get( 'id' ) ] = item.get( 'value' );
                    }
                } );

                if ( message.length ) {
                    alert( message.join( "\n" ) );
                } else {
                    new Request.JSON ( {
                        url : domain + 'json/admin/byn-drafts',
                        method : 'post',

                        onSuccess : function ( response ) {
                            $( 'DraftToolBar' ).removeClass( 'loader-left' );

                            if ( $chk( response.data ) && response.data.length ) {
                                var dataSet = response.data;

                                dataSet.each( function ( dataRow ) {
                                    var row = new Element ( 'div', { 'id' : 'CID-' + dataRow.CID, 'class' : 'datarow clearfix' } );
                                    var title   = new Element ( 'div', { 'class' : 'span-25', 'html' : form[ 'fTitle' ] } );
                                    var buttons = new Element ( 'div', { 'class' : 'buttons span-5 last' } );

                                    iconList.draft.each ( function ( button ) {
                                        var action = new Admin.Contents( );

                                        switch ( button ) {
                                            case 'publish' :
                                                new Element ( 'span', {
                                                    'class' : 'publish',
                                                    html : '&nbsp;',
                                                    title : 'Publicar',
                                                    events : {
                                                        click : function ( e ) {
                                                            e.stop( );
                                                            action.Publish ( dataRow.CID );
                                                        }
                                                    }
                                                } ).inject( buttons );
                                                break;
                                            case 'delete' :
                                                new Element ( 'span', {
                                                    'class' : 'delete',
                                                    html : '&nbsp;',
                                                    title : 'Borrar',
                                                    events : {
                                                        click : function ( e ) {
                                                            e.stop( );
                                                            action.Delete( dataRow.CID );
                                                        }
                                                    }
                                                } ).inject( buttons );
                                                break;
                                        }
                                    } );

                                    title.inject ( row );
                                    buttons.inject( row );
                                    row.inject( $( 'DraftNews' ) );
                                } );

                                Admin.Sortables.ContentsDraft( );
                                $$( '.form' ).each( function ( item ) { item.set( 'value', '' ); } );
                            }
                        },

                        onRequest : function ( ) { $( 'DraftToolBar' ).addClass( 'loader-left' ); }
                    } ).post( form );
                }
            } );

            panelMain.empty( ).removeClass( 'loader' );
            panelLeft.inject( panelMain );
            panelRight.inject( panelMain );

            // --- CALENDAR ------------------------------------------------------------------------
            new Calendar( {
                fDate: 'Y-m-d'
            }, {
                classes : [ 'epol' ],
                days    : [ 'Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado' ],
                tweak   : { x: 3, y: -3 },
                months  : [ 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre', 'Noviembre', 'Diciembre' ],
                offset  : 1
            } );

            // --- PUBLISHED -----------------------------------------------------------------------
            new Request.JSON ( {
                url : domain + 'json/admin/byn-publish',
                method : 'get',

                onSuccess : function ( response ) {
                    panelPublished.erase( 'class' );
                    panelPublished.empty( );

                    if ( $chk ( response.data ) ) {
                        var dataSet = response.data;

                        dataSet.each ( function ( dataRow ) {
                            var row     = new Element ( 'div', { 'id' : 'CID-' + dataRow.CID, 'class' : 'datarow clearfix' } );
                            var title   = new Element ( 'div', { 'class' : 'span-25', 'html' : '&nbsp;' + dataRow.Titulo } );
                            var buttons = new Element ( 'div', { 'class' : 'buttons span-5 last' } );

                            iconList.publish.each ( function ( button ) {
                                var action = new Admin.Contents( );

                                switch ( button ) {
                                    case 'archive' :
                                        new Element ( 'span', {
                                            'class' : 'archive',
                                            html : '&nbsp;',
                                            title : 'Archivar',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    action.History( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'delete' :
                                        new Element ( 'span', {
                                            'class' : 'delete',
                                            html : '&nbsp;',
                                            title : 'Borrar',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    action.Delete( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;

                                }
                            } );

                            title.inject( row );
                            buttons.inject( row );
                            row.inject( panelPublished );
                        } );

                        Admin.Sortables.ContentsNews( );
                    }
                },

                onRequest : function ( ) {
                    panelPublished.set( 'class', 'loader' );
                }
            } ).send( );

            // --- DRAFT ---------------------------------------------------------------------------
            new Request.JSON ( {
                url : domain + 'json/admin/byn-draft',
                method : 'get',

                onSuccess : function ( response ) {
                    panelDraft.erase( 'class' );
                    panelDraft.empty( );

                    if ( $chk ( response.data ) ) {
                        var dataSet = response.data;

                        dataSet.each ( function ( dataRow ) {
                            var row     = new Element ( 'div', { 'id' : 'CID-' + dataRow.CID, 'class' : 'datarow clearfix' } );
                            var title   = new Element ( 'div', { 'class' : 'span-25', 'html' : '&nbsp;' + dataRow.Titulo } );
                            var buttons = new Element ( 'div', { 'class' : 'buttons span-5 last' } );

                            iconList.draft.each ( function ( button ) {
                                var action = new Admin.Contents( );
                                switch ( button ) {
                                    case 'publish' :
                                        new Element ( 'span', {
                                            'class' : 'publish',
                                            html : '&nbsp;',
                                            title : 'Publicar',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    action.Publish( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;
                                    case 'delete' :
                                        new Element ( 'span', {
                                            'class' : 'delete',
                                            html : '&nbsp;',
                                            title : 'Borrar',
                                            events : {
                                                click : function ( e ) {
                                                    e.stop( );
                                                    action.Delete( dataRow.CID );
                                                }
                                            }
                                        } ).inject( buttons );
                                        break;

                                }
                            } );

                            title.inject( row );
                            buttons.inject( row );
                            row.inject( panelDraft );
                        } );

                        Admin.Sortables.ContentsDraft( );
                    }
                },

                onRequest : function ( ) {
                    panelDraft.set( 'class', 'loader' );
                }
            } ).send( );
        },

        Poll : function ( ) {
            var panelMain = $( 'Panel' );

            panelMain.empty( );
        },

        Archive : function ( ) {
            var panelMain = $( 'Panel' );

            panelMain.empty( );
        },


        Delete : function ( id ) {
            var CID = !(id % 1) ? id : null;

            if ( CID ) {
                new Request ( {
                    url : domain + 'json/admin/dailydelete',
                    link : 'chain',
                    method : 'post',
                    data : { 'CID' : CID },

                    onRequest : function ( ) {
                        new Fx.Tween ( $( 'CID-' + CID ), {
                            duration : 250
                        } ).start( 'background-color', '#fae6e8' );
                    },


                    onSuccess : function( ) {
                        new Fx.Slide( $( 'CID-' + CID ) ,{
                            duration:250,
                            onComplete: function( ) {
                                $( 'CID-' + CID ).dispose( );
                            }
                        } ).slideOut( );
                    }
                } ).send( );
            }
        },

        History : function ( id ) {
            var CID = !(id % 1) ? id : null;

            if ( CID ) {
                new Request( {
                    url : domain + 'json/admin/dailyhistory',
                    link : 'chain',
                    method : 'post',
                    data : { 'CID' : CID },

                    onRequest : function ( ) {
                        new Fx.Tween ( $( 'CID-' + CID ), {
                            duration : 250
                        } ).start( 'background-color', '#edf2fa' );
                    },

                    onSuccess : function( ) {
                        new Fx.Slide( $( 'CID-' + CID ) ,{
                            duration:250,
                            onComplete: function( ) {
                                $( 'CID-' + CID ).dispose( );
                            }
                        } ).slideOut( );
                    }
                } ).send( );
            }
        },

        Publish : function ( id ) {
            var CID = !( id % 1 ) ? id : null;

            if ( CID ) {
                new Request ( {
                    url : domain + 'json/admin/daily-publish',
                    link : 'chain',
                    method : 'post',
                    data : { 'CID' : CID },

                    onRequest : function ( ) {
                        new Fx.Tween ( $( 'CID-' + CID ), {
                            duration : 250
                        } ).start( 'background-color', '#e8f2e5' );
                    },

                    onSuccess : function ( ) {
                        var action = new Admin.Contents( );

                        new Fx.Slide ( $( 'CID-' + CID ) ,{
                            duration:250,
                            onComplete: function( ) {
                                $( 'CID-' + CID ).inject( $( 'CurrentNews' ) ).removeProperty( 'style' ).getElement( '.publish' ).removeClass( 'publish' ).addClass( 'archive' ).removeEvents( ).addEvent( 'click', function ( ) {
                                    action.History( CID );
                                } );
                            }
                        } ).slideOut( );

                        Admin.Sortables.ContentsNews( );
                        Admin.Sortables.ContentsDraft( );
                    }
                } ).send( );
            }
        },

        Slide : function ( id ) {
            var CID = !( id % 1 ) ? id : null;

            if ( CID ) {
                var load   = $( 'CID-' + CID ).getParent( ).getNext( );
                var star   = $( 'CID-' + CID ).getElement( 'span[class^=star]' );
                var status = star.hasClass( 'star-on' ) ? 'off' : 'on';

                new Request ( {
                    url : domain + 'json/admin/daily-slide',
                    link : 'chain',
                    method : 'post',
                    data : { 'CID' : CID, 'status' : status },

                    onRequest : function ( ) {
                        load.addClass( 'loader' );
                    },

                    onSuccess : function ( ) {
                        load.removeClass( 'loader' );
                        star.removeProperty( 'class' ).addClass( 'star-' + status );
                    }
                } ).send( );
            }
        }
    } );

    Admin.Events = new Class ( {
        Event : function ( ) {
            var panelEvents  = $( 'Events' );
            var today = new Date( );
            today = today.getFullYear( ) + '-' +  ( ( today.getMonth( ) + 1 ) < 10 ? '0' + ( today.getMonth( ) + 1 ) : today.getMonth( ) ) ;

            new Request.JSON ( {
                url : domain + 'json/admin/events.json',
                method : 'post',

                onSuccess : function ( transport ) {
                    //panelEvents.empty( ).removeClass( 'loader' );

                    /*if ( $chk( transport.data ) && transport.data.length ) {
                        var months = {
                            '1' : 'Enero',
                            '2' : 'Febrero',
                            '3' : 'Marzo',
                            '4' : 'Abril',
                            '5' : 'Mayo',
                            '6' : 'Junio',
                            '7' : 'Julio',
                            '8' : 'Agosto',
                            '9' : 'Setiembre',
                            '10' : 'Octubre',
                            '11' : 'Noviembre',
                            '12' : 'Diciembre'
                        }
                    }*/
                },

                onRequest : function ( ) {
                    //panelEvents.empty( ).set( { 'html' :'&nbsp;', 'class' : 'loader' } );
                }
            } ).post( { 'date' : today} );

        }
    } );

    Admin.PhotoStock = new Class ( {
        Galleries : function ( ) {
            alert ( 'galerias panel' );
        },

        PhotoStock : function ( ){
            var iconList = { };

            var panelMain = $( 'Panel' );

            var panelRight = new Element ( 'div', { 'class' : 'prepend-1 span-30 last', id : 'PanelRight' } );
            var titleImages = new Element( 'h3', { html : 'Ultimas imagenes' } ).inject ( panelRight );
            var panelImages = new Element( 'div', { 'class' : 'clearfix', id : 'ImageList', html : '&nbsp;' } ).inject( titleImages, 'after' );

            panelRight.inject( panelMain, 'bottom' );

            // --- LOAD IMAGES ---------------------------------------------------------------------
            new Request.JSON ( {
                url : domain + 'json/admin/photostock-list',
                method : 'get',

                onSuccess : function ( transport ) {
                    var panel = $( 'ImageList' );

                    panel.removeClass( 'loader' ).empty( );

                    if ( $chk ( transport.data ) && transport.data.length ) {
                        var dataSet = transport.data;

                        dataSet.each ( function ( img, index ) {
                            new Element ( 'img', {
                                    src : 'http://images.elprofesional.com.pe/photostock/' + img.Source
                                } ).inject ( new Element ( 'div', {
                                html : img.Texto,
                                'class' : ( ( index + 1 ) % 3 == 0 ? 'span-10 last' : 'span-10' )
                            } ).inject( panel ), 'top' ).addClass( 'thumbnail' );

                            if ( ( index + 1 ) % 3 == 0 ) {
                                new Element ( 'div', { 'class' : 'clearfix', 'html' : '&nbsp;' } ).inject( panel );
                            }
                        } );
                    }
                },

                onRequest : function ( ) {
                    $( 'ImageList' ).addClass( 'loader' );
                }
            } ).send ( );
        }
    } );

    Admin.Sortables = {
        Last : {
            Draft : [ ],
            News  : [ ]
        },

        ContentsDraft : function ( ) {
            $$( 'div#DraftNews div.datarow' ).each( function ( item ) { Admin.Sortables.Last.Draft.push( item.getProperty( 'id' ).split( /[-]/ )[ 1 ] ); } );

            new Sortables( 'DraftNews', {
                clone:true,
                revert: true,

                onStart: function( el ) { el.setStyle('background','#eee'); },

                onComplete: function( el ) {
                    var sorted = [];
                    el.erase( 'style' );

                    $$( 'div#DraftNews div.datarow' ).each( function ( pub ) { sorted.push( pub.getProperty( 'id' ).split( /[-]/ )[ 1 ] ); } );

                    if ( JSON.encode( Admin.Sortables.Last.Draft ) !== JSON.encode( sorted ) ) {
                        new Request( {
                            url : domain + 'json/admin/daily-sort',
                            method : 'post',

                            onComplete : function ( ) {
                                $( 'DraftToolBar' ).removeClass( 'loader-left' );
                            },

                            onRequest : function ( response ) {
                                $( 'DraftToolBar' ).addClass( 'loader-left' );
                            }
                        } ).post( { 'sort' :  sorted } );

                        Admin.Sortables.Last.Draft.empty( );
                        $$( 'div#DraftNews div.datarow' ).each( function ( item ) { Admin.Sortables.Last.Draft.push( item.getProperty( 'id' ).split( /[-]/ )[ 1 ] ); } );
                    }
                }
            } );
        },

        ContentsNews : function ( ) {
            $$( 'div#CurrentNews div.datarow' ).each( function ( item ) { Admin.Sortables.Last.News.push( item.getProperty( 'id' ).split( /[-]/ )[ 1 ] ); } );

            new Sortables( 'CurrentNews', {
                clone:true,
                revert: true,

                onStart: function( el ) { el.setStyle('background','#eee'); },

                onComplete: function( el ) {
                    var sorted = [];
                    el.erase( 'style' );

                    $$( 'div#CurrentNews div.datarow' ).each( function ( pub ) { sorted.push( pub.getProperty( 'id' ).split( /[-]/ )[ 1 ] ); } );

                    if ( JSON.encode( Admin.Sortables.Last.News ) !== JSON.encode( sorted ) ) {
                        new Request( {
                            url : domain + 'json/admin/daily-sort',
                            method : 'post',

                            onComplete : function ( ) {
                                $( 'PublishToolBar' ).removeClass( 'loader-left' );
                            },

                            onRequest : function ( response ) {
                                $( 'PublishToolBar' ).addClass( 'loader-left' );
                            }
                        } ).post( { 'sort' :  sorted } );

                        Admin.Sortables.Last.News.empty( );
                        $$( 'div#CurrentNews div.datarow' ).each( function ( item ) { Admin.Sortables.Last.News.push( item.getProperty( 'id' ).split( /[-]/ )[ 1 ] ); } );
                    }
                }
            } );
        }
    };

    Admin.ToolBar = {
        ContentsDraft : function ( ) {
            //alert ( 'make toolbar' );
        }
    }

    Gallery = new Class ( {
        initialize: function ( ) {
            this.el = $( 'show' );

            $$('#demos li').each ( function ( li, i ) { li.set( 'events', { 'click': this.start.pass( i + 1, this ) } ); }, this );

            this.start( );
        },

        start: function ( p ) {
            if (this.show) this.show.destroy( 'empty' );

            switch( p ){
                case 1:
                this.show = new Slideshow ( this.el, null, {
                    captions: true,
                    classes: ['', '', '', '', '', '', '', 'alternate-images'],
                    controller: true,
                    duration: 1250,
                    height: 400,
                    hu: 'gallery/',
                    overlap: false,
                    thumbnails: true,
                    transition: 'back:in:out',
                    width: 500
                } );
                break;
                case 2:
                this.show = new Slideshow.KenBurns ( this.el, null, {
                    captions: true,
                    controller: true,
                    delay: 4000,
                    duration: 1000,
                    height: 400,
                    hu: 'gallery/',
                    thumbnails: true,
                    width: 500,
                    zoom: 25
                } );
                break;
                case 3:
                this.show = new Slideshow ( this.el, null, {
                    captions: true,
                    classes: ['', '', '', '', '', '', '', '', '', '', 'alternate-thumbnails'],
                    controller: true,
                    duration: 1250,
                    height: 400,
                    hu: 'gallery/',
                    random: true,
                    thumbnails: true,
                    transition: 'back:out',
                    width: 500,
                    zoom: 25
                } );
                break;
                case 4:
                this.show = new Slideshow.Push ( this.el, null, {
                    captions: true,
                    controller: true,
                    delay: 2500,
                    duration: 2000,
                    height: 400,
                    hu: 'gallery/',
                    transition: 'elastic:out',
                    width: 500,
                    zoom: 25
                } );
                break;
                case 5:
                this.show = new Slideshow ( this.el, null, {
                    captions: true,
                    classes: ['', '', '', '', '', '', '', '', '', 'alternate-controller'],
                    controller: { transition : 'back:in:out' },
                    duration: 1000,
                    height: 400,
                    hu: 'gallery/',
                    linked: true,
                    thumbnails: true,
                    width: 500
                } );

                this.show.slideshow.retrieve( 'images' ).getElements( 'a' ).set( 'rel', 'lightbox' );

                var box = new Lightbox ( {
                'onClose': function ( ) { this.pause( false ); }.bind( this.show ),
                'onOpen': function ( ) { this.pause( true ); }.bind( this.show )
                } );

                box.image.addEvent( 'click', function( ){ this.close(); }.bind( box ));
                break;
                case 6:
                this.show = new Slideshow.Flash ( this.el, null, {
                    captions: true,
                    color: ['#EC2415', '#7EBBFF'],
                    controller: true,
                    delay: 3000,
                    duration: 3000,
                    height: 400,
                    hu: 'gallery/',
                    width: 500
                } );
                break;
                case 7:
                this.show = new Slideshow.KenBurns ( this.el, null, {
                    captions: true,
                    controller: true,
                    delay: 4000,
                    duration: 1000,
                    height: 400,
                    hu: 'gallery/',
                    linked: true,
                    thumbnails: true,
                    width: 500,
                    zoom: 0
                } );

                this.show.slideshow.retrieve('images').getElements('a').set('rel', 'lightbox');

                var box = new Lightbox ( {
                    onClose : function ( ) { this.pause( false ); }.bind( this.show ),
                    onOpen  : function ( ) { this.pause( true ); }.bind( this.show )
                } );

                box.image.addEvent( 'click', function ( ) { this.close( ); }.bind( box ) );
                break;
                case 8:
                this.show = new Slideshow.Fold ( this.el, null, {
                    captions: true,
                    center: false,
                    controller: true,
                    duration: 1000,
                    height: 400,
                    hu: 'gallery/',
                    transition: 'bounce:out',
                    width: 500
                } );
                break;
                default:
                this.show = new Slideshow.KenBurns ( this.el, null, {
                    captions: true,
                    classes: ['', '', '', '', '', '', '', '', '', 'alternate-controller'],
                    /*controller: { transition : 'back:in:out' },*/
                    delay: 4000,
                    /*duration: 1000,
                    height: 400,
                    /*hu: 'gallery/',*/
                    thumbnails: true,
                    transition: 'back:out',
                    width: 500,
                    zoom: 0
                } );

                this.show.slideshow.retrieve( 'images' ).getElements( 'a' ).set('rel', 'lightbox');

                var box = new Lightbox ( {
                    showControls : true,
                    descriptions : true,

                    onClose : function ( ) { this.pause( false ); }.bind( this.show ),
                    onOpen  : function ( ) { this.pause( true ); }.bind( this.show )
                } );

                box.image.addEvent( 'click', function ( ) { this.close( ); }.bind( box ) );
                break;
            }

            if (this.show.options.thumbnails){
            [ 'a', 'b' ].each( function ( p ) {
                new Element( 'div', { 'class': 'overlay ' + p } ).inject( this.show.slideshow.retrieve( 'thumbnails' ) );
            }, this );
            }
        }
    } );


    Pages.FolkSonomy = function ( options ) {
	var URL = document.location.href;

	if ( $defined( options.replace ) ) {
	    URL = URL.replace( options.replace.from, options.replace.to );
	}

        [
            { 'text' : 'Twitter',          'pic' : 'twitter.gif',    'url' : 'http://twitter.com/home?status=Currently reading ' },
	    { 'text' : 'Facebook',         'pic' : 'facebook.gif',   'url' : 'http://www.facebook.com/sharer.php?u=' },
            { 'text' : 'Del.icio.us',      'pic' : 'delicious.gif',  'url' : 'http://del.icio.us/post?url=' },
            { 'text' : 'Technorati',       'pic' : 'technorati.gif', 'url' : 'http://technorati.com/faves/?add=' },
            { 'text' : 'Digg',             'pic' : 'digg.gif',       'url' : 'http://digg.com/submit?phase=2&url=' },
            { 'text' : 'Meneame',          'pic' : 'meneame.gif',    'url' : 'http://meneame.net/login.php?return=/submit.php?url=' },
            { 'text' : 'Google',           'pic' : 'google.jpg',     'url' : 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' },
            { 'text' : 'Yahoo! Bookmarks', 'pic' : 'yahoo.png',      'url' : 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=' },
            { 'text' : 'Live Favorites',   'pic' : 'live.gif',       'url' : 'http://favorites.live.com/quickadd.aspx?marklet=1&mkt=es-us&url=' },
            //{ 'text' : 'Furl',             'pic' : 'furl.gif',       'url' : 'http://www.furl.net/storeIt.jsp?u=' },
            { 'text' : 'Fresqui',          'pic' : 'fresqui.png' ,   'url' : 'http://tec.fresqui.com/post?url=' },
            { 'text' : 'Mister Wong',      'pic' : 'misterwong.gif', 'url' : 'http://www.mister-wong.com/index.php?action=addurl&bm_url=' }
        ].each( function ( item ) {
            new Element( 'img', {
                'src' : icons + item.pic,
                'alt' : item.text,
                'title' : 'Enviar a ' + item.text
            } ).inject( new Element( 'a', {
		href : item.url + encodeURIComponent( URL ),
		events : {
                    'click' : function ( e ) { e.stop( ); window.open( item.url + encodeURIComponent( document.location.href ) ); }
                }
	    } ).inject( $( options.target ) ) );
        } );
    }

    Pages.Main = function ( ) {
        var Slides = new viewer( $( 'Slides' ).getChildren( ), {
            mode : 'right',
            interval : 10000,

            onWalk : function ( current ) {
                Navbar.removeClass( 'active' );
                Navbar[ current ].addClass( 'active' );
            }
        } );

        var Navbar = $$( '#Controls span' );
        Navbar.each( function ( el, i ) { el.addEvent( 'click', Slides.walk.bind( Slides, [ i, true ] ) ); } );

        Slides.play( true );

        // --- Noticias --------
        $$( 'div.daily' ).each( function ( notice ) {
            notice.addEvents( {
                click : function ( ) { window.location = this.get( 'dir' ); },
                mouseenter : function ( ) { this.addClass( 'new' ) },
                mouseleave : function ( ) { this.removeClass( 'new' ) }
            } );
        } );

        /*new Element( 'div', {
            styles: {
                'position' : 'absolute',
                'top'      : 212,
                'left'     : $( 'Main' ).getPosition( ).x,
                'z-index'  : 100
            },
            'height' : 350,
            'id'     : 'FloatColombia',
            'width'  : 830
        } ).inject( document.body );

        new Swiff ( '/online/swf/adColombia.swf', {
            container:'FloatColombia',
            id : 'swfFloatColombia',
            width:830,
            height:350,
            params:{
                wmode:'transparent',
                bgcolor:'#ffffff'
            }
        } )*/
    }

    function closeFloat ( ) {
        var options = Array.link( arguments, { id : String.type } );

        if ( $defined( options.id ) && $defined( $( options.id ) ) ) {
            $( options.id ).setStyles( { visibility: 'hidden', display: 'none' } );
        }
    }

    Request.QueryString = function ( ) {
        var param  = arguments[ 0 ] || null;
        var query  = [ ];

        $$( 'script' ).each( function ( script ) {
            var url = script.src.replace ( /^[^\?]+\??/, '' );
            if ( url.length ) { query.push( url ); }
        } );

        query = query.join( '&' );

        if ( param && param.length && query.length ) {
            var items = query.split( /[&]/ );
            var value = null

            items.each( function ( item ) {
                if ( item.indexOf ( param ) != -1 ) {
                    value = item.split ( /[=]/ )[ 1 ];
                    return;
                }
            } );

            query = value;
        }

        return query;
    };

    var domain  = Request.QueryString ( 'domain' );
    var icons   = Request.QueryString ( 'icons' );
    var images  = Request.QueryString ( 'images' );
    var section = Request.QueryString ( 'section' );

    // --- CORE IMPLEMENT --------------------------------------------------------------------------
    Element.implement( {
        textLimit : function ( limit ) {
            this.addEvents( {
                'keyup' : function ( ) {
                    var label = this.getParent( ).getFirst( 'label' );
                    var text  = this.get( 'value' );
                    var chars = this.get( 'lang' ) - text.length;
                    var msg   = ( chars >= 0 ) ? ' ( ' + chars + ' caracteres restantes )' : ' ( Limite excedido )'

                    label.set( 'text', label.get( 'text' ).split( ' ' )[ 0 ] + msg );
                },

                'blur' : function ( ) {
                    var label = this.getParent( ).getFirst( 'label' );
                    var text  = this.get( 'value' );
                    var chars = this.get( 'lang' ) - text.length;
                    var msg   = ( chars >= 0 ) ? ' ( ' + chars + ' caracteres restantes )' : ' ( Limite excedido )'

                    label.set( 'text', label.get( 'text' ).split( ' ' )[ 0 ] + msg );
                }
            } );
        }
    } );

    window.addEvents( {
        domready : function ( ) {
            var path         = location.pathname;
            var btnHome      = $( 'btnHome' ) || null;
            var btnFavorites = $( 'btnFavorites' ) || null;

            if ( btnHome ) {
                btnHome.set( {
                    'events' : {
                        'click' : function ( ) {
                            if ( Browser.Engine.trident ) {
                                this.style.behavior='url(#default#homepage)';
                                this.setHomePage( domain );
                            }
                        }
                    },
                    'styles' : {
                        'cursor' : 'pointer'
                    }
                } );
            }

            if ( $chk( $( 'DropDown' ) ) && $chk( $( 'DropDown' ).getElement( '.' + section ) ) ) {
                $( 'DropDown' ).getElement( '.' + section ).addClass( 'current' );
            }

            if( btnFavorites ) {
                btnFavorites.set( {
                    'events' : {
                        'click' : function ( ) {
                            if ( window.sidebar ) {
                                window.sidebar.addPanel( document.title, domain, "" );
                            } else if( window.opera && window.print ) {
                                var mbm = document.createElement( 'a' );
                                mbm.setAttribute( 'rel', 'sidebar' );
                                mbm.setAttribute( 'href', domain );
                                mbm.setAttribute( 'title', document.title );
                                mbm.click( );
                            } else if ( document.all ) {
                                window.external.AddFavorite ( domain, document.title );
                            } else {
                                alert ( 'Use Ctrl+D para agregar esta página a sus favoritos' );
                            }
                        }
                    },
                    'styles' : {
                        'cursor' : 'pointer'
                    }
                } );
            }

            if ( $( 'Boletin' ) ) {
                $( 'Boletin' ).addEvent ( 'click', function ( ) {
                    window.location = 'http://www.traveltips.com.pe';
                } )
            }

            if ( $( 'Members' ) ) {
                $( 'Members' ).addEvent ( 'click', function ( ) {
                    window.location = 'http://boletin.traveltips.com.pe/lists/?p=subscribe&id=1';
                } )
            }

            if ( $( 'Online' ) ) {
                $( 'Online' ).addEvent ( 'click', function ( ) {
                    newWindow ( 'http://www.elprofesional.com.pe/online/online.htm', 'EPOL', 882, 600, '' );
                } );
            }

            switch ( section ) {
                case 'admin' :
                    var regexp = /^\/.*\/admin\/?(.*)/;
                    var zone = regexp.exec( path );

                    if ( zone.length == 2 ) {
                        var page = ( zone[ 1 ] == '' ) ? 'inicio' : zone[ 1 ].split( '.' )[ 0 ];

                        $$( 'div#AdminMenu ul.tabs li' ).each( function ( Tab ) { Tab.setProperty( 'class', ( Tab.getProperty( 'id' ) == page ? 'on' : 'off' ) ); } );

                        switch ( page ) {
                            case 'inicio' :
                                break;
                            case 'contenidos' :
                                $$( '#mContents ul li' ).each( function ( buttonTab ) {
                                    buttonTab.addEvent ( 'click', function ( ) {
                                        if ( !this.hasClass( 'on' ) ) {
                                            var Contents = new Admin.Contents( );

                                            this.getParent( ).getElements( 'li[class=on]' ).removeClass( 'on' );
                                            this.addClass( 'on' );

					    //alert( this.getElement( 'strong' ).get( 'id' ) );

                                            Contents[ this.getElement( 'strong' ).get( 'id' ) ]( );
                                        }
                                    } );
                                } );

                                var Contents = new Admin.Contents( );

                                Contents[ $( 'mContents' ).getElement( 'li[class=on] strong' ).get( 'id' ) ]( );
                                break;
                            case 'eventos' :
                                var events = new Admin.Events( );

                                events.Event( );

				$( 'SendEvent' ).addEvent( 'click', function( e ) {
				    console.log( e );
				} );

                                break;
                            case 'fototeca' :
                                $$( '#mFototeca ul li' ).each( function ( buttonTab ) {
                                    buttonTab.addEvent ( 'click', function ( ) {
                                        if ( !this.hasClass( 'on' ) ) {
                                            var PhotoStock = new Admin.PhotoStock ( );

                                            this.getParent( ).getElements( 'li[class=on]' ).removeClass( 'on' );
                                            this.addClass( 'on' );

                                            PhotoStock[ this.getElement( 'strong' ).get( 'id' ) ]( );
                                        }
                                    } );
                                } );

                                var PhotoStock = new Admin.PhotoStock ( );

                                PhotoStock[ $( 'mFototeca' ).getElement( 'li[class=on] strong' ).get( 'id' ) ]( );
                                break;
                        }
                    }
                    break;

                case 'eventos' :
                    var eventHeight = 50;
                    $$( '.event' ).each( function ( day ) {
                        var height = day.getSize( ).y;

                        if( day.getElements( 'ul li' ).length ) { day.addClass( 'active' ); }
                        if ( height > eventHeight ) { eventHeight = height; }
                    } );

                    $$( '.event' ).tween( 'height', eventHeight + 'px' );

                    var TooTips = new Tips ( $$ ( '.tooltip' ), {
                        onShow : function ( tip ) { tip.fade ( 'in' ); },
                        onHide : function ( tip ) { tip.fade ( 'out' ); }
                    } );
                    break;
                case 'galerias' :
                    var TooTips = new Tips ( $$ ( '.tooltip' ), {
                        onShow : function ( tip ) { tip.fade ( 'in' ); },
                        onHide : function ( tip ) { tip.fade ( 'out' ); }
                    } );

                    new Gallery( );
                    break;
                case 'main' :
                    new Tips ( $$ ( '.tooltip' ), {
                        onShow : function ( tip ) { tip.fade ( 'in' ); },
                        onHide : function ( tip ) { tip.fade ( 'out' ); }
                    } );

                    $( 'SurveyVote' ).addEvent( 'click', function ( ) {
                        var selected = false;

                        $$('input[name=VoteOption]').each( function ( option ) {
                            if( option.get( 'checked' ) ) {
                                selected = true;

                                new Request.JSON( {
                                    url : domain + 'json/survey-vote',
                                    method : 'post',

                                    onSuccess : function ( transport ) {
                                        if ( $chk( transport.data ) && transport.data.length ) {
                                            alert( 'Gracias por emitir su voto' );
                                            //mostrar resultados de encuesta
                                        }
                                    }
                                } ).post( { 'vote' : option.get( 'value' ) } );
                            }
                        } );

                        if ( !selected ) {
                            alert( 'Debe de escoger una opcion' );
                        }
                    } );
                    Pages.Main( );
                    break;
                case 'multimedia' :
                    var videos = $$( 'img.video' );

                    if ( $chk( videos ) && videos.length ) {
                        videos.each ( function ( video, index ) {
                            if ( index == 0 ) {
                                var VID  = video.get( 'name' ).split( '-' )[ 1 ];
                                var file = video.get( 'lang' ).split( '|' );

                                var swf =  new SWFObject( 'http://media.elprofesional.com.pe/player.swf', 'player', '335', '270', '9', '#000000' );

                                $( 'VidTitle' ).set( 'text', $( 'Title-' + VID ).get( 'text' ) );
                                $( 'VidText' ).set( 'text', $( 'Text-' + VID ).get( 'text' ) );

                                swf.addVariable( 'flv', 'http://media.elprofesional.com.pe/videos/' + file[ 0 ] );
                                swf.addVariable( 'jpg', 'http://images.elprofesional.com.pe/video/' + file[ 1 ] );
                                swf.addParam( 'effect', true );
                                swf.addParam( 'allowFullScreen', 'true' );
                                swf.write( 'Video' );
                            }

                            video.addEvent ( 'click', function ( ) {
                                var VID  = this.get( 'name' ).split( '-' )[ 1 ];
                                var file = this.get( 'lang' ).split( '|' );

                                var swf =  new SWFObject( 'http://media.elprofesional.com.pe/player.swf', 'player', '335', '270', '9', '#000000' );

                                $( 'VidTitle' ).set( 'text', $( 'Title-' + VID ).get( 'text' ) );
                                $( 'VidText' ).set( 'text', $( 'Text-' + VID ).get( 'text' ) );

                                swf.addVariable( 'flv', 'http://media.elprofesional.com.pe/videos/' + file[ 0 ] );
                                swf.addVariable( 'jpg', 'http://images.elprofesional.com.pe/video/' + file[ 1 ] );
                                swf.addParam( 'effect', true );
                                swf.addParam( 'allowFullScreen', 'true' );
                                swf.write( 'Video' );
                            } );
                        } );
                    }
                    break;
		case 'hemeroteca':
		    Pages.FolkSonomy ( {
			target : 'Folksonomy',
			replace : { from : 'hemeroteca', to : 'noticias' }
		    } );
		    break;
                case 'noticias' :
                    Pages.FolkSonomy ( { target : 'Folksonomy' } );
                    break;
            }
        }
    } );
