Cufon.replace('.access-cufon-text', { fontFamily: 'VAGRounded', hover: true});

var access = {
    init: function()
    {
        access.panel_hover();
        access.title_open();
        access.load_more();
        access.add_comment();
        access.setupCal();
    },
    panel_hover: function()
    {
        $('.access-item').mouseenter(function(e){
            if($.browser.msie)
            {
                $(this).css('backgroundPositionY', '-240px'); 
            }
            else
            {
                var pos = $(this).css('backgroundPosition').split(' ');
                $(this).css({'background-position' : pos[0]+' -240px','cursor':'pointer'});
            }
            $(this).find('img.access-thumb').hide();
            $(this).find('img.access-thumb-hover').show();
            if($(this).find('div.image-overlay').length > 0)
            {
                if($.browser.msie)
                {
                    $(this).find('div.image-overlay').css({'backgroundPositionY' : '-170px','cursor':'pointer'});                
                }
                else
                {
                    var pos_overlay = $(this).find('div.image-overlay').css('backgroundPosition').split(' ');
                    $(this).find('div.image-overlay').css({'background-position' : pos_overlay[0]+' -170px','cursor':'pointer'});                
                }
            }       
        });
        $('.access-item').mouseleave(function(e){
            if($.browser.msie)
            {
                $(this).css('backgroundPositionY', '0px');                
            }
            else
            {
                var pos = $(this).css('backgroundPosition').split(' ');
                $(this).css('backgroundPosition',pos[0]+' 0px');
            }
            $(this).find('img.access-thumb-hover').hide();
            $(this).find('img.access-thumb').show();  
            if($(this).find('div.image-overlay').length > 0)
            {
                if($.browser.msie)
                {
                    $(this).find('div.image-overlay').css({'backgroundPositionY' : '0px','cursor':'pointer'});
                }
                else
                {
                    var pos_overlay = $(this).find('div.image-overlay').css('backgroundPosition').split(' ');
                    $(this).find('div.image-overlay').css({'background-position' : pos_overlay[0]+' 0px','cursor':'pointer'});                      
                }
            }
        });
        $('.access-item').click(function(e){
            window.location = $(this).find('a:first').attr('href');
        });        
    },
    title_open: function()
    {
        $('#access-nav-top li a.about-link').click(function(e){
            e.preventDefault();
            if($('div#access-title-text-container').height() == 0)
            {
                $('div#access-title-text-container').animate({height: '460px'});
            }
            else
            {
                $('div#access-title-text-container').animate({height: '0px'});            
            }
    
        });
        
        $('div#access-title-text-container a.title-link-close').click(function(e){
            e.preventDefault();
            $('div#access-title-text-container').animate({height: '0px'});
        });
        
        /*
        $('div#access-title-container a').toggle(function(e){
            alert('hello');
            e.preventDefault();
            $('div#access-title-text-container').animate({'height':'400px'});
        },function(e){
            e.preventDefault();
            $('div#access-title-text-container').animate({'height':'0px'});
        }); */    
    },
    load_more: function()
    {
        $('#access-more-link a').click(function(e){
            e.preventDefault();
            $.ajax({url: $(this).attr('href'),
                    dataType: 'json',
                    success: function(response){
			         content = $(response.html);
                        $('.access-content-container').append(content);
            			if(!response.link)
            			{
            				$('#access-more-link a').hide();
            			}
            			else
            			{                        
            				$('#access-more-link a').attr('href',response.link);
            				$('#access-more-link a').show();
            			}
                        access.panel_hover();
                        Cufon.refresh();
                }
            });
        });
    },
    add_comment: function()
    {

        $('a.add_comment').click(function(e){
            e.preventDefault();
            $.fn.colorbox({href: $(this).attr('href'),
                           innerWidth: 460,
                           transition: 'none',
                           opacity: 0.90,
                           onComplete: function()
                           {
                            $('#close_comment').click(function(e){
                               e.preventDefault();
                               $.fn.colorbox.close(); 
                            });
                            frontend.form_submit();
                           }
            });
        });

    },
    setupCal: function(){
        if($('.access-event-cal').length > 0){
            var currentDate = new Date();
            $.ajax({url:'/homepage/getEvents/month/'+(currentDate.getMonth()+1)+'/year/'+currentDate.getFullYear()+'/app/scottisharchitecture',
                            dataType: 'json',
                            success: function(response){
                                var initial_data = response;
                                $.jMonthCalendar.Initialize({
                                    containerId: ".access-event-cal",
                                    dragableEvents: false,
                                    onCalLoaded: function(){
                                        $('.access-item-event').remove();
                                        $('.access-content-container').append(initial_data.event_text);
                                        access.panel_hover();
                                        Cufon.refresh();

                                    },
                                    onMonthChanged: function(){
                                        Cufon.refresh();
                                    },
                                    onMonthChanging: function(dateIn) {
                                        var dateIn = dateIn;

                                            $.ajax({url:'/homepage/getEvents/month/'+(dateIn.getMonth()+1)+'/year/'+dateIn.getFullYear()+'/app/scottisharchitecture',
                                                    dataType: 'json',
                                                    success: function(response){
                                                        $.jMonthCalendar.ReplaceEventCollection(response.event_days);
                                                        var text = response.event_text;
                                                            $('.access-item-event').remove();
                                                            $('.access-content-container').append(response.event_text);
                                                            access.panel_hover();
                                                            Cufon.refresh();

                                                    }
                                                    
                                            });   
                                    }
    
                                }, response.event_days);
       
                            }
            });
        }
    }
        
}

$(function(){
    access.init();
});
