function addEvent(obj, evType, fn){

		if(obj.addEventListener){
			obj.addEventListener(evType, fn, false);
			return true;
		}else if (obj.attachEvent){
			return obj.attachEvent("on" + evType, fn);
		}else{
			return false;
		}

}
function ObrePopUp(e, origen){

		if(!document.captureEvents)	e=window.event;

		var boto = (e.which?e.which:e.button);
		var passa = false;

		if(e.type=="click" && (boto==0 || boto==1)) passa = true;
		if(e.type=="keypress" && e.keyCode==13) passa=true;

		if(passa){
			window.open(origen.href, "", "");
			return false;
		}
}
addEvent(window, "load", function(){

	if(!document.getElementsByTagName) return;

		var ancores = document.getElementsByTagName("a");

		for(var i=0, j=ancores.length; i<j; i++){
			if(ancores[i].getAttribute("rel")=="external"){ //fem les accions per tractar enllaços externs
				ancores[i].onkeypress = function(e){return ObrePopUp(e, this);}
				ancores[i].onclick = function(e){return ObrePopUp(e, this);}
			}
		}
	}
	
);
/*if(document.captureEvents) {
	if(Event.KEYPRESS){
		document.captureEvents(Event.KEYPRESS);
	}
}*/


jQuery.noConflict();
jQuery(document).ready(function()
{
	moveFlash();

	//boton prev, sgte
	jQuery('#ngg-next,#ngg-prev').click(function() {
		jQuery('#loading').show();
		startLoad( jQuery(this).attr('href') );
		return false;
	});
	//miro para fixear url
	fixURL();
	//resize imagen
	jQuery('.pic').hide();
	jQuery('.ngg-imagebrowser-nav h3').hide();
	if( jQuery('.pic').length>0 ){
		jQuery('#wrapper').append('<div id="loading">Loading...</div>');
	}
	jQuery('.pic img').load( resizeImg );
	jQuery(window).resize(function() {
		resizeImg();
	});
	//oculto los submenues
	jQuery(".sidebar_portafoli .children").hide();
	//si esta seleccionado lo muestro
	jQuery(".current_page_parent .children").show();
	//le pongo el action al padre
	padre=jQuery(".children").parent().find('a:first');
	padre.click(function(){
		jQuery(this).parent().find("ul").slideDown("slow");
		return false;
	});
	//oculto bordes
	jQuery('a').css("outline","none"); 
	
	//BLOG abrir en ventana
	jQuery('.page-item-225 a').click(function(){
		if( !(jQuery('body').hasClass("blog")) ){
			window.open(jQuery(this).attr('href'), "", "");
			return false;
		}
	});
	//CLIENTES abrir en ventana
	jQuery('.page-item-9 a').click(function(){
		if( !(jQuery('body').hasClass("clients")) ){
			window.open(jQuery(this).attr('href'), "", "");
			return false;
		}
	});

	//contact-map google
	jQuery(".contact-googlemap").css("opacity","0");
	jQuery(".contact-googlemap").css("height","0px");
	if( jQuery('body').hasClass("ca") ){
		ver="Veure Google Map";
	}
	if( jQuery('body').hasClass("es") ){
		ver="Ver Google Map";
	}
	if( jQuery('body').hasClass("en") ){
		ver="View Google Map";
	}
	jQuery(".contact-map").append('<br /><a href="#" class="viewgooglemap" >'+ver+'</a></span>');
	jQuery(".viewgooglemap").click(function(){
		jQuery(".contact-googlemap").animate({
			height:350,
			opacity:1
		});		
	});
});

function startLoad(href){
	jQuery('.pic img').attr("src","/wp-content/themes/blackblanc/css/blank.gif");
	jQuery(".pic img").attr("alt","");
	jQuery('.pic').hide();
	jQuery(".ngg-imagebrowser-nav .title").html("");
	jQuery('.ngg-imagebrowser-nav h3').hide();
	jQuery.ajax({
		   url: href,
		   success: function(data){
			loadIMG(data) 
		   },
		   error: function(){
		   	alert("Error");
		   }
	});
	changeURL(href);
}
function endLoad(){
	jQuery('.pic').show();
	jQuery('.ngg-imagebrowser-nav h3').delay(2000).fadeIn('fast');
	jQuery('#loading').delay(2000).fadeOut('slow');
}

