﻿
//	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(e)
	if (e < 0) e = 0;
	a.style.height = e + "px";
	if (h) {
	//	OD - override width
		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 = "990px";
	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()
{
	jQuery('.goBackArchive').remove();

	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 = escapeProperly(document.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;
			});
		});
	}
//	Logo Twitter
	G_twitterDetect = setInterval(function() {
		jQuery('.twtr-timeline').attr('style','height:auto !important; height:auto; min-height:100px;');
		if (jQuery('.twtr-ft').length > 0) {
			clearInterval(G_twitterDetect);
			var logoFooter = jQuery('.twtr-ft').find('img:eq(0)');
			logoFooter.attr('src','/PublishingImages/logo_Twitter_verde.gif');
		}
	},200);

//	social networks - end

	OD_posHeaderHome();
	OD_posHeaderInterna();
	OD_posBreadcrumbs();
	OD_inputSearch();
	OD_setTabMenu();
	OD_setGreenMenu();
	OD_setGreenElement();
	OD_gestFAQ();
	OD_formPrivacy();
//	OD_removeLeftMenuWhenEmpty();
	OD_iframeYouTube();
	OD_popUpVideoHome();
	OD_formatListResults();
});


String.prototype.OD_trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}

function multiReplace(str, match, repl)
{
/*	do
	{
		
	} */
	while ( str.indexOf(match) !== -1 )
	{
		str = str.replace(match, repl);
	}
	return str;
}

function OD_gest_ribbon()	//	cura la miglior gestione del ribbon in situazione di presenza o non
{
	var ribbon_content  = jQuery('#CrifResRibbon');
	var ribbon_content2 = jQuery('.CrifResRibbon2');

	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');
	//	jQuery('#header_OD').hide();
	}
	else
	{
		jQuery("#body_OD").css('height','auto');
		jQuery("#body_OD").css('overflow','auto');
		jQuery('#header_OD').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%');
}

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_openInfoBox(obj)
{
	
	var box = jQuery(obj).parent('div');
	var bgName = jQuery(obj).parent('div').attr('class').replace('Box','');
	box.css('width','706px');
//	alert(box.css('backgroundImage')+" | "+'/PublishingImages/'+bgName+'Open.png')
	box.css('backgroundImage','url(/PublishingImages/'+bgName+'Open.png)');
	box.children('.openInfo').css('display','none');
	box.children('.closeInfo').css('display','block');
	box.children('.'+bgName+'TitleSmall').css('display','none');
	box.children('.'+bgName+'TitleBig').css('display','block');
	box.children('.contentBox').css('display','block');
	box.children('.moreInfoBox').css('display','block');

	var rootClass = jQuery(obj).parent('div').attr('class');

	if ( rootClass == 'serviziBox' )
		rootClassStr = 'esigenze';
	else
		rootClassStr = 'servizi';

	jQuery('.'+rootClassStr+'Box').css('width','264px');
	jQuery('.'+rootClassStr+'Box').css('backgroundImage','url(/PublishingImages/'+rootClassStr+'Close.png)');
	jQuery('.'+rootClassStr+'Box .openInfo').css('display','block');
	jQuery('.'+rootClassStr+'Box .closeInfo').css('display','none');
	jQuery('.'+rootClassStr+'TitleSmall').css('display','block');
	jQuery('.'+rootClassStr+'TitleBig').css('display','none');
	jQuery('.'+rootClassStr+'Box .contentBox').css('display','none');
	jQuery('.'+rootClassStr+'Box .moreInfoBox').css('display','none');
	OD_openInfoBoxBig('.'+rootClassStr+'Box');

	jQuery('.generalBox').css('display','none');
}

