/*
 scripts
 macharron@inpix.ca
 Last edit: 2010-05-25
 */

/**
 * open links with rel='external' in new tabs
 * @author http://www.456bereastreet.com/archive/200610/opening_new_windows_with_javascript_version_12/
 */
var JSTarget={init:function(att,val,warning){if(document.getElementById&&document.createElement&&document.appendChild){var strAtt=((typeof att==='undefined')||(att===null))?'class':att;var strVal=((typeof val==='undefined')||(val===null))?'non-html':val;var strWarning=((typeof warning==='undefined')||(warning===null))?' (opens in a new window)':warning;var oWarning;var arrLinks=document.getElementsByTagName('a');var oLink;var oRegExp=new RegExp("(^|\\s)"+strVal+"(\\s|$)");for(var i=0;i<arrLinks.length;i++){oLink=arrLinks[i];if((strAtt=='class')&&(oRegExp.test(oLink.className))||(oRegExp.test(oLink.getAttribute(strAtt)))){oWarning=document.createElement("em");oWarning.appendChild(document.createTextNode(strWarning));oLink.appendChild(oWarning);oLink.onclick=JSTarget.openWin;}oWarning=null;}}},openWin:function(e){var event=(!e)?window.event:e;if(event.shiftKey||event.altKey||event.ctrlKey||event.metaKey){return true;}else{var oWin=window.open(this.getAttribute('href'),'_blank');if(oWin){if(oWin.focus){oWin.focus();}return false;}oWin=null;return true;}},addEvent:function(obj,type,fn){if(obj.addEventListener){obj.addEventListener(type,fn,false);}else if(obj.attachEvent){obj["e"+type+fn]=fn;obj[type+fn]=function(){obj["e"+type+fn](window.event);};obj.attachEvent("on"+type,obj[type+fn]);}}};JSTarget.addEvent(window,'load',function(){JSTarget.init("rel","external","");});

/**************************************

 **************************************/

/* ########################################################### */
/* !vars */

var isIe = false;
var isOp = false;
var isSaf = false;

/**
 * if browser is IE
 */
if ($.browser.msie) {
    isIe = true;
}
/**
 * if browser is Opera
 */
if ($.browser.opera) {
    isOp = true;
}
/**
 * if browser is Safari
 */
if ($.browser.safari) {
    isSaf = true;
}


/** *******************************************************
 * put elements at the same height
 * @param group: jquery selector of the elements to equalize
 */
