/*

	[ site.js ]

*/

var Site = {

	start: function(){


		/* Detect if IE6 or older to launch PNG transparency support */
		oldBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");

		if (oldBrowser) jQuery('div.fade','div#slideshow').ifixpng();

		if ( jQuery('#slideshow').length ) Site.home();
		if ( jQuery('form').length ) Site.formValidation();
		if ( jQuery('#list-portfolio').length ) Site.portfolio();

		/* Open external links in new windows */
		jQuery('a[rel="external"]').attr({
			target: "_blank",
			title: "Lien s'ouvrant dans une nouvelle fenêtre"
		});

	},

/* ############################################# */

	home: function(){

		jQuery.getScript('ui/js/jquery.slideviewer.js', function(){
			jQuery('#slideshow').slideView();
			setTimeout("nextImg()", 6000);
		});

	},

/* ############################################# */

	portfolio: function(){

		/* If browser's JavaScript is deactivated or not supported, do not display dynamic elements */
		jQuery('.folio .js').removeClass('js').addClass('accordion');

		/* Portfolio hiding/revealing mechanism */
		jQuery('.folio .entry').hide();

		jQuery('.folio .accordion').toggle(
			function(){
				jQuery(this).removeClass('down').addClass('up');
				var e = jQuery(this).parent().children('.entry');
				jQuery(e).slideDown();
			},
			function(){
				jQuery(this).removeClass('up').addClass('down');
				var e = jQuery(this).parent().children('.entry');
				jQuery(e).slideUp('fast');
			}
		);
		jQuery('.folio h3 a').toggle(
			function(){
				jQuery(this).parent().parent().children('.drop').removeClass('down').addClass('up');
				var e = jQuery(this).parent().parent().children('.entry');
				jQuery(e).slideDown();
			},
			function(){
				jQuery(this).parent().parent().children('.drop').removeClass('up').addClass('down');
				var e = jQuery(this).parent().parent().children('.entry');
				jQuery(e).slideUp('fast');
			}
		);

	},

/* ############################################# */

	formValidation: function(){

		jQuery.getScript('ui/js/jquery.validate.js', function(){

			jQuery.validator.setDefaults({
				submitHandler: function(form) {
					jQuery.post('common/contact.php', jQuery(form).serialize(), function(data){
						jQuery('#content').html(data);
					});
					
				},
				onfocusout: false
			});

			jQuery('form').validate({
				rules: {
					firstname: 'required',
					lastname: 'required',
					organization: 'required',
					position: 'required',
					message: 'required',
					by_phone: 'required',
					email: {
						required: "#by_email:checked",
						email: true
					},
					phone: {
						required: "#by_phone:checked",
						phone: true
					}
				},
				messages: {
					firstname: 'Ce champ est obligatoire.',
					lastname: 'Ce champ est obligatoire.',
					organization: 'Ce champ est obligatoire.',
					position: 'Ce champ est obligatoire.',
					message: 'Ce champ est obligatoire.',
					by_phone: 'Ce champ est obligatoire.',
					email: {
						required: 'Ce champ est obligatoire.',
						email: "S'il vous pla&icirc;t entrer un courriel valide."
					},
					phone: {
						required: 'Ce champ est obligatoire.',
						phone: "S'il vous pla&icirc;t sp&eacute;cifier un num&eacute;ro de t&eacute;l&eacute;phone valide (ex. 1 (789)-555-1234)."
					}
				}
			});


			// Code to change required field
			jQuery('#by_phone').click(function() {
				jQuery('label[for="email"]').children('span.req').remove().appendTo('label[for="phone"]');
			});
			jQuery('#by_email').click(function() {
				jQuery('label[for="phone"]').children('span.req').remove().appendTo('label[for="email"]');
			});

		});

	}

/* ############################################# */

}