clickFromSameBox = false;
function OD_openInfoBoxBig(strObj)
{
	jQuery(strObj).css('cursor','pointer');
	jQuery(strObj).click(function() {
		if ( !clickFromSameBox )
		{
			OD_openInfoBox(jQuery(this).find('.openInfo'));
			jQuery(this).unbind('click');
			jQuery(this).css('cursor','auto');
		}
		else
			clickFromSameBox = false;
	});
}

function OD_posHeaderHome()
{
	var headHome = jQuery('.headerHome');
	if ( headHome.length > 0 )
	{
		var headTop = jQuery('.header');
		headTopHeight = headTop.height();
		headTop.height( headTopHeight + headHome.height() );
		headHome.css('top',headTopHeight);
		jQuery('.bgSxHH').css('top',headTopHeight);
		jQuery('.bgDxHH').css('top',headTopHeight);

	//	action open
		var divOpenInfo = jQuery('.openInfo');
		divOpenInfo.css('cursor','pointer');
		divOpenInfo.each(function() {
			jQuery(this).click(function() {
				OD_openInfoBox(this);
			});
		});
		OD_openInfoBoxBig('.serviziBox');
		OD_openInfoBoxBig('.esigenzeBox');

	//	action close
		var divCloseInfo = jQuery('.closeInfo');
		divCloseInfo.css('cursor','pointer');
		divCloseInfo.each(function() {
			jQuery(this).click(function() {
				var box = jQuery(this).parent('div');
				var bgName = jQuery(this).parent('div').attr('class').replace('Box','');
				box.css('width','264px');
				box.css('backgroundImage','url(/PublishingImages/'+bgName+'Close.png)');
				box.children('.openInfo').css('display','block');
				box.children('.closeInfo').css('display','none');
				box.children('.'+bgName+'TitleSmall').css('display','block');
				box.children('.'+bgName+'TitleBig').css('display','none');
				box.children('.contentBox').css('display','none');
				box.children('.moreInfoBox').css('display','none');

				if ( jQuery('serviziBox').width() < 300 && jQuery('esigenzeBox').width() < 300 )
				{
					jQuery('.generalBox').css('display','block');
					clickFromSameBox = true;	//	serve per evitare che il click di chiusura si "confonda" con il click per la riapertura della funzione che sto per riassegnare
					OD_openInfoBoxBig('.'+box.attr('class'));
				}
			});
		});
		headHome.css('display','block');
		jQuery('.bgSxHH').css('display','block');
		jQuery('.bgDxHH').css('display','block');

		var rndm = Math.floor( Math.random() * 3 ) + 1;
		jQuery('.genBoxItem'+rndm).css('display','block');
	}
}

function OD_posHeaderInterna()
{
	var headInterna = jQuery('.headerInterna');
	if ( headInterna.length > 0 )
	{
		var headTop = jQuery('.header');
		headTopHeight = headTop.height();
		headTop.height( headTopHeight + headInterna.height() );
		headInterna.css('top',headTopHeight);
		jQuery('.bgSxHInt').css('top',headTopHeight);
		jQuery('.bgDxHInt').css('top',headTopHeight);

		headInterna.css('display','block');
		jQuery('.bgSxHInt').css('display','block');
		jQuery('.bgDxHInt').css('display','block');
	}
}

function OD_posBreadcrumbs()
{
	var breadcrumbs = jQuery('.breadcrumbBox');
	if ( breadcrumbs.length > 0 )
	{
		var headTop = jQuery('.header');
		headTopHeight = headTop.height();
		headTop.height( headTopHeight + breadcrumbs.height() + 1 );
		breadcrumbs.css('top',headTopHeight);

		breadcrumbs.css('display','block');
	}
}

function OD_inputSearch()
{
	var inputSearch = jQuery('.inputSearch input');
	if ( inputSearch.length > 0 )
	{
		jQuery(inputSearch).focus(function() {
			if ( jQuery(this).attr('value') == 'Cerca nel sito...' ) 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 site...' )
				{
					executeSearch();
				}
				else
				{
					alert('È necessario specificare almeno un termine di ricerca.');
				}
				return false;
			}
			else
			{
				return true;
			}
		});
	}
}

