jQuery(function( $ ){
    $.easing.elasout = function(x, t, b, c, d) {
        var s=1.70158;var p=0;var a=c;
        if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
        if (a < Math.abs(c)) { a=c; var s=p/4; }
        else var s = p/(2*Math.PI) * Math.asin (c/a);
        return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
    };
});

$(document).ready(function(e) {

    //get frames
    var $strip_frame_1 = $('#strip_frame_1');
    var $strip_frame_2 = $('#strip_frame_2');
    var $movie_frame = $('#movie_frame');
    var $post_content_frame = $('#post_content_frame');

    //make dragscrollable
    $strip_frame_1.dragscrollable();
    $strip_frame_2.dragscrollable();

    //set view triggers
    $('[wr_showview]').click(function(event) {
        //exit on scrolling
        if (window._dragscrollableWasScrolling) return false;
        //hide post content frame
        $post_content_frame.hide();
        //get view
        var $trigger = $(this);
        var view = $trigger.attr('wr_showview');
        //scroll strip1
        if ($trigger.hasClass('cell_frame')) {
            //scroll to 1 cell below top
            var topOffset = 0; //-110;
            //if ($trigger.index() == 1) topOffset = 0;   //dont advance the top nav cell
            $strip_frame_1.stop().scrollTo($trigger, 200,
                { offset: { top: topOffset, left: 0 }, easing: 'elasout' }
            );
        };
        //scroll to home position
        if (view == 'Home') {
            //hide frames
            $movie_frame.hide();
            $post_content_frame.hide();
            //scroll to firstcell
            var $firstCell = $strip_frame_1.find('.cell_frame').eq(0);
            $strip_frame_1.stop().scrollTo($firstCell, 200, { easing: 'elasout' });
        }
        //scroll strip2
        var $strip2Target = $strip_frame_2.find('[wr_view="' + view + '"]').eq(0);
        if ($strip2Target.length == 1) {
            $strip_frame_2.stop().scrollTo($strip2Target, 1200, { easing: 'elasout' });
            $('#click_scroll_up_2, #click_scroll_down_2').show();
        }
        //google analytics
        _gaq.push(['_trackEvent', 'Nav', 'Click', view]);
        //
        return false;
    });

    //set movie triggers
    var $lastMovie;
    $('[wr_playmovie]').click(function(event) {
        //exit on scrolling
        if (window._dragscrollableWasScrolling) return false;
        //hide postcontent
        $post_content_frame.hide();
        //load and play movie
        var movieID = $(this).attr('wr_playmovie');
        var movieName = $(this).attr('wr_moviename');
        loadAndPlayMovie(movieID);
        //hides all descriptions
        $('.movie_description').hide();
        //show description
        var $movieDesc = $('#movie_description_' + movieID);
        $movieDesc.show();
        //show movie frame
        $movie_frame.show();
        //google analytics
        if (movieName == null || movieName == '') movieName = movieID;
        _gaq.push(['_trackEvent', 'Nav', 'PlayMovie', movieName]);
        return false;
    });

    //set content triggers
    $('[wr_showpostcontent]').click(function(event) {
        //exit on scrolling
        if (window._dragscrollableWasScrolling) return false;
        //hide movie frame
        $movie_frame.hide();
        //hide all posts
        $('.post_content').hide();
        //show post
        var postID = $(this).attr('wr_showpostcontent');
        var postName = $(this).attr('wr_postname');
        var $postContent = $('#post_content_' + postID);
        $postContent.show();
        //show post content frame
        $post_content_frame.show();
        //google analytics
        if (postName == null || postName == '') postName = postID;
        _gaq.push(['_trackEvent', 'Nav', 'ShowContent', postName]);
        return false;
    });

    //set logo hover effect
    $('#logo').mouseenter(function() {
        var $firstCell = $strip_frame_1.find('.cell_frame').eq(0);
        $strip_frame_1.stop().scrollTo($firstCell, 3000, { easing: 'elasout' });
    });

    //set click scroll up/down
    $('.click_scroll').mousedown(function(event) {
        var div = this;
        this.wrIsScrolling = true;
        var $div = $(this);
        var scrollto = $div.attr('scrollto');
        var scrolltarget = $div.attr('scrolltarget');
        var $scrolltarget = $('#' + scrolltarget);
        function afterScroll() {
            if (div.wrIsScrolling) {
                $scrolltarget.scrollTo(scrollto, 250, { onAfter: afterScroll });
            }
        }
        $scrolltarget.stop().scrollTo(scrollto, 250, { onAfter: afterScroll });
        event.preventDefault(); //to prevent auto text selection
    }).mouseup(function(event) {
        this.wrIsScrolling = false;
        var $div = $(this);
        event.preventDefault(); //to prevent auto text selection
    }).mouseenter(function() {
        $(this).find('.click_scroll_title').show();
    }).mouseleave(function() {
        $(this).find('.click_scroll_title').hide();
    });

    //set cell hover effects
    $('.cell_frame').mouseenter(function() {
        $(this).addClass('hover_cell');
    }).mouseleave(function() {
        $(this).removeClass('hover_cell');
    });

    //bubble
    function showBubble(bubbleID) {
        //hide if already visible
        var $bubbleContent = $('#bubble_id_' + bubbleID);
        if ($bubbleContent.is(':visible')) {
            hideBubble();
            return;
        }
        //hide all contents
        $('.bubble_contant').hide();
        //show
        $bubbleContent.show();
        $('#bubble').show('slide', { direction: 'down' }, 500);
    }
    function hideBubble(event) {
        var $bubble = $('#bubble');
        if (!$bubble.is(':visible')) return;
        if (event && jQuery.contains($bubble.get(0), event.target)) return;
        $bubble.hide('slide', { direction: 'down' }, 500);
    }
    $('[show_bubble]').mousedown(function(event) {
        var bubbleID = $(this).attr('show_bubble'); ;
        showBubble(bubbleID);
        event.stopPropagation();
        return false;
    });
    $(document).bind('mousedown', hideBubble);

    //audience
    $('#audience').click(function() {
    });

    //set initial scroll positions
    $strip_frame_1.stop().scrollTo('0px', 1);
    $strip_frame_2.stop().scrollTo('#spacer_strip2_bottom', 1);
    var $firstCell1 = $strip_frame_1.find('.cell_frame').get(0);
    setTimeout(function() {
        //swap logo img
        $('#logo_img').attr('src', 'img/logo_productions_150x.jpg');
        //scroll strip 1 after delay
        setTimeout(function() {
            $strip_frame_1.scrollTo($firstCell1, 500, {
                easing: 'elasout'
            });
        }, 1500);
    }, 1000);


});

