/**
 * This is dLibra content modal jquery plugin which customize nyromodal. 
 * @requires: dlibra.js, nyromodal.js, jquery-sizes.js
 */

jQuery(function($) {

	var cookiePrefix = 'dL';
 	
	jQuery.fn.dL_contentModal = function(options) {
		
		if ( options.cookiePrefix ) {
			cookiePrefix = options.cookiePrefix;
		}
		
		var dLSettings = getDefaultSettings();
		dLSettings.endRemove = ensureContentUrlsValidity;

		var maxSize = $("document").maxSize();
		dLSettings.width = maxSize.width; 
		dLSettings.height = maxSize.height;
		
		dLSettings.titleFromIframe = 'false';
		dLSettings.processHandler = forceIframeProcessHandler;
		
		return this.nyroModal(dLSettings);
	}
	
	/** This function creates modal dialog with login type selection*/
	jQuery.fn.dL_simpleModal = function(options) {

		var dLSettings = getDefaultSettings();
				
		var maxWidth = $("document").maxSize().width;
		if ( maxWidth > 600 ) maxWidth = 600;
		dLSettings.width = maxWidth; 
		
		return this.nyroModal(dLSettings);
	}		

	jQuery.fn.dL_smallModal = function(options){
		
		var dLSettings = getDefaultSettings();
		
		var maxWidth = $("document").maxSize().width;
		if ( maxWidth > 600 ) maxWidth = 600;
		dLSettings.width = maxWidth; 
		
		dLSettings.minWidth = 350;
		dLSettings.minHeight = 210;
		
		return this.nyroModal(dLSettings);
		
	}
	
	function getDefaultSettings()
	{
		return {
			 endFillContent: customizeBackground,
			 endShowContent: customizeContentModal,
			 beforeHideContent : hideCustomBackground
		};
	}
	
	function forceIframeProcessHandler(settings) 
	{
		settings.type = "iframe";
		//autoresize should be turned off
		settings.autoSizable = false;
	}


	function ensureContentUrlsValidity(elts, settings) 
	{
		var contentUrl = settings.from.href;
		var mimetype = getRequestParam(contentUrl, "mimetype");
		
		if (mimetype != null ) {
			var issecure = getRequestParam(contentUrl, "sec");
			if (issecure == null ) issecure = 'false'; 
				
			var handlerFromCookie = getCookie(cookiePrefix+mimetype+'_'+issecure);
			if ( handlerFromCookie != null ) 
			{
			 /*
			   cookie was set, check if link to content presentation is actual 
			  */		  
			  var contentUrl = settings.from.href;
			  if ( contentUrl.indexOf('/list?') != -1 || contentUrl.indexOf('/info?') != -1) 
			  {  
			  		safePageRefresh();
			  }
			}	  
		}
	}
	

	function hideCustomBackground(elts, settings, callback)
	{
		$("#dLNavigation").css({'visibility':'hidden'});
		$("#dLUpperCorner").css({'visibility':'hidden'});
		$("#dLLowerCorner").css({'visibility':'hidden'});
		callback();
	}
	
	function customizeBackground(elts, settings)
	{
		elts.bg.css({'background':'url("'+homePageUrl + '/style/common/js/nyromodal/nyro-background.gif")'});
		if(jQuery.browser.msie){
			elts.bg.css({'height': document.documentElement.offsetHeight + 'px'});
		}
	}
	
	function customizeContentModal(elts, settings) 
	{
		$("#dLNavigation").remove();
		$("#dLUpperCorner").remove();
		$("#dLLowerCorner").remove();
		
		var fe = jQuery.browser.msie ? '.gif' : '.png';
		var contUrl = homePageUrl + '/style/common/js/nyromodal/';
		var width = parseInt(elts.contentWrapper.css('width'));
		var height = parseInt(elts.contentWrapper.css('height'));
		width = width + 22;
		height = height + 22;
		mL = parseInt(elts.contentWrapper.css('margin-left'));
		mT = parseInt(elts.contentWrapper.css('margin-top'));
		elts.contentWrapper.wrap('<div id="dlWrapper"></div>');
		$("#dlWrapper").css({
			'z-index' : '1000',
			'background':'transparent',
			'width':width+'px',
			'height':height+'px',
			'left':'50%',
			'top':'50%',
			'position':'absolute',
			'margin-left' : mL,
			'margin-top' : mT
		});
		elts.contentWrapper.css({'border':'0',
								 'position':'relative',
								 'left':'0px',
								 'top':'0px',
								 'bottom':'0px',
								 'margin-left':'0',
								 'margin-top':'0',
								 'clear':'both',
								 'border-left':'#727272 solid 11px',
								 'border-right':'#727272 solid 11px'});
		var cWidth = width - 22;
		var cHeight = height - 22;
		var iconsPath = homePageUrl + '/style/common/img/icons';

		var upperCorner = "<div id='dLUpperCorner'>"
		+"<div class='dLCorner' style='background:url("+contUrl+"left-top"+fe+");'></div>"
		+"<div class='dLCorner' style='background-color:#727272;width:"+(cWidth)+"px;'>"
		+"</div>"
		+ "<div class='dLCorner' style='background:url("+contUrl+"right-top"+fe+");'></div></div>"; 
		var lowerCorner = "<div id='dLLowerCorner'><div class='dLCorner' style='background:url("+contUrl+"left-bottom"+fe+");'></div>"
		+ "<div class='dLCorner' style='background-color:#727272;width:"+(cWidth)+"px;'></div>"
		+ "<div class='dLCorner' style='background:url("+contUrl+"right-bottom"+fe+");'></div></div>"; 
		var jqup = $(upperCorner);
		var jqdown = $(lowerCorner);
		jqup.insertBefore(elts.contentWrapper);
		jqdown.insertAfter(elts.contentWrapper);

		if ( this.gallery != null ) {
			var navigation = '<div id="dLNavigation">';
			
			var prevLink = getGalleryLink(settings, -1);
			if ( prevLink ) {
				navigation += '<a href="'+prevLink.attr('href')+'" id="prevPub">'
						   +'<img src="'+iconsPath+'/go-prev.gif'+'" alt="&lt;"/></a>';
			}
			
			navigation += '<img src="'+iconsPath+'/content.gif'+'" alt="."/>'
			
			var nextLink = getGalleryLink(settings, 1);
			if ( nextLink ) {
				navigation += '<a href="'+nextLink.attr('href')+'" id="nextPub">'
				           +'<img src="'+iconsPath+'/go-next.gif'+'" alt="&gt;"/></a>';
			}
			navigation += '</div>';
			
			var jqnavigation = $(navigation);
			jqnavigation.insertBefore(elts.contentWrapper);
			
			$("#nextPub").click(function() {
				$.nyroModalNext();
				return false;
			});
			$("#prevPub").click(function() {
				$.nyroModalPrev();
				return false;
			});
		}
	}
	
	//taken from jquery-nyromodal-1.4.2
	function getGalleryLink(settings, dir) {
		if (settings.gallery) {
			if (!settings.ltr)
				dir *= -1;
			// next
			var gallery = $('[rel="'+settings.gallery+'"]');
			var currentIndex = gallery.index(settings.from);
			var index = currentIndex + dir;
			if (index >= 0 && index < gallery.length)
				return gallery.eq(index);
		}
		return false;
	}

});