$(document).ready(function(){
	
	/* Rollover Configuration */
	/*
	if ($(".rollover_left").length > 0) {
		$(".rollover_left").hover(function(){
			$(".rollover_left .front").fadeOut();
		}, function(){
			$(".rollover_left .front").fadeIn();
		});
		$(".rollover_right").hover(function(){
			$(".rollover_right .front").fadeOut();
		}, function(){
			$(".rollover_right .front").fadeIn();
		});
	} else {
		$(".rollover").hover(function(){
			$(this).children(".front").fadeOut();
		}, function(){
			$(this).children(".front").fadeIn();
		});
	}
	*/
	if ($(".rollover_left").length > 0) {
		$(".rollover_left").hover(function(){
			$(this).children(".front").stop(true, true).fadeOut('slow');
		}, function(){
			$(this).children(".front").stop(true, true).fadeIn('slow');
		});
		$(".rollover_right").hover(function(){
			$(this).children(".front").stop(true, true).fadeOut('slow');
		}, function(){
			$(this).children(".front").stop(true, true).fadeIn('slow');
		});
	} else {
		$(".rollover").hover(function() {
			$(this).children(".front").stop(true, true).fadeOut('slow');
		}, function() {
			$(this).children(".front").stop(true, true).fadeIn('slow');
		});
	}

	/* Header JPlayer Configuration */
	
	$("#jquery_jplayer_1").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				mp3: "/files/header/tonyjazz.mp3"
			}); // .jPlayer("play");
			if ($('div#wrapper.home').length > 0)
				$(this).jPlayer("play");
		},
		ended: function (event) {
			$(this).jPlayer("play");
		},
		cssSelectorAncestor: "#jp_interface_1",
		solution: "flash, html", // Flash with an HTML5 fallback.
		swfPath: "/include/swf",
		supplied: "mp3"
	});
	
	/* Footer Carousel */
	
	$('#footer .infiniteCarousel').infiniteCarousel(3, 3000, 8000);
	
	/* Navigation Rollover */
	
	$('#navigation ul li a', this)
		.css( {'color': '#838383', backgroundPosition: "0 -75px"} )
		.mouseover(function(){
			$(this).css( {'color': '#ffffff'} );
			$(this).stop().animate({backgroundPosition:"(0 0px)"}, 150, function() { Cufon.replace('#navigation li a'); });
			
		})
		.mouseout(function(){
			$(this).css( {'color': '#838383'} );
			$(this).stop().animate({backgroundPosition:"(0 -75px)"}, 500, function() { Cufon.replace('#navigation li a'); });
		});
	$('#navigation ul li.current a', this)
		.css( {'color': '#ffffff', backgroundPosition: "0 -75px"} )
		.mouseover(function(){
			$(this).css( {'color': '#838383'} );
			$(this).stop().animate({backgroundPosition:"(0 0px)"}, 150, function() { Cufon.replace('#navigation li a'); });
			
		})
		.mouseout(function(){
			$(this).css( {'color': '#ffffff'} );
			$(this).stop().animate({backgroundPosition:"(0 -75px)"}, 500, function() { Cufon.replace('#navigation li a'); });
		});
		
	DD_roundies.addRule('#navigation a', '0 0 5px 5px', true);	
	
	var overlay = $(".overlay").overlay({
		// some mask tweaks suitable for modal dialogs
		mask: {
			color: '#aaa',
			loadSpeed: 200,
			opacity: 0.6
		},
		api:true,
		closeOnClick: true
	});
	
   	Cufon.replace('h40');
   	Cufon.replace('.futura'); 	
   	Cufon.replace('#navigation li a');
   	Cufon.replace('#marketingcumulus .tagCloud .tagClass');
});

function getClassName(obj) {
	// get classname abstracted from
	// constructor property
	var c = obj.constructor.toString();
	var start = c.indexOf('function ') + 9;
	var stop = c.indexOf('(');
	c = c.substring(start, stop);
	return c;
}

//pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0];
    
    if (
    		$(jqForm[0]).parent().parent().attr('id') == 'renseignement' || 
    		$(jqForm[0]).parent().parent().attr('id') == 'candidature'
    ) {
    	if ($(jqForm[0]).children("input[name=nom]").attr('value') == '') {
    		alert('Vous devez renseigner votre nom.');
    		return false;
    	}
    	if ($(jqForm[0]).children("input[name=email]").attr('value') == '') {
    		alert('Vous devez renseigner votre email.');
    		return false;
    	}
    	if ($(jqForm[0]).children("input[name=telephone]").attr('value') == '') {
    		alert('Vous devez renseigner votre numéro de téléphone.');
    		return false;
    	}
    	if ($(jqForm[0]).children("textarea[name=message]").attr('value') == '') {
    		alert('Vous devez renseigner votre message.');
    		return false;
    	}
    }
    
    if ($(jqForm[0]).parent().parent().attr('id') == 'devis') {
    	if ($(jqForm[0]).children("input[name=societe]").attr('value') == '') {
    		alert('Vous devez renseigner votre societe.');
    		return false;
    	}
    	if ($(jqForm[0]).children("input[name=email]").attr('value') == '') {
    		alert('Vous devez renseigner votre email.');
    		return false;
    	}
    	if ($(jqForm[0]).children("input[name=telephone]").attr('value') == '') {
    		alert('Vous devez renseigner votre numéro de téléphone.');
    		return false;
    	}
    	if ($(jqForm[0]).children("textarea[name=message]").attr('value') == '') {
    		alert('Vous devez renseigner votre message.');
    		return false;
    	}
    }
    
   	// alert('About to submit: \n\n' + queryString);
    
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.'); 
} 

function base64_decode (data) {
    // Decodes string using MIME base64 algorithm  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/base64_decode
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Thunder.m
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Pellentesque Malesuada
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_decode
    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
    // *     returns 1: 'Kevin van Zonneveld'
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof this.window['btoa'] == 'function') {
    //    return btoa(data);
    //}
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
        ac = 0,
        dec = "",
        tmp_arr = [];
 
    if (!data) {
        return data;
    }
 
    data += '';
 
    do { // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
 
        bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
 
        o1 = bits >> 16 & 0xff;
        o2 = bits >> 8 & 0xff;
        o3 = bits & 0xff;
 
        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);
 
    dec = tmp_arr.join('');
    dec = this.utf8_decode(dec);
 
    return dec;
}
