$(function() {

    /* css needs to know is js enabled */
    $('body').addClass('has_JS');

    /* Do we have something to scroll? If true show prev & next links */
    if($('#news_container .news_content').size() > 1){
        $('#news_container').after('<a id="next" href="#" title="Next"><img src="/css/cssimg/navigation/next.png" width="85" height="60" alt="Next" /></a><a id="prev" href="#" title="Prev"><img src="/css/cssimg/navigation/prev.png" width="85" height="60" alt="Prev" /></a>');
    }
    
    if ($('#gallery_container ul').size() > 1){
        $('#gallery_container').after('<a id="next" href="#" title="Next"><img src="/css/cssimg/navigation/next.png" width="85" height="60" alt="Next" /></a><a id="prev" href="#" title="Prev"><img src="/css/cssimg/navigation/prev.png" width="85" height="60" alt="Prev" /></a>');
    }
    
    if($('#other_news p').size() > 1){
        $('#other_news h2').after('<a href="#" title="#" class="prev_small"><img src="/css/cssimg/content/prev_small.gif" width="5" height="7" alt="Prev" /></a><span class="current_title_num">3/3</span><a href="#" title="#" class="next_small"><img src="/css/cssimg/content/next_small.gif" width="5" height="7" alt="Next" /></a>');
    }
    
    /*Shadowbox */

    Shadowbox.init({ flvPlayer: '/flash/media/player.swf' });

    
    /* News and gallery scroller*/
    $('#body_news_homepage #news_container, #gallery_container').cycle({ 
        fx:     'scrollHorz', 
        prev:   '#prev', 
        next:   '#next', 
        easing: 'easeOutCirc',
        speed: 400,
        timeout: 0,
        after: show_slide_number,
        before: show_hide_nav 
    });
    
    /* Other news scroller */
    $('#news_title_scroll').cycle({ 
        fx:     'scrollHorz', 
        prev:   '.prev_small', 
        next:   '.next_small', 
        easing: 'easeOutCirc',
        speed: 400,
        timeout: 0,
        after: show_slide_number,
        before: show_hide_nav 
    });
    
    
    /* Which event to show */
    $current_event = 0;
    $real_current_event = 0;    
    $('#body_events_overview .news_content').each(function (){

        $current_event++;
        var class_name = $(this).attr('class').split(' ');
        if(class_name[1] == 'current'){  
            $real_current_event = $current_event - 1;
        }
    });

    /* Event scroller */
    $('#body_events_overview #news_container').cycle({ 
        fx:     'scrollHorz', 
        prev:   '#prev', 
        next:   '#next', 
        easing: 'easeOutCirc',
        startingSlide: $real_current_event,
        speed: 400,
        timeout: 0,
        after: show_slide_number,
        before: show_hide_nav 
    });
    
    function show_slide_number (curr, next, opts){
    
        var elem = $(this).parent().attr('id');
        
        if(elem == 'news_container'){
            $('#news_container .first_child span').html(opts.currSlide + 1 + "/" + opts.slideCount);
        }
        if(elem == 'news_title_scroll'){
            $('.current_title_num').html(opts.currSlide + 1 + "/" + opts.slideCount);
        }
        
        $('#gallery_indicator span:eq('+opts.currSlide+')').addClass('active');
    }
    
    function show_hide_nav (curr, next, opts){
    
        var elem = $(this).parent().attr('id');
        var elem_class = $(this).parent().attr('class');
        
        if(elem == 'news_container'){
            var index = $(this).parent().children().index(this);
        	$('#next')[index == 0 ? 'hide' : 'show']();
        	$('#prev')[index == opts.slideCount - 1 ? 'hide' : 'show']();
        }
        
        if(elem_class == 'event_list'){
            var index = $(this).parent().children().index(this);
        	$('#prev')[index == 0 ? 'hide' : 'show']();
        	$('#next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
        }
        
        if(elem == 'news_title_scroll'){
            var index = $(this).parent().children().index(this);
        	$('.next_small')[index == 0 ? 'hide' : 'show']();
        	$('.prev_small')[index == opts.slideCount - 1 ? 'hide' : 'show']();
        }
        
        if(elem == 'gallery_container'){
        
            /* Different behaviour of prev and next links */
            var index = $(this).parent().children().index(this);
        	$('#prev')[index == 0 ? 'hide' : 'show']();
        	$('#next')[index == opts.slideCount - 1 ? 'hide' : 'show']();

            /* Gallery indicators */
            $('#gallery_indicator').empty();
            for(i=0;i<opts.slideCount;i++){
                $('#gallery_indicator').append('<span>&nbsp;</span> ');
            }
            var width = (opts.slideCount * 4) + (opts.slideCount * 10) + 'px'; 
            $('#gallery_indicator').css({'width':width});
            

        }
    }

    
    /* Fading images in header */
    $('#header #bkg').crossSlide({
      sleep: 3,
      fade: 1  
    },[
      { src: '/images/content/header_bkg/papaya-header-1.jpg'},
      { src: '/images/content/header_bkg/papaya-header-2.jpg'},
      { src: '/images/content/header_bkg/papaya-header-4.jpg'},
      { src: '/images/content/header_bkg/papaya-header-5.jpg'},
      { src: '/images/content/header_bkg/papaya-header-6.jpg'}

    ]);    
    
    
    /* Function for clearing inputs default values on focus */
    $.fn.clearOnFocus = function(){
        
        return this.focus(function(){
            var v = $(this).val();
            $(this).val( v === this.defaultValue ? '' : v );
        }).blur(function(){
            var v = $(this).val();
            $(this).val( v.match(/^\s+$|^$/) ? this.defaultValue : v );
        });

    };

    $('#event_timeline input').clearOnFocus();
    
    $('#event_timeline button').click(function(){
    
        var v = $(this).parent().parent().find('input').val();
        if(v == 'my.email@mymailbox.com' || v == ''){
            return false;
        }
    
    });
    

    

});





