
/* Generic functions
--------------------------------------------------------------- */
	
	_ = function(p){
		if(window.console) {
			console.log(p);
		}
	}
	
	/* Pseudo target blank on class="blank" links
    ----------------------------------------------------------- */
	$.fn.addTargetBlank = function() {
	   $(this).click(function(){
			window.open(this.href);
			return false;
		});
	};
	
	
	/* Mixed content rollover
    ----------------------------------------------------------- */
	$.fn.prepareLinks = function() {
		$(this).live('click',function(){
			var href = $(this).find('a').attr("href");
			document.location.href = href;
			return false;
		});
		
		$(this).live('mouseover',function(){
			$(this).addClass('hover');
		});
		$(this).live('mouseout',function(){
			$(this).removeClass('hover');
		});
	};
	
	/* Inputs setup (empty onfocus, fill onblur
    ----------------------------------------------------------- */
	$.fn.prepareFields = function() {
		this.labelEl = $("label[for='"+$(this).attr('name')+"']");
		this.oldval;
		this.newVal = this.labelEl.html();
			
		if(this.newVal != null) $(this).val(this.newVal);
		this.labelEl.addClass('hide');
		
		$(this).focus(function(){
			this.oldval = $(this).val();
			$(this).val('');	
			
			$(this).blur(function(){
				if($(this).val() == ''){
					$(this).val(this.oldval);		
				}
			});	
		});
	};


	
	function isEmailAddr(email) {
		var result = false
		var theStr = new String(email)
		var index = theStr.indexOf("@");
		if (index > 0)
		{
			var pindex = theStr.indexOf(".",index);
			if ((pindex > index+1) && (theStr.length > pindex+1))
			result = true;
		}
		return result;
	}
	
	
    function isPosInteger (inputVal) {
        inputStr = inputVal.toString()
        for (var i=0; i<inputStr.length; i++) {
            var oneChar = inputStr.charAt(i); 
            if (oneChar < "0" || oneChar > "9") {
                return false;
            }
	   }
	   return true;
    }
    
    function NewWindow(mypage, myname, w, h, scroll) {
	   var winl = (screen.width - w) / 2;
	   var wint = (screen.height - h) / 2;
	   winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable,scrollbars'
	   win = window.open(mypage, myname, winprops)
	   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
    }



	
/* GLOBAL FUNCTION CALLS
--------------------------------------------------------------- */
	
	
	$(document).ready(function() {
	
		$('body').addClass('js-active'); // graceful degradation of some advanced functionalities
		$('a.blank').addTargetBlank(); 	 // calls $.fn.addTargetBlank
		$('#pageContent .rollover').prepareLinks(); // calls $.fn.addTargetBlank
		
		// calls $.fn.prepareFields for each field
		$('input.text, input.datepicker').each(function(){
			$(this).prepareFields();
		});
		
		
		$('#newsletter').submit(function(e){
			 var succes = true;
			 var msg = '';
			 var fullname = $('#fullname').val();
			 var courriel = $('#email').val();
			 if (fullname == 'Nom' || fullname == '') {
			 	succes = false;
			 	msg += "- Veuillez inscrire votre nom\n";
			 }
			 if (courriel == '' || courriel == 'Courriel') {
			 	succes = false;
			 	msg += "- Veuillez inscrire votre courriel\n";
			 } else {
			 	if (!isEmailAddr(courriel)) {
			 		succes = false;
			 		msg += "- Veuillez inscrire un courriel valide\n";
			 	}
			 	
			 }
			 if (succes) {
			 	Shadowbox.open({player:'iframe',content:$(this).attr('action'),width:600});
			 	e.preventDefault();
			 } else {
			 	alert(msg);
			 	return false;
			 }
		});
		
		
	});

