/*------------------------------------------------------------------------------
    JS Document (https://developer.mozilla.org/en/JavaScript)

    project:    STPo
    created:    2010-09-27
    author:     Christophe ANDRIEU
                chris@stpo.fr
                http://www.stpo.fr

                CopyLeft 2010
                Bisous aux pompeurs !

    summary:    CONSTANTES
                UTILITIES
                WINDOW.ONLOAD
                FOOTER SPECIAL THANX
                SPAM FILTER
                PORTFOLIO CARROUSEL
                FORM FOCUS / BLUR
                FORM CHECK / SUBMIT
----------------------------------------------------------------------------- */
/*  =CONSTANTES
----------------------------------------------------------------------------- */
jQuery.noConflict();
var d = document;
var w = window;
stpo = {};


/*  =UTILITIES
----------------------------------------------------------------------------- */
var log = function(x) {
    if (typeof console != 'undefined') {
        console.log(x);
    }
};


/*  =WINDOW.ONLOAD
----------------------------------------------------------------------------- */
jQuery(document).ready(function(){

    // Call Functions
    jQuery('.noJS').hide();								// hide stuff
    swfobject.registerObject("takeMyHead", "6.0.0"); 	// Flash header
    stpo.footer();                 						// footer special thanx
    stpo.antispam();                					// spam filter
    stpo.carrousel();									// portfolio carrousel
    stpo.formFocus();									// form behaviour
    stpo.formCheck();									// form submit

    if (jQuery.browser.msie && jQuery.browser.version == 6) {
        // IE 6 FUNCTIONS ONLY
        
    }

});


/*  =FOOTER SPECIAL THANX
----------------------------------------------------------------------------- */
stpo.footer = function(){
	
    jQuery('#specialThanxLink').toggle(function(){
	
		jQuery(jQuery(this).attr('href')).fadeIn('fast');
		jQuery(this).blur();
		return false;
		
	},function(){
		
		jQuery(jQuery(this).attr('href')).fadeOut('slow');
		jQuery(this).blur();
		return false;
		
	});
};


/*  =SPAM FILTER
----------------------------------------------------------------------------- */
stpo.antispam = function(){
	
	jQuery('.email').each(function(i){
	
		var myString = jQuery(this).html();
		var newString = myString.split('[AT]')[0] + '@' + myString.split('[AT]')[1].split('[DOT]')[0] + '.' + myString.split('[AT]')[1].split('[DOT]')[1];		
		jQuery(this).html('<a href="mailto:' + newString + '">' + newString +'</a>');
		
	});
	
};

/*  =PORTFOLIO CARROUSEL
	ATTENTION, le premier élément du Carousel doit impérativement être doté 
	de dimensions (via HTML ou CSS)
----------------------------------------------------------------------------- */
stpo.carrousel = function(){
	
	jQuery('.imgCarousel div').each(function(i){
		
		var myId = 'carousel_'+i;
		jQuery(this).attr('id',myId);
		
		var myUlLength = jQuery('#'+myId+' ul li').length;
		
		if (myUlLength > 1){
						
			var myLiWidth = jQuery('#'+myId+' ul li')[0].offsetWidth + parseInt(jQuery('#'+myId+' ul li:eq(1)').css('margin-left')) + parseInt(jQuery('#'+myId+' ul li:eq(1)').css('margin-right'));
			
			jQuery('#'+myId+' ul').css('width', (myLiWidth * myUlLength));
			
			var myLiHeight = jQuery('#'+myId+' ul li')[0].offsetHeight + parseInt(jQuery('#'+myId+' ul li:eq(1)').css('margin-top')) + parseInt(jQuery('#'+myId+' ul li:eq(1)').css('margin-bottom'));
			
			jQuery(this).append('<a href="#" class="prevLink" title="image précédente"></a><a href="#" class="nextLink" title="image suivante"></a>');
					
			jQuery('#'+myId+' .prevLink, #'+myId+' .nextLink').css('top', (myLiHeight / 2 - 46)+'px');
			
			var clickPermitted = true;
			
			jQuery('#'+myId+' .prevLink').addClass('linkToOff');
	
			jQuery(this).hover(
				function () {
					jQuery('#'+myId+' .prevLink, #'+myId+' .nextLink').fadeIn('fast');
				}, 
				function () {
					jQuery('#'+myId+' .prevLink, #'+myId+' .nextLink').fadeOut('fast');
				}
			);
			
			jQuery('#'+myId+' .prevLink, #'+myId+' .nextLink').click(function(){
				
				if(clickPermitted){
					
					var myLeft = parseInt(jQuery('#'+myId+' ul').css('left'));
					
					if (jQuery(this).attr('class') == 'prevLink') {
						
						if (myLeft != 0) {
							
							var myNewLeft = myLeft + myLiWidth;
							
							if (myNewLeft == 0) jQuery(this).addClass('linkToOff');
							jQuery('#'+myId+' .nextLink').removeClass('linkToOff');
							
						}
						
					}
					else if (jQuery(this).attr('class') == 'nextLink') {
						if (myLeft != -(myLiWidth * (myUlLength - 1))) {
							
							var myNewLeft = myLeft - myLiWidth;
							
							if (myNewLeft == -(myLiWidth * (myUlLength - 1))) jQuery(this).addClass('linkToOff');
							jQuery('#'+myId+' .prevLink').removeClass('linkToOff');
							
						}
					}
					
					if (myNewLeft != undefined){
						
						clickPermitted = false;
						
						jQuery('#'+myId+' ul').animate({ 
							left: myNewLeft+'px'
						}, 600, function(){ clickPermitted = true; });
					}
			
				}
				
				jQuery(this).blur();
				return false;
				
			});
			
		}
	});
};


