﻿
//	OD - override default SharePoint functions in "_layouts/1033/init.js"

function FixRibbonAndWorkspaceDimensions() {
	ULSA13: ;
	g_frl = true;
	var b = GetCachedElement("s4-ribbonrow"),
		a = GetCachedElement("s4-workspace"),
		f = GetCachedElement("s4-titlerow"),
		c = GetCachedElement("s4-bodyContainer");
	//	OD - add header, footer height and statusbar height (external to "s4-workspace")
	//	head = GetCachedElement("header_OD");
	//	foot = GetCachedElement("footer_OD");
		statusbar = GetCachedElement("s4-statusbarcontainer");
	//	alert("statusbar: "+statusbar.offsetHeight)
		if ( statusbar != null && statusbar != 'undefined' )
			if ( isNaN(statusbar.offsetHeight) || statusbar.offsetHeight == 'undefined' )
				statusbar.offsetHeight = 0;
	if (!b || !a || !c) return;
	if (!g_setWidthInited) {
		var h = true;
		if (a.className.indexOf("s4-nosetwidth") > -1) h = false;
		g_setWidth = h;
		g_setWidthInited = true
	} else
	var h = g_setWidth;
	var l = RibbonIsMinimized() ? 44 : 135,
		j = l + g_wpadderHeight;
	if (GetCurrentEltStyle(b, "visibility") == "hidden") j = 0;b.style.height = j + "px";
	var d = g_viewportHeight;
	if (null === d) {
		d = GetViewportHeight();
		g_viewportHeight = d
	}
//	OD - deduct head height, footer height and statusbar height (external to "s4-workspace")
	var statusBarHeight = statusbar.offsetHeight;
	if ( jQuery('#s4-statusbarcontainer').length > 0 )
	{
		if ( jQuery('#pageStatusBar').html().length == 0 )
		{
			statusBarHeight = 0;
		}
	}
	else
	{
		statusBarHeight = 0;
	}
	var e = d - b.offsetHeight - AbsTop(b) - statusBarHeight;// - head.offsetHeight;// - foot.offsetHeight;
//	alert(d +" | "+ b.offsetHeight +" | "+ AbsTop(b) +" | "+ statusBarHeight)
//	alert(e)
	if (e < 0) e = 0;
	a.style.height = e + "px";
	if (h) {
	//	OD - override width
	//	a.style.width = "986px";
	//	*** Necessario per via del ribbon in italiano ***
		jQuery('.width_pagina').css('width','1010px');
	//	*************************************************
		a.style.width = "1010px";
		if (c.offsetWidth < a.clientWidth) c.style.width = a.clientWidth + "px";
		if (f) {
		//	f.style.width = Math.max(c.offsetWidth - 1, 0) + "px";
		//	OD - override width
		//	f.style.width = "960px";
			f.style.width = "auto";
			f.className += " ms-titlerowborder"
		}
	}
	var m = browseris.ie && browseris.iever == 7 && !browseris.ie8standard;
	if (!g_setScrollPos) {
		browseris.firefox && browseris.firefox36up && window.scroll(0, 0);
		if (window.location.search.match("[?&]IsDlg=1")) if (!m || a.scrollHeight < a.clientHeight) a.style.overflowY = "auto";
		var g = document.getElementById("_maintainWorkspaceScrollPosition");
		if (g != null && g.value != null) a.scrollTop = g.value;
		g_setScrollPos = true
	}
	for (var k = [].concat(g_workspaceResizedHandlers), i = 0, n = k.length; i < n; i++) k[i]();g_frl = false

//	OD - call "OD_gest_ribbon"
//	OD_gest_ribbon(publicLang,privateLang);
	OD_gest_ribbon();
}

function _ribbonFixHeaderWidth(a) {
	ULSA13:;
	if (!g_fhs) return;
	var b = _ribbonCalculateWidth(a);
//	OD - override width
	a.style.width = "997px";
	a._widthAdded = true
}

function addStatus(f, g, e) {
	ULSA13:;
	var a = document.getElementById("pageStatusBar");
	if (a != null) {
		a.setAttribute("aria-live", "polite");
		a.setAttribute("aria-relevant", "all");
		var b = _createStatusMarkup(f, g, true);
		if (!e) a.appendChild(b);
		else {
			var c = a.getElementsByTagName("SPAN"),
				d = c.length > 0 ? c[0] : null;
			if (d != null) a.insertBefore(b, d);
			else a.appendChild(b)
		}
		if (a.childNodes.length == 1) {
			StatusIdWithTopPriority = b.id;
			StatusColorWithTopPriority = 1
		}
		a.style.display = "block";
	//	OD - update height
		FixRibbonAndWorkspaceDimensions();
		return b.id
	}
}