(function($){
	$.fn.equalHeight = function() {
		var group = $(this)
		
        tallest = 0;
        group.each(function() {
            thisHeight = jQuery(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
        
        return this;
    }
})(jQuery);


/** *******************************************************
 * remove and put back default values in field
 */
(function($){
	$.fn.inputLabel = function(){
		targ = $(this);	
		targ.each(function(){
			var targ = $(this);
			var val1 = '';
			$(this).focus(function(){
				if(val1 === ''){val1 = $(this).val();}
				$.fn.inputLabel.clear(targ, val1);
			})
			.blur(function(){
				$.fn.inputLabel.blur(targ, val1);
			})
			.addClass("label-in");
		});
		
		/**
		 * remove the field's default value
		 * @param	jQuery $targ		jquery selector of the field
		 * @param	string $startValue	default input text
		 */
		$.fn.inputLabel.clear = function(targ, startValue){
			if(targ.val() !== startValue){ // if value different from start value
				targ.val();
			}else{
				targ.val("").removeClass("label-in");
			}
		}
		/**
		 * put the default value back
		 * @param	jQuery $targ		jquery selector of the field
		 * @param	string $startValue	default input text
		 */
		$.fn.inputLabel.blur = function(targ, startValue){
			if(targ.val() !== ""){ // if field not empty
				targ.val();
			}else{
				targ.val(startValue).addClass("label-in");
			}
		}
		
		return this;
	}
})(jQuery);

/** *******************************************************
 * control projects
 */
var projects = {
    init: function() {
        $('#portfolio ul li:gt(3)').hide();
        $('#pf-more a').click(function(){
        	var val = 500;
        	$('#portfolio ul li:gt(3)').each(function(){
        		$(this).slideDown(val);
        		val = val + 250;
        	});
        	$(this).parent().fadeOut();
        	return false;
        });
    }
};


/** *******************************************************
 * validate the contact form and send data
 */
var contact = { 
	init: function(){
		
		$("#frm-contact").validate({
			rules: {
				"i-contact-name": {
					required: true
				},
				"email": {
					required: true,
					email: true
				},
				"i-contact-text": {
					//required: true
				},
				"i-contact-file": {
					//accept: "pdf|docx|doc|pages|txt"
				}
			},
			messages: {
				"i-contact-name": {
					required: "Entrez votre nom"
				},
				"email": {
					required: "Entrez votre courriel",
					email: "Entrez un courriel valide"
				},
				"i-contact-text": {
					//required: "Entrez votre message"
				},
				"i-contact-file": {
					//accept: "Nous acceptons les formats suivants: pdf, docx, doc, pages, txt"
				}
			},
			submitHandler: function(form) {
				$("#contact .loader").show();
				$("#contact footer, #contact fieldset").hide();
					
				var alldata = $(form).serialize();
				$.ajax({
					type: 'POST',
					url: "../HiQFM/HiQFM.php",
					data: alldata,
					success: function() {
					
						_gaq.push(['_trackEvent', 'Contact', 'Submit', 'Success']);
						
						$("#contact .loader").hide();
							$("#contact fieldset").show().html('<div class="text">'+
								'<p>Votre courriel a été envoyé.</p><p>Merci.</p>'+
							'</div>');
							$("#contact footer").hide();
					},
					complete: function() {
					    ajax.init();
					},
					error: function() {
						$("#contact .loader").hide();
						
						_gaq.push(['_trackEvent', 'Contact', 'Submit', 'Error']);
						
							$("#contact fieldset").show().html('<div class="text">'+
								'<p>Une erreur c\'est produite.</p><p>Veuillez rafraichir la page et recommencer.</p>'+
							'</div>');
					}
				});
			}
		});
		return false;
	}
};


/** *******************************************************
 * animate scoll
 */
var scroller = {
	init: function(){
		$("#tel-num").find("a").click(function(event){
			
			var full_url = this.href;
			
			var parts = full_url.split("#");
			var trgt = parts[1];
			
			var target_offset = $("#"+trgt).offset();
			var target_top = target_offset.top;
			
			$('html, body').animate({scrollTop:target_top}, 500);
			
			return false;
		});
	}
};


/** *******************************************************
 * fonction to record outbound links
 */
function recordOutboundLink(link, category, action, label) {
	_gat._getTrackerByName()._trackEvent(category, action, label);
	
	_link = link.attr('href');
	setTimeout('document.location = "' + _link+ '"', 100);
}


/** *******************************************************
 * fonction to restart after ajax reload
 */
var ajax = {
    init: function() {
        JSTarget.init("rel", "external", "");
    }
};


/** *******************************************************
 * functions to run when document is ready
 */
jQuery(document).ready(function() {
	$("#i-contact-text").inputLabel();
	contact.init();
	scroller.init();
	projects.init();
});


/** *******************************************************
 * google map loading
 */
jQuery(window).load(function() {
	(function(){
		var latlng = new google.maps.LatLng(45.5637115, -73.7449029);
		var options = {
			zoom: 16,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.TERRAIN,
			mapTypeControlOptions: {
				style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
			},
			navigationControlOptions: {
				style: google.maps.NavigationControlStyle.SMALL
			},
			scrollwheel: false
		}; 
		
		var map = new google.maps.Map(document.getElementById('gmap'), options);
		
		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(45.56345549010074, -73.74528229236603), 
			map: map,
			title: 'Un Zeste de Génie!',
			clickable: false,
			icon: 'img/marker.png'
		});
		
		var infowindow = new google.maps.InfoWindow({
			content: '<h3>Un Zeste de Génie!</h3>'
		});
		
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map, marker);
		});

	})();
});


