﻿// utility to provide some kind of verticalAlign
(function($) {
	$.fn.vAlign = function() {
		return this.each(function(i) {
			var ah = $(this).height();
			var ph = $(this).parent().height();
			var mh = (ph - ah) / 2;
			$(this).css('margin-top', mh);
		});
	};
})(jQuery);

(function ($) {
	$.fn.contentAlign = function (imageHeight, imageWidth) {
		return this.each(function (i) {
			var image = $(this).find("img");
			var hh = image.attr("h");
			var ww = image.attr("w");
			var mh = (imageHeight - hh) / 2;
			var mw = (imageWidth - ww) / 2;
			image.css('margin-top', mh);
			image.css('margin-left', mw);
		});
	};
})(jQuery);

function toTop(layer, action, z, usrName)
{
	var lref;
	if(document.getElementById && (lRef=document.getElementById(layer)))
	{
		lRef.style.visibility = action ? "visible" : "hidden";
		lRef.style.zIndex= z;

		if (lRef.style.visibility == "hidden")
			return;

		var txtObj = document.getElementById(usrName);
		txtObj.focus();
	}
}

function changeActionCountry(country, appPath)
{
	var act = document.forms[0].action;
	var re = new RegExp('\\b' + appPath + '\\b[\\/]\\w{3}[\\/]');
	var newAct = act.replace(re, appPath + '/' + country + '/');
	document.forms[0].action = newAct;
}
/*
function switchActionCountry(newAct)
{
	document.forms[0].action = newAct;
}
*/

function doSearch(searchPage, lang)
{
	var txtSearch = $('.searchInput');
	var strSearch = txtSearch.val(); // x-browser compatible
	window.location.href = searchPage + '?q=' + strSearch + '&cx=002618154740868493230:_ysyldc6xso&cof=FORID:9&hl=' + lang;
}

/* ------------- all the window.open functions are now collected here --------- */
function popupWindowWithTitle(url, title, width, height)
{
	var hWnd = window.open(url, title, "width=" + width + ", height=" + height + ", scrollbars=yes, resizable=no");
	if (!hWnd.opener) hWnd.opener = self;
	if (hWnd.focus != null) hWnd.focus();
}
function popupWindow(url, width, height)
{
	popupWindowWithTitle(url, "", width, height);
}
function NewWindow(url, width, height)
{
	popupWindow(url, width, height);
}
function NewWin(url, larghezza, altezza)
{
	var hWnd = window.open(url, "", "width=" + larghezza + ", height=" + altezza + ", scrollbars=no, resizable=no, left=10, menubar=no,status=no,location=0, toolbar=no");
    if (!hWnd.opener) hWnd.opener = self;
    if (hWnd.focus != null) hWnd.focus();
}

function NewWinCenter(url, larghezza, altezza) {
	var centerWidth = (screen.width - larghezza) / 2 ;
	var centerHeight =(screen.height - altezza) / 2 ;
	var hWnd = window.open(url, "", "width=" + larghezza + ", height=" + altezza + ", scrollbars=no, resizable=no, left=" + centerWidth + ", top=" + centerHeight + ",  menubar=no,status=no,location=0, toolbar=no");
	if (!hWnd.opener) hWnd.opener = self;
	if (hWnd.focus != null) hWnd.focus();
}

	
/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
/* Modified to support Opera */
function bookmarksite(title, url)
{
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print)
	{ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}


/* Used by imagegallery rollover */
/* It's not multi-instance, we'll replace it with jQuery based version */
var j = 0;
var tabs;
function startIt(cntID, secs)
{
	var cnt = document.getElementById(cntID);
	tabs = cnt.getElementsByTagName('td');
	var tim = setInterval('doIt()', secs);
}

function doIt()
{
	for (var i = 0; i < tabs.length; ++i)
	{
		var tab = tabs[i];
		if (i == j)
			tab.style.display = 'block';
		else
			tab.style.display = 'none';
	}
	
	++j;
	if (j == tabs.length)
		j = 0;
}
/* end of code used by imagegallery rollover */



function printPage()
{
	try
	{
		var w = this.window;
		if (w.document)
		{
			w.focus();
			w.print();
		}
	}
	catch (e)
	{
		alert(e.description);
	}
}

/* ---  --- */
/*
Menhir plugin
A clean and monolithical images slider as well as light
*/

(function ($) {
	$.fn.menhir = function (vars) {
		var containerId = $(this).attr("id");
		var tagDivFrame = '<div class="[className]"><p>[text]</p></div>';
		var imgT = '';
		var pos = 0;
		var slidesCount = $("#" + containerId + " div").size();
		if (slidesCount == 0) return;

		// removing container div from the total count
		slidesCount = slidesCount - 2;
		var mOver = false;
		var images = new Array();
		var assignedClass = '';
		var t;
		var container = $("#" + containerId);
		var mouseOver = function () { mOver = true; };
		var mouseOut = function () {
			mOver = false; clearTimeout(t);
			t = setTimeout(slide, vars.timeOut);
		};

		container.live("mouseover", mouseOver);
		container.live("mouseout", mouseOut);

		$("#" + containerId + " div").css("display", "none");

		$("#" + containerId + " div").each(function () {
			if ($(this).attr("class") != 'coreImage') {
				assignedClass = $(this).attr("class");
				imgT = tagDivFrame.replace('[className]', assignedClass);
				imgT = imgT.replace('[text]', $(this).html());
				images.push(imgT);
			}
		});

		// shuffling the array to randomize starting frame
		shuffle(images, slidesCount);

		var tagDivCore = images[pos];
		var tagDivCoreSwap = images[pos + 1];

		$("#" + containerId + " div.coreImage").html(images[pos]);
		$("#" + containerId + " div.coreImage").show();
		$("#" + containerId + " div.coreImageSwap").html(images[pos + 1]);
		$("#" + containerId + " div.coreImageSwap").show();

		var slide = function () {
			if (mOver) return;

			if (pos == slidesCount - 1) {
				tagDivCore = images[pos];
				tagDivCoreSwap = images[0];
			}

			if (pos > slidesCount - 1) {
				pos = 0;
				tagDivCore = images[pos];
				tagDivCoreSwap = images[pos + 1];
			}

			$("#" + containerId + " div.coreImage").html(tagDivCore);
			$("#" + containerId + " div.coreImage").show();
			$("#" + containerId + " div.coreImageSwap").html(tagDivCoreSwap);
			$("#" + containerId + " div.coreImageSwap").show();

			$("#" + containerId + " div.coreImage").fadeOut(2000,
				function () {
					pos++;
					tagDivCore = images[pos];
					tagDivCoreSwap = images[pos + 1];
				});

			clearTimeout(t);
			t = setTimeout(slide, vars.timeOut);
		};

		clearTimeout(t);
		t = setTimeout(slide, vars.timeOut);
	};
})(jQuery);

// Implementation of the Fisher-Yates algorithm to provide a shuffle function

function shuffle(arr, length) {
	var i = length, j, temp;
	if (i == 0) return;
	while (--i) {
		j = Math.floor(Math.random() * (i + 1));
		temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
}

function startMenhir() {
	$(".menhirSlider").show();
	$("#sliderText").menhir({ timeOut: 10000 });
}

/* ---  --- */
function CallAjaxYouTubeVideo(elResult, videoUrl, width, height, prePath) {
	$.get(prePath + '/CMS/YouTubeVideoSnippet.ashx', { videoUrl: videoUrl, width: width, height: height },
		function (response) {
			elResult.html(response);
			elResult.removeAttr('onclick');
		});
}

/* ---  --- */
$(document).ready(function () {
	$(".buttonStyleUI").button();
});
