/*
Javascript Description 
CREDIT http://www.advancestep.com/ 
 */

var Index = new Class({
    options: {
        errorTxt: 'error',
        form: 'formMain',
        feild: {},
        Msg: {},
        proform : ''
    },

    initialize: function(elements, options){
        this.setOptions(options);
        this.elements = $(elements);
        var myCal1 = new Calendar({
            DepartOn: 'd-m-Y'
        }, {
            classes: ['dashboard2'],
            direction: 1,
            tweak: {
                x: -22,
                y: -5
            }
        });
        if( this.elements )
            this.build();
    },
	
    build: function(){
        this.elements.addEvent( 'click', function(){
            __doPostBack('saveBtn$Click', '', this.options.form);
        }.bind(this));
    }
	
});

Index.implement(new Events, new Options);

window.addEvent('load', function(){
    if( $('formMain') )
        var form2page = new Proform( $('formMain'), {
            selectText: 'select',
            classes: 'perpage-'
        });

    var index = new Index($('submit_reservation'), {
        form: 'formMain',
        feild: {
            '1' : 'leaving_form',
            '2' : 'DepartOn',
            '3' : 'TimeOn'
        },
        Msg: {
            '1' : 'please select leaving form',
            '2' : 'please enter departon',
            '3' : 'please enter timeon'
        },
        proform: form2page
    });
});

window.addEvent('domready', function(){
    var arr_pop = new Array();

    $$('ul.attension li[class^="lightbox"]').each(function(el){
        arr_pop.push(el.get('rel'));
    });

    if(arr_pop.length > 0){
        if($defined($('LightBox-overlay'))){
            var Overlay = $('LightBox-overlay');
        }
        else {
            var Overlay = new Element('div', {
                'class':'overlay',
                'id': 'LightBox-overlay'
            }).inject(document.body);
            Overlay.setStyles({
                'position': 'absolute',
                'left': 0,
                'top': 0,
                'width': window.getWidth(),
                'height': window.getScrollHeight(),
                'opacity': 0
            });
        }

        var box = new Element('div', {
            'class': "attensionLightBox",
            'id': 'attensionLightBox'
        }).inject(document.body);
        box.setStyles({
            'opacity': 0
        });

        var req = new Request.JSON({
            url:'_ajax/attension.php',
            onSuccess: function(json,response) {
                if(json) {
                    if(json.result == "error") {
                        alert(json.error);
                    }
                    else if(json.result == 'success') {
                        if(json.data != '') {
                            box.set('html', json.data);

                            Overlay.setStyles({
                                'opacity': 0.5,
                                'display': ''
                            });
                            box.setStyles({
                                'left': (window.getWidth().toInt() - box.getCoordinates().width.toInt()) / 2,
                                'top': (window.getHeight().toInt() - box.getCoordinates().height.toInt()) / 2
                            });
                            box.setStyle('opacity', 1);

                            $$('div.attensionLightBox a.close').each(function(el){
                                el.addEvent("click", function(){
                                    box.setStyle('opacity', 0);
                                    Overlay.setStyles({
                                        'opacity': 0,
                                        'display': ''
                                    });
                                });
                            });
                        }
                    }
                }
                else {
                    alert(response);
                }
            }
        }).send('attensionid='+arr_pop[0]);
		
		$$('ul.attension li a.readmore').each(function(el){
			el.setStyle('cursor', 'pointer');
			el.addEvent('click', function(){ req.send('option=click&attensionid='+this.get('rel')); });
		});
    }
});