// Mètode "in_array" de JavaScript
Array.prototype.in_array = function(val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == val) {
			return true;
		}
	}
	return false;
}

if(typeof String.prototype.trim !== "function") {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/, ""); 
	}
}

function debug(text, type)
{
	if(window.console && window.console.firebug)
	{
		if(typeof type == "undefined") type = 'log';
		eval("console."+type+"(text)");
	}
}

/**
 * Mètode per a convertir els enllaços de les converses de Twitter
 * (23)
 */
if ( typeof String.prototype.twitterize !== "function" ) {
	String.prototype.twitterize = function() {
		var links = /(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp):\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/ig;
		var arrob = /@([a-z\-_0-9]+)/ig;
		var coixi = /#([a-z\-_0-9]+)/ig;
		return this.replace(links, function(){
			return '<a href="'+arguments[0]+'">'+arguments[0]+'</a>';
		}).replace(arrob, function(){
			return '@<a href="http://www.twitter.com/' + arguments[1] + '">'+arguments[1]+'</a>';
		}).replace(coixi, function(){
			return '<a href="http://www.twitter.com/#search?q=%23' + arguments[1] + '">'+arguments[0]+'</a>';
		});
	}
}

///// FOCUS /////

jQuery(document).ready(function () {

	jQuery('li.nosaltres').hover(
		function(){
			jQuery('#submenu .menu-wrapper').show();
		},
		function(){
			jQuery('#submenu .menu-wrapper').hide();;
		}
	);
	
	jQuery('#submenu .menu-wrapper').hover(
		function() {
			if(!jQuery('li.nosaltres').hasClass('hover2')) {
				jQuery('li.nosaltres').addClass('hover2');
			}
		},
		function() {
			if(jQuery('li.nosaltres').hasClass('hover2')) {
				jQuery('li.nosaltres').removeClass('hover2');
			}
		}
	);

	jQuery('#desplegable .nacional').hover(
		function(){
			jQuery('#desplegable #fix-territoris').show();
		},
		function(){
			jQuery('#desplegable #fix-territoris').hide();
		}
	);

	jQuery('#desplegable #fix-territoris').hover(
		function() {
			if(!jQuery('#a_jsc').hasClass('hover1')) {
				jQuery('#a_jsc').addClass('hover1');
			}
		},
		function() {
			if(jQuery('#a_jsc').hasClass('hover1')) {
				jQuery('#a_jsc').removeClass('hover1');
			}
		}
	);

	$("input:text").focus(function() {
		if(jQuery(this).attr('id')=='LoginUserPassword'){
			jQuery(this).replaceWith('<input type="password" id="LoginUserPassword" name="data[User][password]" value="" />');
			jQuery('#LoginUserPassword').focus();
		}

		if(jQuery(this).val() == $(this).attr("title")){
			jQuery(this).val("");
		}
	});
	
	$("input:text").blur(function() {
		var element = $(this);
		if (element.val() == '') {
			element.val(element.attr("title"));
		}
	});
	

	jQuery('#inicia_sessio').toggle(
		function(){
			jQuery('div.sessio').css('display', 'block');
			jQuery(this).addClass("hover");
		},
		function(){
			jQuery('div.sessio').css('display', 'none');
			jQuery(this).removeClass("hover");
		}
	);
	
	jQuery('li.nacional').hover(
		function(){
			jQuery(this).addClass('hover');
		},
		function(){
			jQuery(this).removeClass('hover');
		}
	);

});

