//JQUERY
$(document).ready(function() {
    
    var langue = $.cookie('abakLanguage');
    
    
    // --------- COLORBOX ------------------------------------------------------------------------ //
    
    $("a[rel='colorbox']").colorbox();
    
    // ------------------------------------------------------------------------------------------- //
    
    
    // --------- TWITTER - Get the latest tweet -------------------------------------------------- //
    
    $('#the_tweet').load('/twitter-'+langue);
    
    // ------------------------------------------------------------------------------------------- //
    
    // ---------- LIVE CHAT ---------------------------------------------------------------------- //
    $('.livechat').click( function(){
    	var url = "http://messenger.providesupport.com/messenger/abak.html?ps_s=HJbVoZgZyOpl";
		window.open(url, "_blank", "menubar=0,location=0,scrollbars=auto,resizable=1,status=0,width=500,height=460");
		return false;
    });
    // ------------------------------------------------------------------------------------------- //
    
    // ---------- LEMFLOW ------------------------------------------------------------------------ //
    $('#who_is_using').lemFlow({
		width:469,
		numVisible:1,
		autoplay:true,
		delay:10000,
		loop:true
	});
    // ------------------------------------------------------------------------------------------- //
    
    // ----------- SUBSCRIPTION ------------------------------------------------------------------------- //
    
    function initFormSubscription() {
    	//First name
    	$('#SubscriberAddForm #SubscriberFirstname').focus(function(){
    		if($('#SubscriberAddForm #SubscriberFirstname').val() == 'First name' || $('#SubscriberAddForm #SubscriberFirstname').val() == 'Pr\351nom'){
    			$('#SubscriberAddForm #SubscriberFirstname').val('');
    		}
    	});
    	$('#SubscriberAddForm #SubscriberFirstname').blur(function(){
    		if($('#SubscriberAddForm #SubscriberFirstname').val() == ''){
    			if(langue == 'en'){
    				$('#SubscriberAddForm #SubscriberFirstname').val('First name');
    			} else {
    				$('#SubscriberAddForm #SubscriberFirstname').val('Pr\351nom');
    			}
    		}
    	});
    	
    	//Last name
    	$('#SubscriberAddForm #SubscriberLastname').focus(function(){
    		if($('#SubscriberAddForm #SubscriberLastname').val() == 'Last name' || $('#SubscriberAddForm #SubscriberLastname').val() == 'Nom'){
    			$('#SubscriberAddForm #SubscriberLastname').val('');
    		}
    	});
    	$('#SubscriberAddForm #SubscriberLastname').blur(function(){
    		if($('#SubscriberAddForm #SubscriberLastname').val() == ''){
    			if(langue == 'en'){
    				$('#SubscriberAddForm #SubscriberLastname').val('Last name');
    			} else {
    				$('#SubscriberAddForm #SubscriberLastname').val('Nom');
    			}
    		}
    	});
    	
    	//Email address
    	$('#SubscriberAddForm #SubscriberEmail').focus(function(){
    		if($('#SubscriberAddForm #SubscriberEmail').val() == 'Email address' || $('#SubscriberAddForm #SubscriberEmail').val() == 'Courriel'){
    			$('#SubscriberAddForm #SubscriberEmail').val('');
    		}
    	});
    	$('#SubscriberAddForm #SubscriberEmail').blur(function(){
    		if($('#SubscriberAddForm #SubscriberEmail').val() == ''){
    			if(langue == 'en'){
    				$('#SubscriberAddForm #SubscriberEmail').val('Email address');
    			} else {
    				$('#SubscriberAddForm #SubscriberEmail').val('Courriel');
    			}
    		}
    	});
    }
    
    function validateFormSubscription(){
    	var message = '';
    	var title = '';
    	
    	if(langue == 'en'){
    		title = 'Incomplete information';
    	} else {
    		title = 'Information incompl\350te';
    	}
    	
    	if($('#SubscriberFirstname').val() == '' || $('#SubscriberFirstname').val() == 'First name' || $('#SubscriberFirstname').val() == 'Pr\351nom'){
    		if(langue == 'en'){
    			message += 'Please enter your <strong>first name</strong> \n';
    		} else {
    			message += 'Veuillez entrer votre <strong>pr\351nom</strong> \n';
    		}
    	}
    	if($('#SubscriberLastname').val() == '' || $('#SubscriberLastname').val() == 'Last name' || $('#SubscriberLastname').val() == 'Nom'){
    		if(langue == 'en'){
    			message += 'Please enter your <strong>last name</strong> \n';
    		} else {
    			message += 'Veuillez entrer votre <strong>nom</strong> \n';
    		}
    	}
    	if($('#SubscriberEmail').val() == '' || $('#SubscriberEmail').val() == 'Email address' || $('#SubscriberEmail').val() == 'Courriel'){
    		if(langue == 'en'){
    			message += 'Please enter your <strong>email</strong> \n';
    		} else {
    			message += 'Veuillez entrer votre <strong>adresse courriel</strong> \n';
    		}
    	}
    	
    	if(message != ''){
    		$('#SubscriberAddForm .submit').removeClass('displaynone');
    		$('#SubscriberAddForm .unsubscribe').removeClass('displaynone');
    		jAlert(message, title);
    		return false;
    	} else {
    		return true;
    	}
    	
    }
    
    function processFormSubscription(data){
    	var reponse = '';
    	var title = ''
    	if(data['result'] == 'success'){
    		if(langue == 'en'){
    			title = 'Thank you!';
    			reponse = 'You are now subscribed to the Abak Newsletter';
    		} else {
    			title = 'Merci !';
    			reponse = 'Vous \352tes maintenant inscrit \340 l\'infolettre d\'Abak';
    		}
    		$('#SubscriberAddForm #SubscriberFirstname').val('').trigger('blur');
    		$('#SubscriberAddForm #SubscriberLastname').val('').trigger('blur');
    		$('#SubscriberAddForm #SubscriberEmail').val('').trigger('blur');
    	} else {
    		if(langue == 'en'){
    			title = 'Error!';
    			reponse = '<p>A problem occured. Please try again later.</p>';
    		} else {
    			title = 'Erreur !';
    			reponse = '<p>Un probl\350me est survenu. Veuillez r\351essayer plus tard.</p>';
    		}
    	}
    	jAlert(reponse,title);
    }
    
    if($('#SubscriberAddForm').length > 0) {
    	//Init
    	initFormSubscription();
    	
    	//Submit
    	$('#SubscriberAddForm .submit').click(function() {
    		if(validateFormSubscription()) {    		
	    		$('#SubscriberAddForm').ajaxSubmit({
					url: 'http://abaksoftware.us1.list-manage.com/subscribe/post-json?u=b296a993bd35ecc7a73572b7f&id=73e99004ce&c=?',
					type: 'GET',
					dataType: 'json',
					contentType: "application/json; charset=utf-8",
					success: function(data) {
						processFormSubscription(data);
					}
				});
			}
			
			return false;
    	});
    }
    // --------------------------------------------------------------------------------------------------------------- //
    
    
    // ----------- MENU DE GAUCHE ------------------------------------------------------------------- //
    
    if($('#menu_left_ul').length > 0){
    	var nbreElements = $('#menu_left_ul li').length;
    	for(var i = 0; i < nbreElements; i++){
    		if($('#menu_left_ul li a').eq(i).attr('href').substring($('#menu_left_ul li a').eq(i).attr('href').lastIndexOf('/')+1) == Croogo.params.named.slug){
    			$('#menu_left_ul li a').eq(i).attr('class', 'selected');
    			$('#menu_left_ul li a').eq(i-1).attr('class', 'aboveSelected');
    		}
    	}
    }
    
    // --------------------------------------------------------------------------------------------------------------- //
    
    
    // ----------- HOME - FEATURES BAR ------------------------------------------------------------ //
    
    if($('#features_bar').length > 0){
    	$('.feature').eq(0).addClass('first');
    	$('.feature').eq($('.feature').length-1).addClass('last');
    }
    
    // --------------------------------------------------------------------------------------------------------------- //
    
    
    
	
	// ----------- VALIDATION DE FORMULAIRES -------------------------------------------------- //
	
	if(langue == 'en'){
    	tRequired = 'This field is required.';
    	tEmail = 'Please enter a valid email address.';
    	tNumber = 'Please enter a valid number.';
    	tUrl = 'Please enter a valid URL.';
    } else {
    	tRequired = 'Ce champ est requis';
    	tEmail = 'Entrez une adresse courriel valide';
    	tNumber = 'Entrez un nombre valide';
    	tUrl = 'Entrez une url valide';
    }

	jQuery.extend(jQuery.validator.messages, {
	    required: tRequired,
	    email: tEmail,
	    url: tUrl,
	    number: tNumber,
	});

    
    //Formulaire de Resellers
    $("#PartnerAddForm").validate();
    $("#ResellerAddForm").validate();
    $("#TestimonialAddForm").validate();
    $("#TrialSendEmailForm").validate();
    $("#ContactRequestSendEmailForm").validate();
    $("#purchasethis").validate();
    
    // ------------------------------------------------------------------------------------------------------------------ //
    
    
    
    // ------------- GOOGLE MAPS ---------------------------------------------------------------------------------------- //
    
    if($('.gmap').length > 0){
		if (GBrowserIsCompatible()) {
			var map = $('.gmap').get(0);
			
			var m = new GMap2(map);
			m.setCenter(new GLatLng(46.80119,-71.328135), 11);
			m.addControl(new GSmallMapControl());
			var c = new GMapTypeControl();
			m.addControl(c);
	
			var tinyIcon = new GIcon();
			tinyIcon.image = "/img/common/mrk-google-maps.png";
			tinyIcon.iconSize = new GSize(50, 49);
			tinyIcon.iconAnchor = new GPoint(18, 43);
			tinyIcon.infoWindowAnchor = new GPoint(13, 0);
			markerOptions = { icon:tinyIcon };
			
			var position = new GLatLng(46.80119,-71.328135);
			var marker = new GMarker(position,markerOptions);
	
			m.addOverlay(marker);
		} else {
			$('.gmap').html('<p style="color: #ff0000;">Your browser is not compatible with Google Maps</p>');
		}	
	}
    
    // ------------------------------------------------------------------------------------------------------------------ //
    
});