function loadIMG(data){
	img = jQuery(".pic img",data).attr("src");
	jQuery('.pic img').attr("src",img);
	jQuery('.pic img').load( resizeImg() );
	
	next = jQuery("#ngg-next",data).attr("href");
	prev = jQuery("#ngg-prev",data).attr("href");
	counter = jQuery(".ngg-imagebrowser-nav .counter",data).html();
	desc = jQuery(".ngg-imagebrowser-nav .ngg-imagebrowser-desc",data).html();
	jQuery('#ngg-next').attr('href',next);
	jQuery('#ngg-prev').attr('href',prev);
	jQuery(".ngg-imagebrowser-nav .counter").html(counter);
	jQuery(".ngg-imagebrowser-nav .ngg-imagebrowser-desc").html(desc);
	
	imgTitle =jQuery(".pic img",data).attr("title");
	imgAlt = jQuery(".pic img",data).attr("alt");
	title = jQuery(".ngg-imagebrowser-nav .title",data).html();
	jQuery('.pic img').attr("title",imgTitle);
	jQuery('.pic img').attr("alt",imgAlt);
	jQuery(".ngg-imagebrowser-nav .title").html(title);

	brwTitle = jQuery(data).filter('title').text();
	document.title=brwTitle;
	
	return false;
}
function resizeImg(){
	//veo alto y ancho maximo
	marginside=parseInt( jQuery(window).width()*0.1 );
	margintop=parseInt( jQuery(window).height()*0.1 );
	widthImg=parseInt( jQuery(window).width()-marginside-jQuery('#sidebar').width() );
	heightImg= parseInt( jQuery(window).height()-margintop-jQuery('#header').height()-jQuery('#footer').height() );
	//pongo alto y ancho originales de la imagen	
	jQuery('.pic img').css('width','auto');
	jQuery('.pic img').css("height",'auto');
	
	//si es clientes le doy al contenedor el ancho y alto maximos
	if (jQuery('body').hasClass('clients')) {
		jQuery('.pic').css('max-width', '800px');
		jQuery('.pic').css('max-height', '600px');
		jQuery('.pic').css('margin', '0px');
		jQuery('.pic').css('float', 'left');
		jQuery('.pic img').css('float', 'left');
	//si es portafolio
	} else {
		jQuery('.pic').css('width', widthImg);
		jQuery('.pic').css('height', heightImg);
		jQuery('.pic img').css('float', 'left');
		
		//si la imagen es horizontal
		if( jQuery('.pic img').width()>jQuery('.pic img').height() ){
			//le pongo a imagen el ancho al 100%
			jQuery('.pic img').css('width', '100%');
			jQuery('.pic img').css("height", 'auto');
			//pero si de alto hay menos espacio disponible, pongo el alto al 100%
			if( jQuery('.pic img').height()>heightImg ){
				jQuery('.pic img').css('width','auto');
				jQuery('.pic img').css("height",'100%');
			}
			//pero si el ancho es mayor que 1200 lo escalo proporcionalmente
			if (jQuery('.pic img').width() > 1200) {
				imgwidth = '1200';
				imgheight = parseInt((jQuery('.pic img').height() * 1200) / jQuery('.pic img').width());
				jQuery('.pic img').css('width', imgwidth + 'px');
				jQuery('.pic img').css('height', imgheight + 'px');
			}			
			//pero si el alto es mayor que 780 lo escalo proporcionalmente
			if( jQuery('.pic img').height()>780 ){
				imgwidth=parseInt( (jQuery('.pic img').width()*780)/jQuery('.pic img').height() );
				imgheight='780';
				jQuery('.pic img').css('width',imgwidth+'px');
				jQuery('.pic img').css('height',imgheight+'px');
			}
		//si la imagen es vertical	
		}else{
			//le pongo a la imagen el alto al 100%
			jQuery('.pic img').css('width','auto');
			jQuery('.pic img').css("height",'100%');
			//pero si de ancho hay menos espacio disponible, pongo el ancho al 100%
			if (jQuery('.pic img').width() > widthImg) {
				jQuery('.pic img').css('width', '100%');
				jQuery('.pic img').css("height", 'auto');
			}
			//pero si el ancho es mayor que 1200 lo escalo proporcionalmente
			if (jQuery('.pic img').width() > 1200) {
				imgwidth = '1200';
				imgheight = parseInt((jQuery('.pic img').height() * 1200) / jQuery('.pic img').width());
				jQuery('.pic img').css('width', imgwidth + 'px');
				jQuery('.pic img').css('height', imgheight + 'px');
			}			
			//pero si el alto es mayor que 780 lo escalo proporcionalmente
			if( jQuery('.pic img').height()>780 ){
				imgwidth=parseInt( (jQuery('.pic img').width()*780)/jQuery('.pic img').height() );
				imgheight='780';
				jQuery('.pic img').css('width',imgwidth+'px');
				jQuery('.pic img').css('height',imgheight+'px');
			}
		}
		
	}
	//alert( jQuery('.pic img').width()+"x"+jQuery('.pic img').height() );
	//alert(widthImg+"x"+heightImg);
	if( jQuery('.pic').is( ':hidden' ) ){
		endLoad();		
	}
}
function changeURL(href) {
	href = (href == "") ? "/" : href;
	uri = window.location.href.split("#/");
	window.location.href = uri[0] + "#/" + href;
}
function fixURL() {
	parts = window.location.href.split("#/");
	if(parts.length > 1) {
		window.location.href = parts[parts.length - 1];
	}
}

/* FLASH INICI */
jQuery(window).resize(function(){
	moveFlash();
});
function moveFlash(){
	if(jQuery('#flash').length>0){
		flashX=parseInt( jQuery('.page').width() );
		flashX=parseInt( (flashX/2)-(jQuery('#flash').width()/2 ) );
		flashY=parseInt( jQuery(window).height()-(jQuery('#header').height()+jQuery('#footer').height()) );
		flashY=parseInt( (flashY/2.5)-(jQuery('#flash').height()/2 ) );
		jQuery('#flash').css('margin-right',flashX+"px");
		jQuery('#flash').css('margin-top',flashY+"px");
	}
}

