// JavaScript Document

$(function(){
		   
	 // Automatic navigation locator
   //var path = location.pathname.substring(0);
	 var path = document.location.pathname;
	 var dir = path.substring(path.indexOf('/', 0), path.indexOf('/',4)+1);
	 var lang = path.substring(path.indexOf('/', 0)+1, path.indexOf('/',2));
	 
	 if ( dir )
		 $('#nav a[href$="' + dir + '"]').parent().attr('class', 'here');
		 //alert(lang);return false;
				
	$('#marsuBvContact').click(function() {
	  	$('#marsuBvFoot').css('color','#fff')
	});		
	$('#marsuProdContact').click(function() {
	  	$('#marsuProdFoot').css('color','#fff')
	});		
	
	
	// External link icon

	//$('a.extlinks').filter(function() {	
	//	return this.hostname && this.hostname !== location.hostname;	
	// }).after(' <img src="/lib/img/external.png" alt="external link"/>');
	
	// Cufon font replacement
	//Cufon.replace('#nav ul li', {hover: true});	
	Cufon.replace('#footer h2');	
	Cufon.replace('#marsuProConnect h3');	
	Cufon.replace('#box strong');
	Cufon.replace('h1');	
		
	// Add empty DIV's 
	$("#socialBox").append("<div id='socialTip'></div><div id='speechBox'><p><span></span></p></div><div id='bird'></div>");
		
	// Clear textboxes on focus
    $('.autoclear').autoClear();
	
		
	// Display last twitter message
	$("#speechBox p span").lastTwitterMessage('MarsuProd',lang);
	$("#twitter span").lastTwitterMessage('MarsuProd',lang);
	
	//Hide email
	 //<span class="nospam" title="Send me a letter!">me at mydomain dot com</span>
	$('span.nospam').nospam();
	
	//if (lang!='fr') {
//	$(".newsbox h4, .newsbox .translate").sundayMorning({
//    source:'fr',
//    destination:lang 
//	});
//	}
	

});



// Clears the default text when an input receives
// focus and reinstates it if it is left blank
(function($) {

    $.fn.autoClear = function() {	
        return this.each(function() {
            $(this).focus(function() {
                if( this.value == this.defaultValue ) {
                    this.value = "";
                }
            })
            .blur(function() {
                if( !this.value.length ) {
                    this.value = this.defaultValue;
                }
            });
        });
    };
	
	
	//==========================================================>>>>>>>>>>>>>>>>>>>>>
	/* Usage:
	 * Hide email
	 * 
	 */	
		
	jQuery.fn.nospam = function() {
		var at = / at /;
		var dot = / dot /g;
		this.each( function() {
			var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
			var title = jQuery(this).attr('title')
			$(this)
				.after('<a href="mailto:'+addr+'" title="'+title+'">'+ addr +'</a>')
				.remove();
		});
	};	
		
	//==========================================================>>>>>>>>>>>>>>>>>>>>>
	
	$.fn.lastTwitterMessage = function(username, translateTo) {
				
		var $base = this;
		if(!username || username == "") return this; // username required
		var url = "http://twitter.com/statuses/user_timeline.json?callback=?";
		
		// Request Twitter API
		$.getJSON( url, { count: 6, screen_name: username }, 
			function(data){
			
				if(data && data.length >= 1){
					try {
						var item = null;
						for(var i = 0; i < data.length; i++){
							if(/^@/i.test(data[i].text)) continue;
							item = data[i]; break;
						}
						if(!item) return;
						
												
						var $tweet = $("<p class='translate'></p> ").text(item.text);
						$tweet.html(
							$tweet.html()
								.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>')
								.replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>')
								.replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>')
							)
						
						//$tweet.append(" &nbsp;<a href='http://twitter.com/chriscoyier'>(&#8734;)</a>&nbsp; ").wrapInner("<span>");
						
						//translate on the fly with SundayMorning jQuery plugin
						if (translateTo!='fr') {
						$tweet.sundayMorning({
						source:'fr',
						destination:translateTo 
					});
						}
						
						$base.empty().append($tweet).show();
					
					} catch (e) {
						// Just in case
					};
				};
			});
			
		return this; // Don't break the chain
	};
	
	
	
	

})(jQuery);



// Google analytics
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-10559408-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  
//==========================================================>>>>>>>>>>>>>>>>>>>>> 
 



