// Funciones Ajax --------------

function loadurl(url,id){
	var pagecnx = createXMLHttpRequest();
	pagecnx.onreadystatechange=function(){
		if (pagecnx.readyState == 4 && (pagecnx.status==200 || window.location.href.indexOf("http")==-1))
		document.getElementById(id).innerHTML=pagecnx.responseText;
	}
	pagecnx.open('GET',url,true)
	pagecnx.send(null)
}

function loadurlkey(e,url,id){
	tecla = (document.all) ? e.keyCode : e.which;
	if (tecla==13)
		loadurl(url,id);
}

function createXMLHttpRequest(){
	var xmlHttp=null;
	if (window.ActiveXObject) 
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
		xmlHttp = new XMLHttpRequest();
	return xmlHttp;
}

//-----------------------------------


function ventana(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen) {

	var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
	var int_windowTop = (screen.height - a_int_windowHeight) / 2;
	var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
	var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
	if (parseInt(navigator.appVersion) >= 4) {
	obj_window.window.focus();
	}
};

function mycarousel_initCallback(carousel)
{    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

/*
function initCarousel(carousel) {
    jQuery('a#prevItem').bind('click', function() {
        carousel.prev();
        return false;
    });
    jQuery('a#nextItem').bind('click', function() {
        carousel.next();
        return false;
    });
};
*/



$(document).ready(function() {
	
	// Para mejorar la accion del add en ventanas Modales
	/*
	var script = "http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4c99bcb81d3a88e0#domready=1";
	if (window.addthis){
    	window.addthis = null;
	}
	$.getScript( script );
	*/
	
	
	//Seleccionamos todos los TAG con el nombre MODAL
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//ID objetivo meto el contenido dentro
		var elegida = 'dialog1';
		var id = '#'+ elegida;
		var laweb = $(this).attr('href');
		
		//Cojemos Ancho y Alto de la Página
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Alto y Ancho de la Ventana del navegador
		var winH = $(window).height();
		var winW = $(window).width();
		
		ancho = $(id).width();
		alto = $(id).height();
				
		$(id).load(laweb + '/modal',function() { 
		
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		$('#mask').fadeIn(500);
		$('#mask').fadeTo("fast",0.4);	
	          
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
		
		$(id).show();
		$('#lengueta').css('top',  (winH/2-$(id).height()/2)-23);
		$('#lengueta').css('left', (winW/2-$(id).width()/2)+12);
		$('#lengueta').show();
		
		//ponemos la Sombra en la Ventana
		$('#sombra').fadeTo("fast",0.3);
		
		$('#sombra').css({'width':$(id).width(),'height':'90%'});
		//$('#sombra').css({'width':$(id).width(),'height':$(id).height()+10});
		//$('#sombra').css({'width':1000,'height':1000});
		
		$('#sombra').css('top',  (winH/2-$(id).height()/2)+16);
		$('#sombra').css('left', (winW/2-$(id).width()/2)-7);
		
		
		
		//Funciones Para Carrusel ***********************************
		//***********************************************************
		$(".change_foto").click(function() {
			the_id = $(this).attr('id');
			var arr = the_id.split('-');
			var elvalor = arr[1];
			var elid = arr[0];
			
			$('#foto-ppal').attr("src", elvalor);
			 //fancy(elid-1);
			
			$('#ref-foto-ppal').attr("href", "javascript: " + fancy(elid-1) + ";");
			 //$('#ref-foto-ppal').removeClass('fancy');
			 //$('#ref-foto-ppal').attr("onclick", fancy2(elid-1));
			});
		
		
		
		$('#carrusel').jcarousel({
			auto: 0,
			//wrap: 'last',
			animation: 'slow',
			scroll: 1,
			itemFallbackDimension:335,
			//initCallback: initCarousel,			
			initCallback: mycarousel_initCallback

			});
		
			
		$("a[rel='galeria']").colorbox({transition:"none", width:"750px", height:"650px"});
		$(".youtube").colorbox({iframe:true, innerWidth:550, innerHeight:500});
		

		}); // Se Cierra Funcion de carga
		
	
	});
	
	//Si se pulsa en la Lengueta
	$('#lengueta').click(function (e) {
		e.preventDefault(); //Cancelamos el comportamiento
		$('#mask').fadeOut(500);
		$('.window').fadeOut(500);
		$('#dialog1').html('');
	});		
	
	//Si se Clickea Fuera
	$('#mask').click(function () {
		$(this).fadeOut(500);
		$('.window').fadeOut(500);
		$('#dialog1').html('');
	});	
	
		
	
});
