// Globals:
var currentPhoto;

var post_counter = 3;

function loadVideo(video_id){
    $('#projector-player').html('<object width="666" height="374"><param name="movie" value="http://www.vevo.com/VideoPlayer/Embedded?videoId='+video_id+'&playlist=false&autoplay=0&playerId=62FF0A5C-0D9E-4AC1-AF04-1D9E97EE3961&playerType=embedded&env=0"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.vevo.com/VideoPlayer/Embedded?videoId='+video_id+'&playlist=false&autoplay=0&playerId=62FF0A5C-0D9E-4AC1-AF04-1D9E97EE3961&playerType=embedded&env=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="666" height="374" bgcolor="#000000"></embed></object>');
}

function clearVideo(){
    $('#projector-player').html('');
}

//
// End YouTube Player Functions
//

function showPhoto(id){
    currentPhoto = $('#photo-'+id);
    $('#photo-closeup .photo').hide();
    $('#photo-'+id).show();
    $('#photodots4').stop().animate({"height":"0px"},200,function(){
    $('#photodots3').stop().animate({"height":"0px"},200,function(){
        $('#photodots2').stop().animate({"height":"0px"},200,function(){
                $('#photodots1').stop().animate({"margin-left":"400px"},500);
            });
        });
    });
    $('#photodots6').stop().animate({"margin-left":"450px","width":"0px"},200,function(){
        $('#photodots5').stop().animate({"height":"0px"},200);
    });
    $('#photodots7').stop().animate({"width":"78px","margin-left":"372px"},1000);
    $('#photo-gallery').stop().animate({"margin-left":"-2000px"},1000);
    $('#photo-closeup').stop().animate({"margin-left":"-210px"},1000);
}

function resetPhotos(){
    $('#photo-closeup').stop().animate({"margin-left":"2000px"},1000);
    $('#photo-gallery').stop().animate({"margin-left":"-210px"},1000);
    $('#photodots1').stop().animate({"margin-left":"-140px"},500,function(){
        $('#photodots2').stop().animate({"height":"60px"},200,function(){
           $('#photodots3').stop().animate({"height":"60px"},200,function(){
                $('#photodots4').stop().animate({"height":"60px"},200,function(){
                    $('#photodots5').stop().animate({"height":"132px"},200,function(){
                        $('#photodots6').stop().animate({"margin-left":"402px","width":"50px"},200);
                    });
                });
           });
        });
    });
    $('#photodots7').stop().animate({"width":"54px","margin-left":"402px"},1000);
}


function showAlbum(title){
    $('.album-info').hide();
    $('#album-'+title).show();
    $('#album-gallery').stop().animate({"margin-left":"-2000px"},1000);
    $('#album-closeup').stop().animate({"margin-left":"-200px"},1000);
    $('#discogdots1').stop().animate({"width":"0px"},200);
    $('#discogdots2').stop().animate({"width":"0px"},200);
    $('#discogdots3').stop().animate({"margin-left":"-40px","width":"310px"},200);
    $('#discog-albums').stop().fadeOut(200,function(){
       $('#discogdots4').stop().animate({"width":"476px","margin-left":"-15px"},200,function(){
            $('#discogdots8').stop().animate({"height":"50px"},200);
       });
    });
}

function resetAlbums(){
    $('#album-closeup').stop().animate({"margin-left":"2000px"},1000);
    $('#album-gallery').stop().animate({"margin-left":"-200px"},1000);
    $('#discogdots6').stop().animate({"width":"0px","margin-left":"450px"},200);
    $('#discogdots7').stop().animate({"width":"0px","margin-left":"450px"},200);
    $('#discogdots8').stop().animate({"height":"0px"},200,function(){
        $('#discogdots4').stop().animate({"width":"60px","margin-left":"400px"},200,function(){
            $('#discog-albums').stop().fadeIn(200);
            $('#discogdots1').stop().animate({"width":"660px"},200);
            $('#discogdots2').stop().animate({"width":"660px"},200);
            $('#discogdots3').stop().animate({"margin-left":"-200px","width":"660px"},200);
       });
    });
}

