

/** NyroModal Functions **/
$(function() {
    var fPlayer = null;
	$.fn.nyroModal.settings.debug = false;
	$.fn.nyroModal.settings.minWidth = '100';
	$.fn.nyroModal.settings.minHeight = '100';
	$.fn.nyroModal.settings.maxWidth = '540';
	$.fn.nyroModal.settings.maxHeight = '720';
	$.fn.nyroModal.settings.bgColor = '#ffffff';
	$.fn.nyroModal.settings.css.content.overflow = 'hidden';
	
	$.fn.nyroModal.settings.processHandler = function (settings){
	    var srcObj = settings.from;
	    var newUrl = settings.from.href;
	    
	    if(newUrl.indexOf("GalleryDetails.aspx"))
	    {
	        newUrl = newUrl.replace("GalleryDetails.aspx", "GalleryDetailsLite.aspx");
	        $.nyroModalSettings({
			    height: 336,
			    width: 448,
			    url: newUrl
		    });
	    }
	    
	    if($('#homeRotator').length != 0)
	    {
	        $('#homeRotator').cycle('pause');
	    }
	};
	
	$.fn.nyroModal.settings.endShowContent = function(elts, settings)
	{
	    var elem = null;
	    var maxWidth = settings.maxWidth;
	    var maxHeight = settings.maxHeight;
	    var width = 0;
	    var height = 0;
	    
	    if($("#imgGalleryImage", elts.contentWrapper).length != 0)
	    {
	        elem = $("#imgGalleryImage", elts.contentWrapper);
	    }
	    else if ($("#playerDiv", elts.contentWrapper).length != 0)
	    {
	        fPlayer = loadPlayer();
	        elem = $("#playerDiv", elts.contentWrapper);
	    }
	    
	    if(elem != null)
	    {
	        width = elem.width();
	        height = elem.height();

	        if(width > maxWidth || height > maxHeight)
	        { 
	            //scale image down  
	            if(width > height)
	            {
	                height = (height/width) * maxWidth;
	                width = maxWidth;
	            }
	            else if(height > width)
	            {
	                width = (width/height) * maxHeight;
	                height = maxHeight;
	            }
	            else
	            {
	                width = maxWidth;
	                height = maxHeight;
	            }
	            
	            elem.width(width);
	            elem.height(height);
	        }
	            
	        $.nyroModalSettings({
		        height: height,
		        width: width
	        });
	    }
	};
	
	$.fn.nyroModal.settings.hideContent = function(elts, settings, callback)
	{
	    //Pause the nyroModal before removing the containing DOM
	    if(fPlayer != null)
	    {
	        fPlayer.Pause();
	    }
	    
	    setTimeout(callback, 300);
	}
	
	$.fn.nyroModal.settings.endRemove = function (elts, settings)
	{
	    if($('#homeRotator').length != 0)
	    {
	        $('#homeRotator').cycle('resume');
	    }
	}
});    


function preloadImg(image) {
	var img = new Image();
	img.src = image;
}

var galleryPlayList = new Array();
/** Adds gallery items to a playlist used by the following plugins: nyroModal, JCarousel, FlowPlayer **/
function addToPlayList(itemURL, thumbURL, titleStr, linkType)
{
    galleryPlayList[galleryPlayList.length] = {
        url: itemURL,
        thumb:thumbURL, 
        title: titleStr,
        type: linkType
    };
}



/** -- JCarousel --
    This function references the galleryPlayList array.
    This function is called when a item comes into view. 
    If the item from the 'galleryPlayList' is not already 
    in the carousel, it is added.
**/
function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    if(state == "next")
    {
        if(i <= galleryPlayList.length && !carousel.has(i))
        {
            carousel.add(i, mycarousel_getItemHTML(galleryPlayList[i - 1], i - 1));
        }
    }
}

function mycarousel_attachNyroModal(carousel, state)
{
    //Assign new links to nyroModal
    $(".nyroModal").nyroModal();
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item, idx)
{
    var imgHtml = '<img src="' + item.thumb + '" alt="' + item.title + '" />';

    if(item.type && item.type == "video")
    {
        imgHtml += '<span>&nbsp;</span>';
    }

    imgHtml = '<a href="'+item.url+'" id="galItem_'+idx+'" class="nyroModal">'+imgHtml+'</a>';

    return imgHtml;
};