//load player
var _countDownInt = 0;
function loadAndPlayMovie(clipID) {
    
    //get frame and set src. Note: src must be set before Froogaloop can be called
    var $movieIFrame = $('#movie_iframe');
    //$movieIFrame.hide();
    var src = "http://player.vimeo.com/video/" + clipID
        + "?js_api=1&show_portrait=0&show_byline=0&show_title=0&fullscreen=1&autoplay=1&js_swf_id=movie_iframe";
    $movieIFrame.attr('src', src);
    //init and bind events
    var movieIFrame = $movieIFrame.get(0);
    Froogaloop.init([movieIFrame]);
    movieIFrame.addEvent('onLoad', function(player_id) {
        //movieIFrame.addEvent('onPlay', function(id) { alert('play'); });
        //movieIFrame.addEvent('onPause', function(id) { alert('pause'); });
        //movieIFrame.addEvent('onLoading', function(time, id) { alert('seek'); });
        //movieIFrame.addEvent('onProgress', function(time, id) { alert('prog'); });
        movieIFrame.addEvent('onFinish', function(player_id) {
        });
    });
    //do countdown
    var $countDown = $('#movie_countdown');
    _countDownInt = 3;
    $countDown.html(_countDownInt);
    $countDown.show();
    var intervalId = setInterval(function() {
        _countDownInt--;
        if (_countDownInt < 1) {
            clearInterval(intervalId);
            $countDown.hide();
            //$movieIFrame.show();
        } else {
            $countDown.html(_countDownInt);
        }
    }, 500);
    //
    return;
}



