/* widgets */
function cargarWidgets() {
	$('[class*=widget:]').each(function() {
		var parts = this.className.split(' ');
		var widget = '';
		for (var i = 0; i < parts.length; i++) {
			var pos = parts[i].indexOf('widget:');
			if (pos == 0) {
				widget = parts[i].substring(pos + 7);
				break;
			}
		}
		cargarWidget(this, widget);
	});
}

function cargarWidget(divWidget, widget) {
		var url = 'http://localhost/HUSA_Princesa/ajax-widget.php';
		var $elDiv = $(divWidget);
		$.get(url, {"widget" : widget}, function(data, textStatus) {
			$elDiv.html(data);
		}, 'html');
}

/* fancybox para galerias */
function extendLinks(scope) {
	var elScope = scope || document,
		$a = $('a', scope);
	// adicion del atributo target
	$a.filter('[rel*=external]').attr('target', '_blank');
	// fancybox 
	$a.filter(function(){
		var $t=$(this);
		return ($t.hasClass('fancylink') && !$t.data('fancyAsignado'));
	}).data('fancyAsignado', true).fancybox({
		titlePosition:'inside',
		onComplete: function() {
			var $title = $('#fancybox-title');
			if (/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test($title.text())) {
				$title.html(['<a class="goudy-bookletter" href="', $title.text(), '">', txtDescargarAlta , '</a>'].join(''));
			}
		}
	});
	
	// anyadir a favoritos
	$a.filter('.addFavorites').click( function() {
		title = document.title;
		if (window.sidebar) { // Firefox
			window.sidebar.addPanel(title, window.location.href,"");
		} else if( window.external ) { // IE
			window.external.AddFavorite( window.location.href, title);
		} else {
			alert(txtBrowserNoSoportado);
		}
		return false;
	});
	
	// anyadir a GPS
    $a.filter('.addToGPS, .rSociales').one('click', function() {
	    if (!this.asignadoFancyBox) {
	        this.asignadoFancybox = true;
	        $(this).fancybox({
	            hideOnContentClick:false,
	            width: 300,
	            height: 350,
				autoDimensions: false,
	            padding: 15,
	            overlayShow: false
	        });
	        $(this).click();
	    }
	    return false;
	});
	$('#ajaxSendToGPS a.boton').live('click', function() {
		var uri = $('#ajaxSendToGPS select').val(),
			popup;
		if (uri && uri != 0) {
			popup = window.open(uri, 'popupGps', 'width=800, height=450');
			popup.focus();
		}
	});

}

function initSlideshow(scope) {
	var $div = $('div.slideshow', scope || document);
	$div.find('img').not(':first-child').hide();
	function nextImg() {
		$div.each(function() {
			var $img = $div.find('img'),
				$aOcultar = $img.filter(':visible'),
				$aMostrar = $aOcultar.next();
			if (!$aMostrar.is('img')) {
				$aMostrar = $img.eq(0);
			}
			$aMostrar.css({
				display:'',
				position:'absolute',
				zIndex:4
			});
			$aOcultar.fadeOut(1000, function() {
				$aMostrar.css({
					position:'',
					zIndex:''
				});
			});
		});
		setTimeout(nextImg, 5000);
	}
	setTimeout(nextImg, 4000);
}

function prepararEventos(scope) {
	initSlideshow(scope);
	extendLinks(scope);
}

$(function() {
	prepararEventos(document);
});
