
var site_success_message_timeout_period = 5000; //5 seconds
var site_error_message_timeout_period = 10000; //5 seconds   

$(document).ready(function() {

	//Remove site message boxes
	setTimeout(clear_site_success_message, site_success_message_timeout_period);
	setTimeout(clear_site_error_message, site_error_message_timeout_period);
    
    //Start ticker tape msg
    do_ticker();
    
    // Find a team city search
    if($('#findteamcountry').val() != 'GB')
    {
        $('#findteamcity, #ftc_label').hide();
    }
    if($('#findteamcountry').val() != 'US')
    {
        $('#findteamstate, #fts_label').hide();
    }
    if($('#findteamcountry').val() != 'GB' && $('#findteamcountry').val() != 'US')
    {
        $('#fts_label, #ftc_label').hide();       
    } 
    $('#findteamcountry').change(function(){
        var cc = $(this).val();
        if(cc == 'GB')
        {
            $('#findteamcity, #ftc_label').show();
            $('#findteamstate, #fts_label').hide();
        }
        else if(cc == 'US')
        {
            $('#findteamstate, #fts_label').show();
            $('#findteamcity, #ftc_label').hide();
        }
        else
        {
            $('#findteamcity, #findteamstate, #ftc_label, #fts_label').hide(); 
        }
    });
                   
    
    // Toggle comments
    $('a#togglecomments').click(function(){
        $('div#commentbulk').slideToggle('slow');
    });  
    
    // Hide who's attending
    $('.live_content').hide();
    
    // Toggle who's attending
    $('a.who').click(function(){
        $('div#live_content_' + this.id).slideToggle('slow');
    });
    
    // Mark attending
    $('a.markattending').click(function(){
        var thisId = this.id;
        $.post("./packages/members/data/attend.php", {item: thisId}, function(data){
            $("#live_content_" + thisId).html(data);
            $('div#live_content_' + thisId).slideDown('slow');    
          });
    });
    
       
    $('a.togglemembercontent').click(function(){   
        if($('div#container_' + this.id).is(':visible')){
            $('img#img_' + this.id).attr({src : "../../images/icon_panel_expand.jpg"}); 
        } 
        else{
            $('img#img_' + this.id).attr({src : "../../images/icon_panel_collapse.jpg"}); 
        }
        $('div#container_' + this.id).slideToggle('slow');
        
    });
    
    // Reporting
    $('a.reportcomment').click(function(){
        var thisId = this.id;
        $.post("./packages/members/data/report.php", {item: thisId}, function(data){
            Boxy.alert("<p>Thank you for reporting this content.  A moderator will look at it, and remove or edit if deemed inappropriate.</p>", null, {title: 'Report Content'});
        });
    });
    
    $('a.reportmembercomment').click(function(){
        var thisId = this.id;
        $.post("../../packages/members/data/report.php", {item: thisId}, function(data){
            Boxy.alert("<p>Thank you for reporting this content.  A moderator will look at it, and remove or edit if deemed inappropriate.</p>", null, {title: 'Report Content'});
        });
    });
    
    // Reporting
    $('input.addyourcomment').click(function(){
        if($('textarea.addcomment').val() == '' || $('textarea.addcomment').val() == 'Add your comment...')
        {
            Boxy.alert("<p>Please enter your comment.</p>", null, {title: 'Error'});
            return false;
        }
    });
    
    // Popout player
    $('a.popoutplayer').click(function(){
        var thisId = this.id;
        var offset = '';
        if(thisId == 'members'){
            offset = '../../';
        }
        window.open(offset + "popoutplayer.php", 'popoutplayer', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=164,left = 322,top = 183');
    });
    
    /** Gallery Thumb Scroller */ 
    var mover;
    var pos = 0;
    var framerate = 30;
    var max_up = $('.thumbnails').height() - $('#thumbcontainer').height();
    $('a#gallerythumbsup, a#gallerythumbsdown').mousedown(function(){
        var move;
        if(this.id == 'gallerythumbsup')
        {
            move = 'down';    
        }
        else if(this.id == 'gallerythumbsdown')
        {
            move = 'up';    
        }
        mover = setInterval(function() {
            scroll_thumbs(move)
        }, 1000/framerate); 
    });
    $('a#gallerythumbsup, a#gallerythumbsdown').mouseup(function(){
        clearInterval(mover);  
    });
    
    
    
    $('#albumpicker').change(function(){
        window.location = base_url + 'photos/' + $(this).val();
    });
    
    $('a.toggle_attending').click(function(){
        $('#attending_' + this.id).slideToggle();
    });
    
    // Galleries
    var loading_img = false;
    $('a.prevgallimg, a.nextgallimg, a.thumbgallimg').click(function(){
        if(!loading_img)
        {
            loading_img = true;
            var parts = this.id.split('_');
            var album = parts[0];
            var img = parts[1]; 
            //alert(img);
            //document.location.hash = 'album=' + album.toString() + '&img=' + img.toString();
            get_image(album, img);  
            get_image_comments(album, img);  
        }      
    });


    function do_ticker()
    {
        var ticker_width = $('#tickermsg').width();
        var container_width = ($('#ticker_overflow').width()) + 50;
        $('#tickermsg').css('left', container_width + 'px');
        $('#tickermsg').animate({
            'left': '-=' + (ticker_width + container_width)
            }, 20000, 'linear', function() {
                do_ticker();
        });
    }

    function scroll_thumbs(dir)
    {
        switch(dir)
        {
            case 'up':
                pos = pos - 10;
                if(pos < max_up)
                {
                    pos = max_up;
                }
                break;
            case 'down':
                pos = pos + 10;
                if(pos > 0)
                {
                    pos = 0;
                }
                break;
        }
        if(pos <= 0 && pos >= max_up)
        {
            $('#thumbcontainer').css('top', pos.toString() + 'px');
        }
    }

    function get_image(album, img)
    {
        $.ajax({
            type: "POST",
            dataType: 'json',
            //data: 'album=' + album.toString() + '&img=' + img.toString(),
            //url: "data/change_gallery.php?type=image",
            url: base_url + 'gallery/change_image/' + album.toString() + '/' + img.toString(),
            beforeSend: function(){
                // Show loading
                $('#gallery_caption').text('');     
                $('#main_gallery_image').html('<img src="' + base_url + 'css/images/ajax-loader.gif" style="margin-top: 200px" /><br /><p>Loading Image</p>');
                $('#melikey').html('');
            },
            success: function(data){
                // Get JSON response
                var res = data.image[0];
                $('#gallery_caption').text(res.alt);
                $('#main_gallery_image').html('<img src="' + base_url + res.image_url + '" height="' + res.height + '" />');
                // Do Prev?
                if(res.do_prev)
                {
                    $('a.prevgallimg').attr('id', res.album + '_' + res.prev);
                    $('a.prevgallimg').show();
                }
                else
                {
                    $('a.prevgallimg').hide();
                }
                // Do Next?
                if(res.do_next)
                {
                    $('a.nextgallimg').attr('id', res.album + '_' + res.next);
                    $('a.nextgallimg').show();
                }
                else
                {
                    $('a.nextgallimg').hide();
                }
                // Facebook like
				$('#melikey').html('<iframe src="http://www.facebook.com/plugins/like.php?href=' + base_url + '%2Fgallery%2F' + res.album + '%2F' + res.image_id + '&amp;layout=button_count&amp;show_faces=false&amp;width=90&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:90px; height:21px;" allowTransparency="true"></iframe>');
                loading_img = false;
            }
        });
    }

    function get_image_comments(album, img)
    {
        $.ajax({
            type: "POST",
            data: 'album=' + album.toString() + '&img=' + img.toString(),
            url: base_url + "gallery/change_gallery/comments",
            beforeSend: function(){
                //
                //$('#gallerycomments').html('<div align="center"><img src="./images/ajax-loader.gif" /><br /><p>Loading Comments</p></div>');
                //$('#gallerycomments').fadeTo('fast', '0');
            },
            success: function(data){
                // Get comments
                //$('#gallerycomments').html(data);
                //$('#gallerycomments').fadeTo('slow', '1');
            }
        });
    }
    
    // Attending
    $('.whoisattending').hide();
    $('a.view_attending').click(function(){
        var thisId = this.id; 
        $('#attending_' + thisId).slideToggle(); 
        $('#attending_' + thisId).html('<p>Fetching attending users...</p>');
        $.post(base_url + "ajax/attending/view", { show_id: thisId },
            function(data) {
               $('#attending_' + thisId).html(data);
               $('#attending_' + thisId).show();
         });   
    });
        
    //$('input').placeholder();
});

function set_and_display_error(message)
{
	$('.error_message').text(message);
	$('.error_message').slideDown();
	setTimeout(clear_site_error_message, site_error_message_timeout_period);
}
function set_and_display_success(message)
{
	$('.success_message').text(message);
	$('.success_message').slideDown();
	setTimeout(clear_site_success_message, site_success_message_timeout_period);
}


function clear_site_success_message() {
	$('.success_message').slideUp();
}

function clear_site_error_message() {
	$('.error_message').slideUp();
}

function checkSignup(form){
    if( form.name.value == '' || form.email.value == '' || form.name.value == 'name' || form.email.value == 'email' ){
        alert('Please enter your name and email address to sign up for the newsletter');
        return false;
    }
    else{
        return true;
    }
}
