// ----------------------------------------------------------------------------
// global variables
// ----------------------------------------------------------------------------

var timeToFade = 500; // length of fading (in miliseconds)

// list of pictures to cycle through
var aFadeImg = new Array();
aFadeImg.push('/Portals/0/rosasPics/2007_01_a.jpg');
aFadeImg.push('/Portals/0/rosasPics/2007_02_a.jpg');
aFadeImg.push('/Portals/0/rosasPics/2007_03_a.jpg');
aFadeImg.push('/Portals/0/rosasPics/2007_04_a.jpg');
aFadeImg.push('/Portals/0/rosasPics/2007_05_a.jpg');
aFadeImg.push('/Portals/0/rosasPics/2007_06_a.jpg');
aFadeImg.push('/Portals/0/rosasPics/2007_07_a.jpg');
aFadeImg.push('/Portals/0/rosasPics/2007_08_a.jpg');
aFadeImg.push('/Portals/0/rosasPics/2007_09_a.jpg');
aFadeImg.push('/Portals/0/rosasPics/2007_10_a.jpg');
aFadeImg.push('/Portals/0/rosasPics/2007_11_a.jpg');

// -----
var fadeCookieName = 'fadeIdx';
var fadeCookieIdx = null;
var fadeNumLoaded = 0; // number of images loaded
var fadeNumItems = 0; // number of images
var fadeCurrentItem = 0;
var fadeTimer;

// initialize document
$(document).ready(function() {
	// get full page height
	var fwh = getWindowFullHeight();
	// show bg
	$('#fadeBG').css('height', fwh);
	$('#fadeBG').css('display', 'block');
	// get cookie value
	fadeCookieIdx = getCookie(fadeCookieName);
	if (fadeCookieIdx==null) fadeCookieIdx = -1;
	fadeCookieIdx = parseInt(fadeCookieIdx)+1;
	if (fadeCookieIdx < 0) fadeCookieIdx = 0;
	else if (fadeCookieIdx >= aFadeImg.length) fadeCookieIdx = 0;
	// set new picture source
	fadeNumItems = $('#fadeContainer .fadeItem').length;
	$('#fadeContainer .fadeItem').get(0).src = aFadeImg[fadeCookieIdx];
	// monitor the loading of the pictures
	$('#fadeContainer .fadeItem').load( function() { fadeNumLoaded++; callFadeShow() } );
});
// check when the document is fully loaded to verify the number of pictures loaded
// (IE don't always call the load event on the pictures)
$(window).load( function() {
	if (fadeNumLoaded < fadeNumItems) {
		fadeNumLoaded = fadeNumItems;
		fadeShow();
	}
});

// check if all images are loaded
function callFadeShow() {
	if (fadeNumLoaded >= fadeNumItems) {
		fadeShow();
	}
}

// do the fade
function fadeShow() {
	// set new cookie value
	setCookie(fadeCookieName, fadeCookieIdx, null, '/');
	// get window width and height
	var ww = getWindowWidth();
	var wh = getWindowHeight();
	// get full page height
	var fwh = getWindowFullHeight();
	// prepare pictures
	$('#fadeContainer .fadeItem').each(function(i) {
		// get and calculate sizes
		var iw = parseInt($(this).attr('width'));
		var ih = parseInt($(this).attr('height'));
		if (typeof(iw) == 'number' && typeof(ih) == 'number') {
			var n = Math.round(ih / (iw / ww));
			// test if new height is enough, if not resize based on height
			if (n < wh) {
				n = Math.round(iw / (ih / wh));
				iw = n;
				ih = wh;
			} else {
				iw = ww;
				ih = n;
			}
			$(this).attr('width', iw);
			$(this).attr('height', ih);
			// set new picture position
			$(this).css('left', -Math.floor((iw-ww) / 2));
			$(this).css('top', -Math.floor((ih-wh) / 2));
		}
	});
	// show container
	$('#fadeContainer').css('visibility', 'visible');
	// hide bg
	$('#fadeBG').css('display', 'none');
	// do the fade
	$('#fadeContainer .fadeItem').eq(fadeCurrentItem).animate({opacity: 0}, timeToFade, 'linear', fadeHide);
}

// hide the fade
function fadeHide() {
	$('#fadeContainer').css('visibility', 'hidden');
}

// ----------------------------------------------------------------------------
// size functions
// ----------------------------------------------------------------------------

// library of functions
function getWindowFullHeight() {
	if (window.innerHeight && window.scrollMaxY) {	
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
  	}
  	return yScroll;
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		} else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	// shorter version but not foul proof
	//windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	if (typeof(windowHeight) != 'number') windowHeight = 0;
	return windowHeight;
}

function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	} else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		} else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	// shorter version but not foul proof
	//windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
	if (typeof(windowWidth) != 'number') windowWidth = 0;
	return windowWidth;
}

// ----------------------------------------------------------------------------
// scroll functions
// ----------------------------------------------------------------------------

// get xy scroll position
function getScrollXY() {
	var pos = [];
	pos[0] = self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
	pos[1] = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
	return pos;
}

// set xy scroll position
function setScrollXY(px, py) {
	window.scrollTo(px, py);
}

// ----------------------------------------------------------------------------
// cookie functions
// ----------------------------------------------------------------------------

// set a cookie
// param: name, value, date (expire), path, domain, secure
// example: date=new Date;
//			date.setMonth(date.getMonth()+1); // expires in 1 month
//			setCookie("name", "foobar", date, "/", "example.com", false)
function setCookie(n, v) {
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = n + "=" + escape(v) +
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}

// get the value of a cookie
function getCookie(n) {
	var cookies = document.cookie.split(/;/);
	for (var i=0; i < cookies.length; i++) {
		var ci = cookies[i].split(/=/);
		if (ci[0]==n) return unescape(ci[1]);
	}
}

// delete a cookie
function delCookie(n) {
	setCookie(n, "", -1);
}


function expandText(sID)	{
   var param = document.getElementById(sID);
   param.style.display = (param.style.display=="none")?"":"none";	
}