/*  =FORM FOCUS / BLUR
----------------------------------------------------------------------------- */
stpo.formFocus = function(){
	
	jQuery('input[type=text], textarea').each(function(i){
		
		// remplissage des champs
		jQuery(this).val(jQuery(this).attr('title'));
		
		// au focus, on selectionne (sauf pour Safari ou on cleane le champ)
		jQuery(this).focus(function(){
			if (jQuery(this).val() == jQuery(this).attr('title')){
				 if (jQuery.browser.safari) jQuery(this).val('');
				 else jQuery(this).select();
			 }
		});
		
		// au change, si vide on remet la valeur originelle
		jQuery(this).blur(function(){
			if (!/[a-zA-Z0-9]/.test(jQuery(this).val())) jQuery(this).val(jQuery(this).attr('title'));
		});
	
	});
}


/*  =FORM CHECK / SUBMIT
----------------------------------------------------------------------------- */
stpo.formCheck = function(){
	
	jQuery('#contactForm, #commentform').submit(function(){
		
		var hasError = false;
		jQuery('.mandatory').parent('p').removeClass('error');
		jQuery('#responseArea').removeClass('errorMsg');
		jQuery('#responseArea').removeClass('validMsg');
		
		jQuery('.mandatory').each(function(i){
			
			var myVal = jQuery(this).val();
			
			if ((myVal == '')||(myVal == jQuery(this).attr('title'))){
				
				jQuery(this).parent('p').addClass('error');
				hasError = true;
				
			}
			else if(jQuery(this).hasClass('mandatoryEmail')){
				
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				
				if (!emailReg.test(myVal)){ 
					
					jQuery(this).parent('p').addClass('error'); 
					hasError = true;
					
				}
			}
		});
		
		if(hasError) {
			
			jQuery('#responseArea').addClass('errorMsg');
			jQuery('#responseArea').html(jQuery('#inputError').val());
			jQuery(this).find('input[type=submit]').blur();
			
			return false;
		}
		
		else{
			
			// Loader
			jQuery('#responseArea').html('<img src="' + jQuery('#inputLoad').val() + '" alt="" id="responseAreaLoader" />');
			
			// empty the fields with default value
			jQuery(this).find('input[type=text], textarea').each(function(){
				
				if (jQuery(this).val() == jQuery(this).attr('title')) jQuery(this).val('');
				
			});
			
			// AJAX send
			s = jQuery(this).serialize();
			jQuery.ajax({ 
				type: "POST", 
				data: s, 
				url: jQuery(this).attr("action"), 
				error: function(retour){
					jQuery('#responseArea').addClass('errorMsg');
					jQuery('#responseArea').html(jQuery('#emailNOK').val());
				},
				success: function(retour){
					jQuery('#responseArea').addClass('validMsg');
					jQuery('#responseArea').html(jQuery('#emailOK').val());
					jQuery('.submitButton').blur();
					jQuery('input[type=text], textarea').each(function(i){
						jQuery(this).val(jQuery(this).attr('title'));
					});
					jQuery('#commentsArea ol').load(window.location.href.toString().split('#comments')[0] + " #commentsArea ol", function(){ });
	
				} 
			});
			return false; 
		
		}
		
	});
}