//    //stop current
//    var $moogaloop = $('#moogaloop');
//    if ($moogaloop.length > 0) {
//        Swiff.remote($moogaloop.get(0), 'api_pause');
//        Swiff.remote($moogaloop.get(0), 'api_unload');
//    }
//    //loaded handler      
//    function mooplayer_loaded(swf_id) {
//        var $moogaloop = $('#moogaloop');
//        Swiff.remote($moogaloop.get(0), 'api_play');
//    }
//    //create player
//    var mooPlayer = new Swiff('http://vimeo.com/moogaloop.swf', {
//        id: 'moogaloop',
//        container: 'movie_frame',
//        //width: 640,
//        //height: 360,
//        width: '100%',
//        height: '100%',
//        params: { allowfullscreen: 'true' },
//        vars: {
//            clip_id: clipID,
//            show_portrait: 0,
//            show_byline: 0,
//            show_title: 0,
//            fullscreen: 1,
//            js_api: 1, // required in order to use the Javascript API
//            js_onLoad: mooplayer_loaded, // moogaloop will call this JS function when it's done loading (optional)
//            js_swf_id: 'moogaloop' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
//        }
//    });



//        var moviePostID = $(this).attr('wr_playmovie');
//        var $movie = $('#movie_' + moviePostID);
//        if ($lastMovie) {
//            $lastMovie.hide(0, function() {
//                $movie.show();
//            });
//        } else {
//            var h = $movie.html().trim();
//            h = h.substring(4, h.length - 3);
//            $movie.html(h);
//            var iframe = $movie.children().get(0);
//            iframe.id = "iplayer";
//            Froogaloop.init([iframe]);
//            iframe.addEvent('onLoad', function(iframeID) {
//                alert(iframeID);
//                //        });
//                //        //        //Simple Buttons
//                //        //        $('#' + player_id).nextAll('button.simple').bind('click', { 'player_id': player_id }, function(e) {
//                //        //            var iframe = $('#' + e.data.player_id).get(0);
//                //        //            iframe.api($(e.target).attr('id'), null);
//                //        //        });
//            });
//            $movie.show();
//        }
//        $lastMovie = $movie;
//$movie.show('blind', 2000);


//froogaloop
//Listen to the load event for all the iframes on the page
//    Froogaloop.init($('iframe').get());
//    $('iframe').each(function(index, iframe) {
//            iframe.addEvent('onLoad', function(iframeID) {
//                alert(iframeID);
//            });
//    //        //        //Simple Buttons
//        //        $('#' + player_id).nextAll('button.simple').bind('click', { 'player_id': player_id }, function(e) {
//        //            var iframe = $('#' + e.data.player_id).get(0);
//        //            iframe.api($(e.target).attr('id'), null);
//        //        });
//    });
//  <script src="http://github.com/vimeo/froogaloop/raw/master/froogaloop.min.js" type="text/javascript" ></script>





//    function gotoHome() {
//        $scrollerWindow.stop().scrollTo($('#home'), $speed, { axis: 'x', offset: { left: -20, top: 0} });
//        $('h1').stop().animate({ backgroundPosition: '(0px 0px)' }, $speed);
//        $('#title2').stop().animate({ backgroundPosition: '(20px 0px)' }, $speed + 300);
//        $('#title3').stop().animate({ backgroundPosition: '(20px 0px)' }, $speed + 600);
//        $('#nextControl').removeClass("noScroll goto_home goto_web goto_about goto_contact");
//        $('#prevControl').removeClass("noScroll goto_home goto_web goto_about goto_contact");
//        $('#nextControl').addClass("goto_web");
//        $('#prevControl').addClass("noScroll");
//        $('h1').text('Digital Portfolio');
//        _gaq.push(['_trackEvent', 'Nav', 'Home']);
//        return false;
//    }

