window.addEvent('domready', function() {

//     // just for Internet Explorer
//     var navigationlinks = $$('#navigation a');
//
//     navigationlinks.addEvent('mouseover', function() {
//
//         navigationlinks.setStyle('visibility', 'hidden');
//         this.setStyle('visibility', 'visible');
//
//     });
//
//     navigationlinks.addEvent('mouseout', function() {
//
//         navigationlinks.setStyle('visibility', 'visible');
//
//     });

    httpRequest = new Request();

    function load_page(url)
    {

        $$('.page-element').tween('opacity', [1, 0]);

        (function() {

            httpRequest.options.url = url;

            httpRequest.addEvent('success', function(result) {

                $('content').set('html', result);

                show_page();

            });

            httpRequest.send();

        }).delay(250);

    }

    function show_page() {

        if ($('aplica-acum')) {

            $('judet').addEvent('change', function() {

                $('aplica-acum').submit();

            });

            $('localitate').addEvent('change', function() {

                $('aplica-acum').submit();

            });

        }

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

        $$('.lightbox').addEvent('click', function(e) {e.stop()});

        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;
        		showNumbers: false,//show numbers such as "4 of 12"
        		showControls: true,//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)
            });

        }

        var delay = 250;

        var counter = 0;

        $$('.page-element').each(function(elem) {

            (function() {$(elem).set('tween', {duration: 500}).tween('opacity', [0, 1])}).delay(counter++ * delay);

        });

        $$('.HTMLForm').each(function(form) {

            var tooltip = form.getElement('.tooltips');

            if (null != tooltip) {

                tooltip.set('reveal', {duration: 250});

                form.set('send', {

                    url:    form.get('action'),
                    method: 'post',
                    onSuccess: function(result) {

                        if (result != '') {

                            tooltip.set('html', result);
                            tooltip.reveal();
                            (function() {tooltip.dissolve()}).delay(2000);

                        } else {

                            if (form.get('id') == 'aplica-acum') {

                                var base_path = $$('base').getLast().get('href');

                                document.location.href = base_path + 'aplica-acum/confirmare/';

                            } else {

                                form.set('tween', {duration: 250});
                                form.tween('opacity', [1, 0]);
                                (function() {$$('.confirmare').reveal()}).delay(250);

                            }

                        }

                    }

                });

                form.getElement('.form-submit').addEvent('click', function(e) {

                    e.stop();

                    if (tooltip.getStyle('display') == 'none') {

                        form.send();

                    }

                });

            }

        });

        if ($('slider-outer-container')) {

            var outer_container = $('slider-outer-container');

            var dimensions = outer_container.getSize();

            var pages = $$('.slider-page');

            $('slider-inner-container').setStyle('width', dimensions.x * pages.length);

            var tabs = $$('ul.tabs a');

            var default_index = 0;

            var clip = null;

            tabs.each(function(tab, index) {

                if (tab.get('class').match(/\-selected/)) {

                    default_index = index;

                }

                tab.addEvent('click', function(e) {

                    if (!tab.hasClass('skip')) {

                        e.stop();

                        tabs.each(function(tb) {

                            tb.set('class', tb.get('class').replace(/\-selected/, ''));

                        })

                        tab.set('class', tab.get('class') + '-selected');

                        $('slider-inner-container').tween('left', -(index * dimensions.x));

                        if ($('studentomat-tabs')) {

                            if (index != 0 && $('player')) {

                                clip = $('player').dispose();

                            } else if (index == 0 && clip != null) {

                                clip.inject('page1');

                            }

                            if (index == 0) {
                                $('fb_programe_pentru_studenti').setStyle('display', 'block');
                                $('fb_promotii').setStyle('display', 'none');
                            } else if (index == 1) {
                                $('fb_programe_pentru_studenti').setStyle('display', 'none');
                                $('fb_promotii').setStyle('display', 'none');
                            } else if (index == 2) {
                                $('fb_programe_pentru_studenti').setStyle('display', 'none');
                                $('fb_promotii').setStyle('display', 'block');
                            }

                        } else if ($('studentocard-tabs')) {

                            if (index == 0) {
                                $('fb_studentocardul').setStyle('display', 'block');
                                $('fb_pachetul_pentru_studenti').setStyle('display', 'none');
                                $('fb_cum_aplici').setStyle('display', 'none');
                            } else if (index == 1) {
                                $('fb_studentocardul').setStyle('display', 'none');
                                $('fb_pachetul_pentru_studenti').setStyle('display', 'block');
                                $('fb_cum_aplici').setStyle('display', 'none');
                            } else if (index == 2) {
                                $('fb_studentocardul').setStyle('display', 'none');
                                $('fb_pachetul_pentru_studenti').setStyle('display', 'none');
                                $('fb_cum_aplici').setStyle('display', 'block');
                            }

                        }

                    }

                });

            });

            $('slider-inner-container').setStyle('left', -(default_index * dimensions.x));

            // on the home page
            if ($$('img.pagination') != '') {

                if (pages.length == 1) $$('img.pagination').setStyle('visibility', 'hidden');

                function nextpage()
                {

                    if (onfx == false && pages.length > 1) {

                        timer = $clear(timer)

                        onfx = true;

                        ++slide;

                        if (slide > 2) { slide = 0 }

                        $('slider-inner-container').tween('opacity', [1, 0]);

                        (function(){

                            $('slider-inner-container').setStyle('left', -(slide * dimensions.x))

                            $('slider-inner-container').tween('opacity', [0, 1]);

                            (function(){

                                onfx = false;

                            }).delay(250);

                        }).delay(250);

                        timer = nextpage.periodical(5000);

                    }

                    //$('slider-inner-container').tween('left', -(slide * dimensions.x))

                }

                var slide = 0;

                var onfx = false;

                $('slider-inner-container').set('tween', {duration: 250});

                $$('area.next').addEvent('click', function(e) {

                    e.stop();

                    if (pages.length > 1) nextpage();

                });

                $$('area.prev').addEvent('click', function(e) {

                    e.stop();

                    if (onfx == false && pages.length > 1) {

                        timer = $clear(timer)

                        onfx = true;

                        --slide;

                        if (slide < 0 ) { slide = 2 }

                        $('slider-inner-container').tween('opacity', [1, 0]);

                        (function(){

                            $('slider-inner-container').setStyle('left', -(slide * dimensions.x))

                            $('slider-inner-container').tween('opacity', [0, 1]);

                            (function(){

                                onfx = false;

                            }).delay(250);

                        }).delay(250);

                        timer = nextpage.periodical(5000);

                    }

                    //$('slider-inner-container').tween('left', -(slide * dimensions.x))

                });

                var timer = nextpage.periodical(15000);

            }

        }

        $$('div.slider-page').each(function(content) {

            var slider_items = content.getElements('div.accordion-item');

            var default_accordion_index = 0;

            content.getElements('div.accordion-item').each(function(item, index) {

                if (item.hasClass('preselected')) {

                    default_accordion_index = index;

                }

            });

            content.getElements('.accordion-handler').each(function(handler, index) {

                var item = handler.getNext('div.accordion-item');

                item.set('reveal', {duration: 250});

                if (index == default_accordion_index) {

                    item.setStyle('display', 'block');

                }

                handler.addEvent('click', function() {

                    if (item.getStyle('display') == 'none') {

                        slider_items.dissolve(),

                        (function() { item.reveal() }).delay(250);

                    }

                });

            });

        });

