/**
 * extension for article template
 */

/**
 * ##### article media extension #####
 */

var CHECK_MEDIABOX_HASH_REGEX = /^.*#([^#]+).*$/;

document.observe('dom:loaded', function() 
{
	// init mediabox if class exists
	if($A($$('div.wide-column.mediabox')).length > 0)
	{
	    // init click event to thumbnail images
	    setMediaThumbClick();
	      
	    //  show specific media box addicted to hashlink; otherwise show defaul/first mediabox content
	    var mediahashlink = document.location.href.replace(CHECK_MEDIABOX_HASH_REGEX, "$1");
	    var mediaId = mediahashlink.match('toc-') ? mediahashlink : 'toc-1'; 
	    showMediaBox(mediaId);		
	}
});

function showMediaBox(elmId)
{
    // id of mediabox element that is shown on page
    var elmId = elmId ? elmId : 'toc-1';    
    
    $A($$('div.mediabox-l3')).each(function(box)
    {
        // show/hide elements
        var displayOption = box.id == elmId ? 'block' : 'none'
        box.setStyle({
            'display': displayOption
        });
        
       // also hide next "back-to-top"-spacer
       box.next('.separator').setStyle({
           'display': 'none'
       }); 
       
       sIFR.replaceElement(named({sSelector:"div.mediabox-l3 div.sifr-h3 h3", sFlashSrc: SIFR_SLAB_PATH, sColor:"#333333", sWmode:"transparent"}));
    });
}

function setMediaThumbClick()
{
    if($('thumbnail_row'))
    {
        $A($$('#thumbnail_row a')).each(function(link)
        {
            //  change link href for preventing hash jump in script mode
            var linkDir = link.href;
            link.href = "javascript:void(0);";

            link.observe('focus', function(event) { Event.element(event).blur(); });
            
            link.observe('click',function(event)
            {
               $A($$('#thumbnail_row li')).each(function(item) { 
            	   $(item).removeClassName('active'); 
               });
               Event.element(event).up('li').addClassName("active");
            	
               //   stop current playing videos (fix for: IE video stop problem on mediabox content change)
               $(mediaBoxVideoController).each(function(video)
               {
                   var videoBox = $(video).up('.mediabox-l3');
                   if(videoBox.getStyle('display') == 'block' ) $(video).doPause();
                   
               });
                              
               //   show clicked media content
               var mediahashlink = linkDir.replace(CHECK_MEDIABOX_HASH_REGEX, "$1");
               var mediaId = mediahashlink.match('toc-') ? mediahashlink : 'toc-1'; 
               showMediaBox(mediaId);
           });
        });
    }
}
/**
 * ##### end article media extension #####
 */
