Maccabi = {

    calendar : null,

    init : function() {

        this.initNewsletter();
        this.initSearchBar();
        this.initSideMenu();
        this.initCalendarUrl();
        
        this.flash.init();
        this.navigator.init();
    },

    initColorbox : function() {

        $('a.colorbox').colorbox({});
        $('a.colorboxPage').live('click', this.colorboxPage);
    },

    initNewsletter : function() {
        
        $('#newsletter a').live('click', function(event) {

            $(this).closest('form').submit();

            return false;
        });
        
        $('form.newsletter').live('submit', function(event) {

            $(this).closest('form').ajaxSubmit({
                target: '#sf_content'
            });
            
            location.hash = '';

            return false;
        });
    },

    initSearchBar : function() {

        $('#searchBar input').live('focus', function(event) {

            if($(this).val() == 'Keresés...')
                $(this).val('');

            return false;
        });

        $('#searchBar input').live('blur', function(event) {

            if($(this).val() == '')
                $(this).val('Keresés...');

            return false;
        });

        $('#searchBar a').live('click', function(event) {

            $(this).closest('form').submit();

            return false;
        });
        
        $('#searchBar form').live('submit', function(event) {
            
            var form = $(this).closest('form');
            
            /*form.ajaxSubmit({
                target: '#sf_content'
            });*/
            
            location.hash = '#' + Maccabi.routing.search + '?' + form.formSerialize();
            
            return false;
        });
    },

    initSideMenu : function() {

        $('#sideMenu .item').live('mouseover', function(event) {

            $(this).addClass('over');

            var submenu = $(this).find('div.submenu');

            if(submenu.length) {

                submenu.show();
            }
        });

        $('#sideMenu .item').live('mouseout', function(event) {

            $(this).removeClass('over');
            $(this).find('div.submenu').hide();
        });

        $('#sideMenu .subitem').live('mouseover', function(event) {

            $(this).addClass('over');
        });

        $('#sideMenu .subitem').live('mouseout', function(event) {

            $(this).removeClass('over');
        });
    },
    
    colorboxPage : function() {
        
        $.colorbox({
            onComplete: function() {Maccabi.modules.comment.init()},
            opacity: 0.5,
            width: 605,
            href: $(this).attr('href').replace('#','')
        });

        return false;
    },
    
    initCalendarUrl : function() {
        
        $('#calendar a.fc-event').live('click', this.colorboxPage);
    },
    
    initCalendar : function(data) {
        
        this.calendar = $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            titleFormat: {
                month: 'yyyy. MMMM',
                week: "yyyy. MMMM d[ yyyy].{ '&#8212;'[ MMM] d.}",
                day: 'yyyy. MMMM d., dddd'
            },
            timeFormat: {
                agenda: 'H:mm{ - H:mm}',
                '': 'H:mm'
            },
            columnFormat: {
                month: 'ddd',
                week: 'ddd M/d',
                day: 'dddd M/d'
            },
            axisFormat: 'H:mm',
            allDaySlot: false,
            allDayText: 'Egész nap',
            buttonText: {
                today:  'ma',
                month:  'hónap',
                week:   'hét',
                day:    'nap'
            },
            monthNames : [
                'Január','Február','Március','Április','Május','Júnuis',
                'Július','Augusztus','Szeptember','Október','November','December'
            ],
            monthNamesShort: [
                'Jan','Feb','Már','Ápr','Máj','Jún',
                'Júl','Aug','Szep','Okt','Nov','Dec'
            ],
            dayNames: [
                'Vasárnap','Hétfő','Kedd','Szerda','Csütörtök','Péntek','Szombat'
            ],
            dayNamesShort: [
                'Vasárnap','Hétfő','Kedd','Szerda','Csütörtök','Péntek','Szombat'
            ],
            //theme: true,
            firstDay: 1,
            editable: false,
            
            /*selectable: true,
            selectHelper: true,
            select: function(start, end, allDay) {

                    var title = prompt('Event Title:');

                    if(title) {

                            Maccabi.calendar.fullCalendar('renderEvent',
                                    {
                                            title: title,
                                            start: start,
                                            end: end,
                                            allDay: allDay
                                    },
                                    true // make the event "stick"
                            );
                    }

                    Maccabi.calendar.fullCalendar('unselect');
            },*/
            
            eventSources: [ {
                    url: Maccabi.routing.event_ajaxFeed,
                    data: data
                }
            ],
            
            loading: function(isLoading, view) {
                
                if(!isLoading) {
                }
            }
        });
    },

    trackUrl : function(url) {
return;
        //alert('x' + typeof(window.pageTracker))
        window._gaq.push(['_trackPageview', url]);
        window._gaq.push(['_trackEvent', 'view', url]);
        //_trackEvent('category', 'action', 'opt_label', 'opt_value');
    },
    
    showLoader : function() {
        
        $('#sf_content').html('<img style="margin: 35px 270px;" src="/images/ajax-loader.gif" />');
    },
    
    hideLoader : function() {
        
        
    }
}

