var healthierplaces = {
    init: function()
    {
    	if($("#events_cal").length > 0)
        {
            healthierplaces.setupEvents();
        }
        healthierplaces.scale_image();
        healthierplaces.cycle();
        healthierplaces.getting_started();
        healthierplaces.browser();
        healthierplaces.imageCycle();
        healthierplaces.imageCycleArchive();
    },
    scale_image: function(){
        $(".full_image img").cjObjectScaler({
            destObj: $(".full_image"),
            method: "fit", 
            fade: 800
        });
    },
    cycle: function(){
        if($('#cycle').length > 0)
        {
            $('#cycle').cycle({timeout: 0, speed: 500, height: 380, startingSlide: 0});
            $('.cycle_thumbnail').click(function(e){
                e.preventDefault();
                $('#cycle').cycle(parseInt($(this).attr('rel')));
                return false;
            });
        }
    },
    imageCycle: function(){
        if($('.thumbnail_cycle').length > 0)
        {
            $('.thumbnail_cycle').cycle({timeout: 0, speed: 500, height: 220, next: '.thumbnail_cycle_next', prev: '.thumbnail_cycle_prev',after: onAfterThumbnails,fx:'scrollHorz'});
        }
    },
    imageCycleArchive: function(){
        if($('.archive_thumbnail_cycle').length > 0)
        {
            $('.archive_thumbnail_cycle').cycle({timeout: 0, speed: 500, height: 220, next: '.archive_thumbnail_cycle_next', prev: '.archive_thumbnail_cycle_prev',after: onAfterThumbnailsArchive,fx:'scrollHorz'});
        }
        $('#show_archive_images').click(function(e){
            e.preventDefault();
            if($('#archive_images:visible').length > 0)
            {
                $('#archive_images').hide();
            }
            else
            {
                $('#archive_images').show();
            
            }
        });
    },
    getting_started: function(){
        if($('.hp_getting_started_content_panels').length > 0)
        {
            $('#gs_show_hide_link').click(function(e){
                e.preventDefault();
                $('.hp_getting_started_content').animate({height: 'toggle',border:'1px'},500,'linear',function(){
                    if($('#gs_show_hide_link').hasClass('open'))
                    {
                        $('#gs_show_hide_link').text('Getting started');
                        $('#gs_show_hide_link').css('backgroundImage','url(/images/healthierplaces/gs_open.png)');
                        $('.hp_getting_started h2').animate({opacity: 0}, 500);
                        $('#gs_show_hide_link').removeClass('open');
                        $('.hp_getting_started').css('borderBottom','1px solid #444444');
                    }
                    else
                    {
                        $('#gs_show_hide_link').text('Hide this');
                        $('#gs_show_hide_link').css('backgroundImage','url(/images/healthierplaces/gs_close.png)');
                        $('.hp_getting_started h2').animate({opacity: 1}, 500);                    
                        $('#gs_show_hide_link').addClass('open');
                        $('.hp_getting_started').css('borderBottom','3px solid #444444');
                    }                
                });
            });        
            $('.hp_getting_started_content_panels').cycle({timeout: 0,speed: 800, width: 860, height: 271, next: '.next', prev: '.prev', after: onAfter,fx:'scrollHorz'});
            
        }
    },
    browser: function(){
        if($('.hp_browser_slides').length > 0)
        {
            $('.hp_browser_slides').cycle({timeout: 0, speed: 800, next: '.next', prev: '.prev', after: onAfterBrowser,fx:'scrollHorz'});        
        }
    
    
    },
    setupEvents: function(){
        var currentDate = new Date();
        $.ajax({url:'/homepage/getEvents/month/'+(currentDate.getMonth()+1)+'/year/'+currentDate.getFullYear()+'/app/healthierplaces/landing/true',
                        dataType: 'json',
                        success: function(response){
                            var initial_data = response;
                            $.jMonthCalendar.Initialize({
                                containerId: "#events_cal",
                                dragableEvents: false,
                                onCalLoaded: function(){
                                    $("#events_cal").css('background-image','none');
                                    $('#events_text').html(initial_data.event_text);
                                    $('.clickable_nolink').fitted({'doLink':false});
                                },
                                onMonthChanging: function(dateIn) {
                                    var dateIn = dateIn;
                                    $('#events_text').animate(frontend.opacity_hide, 300, function(){
                                        $.ajax({url:'/homepage/getEvents/month/'+(dateIn.getMonth()+1)+'/year/'+dateIn.getFullYear()+'/app/healthierplaces/landing/true',
                                                dataType: 'json',
                                                success: function(response){
                                                    $.jMonthCalendar.ReplaceEventCollection(response.event_days);
                                                    var text = response.event_text;
                                                    
                                                        $('#events_text').html(response.event_text);    
                                                        $('#events_text').animate(frontend.opacity_show,300);
                                                        $('.clickable_nolink').fitted({'doLink':false});
                                                }
                                                
                                        });
                                    });                    
                                }

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

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

function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $('.prev')[index == 0 ? 'hide' : 'show']();
    $('.next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

function onAfterThumbnails(curr, next, opts) {
    var index = opts.currSlide;
    $('.thumbnail_cycle_prev')[index == 0 ? 'hide' : 'show']();
    $('.thumbnail_cycle_next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

function onAfterThumbnailsArchive(curr, next, opts) {
    var index = opts.currSlide;
    $('.archive_thumbnail_cycle_prev')[index == 0 ? 'hide' : 'show']();
    $('.archive_thumbnail_cycle_next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

function onAfterBrowser(curr, next, opts) {
    var index = opts.currSlide;
    
    var from = (index * 8) + 1;
    var to = from + 7;
    if(index == (opts.slideCount - 1))
    {
        to = $('#browser_count').val();
    }        
    $('.pager_text').text(from+' to '+to);
    
    
    $('.prev')[index == 0 ? 'hide' : 'show']();
    $('.no_prev')[index == 0 ? 'show' : 'hide']();    
    $('.next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
    $('.no_next')[index == opts.slideCount - 1 ? 'show' : 'hide']();

}