$(document).ready(function() {

        $('#nav ul li a,#logo-link a').click(function() {
            var elementClicked = $(this).attr("href");
            var destination = $(elementClicked).offset().top;
            $('#bg_container').fadeTo(500,1.0);
            $("html,body").stop().animate({ scrollTop: destination}, {duration:800,easing:'easeOutExpo'});
        });

        $("#bg1").css("background-image","url(wp-content/themes/soad/image/bg-1.jpg)");
        $("#bg1").fadeTo(2000,1.0);
        $("#bg2").css("background-image","url(wp-content/themes/soad/image/bg-2.jpg)");
        $("#bg2").fadeTo(2000,1.0);
        $("#bg3").css("background-image","url(wp-content/themes/soad/image/bg-3.jpg)");
        $("#bg3").fadeTo(2000,1.0);
        $("#bg4").css("background-image","url(wp-content/themes/soad/image/bg-4.jpg)");
        $("#bg4").fadeTo(2000,1.0);
        $("#bg5").css("background-image","url(wp-content/themes/soad/image/bg-5.jpg)");
        $("#bg5").fadeTo(2000,1.0);
        $("#bg6").css("background-image","url(wp-content/themes/soad/image/bg-6.jpg)");
        $("#bg6").fadeTo(2000,1.0);
        $("#bg7").css("background-image","url(wp-content/themes/soad/image/bg-7.jpg)");
        $("#bg7").fadeTo(2000,1.0);


        $('#photo-previous').click(function(){
            currentPhoto = $('.photo:visible');
            previousPhoto = currentPhoto.prev('.photo');

            if(previousPhoto.length != 0){
                currentPhoto.fadeOut(function(){
                    previousPhoto.fadeIn();
                });
                currentPhoto = previousPhoto;
            }
        });

        $('#photo-next').click(function(){
            currentPhoto = $('.photo:visible');
            nextPhoto = currentPhoto.next('.photo');

            if(nextPhoto.length != 0){
                currentPhoto.fadeOut(function(){
                    nextPhoto.fadeIn();
                });
                currentPhoto = nextPhoto;
            }
        });



        $('.video-link').click( function() {


                $('.video-link').each(function(){

                        $(this).css({'background-color':'transparent','color':'#d9de82'});
                        $(this).css('background-image','url(http://blog.systemofadown.com/wp-content/themes/soad/image/black-transparency.png)');
                        $(this).children('span').css({'color':'#ddd'});

                });


                the_link = $(this);
                
                var destination = $('#videos').offset().top;
                
                var selector = "html:not(:animated)";
                
                if($.browser.webkit){
                    
                        var selector = "body:not(:animated)";
                        
                }
               
                $(selector).animate({ scrollTop: destination}, 1000, function(){

                        // FadeTo is currently broken in FF :(  -  at least do the fadeTo in Webkit where it does work:

                        if($.browser.webkit){
                            
                                $('div#bg_container').fadeTo(500,0.3,function(){
                                    
                                        setup_video();
                                        
                                });  
                                
                        } else {
                            
                                setup_video();
                                
                        }
                        
                        function setup_video(){

                                video_id = $(the_link).attr('rel');
                                
                                $(the_link).css({'background-image':'none','background-color':'#d9de82','color':'#000'});
                                $(the_link).children('span').css({'color':'#000'});

                                loadVideo(video_id);
                                
                        }

                });

        });

        /*
        $('#player-stop, a.close-video').click(function(){
                clearVideo();
                $('#projector-player').hide();
                $('#bg_container').fadeTo(1000,1.0);
                $('#projector-controls img, #projector-controls a').fadeOut(500);
                
                $('.video-link').each(function(){
                        $(this).css({'background-color':'transparent','color':'#d9de82'});
                        $(this).css('background-image','url(http://blog.systemofadown.com/wp-content/themes/soad/image/black-transparency.png)');
                        $(this).children().css({'color':'#ddd'});
                });
                
        });
        */

        $('#photo-closeup .photo').click(function(){
                resetPhotos();
        });

        $('.album a img').mouseover(function(){
           $(this).parent().parent().stop().animate({"background-position":"150px 10px"},200);
           $(this).css("cursor","pointer");
        });
        $('.album').mouseout(function(){
           $(this).stop().animate({"background-position":"110px 10px"},200);
        });

        /*
        $(window).scroll(function(event){
                var x = (window.pageYOffset);

                $('.bg').each(function(){
                        if(x > $(this).offset().top && x < ($(this).offset().top + $(this).height())){
                                $('#nav ul li').removeClass('active');
                                $('#nav ul li a[href="#'+$(this).attr('rel')+'"]').parent().addClass('active');
                        }

                });

        });
        */

        /*
        $('a.more-link, .post a').click(function(event){
                event.preventDefault();
                $('body').fadeOut(function(){
                        window.location = event.currentTarget.href;
                });
        });
        */
        $('.newer-posts-button').click(function(){

                var x = ($('.post:visible:first').attr('rel'));
                var visible_posts = $('.post:visible');

                if(x < posts_per_page){
                        return;
                }

                $('.blog_posts').fadeOut(function(){

                        visible_posts.each(function(){
                                $(this).hide();
                        });

                        x--;
                        if($('#post-'+x).length != 0){
                                $('#post-'+x).fadeIn();
                        }
                        x--;
                        if($('#post-'+x).length != 0){
                                $('#post-'+x).fadeIn();
                        }
                        x--;
                        if($('#post-'+x).length != 0){
                                $('#post-'+x).fadeIn();
                        }
                        
                        $('.blog_posts').fadeIn();

                });

                $("html,body").stop().animate({ scrollTop: 0}, {duration:800,easing:'easeOutExpo'});

        });

        $('.previous-posts-button').click(function(){

                var x = ($('.post:visible:last').attr('rel'));
                var visible_posts = $('.post:visible');

                if(visible_posts.length < posts_per_page){
                        return;
                }
                
                $('.blog_posts').fadeOut(function(){
                    
                        visible_posts.each(function(){
                                $(this).hide();
                        });
 
                        x++;
                        if($('#post-'+x).length != 0){
                                $('#post-'+x).show();
                        }
                        x++;
                        if($('#post-'+x).length != 0){
                                $('#post-'+x).show();
                        }
                        x++;
                        if($('#post-'+x).length != 0){
                                $('#post-'+x).show();
                        }
                        
                        $('.blog_posts').fadeIn();

                });
                
                

                $("html,body").stop().animate({ scrollTop: 0}, {duration:800,easing:'easeOutExpo'});

        });

});
