// ###########################################################################   GLOBAL VARIBLES

var scroll_position = 0;
var step = 142*3;
var akt_category = 'video'




// ###########################################################################   FUNCTIONS
function showCategory(cat)
{
    if(cat == 'photo')
    {
        $("#panelLinks a.active").removeClass('active');
        $("#link-photos").addClass('active');

        $('#photoGallery').show();
        $('#videoGallery').hide();

        akt_category = "photo"


        show_bottomspinner_if_should();
        $("#bottombox_link").attr("innerHTML","<a href='http://picasaweb.google.com/youthtv.eu' target='_blank' >see all YouthTV's photos on Picasa</a>");
    }

    if(cat == 'video')
    {
        $("#panelLinks a.active").removeClass('active');
        $("#link-videos").addClass('active');

        $('#videoGallery').show();
        $('#photoGallery').hide();

        akt_category = "video"
        
        show_bottomspinner_if_should();
        $("#bottombox_link").attr("innerHTML","<a href='http://www.youtube.com/user/wkruszewski' target='_blank' >see all videos on YouTube</a>");

    }

    scroll_position = 0;
    init_scroller_buttons();

}


function show_bottomspinner_if_should()
{
    if( typeof(photo_size) == "undefined") $('#bottomBox_spinner').show();
    if (akt_category == "video") $('#bottomBox_spinner').hide();
    return false;
}


function init_scroller_buttons()
{

    scrollable().animate({left: 0 });


    if (akt_category == "video") scroll_elements = video_size;
    if (akt_category == "photo" &&   typeof(photo_size) != "undefined")
    {
         scroll_elements = photo_size;
    }
     else
    {
        scroll_elements == 0;
    }

    if(scroll_elements <=4)
    {
        block_right_scroll();        
        block_left_scroll();

    }
    else
    {
       block_left_scroll();
       unblock_right_scroll();
    }

    return false;
}


function block_left_scroll()
{
    $("#link-scroll-left").css({backgroundPosition: '-40px 0px'});
    return false;
}

function block_right_scroll()
{
    $("#link-scroll-right").css({backgroundPosition: '-40px 0px'});
    return false;
}

function unblock_left_scroll()
{
    $("#link-scroll-left").css({backgroundPosition: '0px 0px'});
    return false;
}

function unblock_right_scroll()
{
    $("#link-scroll-right").css({backgroundPosition: '0px 0px'});
    return false;
}


function getPhotos(token)
{
    $.ajax({
        beforeSend:function(request){},
        complete:function(request){ if( akt_category == "photo" ){init_scroller_buttons();} $('#bottomBox_spinner').hide(); },
        data:'authenticity_token=' +token,
        success:function(request){$('#photoGallery').html(request+"asdasd");},
        type:'post',
        url:'photo/show'});

    return false;
}


function set_css_for_newsletter_submit()
{
    ob = $("input:submit", document.forms[0]).addClass('submit');
    return false;
}


function scrollable()
{
   if (akt_category == "video") { ob = $("#videoGallery .scrolling"); }
   if (akt_category == "photo") { ob = $("#photoGallery .scrolling"); }
   return ob
}


// ###########################################################################   DOCUMENT::READY
$(document).ready(function() {


    init_scroller_buttons();


    $("#link-scroll-left").click(function(){

                                   if ( scroll_position+step <= 0)
                                   {
                                       scroll_position = scroll_position+step;
                                       scrollable().animate({left: scroll_position });
                                   
                                       unblock_right_scroll();

                                       if ( scroll_position+step > 0 )
                                       {
                                            block_left_scroll();
                                       }
                                   }

                                 });


   $("#link-scroll-right").click(function(){

                                    if (akt_category == "video") {scroll_elements = video_size;  }
                                    if (akt_category == "photo") {scroll_elements = photo_size;  }

                                
                                   if ( scroll_elements - (Math.abs(scroll_position-step) / (step/3)) > 0 )
                                   {
                                       scroll_position = scroll_position-step;
                                       scrollable().animate({left: scroll_position });

                                       unblock_left_scroll();

                                       if ( scroll_elements - (Math.abs(scroll_position-step) / (step/3)) < 1 )
                                       {
                                            block_right_scroll();
                                       }
                                   }

                                
                                 });

                                 

        
    set_css_for_newsletter_submit();

})


