$(function () {
	
	// Menu idiomas
   	$('#menu li:has(ul)').hover( 
      function(e) 
      { 
         $(this).find('ul').fadeIn();
		 $(this).addClass('activo');
      }, 
      function(e) 
      { 
         $(this).find('ul').fadeOut('fast'); 
		 $(this).removeClass('activo');
      } 
   	);
	
	//hide div.intro pinturas
	$("#overlay .intro a").click(function(){
	$("#overlay .intro").hide("slow");	
	});
	// Carouseles img opacity
	$('#mycarousel img, #matter-gal img').css('opacity','0.5');
	$('#mycarousel a.activo img, #matter-gal a.activo img').css('opacity','1');
	$("#mycarousel a:not(.activo) img, #matter-gal a:not(.activo) img").hover(function () { // on mouse over
		$(this).stop().animate({opacity: 1}, "normal");
	},
	function () { // on mouse out
		$(this).stop().animate({opacity: 0.5}, "normal");
	});
	
	// Carouseles
	$('#mycarousel').jcarousel({scroll: 1, animation: 700});
	$('#matter-gal').jcarousel({scroll: 1, animation: 700});
	 
	// Colorbox
	$('.contact').colorbox({transition:'fade', opacity:0, width:"440", height:"340", initialWidth: '100', initialHeight: '100', iframe: true});
	$('.news-cbox').colorbox({transition:'fade', opacity:0, width:"588", height:"358", initialWidth: '100', initialHeight: '100', iframe: true});
	$('.img-detalle').colorbox({transition:'fade', opacity:0});

});
//imagen pintura grande
function obtenerImagenPintura(id_obra){
	$('#imagen_pintura').load('ajax.php?f=agregarImagenPintura&id_obra='+id_obra);
}

// INPUTS
function eFocus(field){if (field.value == field.defaultValue){field.value ='';}}
function eBlur(field){if (field.value == ''){field.value = field.defaultValue;}}

// REFRESCA CAPTCHA
function captchaRefresh(){rand = $.random(99999);$('#captcha_container').attr('src', 'captcha.php?'+rand)}

// White wall
$(document).ready(function(){
						   	
							
							
	// Executed once all the page elements are loaded
	var preventClick=false;
	$(".pic a").bind("click",function(e){

		/* This function stops the drag from firing a click event and showing the lightbox */
		if(preventClick)
		{
			e.stopImmediatePropagation();
			e.preventDefault();
		}
	});

	$(".pic").draggable({

		/* Converting the images into draggable objects */
		containment: 'parent',
		start: function(e,ui){
			/* This will stop clicks from occuring while dragging */
			preventClick=true;
		},
		stop: function(e, ui) {
			/* Wait for 250 milliseconds before re-enabling the clicks */
			setTimeout(function(){ preventClick=false; }, 250);
		}
	});

	$('.pic').mousedown(function(e){
		/* Executed on image click */
		var maxZ = 0;

		/* Find the max z-index property: */
		$('.pic').each(function(){
			var thisZ = parseInt($(this).css('zIndex'))
			if(thisZ>maxZ) maxZ=thisZ;
		});

		/* Clicks can occur in the picture container (with class pic) and in the link inside it */
		if($(e.target).hasClass("pic"))
		{
			/* Show the clicked image on top of all the others: */
			$(e.target).css({zIndex:maxZ+1});
		}
		else $(e.target).closest('.pic').css({zIndex:maxZ+1});
	});

	
	/* Converting the share box into a droppable: */
	$('.drop-box').droppable({
		hoverClass: 'active',
		drop:function(event,ui){

			/* Fill the URL text field with the URL of the image. */
			/* The id of the image is appended as a hash #pic-123 */
			$('#url').val(location.href.replace(location.hash,'')+'#' + ui.draggable.attr('id'));
			$('#modal').dialog('open');
		}
	});

	/* Converts the div with id="modal" into a modal window  */
	$("#modal").dialog({
		bgiframe: true,
		modal: true,
		autoOpen:false,

		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});

	if(location.hash.indexOf('#pic-')!=-1)
	{
		/* Checks whether a hash is present in the URL */
		/* and shows the respective image */
		$(location.hash+' a.fancybox').click();
	}
});