//         $$('a.ajax').addEvent('click', function(e) {
//
//             e.stop();
//
//             load_page(this.href);
//
//         });

    }

    if ($('teaser')) {
        function highlight(index)
        {
            el = clickables[index];
            clickables.each(function(clickable) {
                clickable.set('class', clickable.get('class').replace(/\-selected/, ''));
                if (!clickable.hasClass('png')) {
                    clickable.set('class', clickable.get('class') + ' png');
                }
            });
            var show_cladire = el.get('class').match(/^(.*?)\spng/);
            show_cladire = show_cladire[1];
            el.set('class', show_cladire + '-selected png');
            cladiri.setStyles({
                'opacity': 0,
                'display': 'none'
            });
            tip.setStyle('opacity', 0);
            cladiri.each(function(cladire) {
                var current_cladire = cladire.get('class').match(/^cladire\s(.*?)\s|^cladire\s(.*?)$/);
                if (
                    (undefined != current_cladire[1] && show_cladire == current_cladire[1]) ||
                    (undefined != current_cladire[2] && show_cladire == current_cladire[2])
                ) {
                    cladire.setStyle('display', 'block');
                    fxs[index].start('opacity', [0, 1]);
                    tip_content.set('html', cladire.get('html'));
                    tip.setStyle('display', 'block');
                    tip.tween('opacity', 1);
                }
            });
        }

        var stepper = function()
        {
            position++;
            if (position > clickables.length - 1) position = 0;
            highlight(position);

        }

        var fxs = new Array;
        var position = 0;
        var tip = $('tip');
        var tip_content = tip.getElement('.body');
        var cladiri = $$('.cladire');
        var clickables = $$('#cladiri a');
        tip.set('tween', {duration: 250});
        clickables.each(function(clickable, index) {
            fxs[index] = new Fx.Tween(cladiri[index], { duration: 250 });
            clickable.addEvent('click', function(e) {
                e.stop();
                this.blur();
                highlight(index);
                position = index;
                $clear(timeout);
                timeout = stepper.periodical(6000);
            });
        });

        var timeout = stepper.periodical(6000);

    }

    show_page();

    if ($$('a.showpopup').length > 0) {

//         initMultiBox = new multiBox({
//             mbClass: '.showpopup',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
//             autoOpen: 1,
//             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;
//     		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)
//         });

        Mediabox.open('popup.html', 'PimpMyRoom - Da Refresh camerei tale de camin!', '953 580');

        var today = new Date(),
            expire = new Date();
        expire.setTime(today.getTime() + 3600000 * 24 * 1);
        document.cookie = 'homepopup=1;expires=' + expire.toGMTString();

    }

});
