var jp = jp || {};

// Constants
jp.DEBUG_MODE =                 false;
jp.PANEL_OFFSET =               700;

// ..........................................................................

/*
 * Firefox 3.6 hack to avoid a rendering issue... :(

    JP added 02-15-10 conditions 
 	We do the hack only when (a) the width is even and the scrollbar is present, or (b) the width is odd and no scrollbar is present.
    (Previously, we detected only if the window was even, but we discovered that if the scrollbar wasn't there, then the hack
	 was needed if the window was odd)
    
*/

 jp.firefox36hack = function  () {
	   if (navigator && navigator.userAgent && navigator.userAgent.indexOf("Firefox/3.6")) 
	   {														   
	   // JP 02-15-10 If scrollheight of the document body is bigger than the window height, scrolling is needed
 		var hasVScroll = document.body.scrollHeight > window.innerHeight;

        if ( ((window.innerWidth % 2 == 0) && hasVScroll)  || ((window.innerWidth % 2) == 1 && !hasVScroll)  ) 
	    {
        	 window.innerWidth = window.innerWidth - 1;
      	}

}
};

// ..........................................................................

/**
 * Main init.
 */
jp.init = function () {
    jp.log('Initing.');

    // Hide nav
    $('.jp-arrow-left').hide();
    $('.jp-arrow-right').hide();
    
    // How many items?
    jp.frame_items = $('.jp-frameitem');
    jp.num_panels = jp.frame_items.length;
    jp.cur_panel = 0;
    
    // Move items around, add nav dots.
    for (var p = 0; p < jp.num_panels; p++) {
        var l_offs = p * jp.PANEL_OFFSET;
        $(jp.frame_items[p]).css({left: l_offs}).attr('data-frame-id', p);
        jp.log('Panel ' + p + ' left: ' + l_offs);
        
        // Enumerate the embeds?
        var $em = $('embed', jp.frame_items[p]);
        if ($em && $em.length == 1) {
            // Add an id.
            if ($em.attr('id') == '' || $em.attr('id') == null) {
                var new_id = 'video_embed_' + p;
                $em.attr('id', new_id);
                jp.log('Panel ' + p + ' embed ID set: ' + new_id);
            }
        }
        
        // Enumerate the object?
        var $ob = $('object', jp.frame_items[p]);
        if ($ob && $ob.length == 1) {
            // Add an id.
            if ($ob.attr('id') == '' || $ob.attr('id') == null) {
                var new_id = 'video_obj_' + p;
                $ob.attr('id', new_id);
                jp.log('Panel ' + p + ' obj ID set: ' + new_id);
            }
        }
        
        // Add a nav dot, fade in the dots!
        jp.$navdot_holder.append('<div class="jp-nav-dot" data-frame-id="'+p+'">&nbsp;</div>');
    }
    
    // Show/hide arrows.
    jp.updateNav();
    
    // Bind events
    $('.jp-arrow-left').click(function () {jp.panelSeek(jp.cur_panel - 1);} );
    $('.jp-arrow-right').click(function () {jp.panelSeek(jp.cur_panel + 1);} );
    $('.jp-nav-dot').click(function () {
        jp.panelSeek($(this).attr('data-frame-id'));
    });
    $(window).resize(jp.firefox36hack);
    $(window).unload(function () {
        // IE7 is throwing errors!
        __flash__removeCallback = function (instance, name) {
            if (instance) {
                instance[name] = null;
            }
        }
    });
    
    // Fade in the dots in a second if we have enough window width...
    if ($(window).width() > 480) {
        window.setTimeout(function () {
            $('.jp-nav-dots').fadeTo(800, 1.0);
        }, 600);
        
        jp.firefox36hack();
    }
};

// ..........................................................................

/**
 * Output a log message.
 *
 * @param str - (String) Message to output.
 */
jp.log = function (str) {
    if (jp.DEBUG_MODE && window.console && window.console.log) {
        window.console.log(str);
    }
};

// ..........................................................................

/**
 * Seek to panel_id.
 *
 * @param panel_id - (int) Desired panel.
 */
jp.panelSeek = function (panel_id) {
    jp.log('Seeking from current to: ' + panel_id);

    // Pause all playing ones
    var $movies = $('.jp-movie embed');
    if ($movies.length > 0) {
        $movies.each(function () {
            if (this.getPlayerState && this.getPlayerState() == 1) {
                this.pauseVideo();
                jp.log('Paused playing video ' + this.id);
            }
        });
    }
    
    // Keep it in bounds.
    jp.cur_panel = panel_id * 1;
    if (jp.cur_panel < 0) {
        jp.cur_panel = 0;
        jp.log('At start already');
    }
    if (jp.cur_panel >= jp.num_panels) {
        jp.cur_panel = jp.num_panels - 1;
        jp.log('At last panel already');
    }
    
    // Animate!
    var target_x = (jp.cur_panel * jp.PANEL_OFFSET * -1);
    $('.jp-viewframe').stop().animate({left: target_x}, 500);

    // Nav
    jp.updateNav();
};

// ..........................................................................

/**
 * Show/hide nav arrows as appropriate.
 */
jp.updateNav = function () {
    if (jp.cur_panel > 0) {
        $('.jp-arrow-left').fadeIn(500);
    } else {
        $('.jp-arrow-left').fadeOut(500);
        jp.log('Disabling left arrow');
    }

    if (jp.cur_panel < (jp.num_panels - 1)) {
        $('.jp-arrow-right').fadeIn(500);
    } else {
        $('.jp-arrow-right').fadeOut(500);
        jp.log('Disabling right arrow');
    }
    
    $('.jp-nav-dot', jp.$navdot_holder).each(function () {
        var $t = $(this);
        if ($t.attr('data-frame-id') == jp.cur_panel) {
            $t.addClass('jp-nav-dot-selected');
        } else {
            $t.removeClass('jp-nav-dot-selected');
        }
    });
};

// ..........................................................................

$(document).ready(function () {
    // Shortcuts
    jp.$navdot_holder = $('.jp-nav-dots');
    
    jp.log('Ready!  Going to init...');
    jp.init();
});
