try{document.execCommand("BackgroundImageCache", false, true);}catch(err){};


$('document').ready(function(){

/* Clic en un menu... */

$('#menu li.inside').click(function(e){
	e.preventDefault();
	var a = $(this).children().attr("href");
	$('#text').fadeOut('normal', function(){
		$('#context').slideUp('fast');
		$('#text').load(a, function(){
			$('#context').slideDown('slow', function(){
				$('body').css('background-color', '#000');
				$('#background').fadeTo('normal', 0.5);
				$('.focus').focus(function(){if($(this).attr('title')==$(this).val())$(this).val("");});
				$('.focus').blur(function(){if($(this).val()=='')$(this).val($(this).attr('title'));});
				return false;
			});
			$('#text').fadeIn('normal');
			return false;
		});
		return false;
	});
	return false;
});

$(".galeria").colorbox({rel:'galeria', slideshow:true});


$('#close').click(function(e){
	e.preventDefault();
	$('#context').slideUp('slow');
	$('#background').fadeTo('normal', 1, function(){
		$('body').css('background-color', '#fff');
		return false;
	});
	return false;
});

/* Slideshow */

var imgs = new Array();

$.get('inc/listdir.php', function(Data){
	imgs = Data.split(',');
	slide(1);
});

function slide(cc){
	if(cc>=imgs.length)cc=0;
	$('#backimg').animate({opacity: 1.0}, 10000, function(){
		$('#backimg').fadeOut('slow').attr('src',imgs[cc]).fadeIn(2000,function(){slide(cc+1);});
	});
}

/*  OLDIE function slide(cc){
	if(cc>=imgs.length)cc=0;
	$('#backimg').fadeOut(500,function(){
		$('#backimg').attr('src',imgs[cc]).fadeIn(500,function(){slide(cc+1);});
	});
}*/

/* Carga de un mp3 */

/*var so = new SWFObject('js/player.swf','mpl','0','0','8');
so.addParam('allowscriptaccess','always');
so.addParam('allowfullscreen','true');
so.addVariable('height','0');
so.addVariable('width','0');
so.addVariable('file','inc/sonido.mp3');
so.addVariable('id','player');
so.addVariable('searchbar','false');
so.addVariable('shownavigation','false');
so.addVariable('showdigits','false');
so.addVariable('usefullscreen','false');
so.addVariable('thumbsinplaylist','false');
so.addVariable('autostart','true');
so.addVariable('repeat','true');
so.addVariable('enablejs','true');
so.write('player');*/

/* Background al 100%  http://klippoglim.no/blog/2009/04/full-screen-images/ */

(function(){
    var db=$('body');
    var img=$('#background img:eq(0)');
    var dbsize={}; //needed to store body size
    var imgsrc=img.attr("src"); //needed to store images src
    var keyStop=function(e){
        var e=window.event||e||{};
        var tag=e.target.tagName.toLowerCase();
        if(tag!='textarea'&&!(tag=='input'&&(e.target.type=='text'||e.target.type=='password'))){ //are the user not writing?
            if(e.keyCode==32||e.keyCode==39||e.keyCode==40){ //Did he press any of the "scrolling-keys"? (down, right, and space key)
                if(e.preventDefault)e.preventDefault();
                else e.returnValue=false;
            }
        }
    }

    if(this.addEventListener)window.addEventListener('keydown',keyStop,false);
    else window.attachEvent('onkeydown',keyStop);

    setInterval(function(){
        window.scrollTo(0,0);
        if(img[0].complete){ 
            if(db[0].clientWidth!=dbsize.w||db[0].clientHeight!=dbsize.h||img.attr("src")!=imgsrc){ //check if size or img size has changed
                imgsrc=img.attr("src"); //store current src
                dbsize.w=db[0].clientWidth; //store current body width
                dbsize.h=db[0].clientHeight; //store current body height

                var newwidth=Math.round(dbsize.h*(img.attr("offsetWidth")/img.attr("offsetHeight"))); //calculate new width based on height

                img.width(dbsize.w>newwidth?dbsize.w:newwidth); //use the largest value of body-width and newwidht
                //and this is the real trick: if there's no specified height, the height is automaticly calculated relative to the with
            }
        }
    },300);
})();

/* Control reproduccion mp3 */

$("#play").click(function(){
	sendEvent('mpl','playpause');
});

$("#pause").click(function(){
	sendEvent('mpl','stop');
});

function sendEvent(swf,typ){thisMovie(swf).sendEvent(typ);};
function thisMovie(swf){if($.browser.msie){return window[swf]}else{return document[swf];}}


/* Funcion para validar el formulario de contacto */

$('.send').live('click', function(e){
	e.preventDefault();
	vamos=true;
	$('.required').each(function(){
		if ($(this).val()=='' || $(this).val() == $(this).attr("title")) {
			console.log('Val: '+ $(this).val() + " Title:" + $(this).attr("title"));
			vamos=false;
		} else if ($(this).attr('name')=='email' && !checkEmail($(this).val())) {
			vamos=false;
		}
	});
	 if(!vamos){
		overDiv('Revisa los campos marcados como obligatorios',0);
	} else { 
		$('#formcontact').submit();
	}		
});


/* Funcion para mostrar mensajes en pantalla mediante un overDiv */

if(msg){
	overDiv(msg,0);
}


function overDiv(msg,f){
	if(f==0){
		var css={
			border: 'none',
			padding: '15px',
			'font-size': '14px',
			backgroundColor: '#000',
			'-webkit-border-radius': '10px',
			'-moz-border-radius': '10px',
			opacity: '.5',
			color: '#fff',
			width:'30%'
		};
	}else{
		var css={
			border: 'none',
			padding: '15px 40px',
			'font-size': '14px',
			backgroundColor: '#fff',
			'-webkit-border-radius': '10px',
			'-moz-border-radius': '10px',
			color: '#000',
			left: '17.5%',
			top: '15%',
			height: '425px',
			'text-align': 'left',
			width:'60%',
			'overflow': 'auto'
		};
	}

	$.blockUI({
		message: msg,
		css:css
	})

	$('.blockUI').click(function(){
		$.unblockUI();
	});

}

}); /* End DOMReady */

/* Expresión regular para validar email */

function checkEmail(d){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(d);
}
