var myRequest = null;var valid = null;
function validate_code(value)
{
    myRequest.send('cod=' + value);
    if (valid === true) return true;
    return false;
}

function is_email_registered(value)
{
    myRequest.send('email=' + value);
    if (valid === true) return true;
    return false;
}

function is_phone_registered(value)
{
    myRequest.send('mobil=' + value);
    if (valid === true) return true;
    return false;
}

function check_age()
{
    if ($('anul').value != '' && $('luna').value != '' && $('ziua').value != '') {
        var year;
        var now = new Date();
        var current_day = now.getDate();
        var current_month = now.getMonth();
        var current_year = now.getFullYear();
        if (
            current_month > parseInt($('luna').value) ||
            (
                current_month == parseInt($('luna').value) &&
                current_day >= parseInt($('ziua').value)
            )
        ) {
            year = parseInt($('anul').value);
        } else {
            year = parseInt($('anul').value) + 1;
        }
        var age = current_year - year;
        if (age > 18) {
            return true;
        }
    }
    return false;
}

window.addEvent('domready', function() {
    var mySmoothScroll = new Fx.SmoothScroll();
    myRequest = new Request({
        url: 'validate/',
        async: false,
        onRequest: function() {
            valid = null;
        },
        onSuccess: function(response) {
            if (response == 'valid') {
                valid = true;
            } else {
                valid = false;
            }
        }
    });
    $$('a[class*=-submit]').each(function(anchor) {
        anchor.addEvent('click', function(e) {
            e.stop();
            var matches = this.get('class').match(/([^\s]+)\-submit/);
            window[matches[1] + '_object'].submit();
        });
    });
    if ($('confirma')) {
        $('confirma').addEvent('click', function(e) {
            e.stop();
            data_nasterii_object.submit();
        });
    }
    //var mootips = new Tips('.tooltip', {fixed: true, offset: {'x': -9, 'y': -58}});
    $$('a').addEvent('focus', function() { this.blur(); });
    if ($('slider')) {
        var inner_container = $('inner-container');
        var message = $('message');
        var messages = message.getElements('span');
        var images = inner_container.getElements('img').length;
        var position = 1;
        var coordinates = null;
        inner_container.setStyle('width', images * 520);
        message.set('tween', {
            duration: 250,
            onComplete: function() {
                if (message.getStyle('bottom') == '-34px') {
                    messages.setStyle('display', 'none');
                    $('message' + position).setStyle('display', 'block');
                    message.tween('bottom', 0);
                }
            }
        });
        inner_container.set('tween', {
            duration: 500,
            onStart: function() {
                message.tween('bottom', -34);
            }
        });
        $('next').addEvent('click', function() {
            if (position < images) {
                position++;
                coordinates = inner_container.getCoordinates($('outer-container'));
                inner_container.tween('left', - ((position - 1) * 520));
            }
        });
        $('previous').addEvent('click', function() {
            if (position > 1) {
                position--;
                coordinates = inner_container.getCoordinates($('outer-container'));
                inner_container.tween('left', - ((position - 1) * 520));
            }
        });
    }
    if ($('current')) {
        $('orase').set('tween', { duration: 250 });
        var dimensions = $('orase').getSize();
        var position = $('current').getCoordinates($('content'));
        $('orase').setStyles({
            top: position.top - (dimensions.y / 2) + 'px',
            left: position.left,
            opacity: 0
        });
        $('orase').addEvents({
            'mouseout': function() {
                $('orase').setStyle('visibility', 'hidden');
            },
            'mousemove': function() {
                $('orase').setStyle('visibility', 'visible');
            }
        });
        $('current').addEvent('click', function(e) {
            e.stop();
            $('orase').tween('opacity', [0, 1]);
        });
    }
    if ($('gallery')) {
        function managebutton()
        {
            if (current_page == pages - 1 && current_page > 0) {
                $('gallery-navigation').set('class', 'nonext');
            } else if (current_page == 0 && pages > 1) {
                $('gallery-navigation').set('class', 'noprev');
            } else if (pages == 1) {
                $('gallery-navigation').set('class', 'nonav');
            }
        }
        var pages = $('gallery').getElements('.page').length;
        var width = $('gallery').getStyle('width').replace(/px/, '');
        var inner_container = $('inner-container');
        var current_page = 0;
        managebutton();
        inner_container.set('tween', {duration:250});
        inner_container.setStyle('width', pages * width);
        $('gallery-navigation').getElement('.next').addEvent('click', function(e) {
            e.stop();
            if (current_page < pages - 1) {
                current_page++;
                inner_container.tween('left', -(current_page * width));
                managebutton();
            }
        });
        $('gallery-navigation').getElement('.previous').addEvent('click', function(e) {
            e.stop();
            if (current_page > 0) {
                current_page--;
                inner_container.tween('left', -(current_page * width));
                managebutton();
            }
        });
    }
    if ($('peronizeaza')) {
        var form = null;
        var button1 = null;
        var button2 = null;
        var spinner = null;
        var error_message = null;
        var uploaded_picture = null;
        var coords = null;
        function init()
        {
            var file_upload_control = $('imagine');
            js_file_upload_control = new Element('input', {
                type: 'file',
                name: 'imagine',
                id: 'imagine',
                'events': {
                    'change': function() {
                        form.submit();
                        form.setStyle('display', 'none');
                        spinner.setStyle('display', 'block');
                        error_message.setStyle('display', 'none');
                        button1.setStyle('display', 'none');
                        image_notice.setStyle('display', 'none');
                        button2.setStyle('display', 'none');
                    }
                }
            });
            js_file_upload_control.replaces(file_upload_control);
        }
        form = $('peronizeaza');
        spinner = $('spinner');
        error_message = $('error-message');
        uploaded_image = $('uploaded-image');
        button1 = $('upload');
        image_notice = $('image-notice');
        button2 = $('cancel');
        init();
        form.addEvent('submit', function(e) {
            e.stop();
        });
        button1.addEvent('click', function(e){
            e.stop();
            //$(window).scrollTo(0, 0);
            init();
            error_message.setStyle('display', 'none');
            form.setStyle('display', 'block');
            if (null == coords) {
                coords = form.getCoordinates();
                spinner.setStyles({
                    width: coords['width'],
                    height: coords['height']
                });
            }
            button1.setStyle('display', 'none');
            image_notice.setStyle('display', 'none');
            button2.setStyle('display', 'block');
        });
        button2.addEvent('click', function(e){
            e.stop();
            form.setStyle('display', 'none');
            error_message.setStyle('display', 'none');
            button2.setStyle('display', 'none');
            button1.setStyle('display', 'block');
            image_notice.setStyle('display', 'block');
        });
    }

    // initialize the multibox engine
    if ($$('.lightbox').length > 0) {
        initMultiBox = new multiBox({
            mbClass: '.lightbox',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
            container: $(document.body),//where to inject multiBox
            descClassName: 'lightbox-description',//the class name of the description divs
            path: './public/files/javascripts/multibox/',//path to mp3 and flv players
            useOverlay: true,//use a semi-transparent background. default: false;
    		contentColor: '#FFF',//background colour of the content holder within the pop-up
    		showNumbers: false,//show numbers such as "4 of 12"
    		showControls: false,//show the previous/next, title, download etc
            maxSize: null,//max dimensions (width,height) - set to null to disable resizing
            addDownload: false,//do you want the files to be downloadable?
            pathToDownloadScript: '',//if above is true, specify path to download script (classicASP and ASP.NET versions included)
            addRollover: false,//add rollover fade to each multibox link
            addOverlayIcon: false,//adds overlay icons to images within multibox links
            addChain: false,//cycle through all images fading them out then in
            recalcTop: true,//subtract the height of controls panel from top position
            addTips: false//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
        });

    }


    $$('a._blank').set('target', '_blank');
});