//	OD - funzioni personalizzate

jQuery.noConflict();
jQuery.browser.chrome = function() {
	var appver = navigator.appVersion + '';
	if (appver.indexOf('Chrome')>=0) {
		return true;
	}
	return false;
}
jQuery(document).ready(function()
{
	var externalLinks = jQuery('.linkEsterno');
	if (externalLinks.length > 0) {
		externalLinks.each(function() {
			jQuery(this).attr('target','_blank');
		});
	}
//	social networks - init
	var socialnetworks = jQuery('.socialBox');
	if (socialnetworks.length > 0) {
		var currentUrl = escapeProperly(window.location.href);	//	assegnazione link pagina corrente
		var currentTitle = jQuery(this).attr('title');
		socialnetworks.find('a').each(function() {
			var hrefThis = jQuery(this).attr('href').replace('{currentUrl}',currentUrl);
				hrefThis = hrefThis.replace('{currentTitle}',currentTitle);
			jQuery(this).attr( 'href' , hrefThis )
			jQuery(this).click(function() {
				window.open(jQuery(this).attr('href'), "social_network_link");
				return false;
			});
		});
	}
//	social networks - end
	OD_inputSearch();
	OD_setHomeImages();
	OD_setBorderImages()
	OD_adminTabsHome();
	OD_zoomPhotogallery();
//	OD_newsScrollerBox();
//	OD_removeLeftMenuWhenEmpty();
//	OD_gestZoomImages();
	OD_formatListResults();
//	OD__doPostBackORIG = __doPostBack;
});


String.prototype.OD_trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function getOS()
{
	var OSName = "Unknown OS";
	if ( navigator.appVersion.indexOf("Win")   != -1 ) OSName = "Windows";
	if ( navigator.appVersion.indexOf("Mac")   != -1 ) OSName = "MacOS";
	if ( navigator.appVersion.indexOf("X11")   != -1 ) OSName = "UNIX";
	if ( navigator.appVersion.indexOf("Linux") != -1 ) OSName = "Linux";
	return OSName;
}

function OD_gest_ribbon()	//	cura la miglior gestione del ribbon in situazione di presenza o non
{
	var ribbon_content  = jQuery('#RoccaBrunaRibbon');
	var ribbon_content2 = jQuery('.RoccaBrunaRibbon2');

	if ( ribbon_content.length > 0 && ribbon_content2.length > 0 )
	{
	//	jQuery('#header_OD').css('display','none');
	//	jQuery('#breadcrumb_OD').css('display','none');
	//	jQuery('.header').css('height','120px');

//		----- Per liberare spazio ai contenuti -----
//		jQuery('.homepageBox').hide();
//		jQuery('.headlineBox').hide();
//		--------------------------------------------
		jQuery("#body_OD").css('overflow','hidden');
		jQuery("#body_OD").css('overflowX','hidden');
		jQuery("#body_OD").css('overflowY','hidden');
		jQuery("#s4-workspace").css('overflow','auto');
		jQuery("#s4-workspace").css('overflowX','auto');
		jQuery("#s4-workspace").css('overflowY','scroll');
		OD_hidePageHeader();
		OD_hideBoxUsers();
	}
	else
	{
	/*	jQuery("#body_OD").css('height','auto');
		jQuery("#body_OD").css('overflow','auto');
		jQuery('#header').css('display','inherit');
		jQuery('#breadcrumb_OD').css('display','inherit');
		jQuery("#s4-workspace").css('height','auto');
		jQuery("#s4-workspace").css('overflow','hidden');
		jQuery("#s4-workspace").css('overflowX','hidden');
		jQuery("#s4-workspace").css('overflowY','hidden'); */
	}

//	jQuery("#s4-titlerow").css('float','left');
//	jQuery("#s4-titlerow").css('width','99%');

//	hidden bottom table SP empty
	var tdEmpty = jQuery('#_invisibleIfEmpty:eq(0)');
	if ( tdEmpty.length > 0 )
	{
		jQuery('td[name=_invisibleIfEmpty]:eq(0)').closest('table').parent().closest('table').remove();
	}
}