/* ############################################# */

   /* 1.0. Slideviewer Automation ############# */

	var img = 0;

	function nextImg()
	{
		var slides = jQuery('#slideshow .stripViewer ul').find('li').length;
		jQuery('div#stripTransmitter0 a:eq('+img+')').click();
		img = ( (img+1) >= slides ) ? 0 : img + 1;
		setTimeout("nextImg()", 6000);
	}

   /* 1.1. Google Analytics ################### */

	function googleAnalytics() {
		var gaJsHost = (('https:' == document.location.protocol) ? 'https://ssl.' : 'http://www.');
		jQuery.getScript(gaJsHost + 'google-analytics.com/ga.js', function(){
			var pageTracker = _gat._getTracker(''); // Insert GA Code
				pageTracker._initData();
				pageTracker._trackPageview();			
		});
	}

   /* 5.6. Spamless Email ##################### */

	function noSpam(user) {
		var mail = 'mailto:' + user + '@bectrol.com';
		window.location.href = mail;
	}

/* ############################################# */

jQuery.noConflict();

(function($) {

	/**
	 * helper variables and function
	 */
	$.ifixpng = function(customPixel) {
		$.ifixpng.pixel = customPixel;
	};
	
	$.ifixpng.getPixel = function() {
		return $.ifixpng.pixel || 'ui/img/pixel.gif';
	};
	
	var hack = {
		ltie7  : $.browser.msie && $.browser.version < 7,
		filter : function(src) { return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')"; }
	};
	
	/**
	 * Applies ie png hack to selected dom elements
	 *
	 * $('img[@src$=.png]').ifixpng();
	 * @desc apply hack to all images with png extensions
	 *
	 * $('#panel, img[@src$=.png]').ifixpng();
	 * @desc apply hack to element #panel and all images with png extensions
	 *
	 * @name ifixpng
	 */
	 
	$.fn.ifixpng = hack.ltie7 ? function() {
    		return this.each(function() {
			var $$ = $(this);

			// in case rewriting urls
			/*
			var base = $('base').attr('href');
			if (base) base = base.replace(/\/[^\/]+$/,'/'); // remove anything after the last '/'
			*/

			if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
				if ($$.attr('src')) {
					if ($$.attr('src').match(/.*\.png([?].*)?$/i)) { // make sure it is png image
						// use source tag value if set 
						var source = /*(base && $$.attr('src').search(/^(\/|http:)/i)) ? base + $$.attr('src') : */$$.attr('src');
						// apply filter
						$$.css({ 'filter':hack.filter(source), 'width':$$.width(), 'height':$$.height() })
						  .attr({ src: $.ifixpng.getPixel() })
						  .positionFix();
					}
				}
			} else { // hack png css properties present inside css
				var image = $$.css('background-image');
				if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)) {
					image = RegExp.$1;
					image = /*(base && image.substring(0,1)!='/') ? base + image : */image;
					$$.css({ 'background':'none', 'filter':hack.filter(image) })
					  .children().children().positionFix();
				}
			}
		});
	} : function() { return this; };
	
	/**
	 * Removes any png hack that may have been applied previously
	 *
	 * $('img[@src$=.png]').iunfixpng();
	 * @desc revert hack on all images with png extensions
	 *
	 * $('#panel, img[@src$=.png]').iunfixpng();
	 * @desc revert hack on element #panel and all images with png extensions
	 *
	 * @name iunfixpng
	 */
	 
	$.fn.iunfixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $$ = $(this);
			var src = $$.css('filter');
			if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
				src = RegExp.$1;
				if ($$.is('img') || $$.is('input')) {
					$$.attr({ 'src':src }).css({ 'filter':'' });
				} else {
					$$.css({ 'filter':'', 'background':'url('+src+')' });
				}
			}
		});
	} : function() { return this; };
	
	/**
	 * positions selected item relatively
	 */
	 
	$.fn.positionFix = function() {
		return this.each(function() {
			var $$ = $(this);
			var position = $$.css('position');
			if (position != 'absolute' && position != 'relative') {
				$$.css({ position:'relative' });
			}
		});
	};

})(jQuery);

jQuery(document).ready(function(){
	//googleAnalytics();

	Site.start();
});

/* Code is poetry */