Maccabi.routing = {
    homepage: '',
    e401: '',
    e404: '',
    e500: '',
    event_ajaxFeed: ''
    // and more ...
}

Maccabi.navigator = {
    
    url : null,
    
    init : function() {
        
        $('a.ajaxized').live('click', function() {
            
            Maccabi.navigator.go($(this).attr('href'));
            
            return false;
        });
        
        $('form.ajaxized').live('submit', function() {
            
            var form = $(this).closest('form');
            
            form.ajaxSubmit({
                target: '#content'
            });
            
            return false;
        });
        
        $.fn.hashchange.src = Maccabi.routing.homepage;

        $(window).hashchange(function() {

            var hash = location.hash;
            var url = hash.replace(/^#/, '');

            //if(hash == '')
            //    url = $.fn.hashchange.src;

            //document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';

            if(url && url.match('^/') == '/') {

                Maccabi.navigator.load(Maccabi.navigator.url = url);
                
            } else {
                
                Maccabi.navigator.initUrl(Maccabi.navigator.url = window.location.pathname);
            }
        })

        $(window).hashchange();
    },

    go : function(url) {

        url = url.replace('#', '');
        location.hash = '#' + url;
    },

    load : function(url) {

        Maccabi.showLoader();

        if(Maccabi.currentModule != null) {

            if(Maccabi.currentModule.unInit)
                Maccabi.currentModule.unInit();
            
            Maccabi.currentModule = null;
        }

        Maccabi.trackUrl(url);

        $('#sf_content').load(url, function(responseText, textStatus, XMLHttpRequest) {

            if(XMLHttpRequest.status == 401) {

                Maccabi.navigator.go(Maccabi.routing.e401);

                if(Maccabi.user.isAuthenticated)
                    Maccabi.welcomeBox.logout();

            } else if(XMLHttpRequest.status == 404) {

                Maccabi.navigator.go(Maccabi.routing.e404);
            }

            Maccabi.navigator.initUrl(url);
            Maccabi.hideLoader();
        });
    },
    
    initUrl : function(url) {
        
        url = url.replace(new RegExp('(.*)(/esemenyek)/(.*).html'), '$1/event/show');
        url = url.replace(new RegExp('(.*/szakosztaly)/(.*).html'), '$1');
        url = url.replace(new RegExp('(.*/galeria)(.*)(\\.html)'), '$1/show');
        url = url.replace(new RegExp('(.*/galeria/show)(.+)'), '$1Image');
        
        switch(url) {

            default:
                //debug('default url:' + url);
                break;

            case Maccabi.routing.e401:
            case Maccabi.routing.e404:

                break;

            case Maccabi.routing.event_show:
            case Maccabi.routing.galeria_show:
            case Maccabi.routing.galeria_showImage:
                
                Maccabi.modules.comment.init();

                break;

            case Maccabi.routing.szakosztaly_show:

                Maccabi.currentModule = Maccabi.modules.szakosztaly;
                Maccabi.currentModule.init();

                break;
        }

        Maccabi.initColorbox();
    }
}

Maccabi.flash = {

    container_id : 'flash',
    queue : [],
    created : false,

    init : function() {

        var flashvars = {};
        var params = {wmode: 'transparent'};
        var attributes = { };

        swfobject.embedSWF(
            "/swf/Maccabi.swf",
            'flash',
            "840", "280",
            "9.0.0",
            "expressInstall.swf",
            flashvars, params, attributes
        );
    },

    onCreated : function() {

        this.created = true;
        this.execQueue();
    },

    addQueue : function(f) {
        
        if(this.created)
            f();
        else
            this.queue.push(f);
    },
    
    execQueue : function() {

        while(this.queue.length)
            this.queue.shift()();
    },

    getObject : function() { // FIXME

        if(navigator.appName.indexOf("Microsoft") !=-1) {
            player = window[this.container_id];
        } else {
            player = document[this.container_id];
        }

        return player;
    }
}

Maccabi.modules = {
}