function OD_gest_lang(publicLang,privateLang)
{
	try
	{
		var userid = _spUserId;
		if ( _spPageContextInfo.currentLanguage != privateLang )
		{
			OnSelectionChange(privateLang); //default language for authenticated  user
		}
  	}
	catch (err)
	{
		if ( _spPageContextInfo.currentLanguage != publicLang )
		{
			OnSelectionChange(publicLang); //default language for anonymous  user
		}
	}
}

function OD_inputSearch()
{
	var inputSearch = jQuery('.inputSearch input');
	if ( inputSearch.length > 0 )
	{
		jQuery(inputSearch).focus(function() {
			if ( jQuery(this).attr('value') == 'Search on site...' ) jQuery(this).attr('value','');
		});
		jQuery(inputSearch).removeAttr('OnKeypress');
		jQuery(inputSearch).keypress(function(event) {
			var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
			if ( keycode == 13 )	//	keycode for enter key
			{
				var valSearched = jQuery.trim(jQuery(this).val());
				if ( valSearched.length != '' && valSearched != 'Cerca nel sito...' && valSearched != 'Search on site...' )
				{
					executeSearch();
				}
				else
				{
					alert('È necessario specificare almeno un termine di ricerca.');
				}
				return false;
			}
			else
			{
				return true;
			}
		});
	}
}

function OD_setHomeImages()
{
	var bodyBg = jQuery('#body_OD');
	var homeBg = jQuery('.headerImage');
	if ( bodyBg.length > 0 && homeBg.length > 0 )
	{
		if ( imagesArrayBg.length > 0 )
		{
			var selImgIndx = jQuery.random(imagesArrayBg.length);
			bodyBg.css('backgroundImage','url(\''+imagesArrayBg[selImgIndx]+'\')');
			homeBg.css('backgroundImage','url(\''+imagesArrayFg[selImgIndx]+'\')');
			homeBg.css('height',imagesArrayFgHeight[selImgIndx]+'px');
		}
	}
}

function OD_setBorderImages()
{
	//20111213 mtelch
	//tolgo il bordo alla immagine nelle pagine di contenuto
	//jQuery('.imageContent .imageContentBorder img').css({ 'border': '1px solid #666666', 'padding': '4px' });
	jQuery('.imageContent .imageContentBorder img').css({ 'border': '0'});

	/* tolgo il blocco immagine, se vuota */
	var bloccoImmagine = jQuery('.imageContent .ms-rtestate-field .ms-rtestate-field');
	if ( bloccoImmagine.length == 0 || jQuery.trim(bloccoImmagine.html()) == "" || jQuery.trim(bloccoImmagine.text()) == "" )
	{
	//	jQuery('.imageContent').remove();
	}

	/* tolgo anche la paginazione, se vuota */

	var paginazione= jQuery('.paginazione');
	if ( paginazione.length > 0 )
	{
		paginazione.each(function() {

			var thisItem = jQuery(this);
			if ( jQuery.trim(thisItem.html()) == "" || jQuery.trim(thisItem.text()) == "" ) thisItem.remove();
			
		});
		
	}
/*
	var floatLeft= jQuery('.ms-rtePosition-1 img');
	if ( floatLeft.length > 0 )
	{
		floatLeft.each(function() {

			jQuery(this).attr('class','ms-rtePosition-1 ms-rteImage-imageBorder');
			
		});
		
	}
	var floatRight= jQuery('.ms-rtePosition-2 img');
	if ( floatRight.length > 0 )
	{
		floatRight.each(function() {

			jQuery(this).attr('class','ms-rtePosition-2 ms-rteImage-imageBorder');
			
		});
		
	}
*/
	jQuery('.ms-rtePosition-1').attr('class','ms-rtePosition-1 ms-rteImage-imageBorder');
	jQuery('.ms-rtePosition-1').css({ 'margin': ''});
	jQuery('.ms-rtePosition-2').attr('class','ms-rtePosition-2 ms-rteImage-imageBorder');
	jQuery('.ms-rtePosition-2').css({ 'margin': ''});
}

