Cufon.replace('h1', {hover: true});
Cufon.replace('h2', {hover: true});

$(document).ready(function()  {
	
	$(function() {
		$('.external').attr('target','_blank');
	});

	$('a').focus(function() {
		$(this).blur();
	});
	
    $('.tabs a').click(function(){
		$('.tabs a').removeClass('current');										
		$(this).addClass('current');										
    });	
	
	/*main menu*/
	var apartat = $('body').attr('id');
	var seccio = $('body').attr('class');
	$('#menu .' + apartat).addClass('active');
	$('.submenu').hide();
	$('.submenu.' + apartat).show();
	$('.submenu .' + seccio).addClass('active');
	
	$('#menu li a').hover(function(){	
		if (!$(this).hasClass('active')) {
			$('.submenu').hide();
		}
		
		var submenuShow = '.' + $(this).attr('class');
		$(submenuShow).show();
		
		$('#menu *  a').removeClass('active');
		$(this).addClass('active');		
	});	

	$('.nav').mouseleave(function(){
		if($('.submenu .' + seccio).is(':hidden') == true) {			
			$('.submenu').fadeOut(75);
			$('#menu * a').removeClass('active');
			$('.submenu.' + apartat).delay(250).fadeIn(75);
			$('#menu .' + apartat).addClass('active');
			$('.submenu .' + seccio).addClass('active');
		}
	});

	//Queness.com Modal jquery window
	
	//lightbox on page load
	function launchWindow(id) {	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height());
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
	}
	
	/*
	if ($('body').attr('id')=='home') {
		launchWindow('#promo'); 
	}
	*/
	
	
	//select all the a tag with name equal to modal
	$('a[rel=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(750);	
		$('#mask').fadeTo(500, 0.30);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(1250); 
		
		//return false;
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});
	
	//end modal

	//right join form
	if($('body').attr('class')=='home frmJoin'){
		_gaq.push(['_trackEvent', 'frmApuntate', 'visitar-lateral']);
	}
	
	if( $('body').attr('class')=='join' && $('body').attr('class')!='ok' ){
		_gaq.push(['_trackEvent', 'frmApuntate', 'visitar-pagina']);
	}

	$('#right input').focus(function () {		
		var $obj = $(this).attr('id');
		$('#right #mensaje_'+$obj).css('visibility','hidden');
	});
	
	$('#right .message').click(function () {		
		$(this).css('visibility','hidden');
	});
	
	$('#right #frmApuntate button').click(function () {		
		_gaq.push(['_trackEvent', 'frmApuntate', 'enviar-lateral']);
	});
	
	
	jQuery.exists = function(selector) {return ($(selector).length > 0);}
	
	if ($.exists('.bannerApuntat')) { 
			_gaq.push(['_trackEvent', 'bannerApuntate', 'impressio']);
	}
	if ($.exists('#frmApuntate')) { 
			_gaq.push(['_trackEvent', 'frmApuntate', 'visitar-lateral-no-home']);
	}


	//chat banner hack	
	$('.lpPoweredBy').hide();
	$('.lpEmtStarRating').hide();

}); 
//end jQuery


/* http://jonraasch.com/blog/a-simple-jquery-slideshow */
function slideSwitch(loop) {
	var $active = $('#slideshow li.active');

	if ($active.length==0) $active = $('#slideshow li:last');

	if (loop=0) {
		var $next =  $active.next()	;
	}else{
		var $next =  $active.next()	.length ? $active.next() : $('#slideshow li:first');
	}

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 750, function() {
			$active.removeClass('active last-active');
		});
}

// form validation

function ValidarEMail(strMail) {
	return (strMail.search(/^\w[\w-\+\.]*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}


function formSubmit() {
   var form_name = formSubmit.arguments[0];
   var tot_ok = true;
   for (var i = 1;i<formSubmit.arguments.length;i++) {
	  var temp = formSubmit.arguments[i].split("|");
	  var tipo = temp[2];
	  var nom = temp[0];
	  var descr = temp[1];
	  var nom_alternatiu = "";
	  if (nom.indexOf("$") >0) {
		 temp = nom.split("$");
		 nom = temp[0];
		 nom_alternatiu = temp[1];
	  }
	  switch (tipo) {
		 case "int":
			ok = eval("document.getElementById('"+nom+"').value!=''");
			if (ok) ok = eval("!isNaN(parseInt(document.getElementById('"+nom+"').value))");
		 break;

		 case "text":
			ok = eval("document.getElementById('"+nom+"').value!=''");
			if (!ok && nom_alternatiu.length>0){
			   ok = eval("ok || (document.getElementById('"+nom_alternatiu+"').value!='')");
			}			
		 break;

		 case "telf":
			ok = eval("document.getElementById('"+nom+"').value!='' "+" && "+"!isNaN(parseInt(document.getElementById('"+nom+"').value))"+" && "+"document.getElementById('"+nom+"').value.length>8");
		 break;

		 case "textarea": ok = eval("document.getElementById('"+nom+"').value!=''"+" && "+"document.getElementById('"+nom+"').value!='<br>'");
		 break;
		 
		 case "radio":
			ok=false;
			for(var j=0;!ok && j<eval("document.getElementById('"+nom+"').length");j++){
				ok = eval("document.getElementById('"+nom+"')["+j+"].checked!=false");
			}
		 break;
		 
		 case "checkbox":
			ok = eval("document.getElementById('"+nom+"').checked!=false");
		 break;
		 
		 case "email":
			var valor = eval("document.getElementById('"+nom+"').value");
			ok = ValidarEMail(valor);
		 break;
		 
		 default:
	  }

	  obj = MM_findObj("mensaje_"+nom);
	  if (!ok) {
		 tot_ok = false;
		 obj.style.visibility = 'visible'
	  } else {
		 obj.style.visibility = 'hidden'    
	  }
   }
   
   if (tot_ok) {
		if (document.getElementById('form1_enter')) {
			document.getElementById('form1_enter').disabled = true;
			document.getElementById('form1_enter').className = 'disabled';
			}
		eval("document.getElementById('"+form_name+"').submit()");   
	}
	
   return false;
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
				if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
	obj.visibility=v; }
}