function OD_twitterWidget()
{
	new TWTR.Widget({
		version: 2,
		type: 'profile',
		rpp: 2,
		interval: 30000,
		width: 225,
		height: 180,
		theme: {
			shell: {
				background: '#FFFFFF',
				color: '#000000'
			},
			tweets: {
				background: '#FFFFFF',
				color: '#000000',
				links: '#B9C108'
			}
		},
		features: {
			scrollbar: false,
			loop: false,
			live: true,
			behavior: 'all'
		}
	}).render().setUser('CRIFValimmobili').start();
}

function OD_setTabMenu()
{
	var divBg = jQuery('.headerInternaBox');
	if ( divBg.length > 0 )
	{
		var currentUrl = window.location.href;
		if ( currentUrl.indexOf('/servizi/') != -1 )
		{
			divBg.css('backgroundImage','url(/PublishingImages/bgInternaServiziBox.jpg)');
		}
		else if ( currentUrl.indexOf('/esigenze/') != -1 )
		{
			divBg.css('backgroundImage','url(/PublishingImages/bgInternaEsigenzeBox.jpg)');
		}
		else if ( currentUrl.indexOf('/chi-siamo/') != -1 )
		{
			divBg.css('backgroundImage','url(/PublishingImages/bgInternaChiSiamoBox.jpg)');
		}
		else if ( currentUrl.indexOf('/domande-frequenti/') != -1 )
		{
			divBg.css('backgroundImage','url(/PublishingImages/bgInternaDomandeFrequentiBox.jpg)');
		}
	}

	var greenMenu = jQuery('.tabMenu li');
	if ( greenMenu.length > 0 )
	{
		greenMenu.each(function() {
			var branch = jQuery(this);
			var htmlText = branch.html();
			var newHtmlText  = '<div class="tabSx"></div>';
				newHtmlText += '<div class="tabMid">';
				newHtmlText += htmlText;
				newHtmlText += '</div>';
				newHtmlText += '<div class="tabDx"></div>';
			branch.html(newHtmlText);
		});
	}
}

function OD_setGreenMenu()
{
	var greenMenu = jQuery('.greenBalloon li');
	if ( greenMenu.length > 0 )
	{
		greenMenu.each(function() {
			var branch = jQuery(this);
			var htmlText = branch.html();
			var newHtmlText  = '<div class="bgGM">';
				newHtmlText += '<div class="sxGM"><div class="topGM"><div class="dxGM"><div class="downGM">';
				newHtmlText += '<div class="sxTopGM"><div class="dxTopGM"><div class="dxDownGM"><div class="sxDownGM">';
				newHtmlText += '<div class="paddGM">';
				newHtmlText += htmlText;
				newHtmlText += '</div>'+'</div></div></div></div>'+'</div></div></div></div>'+'</div>';
			branch.html(newHtmlText);
		});
	}
}

function OD_setGreenElement()
{
	var greenItems = jQuery('[class*=-FocusItem]');
	if ( greenItems.length > 0 )
	{
		greenItems.each(function() {
			var branch = jQuery(this);
			var htmlText = branch.html();
			var newHtmlText  = '<div class="bgGM">';
				newHtmlText += '<div class="sxGM"><div class="topGM"><div class="dxGM"><div class="downGM">';
				newHtmlText += '<div class="sxTopGM"><div class="dxTopGM"><div class="dxDownGM"><div class="sxDownGM">';
				newHtmlText += '<div class="paddGM">';
				newHtmlText += htmlText;
				newHtmlText += '</div>'+'</div></div></div></div>'+'</div></div></div></div>'+'</div>';
			var newHtmlTextSpan = newHtmlText;
			//	newHtmlTextSpan = multiReplace(newHtmlTextSpan, '<div', '<span');
			//	newHtmlTextSpan = multiReplace(newHtmlTextSpan, '</div', '</span');
			branch.html(newHtmlTextSpan);
			var aPresent = branch.find('a');
			if ( aPresent.length > 0 )
			{
				aPresent.attr('class','ico_fumetto');
			}
			else
			{
				var htmlText = branch.find('.paddGM').html();
				branch.find('.paddGM').html('<span class="ico_fumetto">' + htmlText + '</span>');
			}
		//	branch.find('span').css('display','block');
		});
	}
}