function OD_adminTabsHome()
{
	var eventsTabs = jQuery('.eventsBox li');
	if ( eventsTabs.length > 0 )
	{
		eventsTabs.each(function(i) {
			var thisItem = jQuery(this);
			thisItem.css('float','left');
			thisItem.css('display','block');
			thisItem.find('.tabTitle').css('cursor','pointer');
			thisItem.find('.tabTitle').data('indice',i);
			thisItem.find('.tabTitle').click(function() {
				OD_setTabsHome(jQuery(this).data('indice'));
			});
			thisItem.find('.tabBox').css('display','none');
		});
		OD_setTabsHome(0);
	}
}
function OD_setTabsHome(id)
{
	var eventsBox = jQuery('.eventsBox');
	if ( eventsBox.length > 0 && jQuery('.eventsContainer').length == 0 )
	{
		eventsBox.append('<div class="eventsContainer"><div class="eventBox">&nbsp;</div></div>');
	}
	var eventsTabs = jQuery('.eventsBox li');
	if ( eventsTabs.length > 0 )
	{
		eventsTabs.each(function(i) {
			var thisItem = jQuery(this);
			if ( i == id )
			{
				thisItem.find('.tabTitle').attr('class','tabTitle tabSelected');
			//	thisItem.find('.tabBox').show();
				var content = thisItem.find('.tabBox').html();
				jQuery('.eventsContainer').html(content);
			}
			else
			{
				thisItem.find('.tabTitle').attr('class','tabTitle');
			//	thisItem.find('.tabBox').hide();
			}
		});
	}
}

jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});

indxNewsOpen = 1;
timerNews = null;
stepMove = 0;
noClickStep = false;
function OD_newsScrollerBox()
{
	var newsScroller = jQuery('.newsScrollerList li');
	if ( newsScroller.length > 0 )
	{
		var idIndx = 0;
		newsScroller.each(function() {
			idIndx++;
			thisItem = jQuery(this);
			thisItem.attr('id','news'+idIndx);
			
		});
		var arrowSx = jQuery('.newsScrollerBox .arrowSx');
		var arrowDx = jQuery('.newsScrollerBox .arrowDx');
		arrowSx.click(function() {
			OD_newsScroller(-1);
		});
		arrowDx.click(function() {
			OD_newsScroller(1);
		});
		jQuery('.newsScrollerList #news1').show();
		setNextTimeoutNews();
	}
}
function OD_newsScroller(step)
{
	if ( ! noClickStep )
	{
		stepMove = step;
		clearTimeout(timerNews);
		setNextTimeoutNews(true);
	}
}
function fadeComplete(bool)
{
	if ( bool )
	{
		clearTimeout(timerNews)
		var scrollerNews = jQuery('.newsScrollerList li');
		if ( stepMove != 0 ) indxNewsOpen += stepMove;
		else indxNewsOpen++;
		if ( indxNewsOpen > scrollerNews.length ) indxNewsOpen = 1;
		else if ( indxNewsOpen < 1 ) indxNewsOpen = scrollerNews.length;
		jQuery('.newsScrollerList #news'+indxNewsOpen).fadeIn(600,'linear',function()
		{
			fadeComplete(false);
		});
		stepMove = 0;
	}
	else
	{
		noClickStep = false;
		setNextTimeoutNews();
	}
}
function setNextTimeoutNews(bool)
{
	if ( bool )
	{
		noClickStep = true;
		jQuery('.newsScrollerList #news'+indxNewsOpen).fadeOut(600,'linear',function()
		{
			fadeComplete(true);
		});
	}
	else
	{
		timerNews = setTimeout(function()
		{
			noClickStep = true;
			jQuery('.newsScrollerList #news'+indxNewsOpen).fadeOut(600,'linear',function()
			{
				fadeComplete(true);
			});
		},3000);
	}
}

function OD_removeLeftMenuWhenEmpty()
{
	var divMenuLeft = jQuery('.leftNav');
//	alert(divMenuLeft.length+" | "+jQuery.trim(divMenuLeft.html())+" | "+jQuery.trim(divMenuLeft.text()))
	if ( divMenuLeft.length == 0 || jQuery.trim(divMenuLeft.html()) == "" || jQuery.trim(divMenuLeft.text()) == "" )
	{
		var divSearchBox = jQuery('#s4-leftpanel .searchBox');
		if ( divSearchBox.length > 0 )
		{
			jQuery('.searchBoxNav').css('marginTop','0px');
		}
		else
		{
		//	jQuery('#s4-leftpanel').remove();				//	in questo sito, essendoci il searchbox, meglio non usarlo
			jQuery('.s4-ca').css('marginLeft','0px');
			jQuery('.welcome').css('paddingLeft','0px');
		}
	}
}
function OD_hidePageHeader()
{
	var divPageHeader = jQuery('.headerImage');
//	alert(divPageHeader.length+"\n-------\n"+jQuery.trim(divPageHeader.html())+"\n-------\n"+jQuery.trim(divPageHeader.text()+"\n-------\n"+jQuery.trim(divPageHeader.text()).indexOf("Abstract")))
/*	if ( divPageHeader.length == 0 || jQuery.trim(divPageHeader.html()) == "" || (jQuery.trim(divPageHeader.text()) == "" || jQuery.trim(divPageHeader.text())=="Abstract") )
	{ */
		divPageHeader.hide();
//	}
}

