/* http://www.quirksmode.org/js/cookies.html */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// http://portfolio.gizone.co.uk/applications/slideshow/

$.fn.alidatashow = function(options) {
	var settings = {
		delay: 2000,
		order: 'sequence'
	}
	
	if (options)
		$.extend(settings, options);

	$.alidatashow.settings = settings;

	this.css('position', 'relative');

	$.alidatashow.slides = this.find('img').get();
	$.alidatashow.length = $.alidatashow.slides.length;
	for (var i = 0; i < $.alidatashow.length; i++) {
		$($.alidatashow.slides[i]).css({
			zIndex: $.alidatashow.length - i,
			position: 'absolute',
			top: '0',
			left: '0'
		});
	}

	$.alidatashow.previous = 0;
	$.alidatashow.current = 1;

	setInterval(function() {  $.alidatashow.next();  }, settings.delay);
}

$.alidatashow = {
	settings: null,
	slides: null,
	length: null,
	previous: null,
	current: null,
	timeout: null,

	next: function() {
//		alert('Length: ' + this.length + ' Prev: ' + this.previous + ' Curr: ' + this.current);
		for (var i = 0; i < this.length; i++) {
			//if (i == this.previous) {
			//	$(this.slides[i]).css('display', 'block');
			//	continue;
			//}
			$(this.slides[i]).css('display', 'none');
		}
		$(this.slides[this.previous]).css('display', 'block').css('zIndex', '0');
		$(this.slides[this.current]).css('zIndex', '1').fadeIn('slow');

		if (this.settings.order == 'sequence') {
			if ((this.current + 1) < this.length) {
				this.previous = this.current;
				this.current = this.current + 1;
			} else {
				this.previous = this.length - 1;
				this.current = 0;
			}
		} else if (this.settings.order == 'random') {
			this.previous = this.current;
			while (this.current == this.previous)
				this.current = Math.floor(Math.random() * (this.length));
		} else {
			alert('jQuery SlideShow plugin: order settings must either be \'sequence\' or \'random\'');
		}
	}
}

/*$(document).ready(function() {
	$('body').append('<form style="display:none" id="login-form"><input /><br /><input type="password" /></form>');
	$('#nav-login a').click(function() {
		$('#login-form').slideToggle('slow');
		return false;
	});
});*/

function extractRequestPath(url) {
	var path = url, i = url.indexOf('//');
	if(i > -1) path = path.substr(i+2);
	i = path.indexOf('/');
	if(i > -1) path = path.substr(i);
	return path;
}

function intro() {
	var path = extractRequestPath(window.location.href);
	if (!readCookie('intro.desactivar') && path == '/') {
		window.location.href = '/intro/';
	}
	if (path == '/intro/') {
		createCookie('intro.desactivar', 1);
	}
}

if ($.browser.msie) $(window).unload(function() { // clean events
    var event = jQuery.event, global = event.global;
    for (var type in global) {
        var els = global[type], i = els.length;
        if (i>0) do event.remove(els[i-1], type); while (--i);
    }
});

intro();