indxOpen = 0;
function OD_gestFAQ()
{
	var FAQq = jQuery('[class*=faqQuest]');
	var FAQa = jQuery('[class*=faqAsk]');
	if ( FAQq.length > 0 && FAQa.length > 0 )
	{
		var indx = 0;
		var maxItems = FAQq.length;
		FAQq.each(function() {
			indx++;
			var thisItem = jQuery(this);
			thisItem.attr('id','quest'+indx);
			thisItem.css('cursor','pointer');
			var htmlNow = thisItem.html();
			var aOpen = '';
			var aClose = '';
			thisItem.html(aOpen+htmlNow+aClose);
			thisItem.click(function() {
				var idAsk = jQuery(this).attr('id').replace('quest','ask');
				for (i=1; i<=maxItems; i++)
				{
					jQuery('#ask'+i).slideUp();
				}
				var indxThis = jQuery(this).attr('id').replace('quest','');
				if ( indxThis == indxOpen )
				{
					jQuery('#'+idAsk).slideUp();
					indxOpen = 0;
				}
				else
				{
					jQuery('#'+idAsk).slideDown();
					indxOpen = indxThis;
				}
			});
		});
		var indx = 0;
		FAQa.each(function() {
			indx++;
			var thisItem = jQuery(this);
			thisItem.attr('id','ask'+indx);
		//	thisItem.hide();
			thisItem.slideToggle(0);
		});
	}
}

function OD_formPrivacy()
{
/*	var formSpanMsg = jQuery('.subscription span.errorMsg');
	if ( formSpanMsg.length > 1 )
	{
		formSpanMsg.each(function() {
			jQuery(this).css({
				'visibility':	'visible',
				'display':		'none'
			});
		});
	} */
	var formBox = jQuery('.subscription .inputField');
	if ( formBox.length > 1 )
	{
		var inputPrivacy = formBox.find('input[id*=privacy]');
		inputPrivacy.parent('.inputField').css({
			'width':	'auto',
			'height':	'auto',
			'border':	'0'
		});
		inputPrivacy.css({
			'margin':	'0',
			'padding':	'0',
			'width':	'auto',
			'height':	'auto'
		});
		inputPrivacy.next('label:eq(0)').remove();
	}

	var inputCheckboxPrivacy = jQuery('.inputCheckboxPrivacy input');
	if ( inputCheckboxPrivacy.length > 0 )
	{
		inputCheckboxPrivacy.each(function() {
			var thisItem = jQuery(this);
			thisItem.click(function() {
				var divErrorMsg = jQuery(this).closest('.boxField').find('.errorMsg').closest('div');
				if (jQuery(this).attr('checked'))
					divErrorMsg.attr('class','invisible');
			});
		});
	}
}

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_removeLeftMenuWhenEmpty()
{
	var divMenuLeft = jQuery('.leftMenu_lev1');
	if ( divMenuLeft.length == 0 )
	{
	//	jQuery('#s4-leftpanel').remove();
		jQuery('.s4-ca').css('marginLeft','0px');
		jQuery('.welcome').css('paddingLeft','0px');
	}
}

function OD_iframeYouTube()
{
	jQuery('.ms-rteElement-iframeYouTube, .ms-rteStyle-iframeYouTube').html("<iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/RYYHmt2rPYo\" frameborder=\"0\" allowfullscreen></iframe>");
}

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);
		});
	}
}