function OD_hideBoxUsers()
{
	if ( jQuery('.usersBox').length > 0 ) jQuery('.usersBox').hide();
	if ( jQuery('.usersBoxInt').length > 0 )
	{
	//	jQuery('.usersBoxInt').hide();
		jQuery('.usersBoxInt').css('top','234px');
	}
}

//	*** Override funzione popup sulla privacy, nelle form ***
jQuery (function() {
	var linkPrivacy = jQuery('.labelFieldPrivacy a');
	if ( linkPrivacy.length > 0 )
	{
		linkPrivacy.attr('href',"javascript:OD_gestPopUpPrivacy();");
	}
});
function OD_gestPopUpPrivacy()
{
	ShowDialog_Click();
	var popUp = jQuery('.ms-dlgContent');
	if ( popUp.length > 0 )
	{
	//	alert('111');
		popUp.wrap('<div class="popUpInfoPrivacy" />');
		jQuery('.ms-dlgFrameContainer').height(jQuery('.ms-dlgFrameContainer').height()-10);
	};
}
//	*** END ***

function OD_zoomPhotogallery()
{
	var photoGallery = jQuery('#photogallerySlider .slideImage a');
	if ( photoGallery.length > 0 )
	{
		photoGallery.lightBox();
	}
}

function OD_gestZoomImages()
{
	var zoomType1 = jQuery('.ms-rteElement-zoomMe');
	var indxDiv = 0;
	if (zoomType1.length > 0 )
	{
		zoomType1.each(function()
		{
			indxDiv++;
			var zoomBox = jQuery(this);
			var img = jQuery('<div class="deleteAfterUse" idFake="'+indxDiv+'"><img src="'+zoomBox.attr('href')+'" /></div>')
			img.children('img').load(function()
			{
				var arrTmp = [{
								url:		zoomBox.attr('href'),
								width:		jQuery(this).width(),
								height:		jQuery(this).height()
							}];
				jQuery(this).parent('div').remove();
				zoomBox.css('cursor','pointer');
				zoomBox.click(function ()
				{
					zoomin(0,arrTmp);
					return false;
				});
			});
			zoomBox.after(img);
		});
	}

	var zoomType2 = jQuery('.ms-rteStyle-zoomMe');
	if (zoomType2.length > 0 )
	{
		zoomType2.each(function()
		{
			indxDiv++;
			var zoomBox = jQuery(this);
			var img = jQuery('<div class="deleteAfterUse" idFake="'+indxDiv+'"><img src="'+zoomBox.attr('href')+'" /></div>')
			img.children('img').load(function()
			{
				var arrTmp = [{
								url:		zoomBox.attr('href'),
								width:		jQuery(this).width(),
								height:		jQuery(this).height()
							}];
				jQuery(this).parent('div').remove();
				zoomBox.css('cursor','pointer');
				zoomBox.click(function ()
				{
					zoomin(0,arrTmp);
					return false;
				});
			});
			zoomBox.after(img);
		});
	}
}

function OD_popUpVideoHome()
{
	var divBtn = jQuery('.imgVideoBtn a');
	if ( divBtn.length > 0 )
	{
		var video_link = '/Documents/video/Banca_IFIS_it.mp4';
		var video_width = '640';
		var video_height = '360';
		var video_preview = '/Documents/video/preview_videoHome.gif';

		divBtn.attr('class','zoomme');
//		divBtn.attr('onclick','return zoom_video("' + video_link + '","' + video_width + '","' + video_height + '");');
		divBtn.click(function() {
			zoom_video(video_link, video_width,video_height,video_preview);
		});
	}
}

function OD_formatListResults()
{
	var divMetaData2 = jQuery('.srch-Metadata2');
	if ( divMetaData2.length > 0 )
	{
	//	tolgo uno spazio vuoto all'inizio di alcune stringhe dentro div di classe srch-Metadata2
		divMetaData2.each(function() {
			var htmlText = jQuery(this).html();
			var arr_tmp = escape(htmlText).split('%u2003');
			if ( arr_tmp[0] == '' ) arr_tmp.splice(0,1);
			var newText = arr_tmp.join('&emsp;');
			newText = unescape(newText);
			jQuery(this).html(newText);
		});
	}
}


