
window.addEvent('domready',function(){
    
    if ($$('form#addpage input[name=url]')[0].value != '') addPresenter();
    
    $('capture').addEvent('submit', function(e){
        
        if (e) e.stop();
        if ($$('form#capture input[name=url]')[0].value != '') {
            $$('form#addpage input[name=url]')[0].value = $$('form#capture input[name=url]')[0].value;
            addPresenter();
        }
        
    });
    
    /*$$('#popupClose'/*,'#popupEnquire'*//*).addEvent('click', function(e){
        if (e) e.preventDefault();
        
        new Fx.Tween($('popup')).set('opacity', 0);
        
        new Fx.Tween($('overlay'), {
            duration: 500,
            onComplete: function(){
                $('overlay').destroy();
            }
        }).start('opacity', 0);
        
        $('popup').setStyles({'display': 'none', 'top': 0, 'opacity': 1});
        $('popupContent').empty();
        
        $$('.tab-intro p').each(function(el){el.setStyle('display','none');});
        $('when_closed').setStyle('display', 'block');
        
        if (this.id == 'popupEnquire') $('showOnClose').fireEvent('click');
    });*/
    
});

function addPresenter()
{
    $$('.tab-intro p').each(function(el){el.setStyle('display','none');});
    $('when_loading').setStyle('display', 'block');
    
    // validate url
    var inputUrl = $$('form#addpage input[name=url]')[0];
    var pgUrl = inputUrl.getProperty('value').toLowerCase();
    
    if (pgUrl.substr(0,7) != 'http://') {
        pgUrl = 'http://'+pgUrl;
        inputUrl.setProperty('value', pgUrl);
    }
    
    // validate page title
    var pgTitle = $$('form#addpage input[name=title]')[0].getProperty('value').substr(0, 30);
    if (pgTitle == '' || pgTitle == $$('form#addpage input[name=title]')[0].defaultValue) {
        pgTitle = pgUrl.replace('http://www.', '').replace('http://', '').substr(0, 30);
    }
    
    new Request.JSON({
        url       : base+'ajax.php',
        data      : 'action=webshot&url='+pgUrl+'&title='+pgTitle,
        noCache   : true,
        onRequest : function(){
            $('addgo').setStyle('display', 'block');
        },
        onSuccess : function(json){
            
            $('addgo').setStyle('display', 'none');
            
            if (!json || json.error) {
                
                $$('.tab-intro p').each(function(el){el.setStyle('display','none');});
                $('when_error').setStyle('display', 'block');
                
                alert((!json ? 'No data was returned on the last request.\nThe web service may be temporarily unavailable.' : json.error));
                
            } else {
                
                var presenter = json.id;
                
                new Element('div', {
                    id: 'overlay',
                    styles:{
                        'width'  : window.getScrollSize().x,
                        'height' : window.getScrollSize().y,
                        'opacity': 0
                    }
                }).inject(document.body);
                
                new Fx.Tween($('overlay'), {
                    duration: 500,
                    onComplete: function(){
                        
                        $('popupStartOver').removeEvents();
                        
                        var popupW = window.getSize().x - 60;
                        var popupH = window.getSize().y - 60;
                        
                        if (popupW < 316) popupW = 316;
                        if (popupH < 500) popupH = 500;
                        if (popupW > 1280) popupW = 1280;
                        
                        $$('#popup div.container').setStyle('height', popupH-20);
                        $('popupContent').setStyle('height', popupH-58);
                        $$('#popup div.action').setStyle('padding-left', popupW-464);
                        
                        $('popupStartOver').addEvent('click', function(e){
                            
                            if (e) e.preventDefault();
                            
                            $('popupContent').set('html', '');
                            
                            new Element('iframe', {
                                src: 'ajax.php?action=place&presenter='+presenter+($defined(e) ? '&startover=true' : ''),
                                frameborder: 0,
                                styles: {
                                    'border' : 0,
                                    'width'  : '100%',
                                    'height' : $('popupContent').getStyle('height').toInt(),
                                    'float'  : 'left'
                                }
                            }).inject($('popupContent'));
                            
                        });
                        
                        $('popupStartOver').fireEvent('click');
                        
                        presenterPopup = presenter;
                        
                        $('popup').setStyles({
                            'display'     : 'block',
                            'top'         : window.getScrollTop() + 30,
                            'margin-left' : -(popupW/2).round(),
                            'width'       : popupW,
                            'height'      : popupH,
                            'opacity'     : 1
                        });
                        
                    }
                }).start('opacity', 0.8);
                
            }
            
        }
    }).send();   
}

