/**
 * 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
 */
var equalHeight = {
    init: function(group) {
        tallest = 0;
        group.each(function() {
            thisHeight = jQuery(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }
};


/**
 * remove and put back default values in field
 * @param targ: jquery selector of the chosen field
 */
var inputLabel = {
	/**
	 * add events on field and set default value;
	 */
	init: function(targ){
		var val1 = '';

		$(targ).each(function(){
			$(targ).focus(function(){
				if(val1 == ''){val1 = $(this).val();}
				inputLabel.clear(targ, val1);
			})
			.blur(function(){
				inputLabel.blur(targ, val1);
			})
			.addClass("label-in");
		});
	},
	/**
	 * clear the field of default value
	 */
	clear: function(targ, startValue){

		if($(targ).val() !== startValue){
			$(targ).val();
		}else{
			$(targ).val("").removeClass("label-in");
		}
	},
	/**
	 * put the default value back
	 */
	blur: function(targ, startValue){

		if($(targ).val() !== ""){
			$(targ).val();
		}else{
			$(targ).val(startValue).addClass("label-in");
		}
	}
};

/**
 * loader to insert just before ajax calls
 */
var loader = '<p class="loader center"><img src="/img/ajax-loader.gif" alt="" /></p>';


/**
 * popup creator
 * @param _url: url to call in ajax
 * @param _id: action link id
 * @param _popId: id of popup
 * @param _url: url to get and load in popup
 * @param _title: popup titlebar text
 * @param _width: popup width
 */

var popup = {
    init: function(_id, _popId, _url, _title, _width) {
		this.popId = '#'+_popId;
		var popHtml = '<div id="'+_popId+'"></div>';
		$('body').append(popHtml);

		$('#'+_id).click(function(){

			$(popup.popId).load(_url, function(){

			$(popup.popId).dialog({
					modal: true,
					autoOpen: false,
					title: _title,
					draggable: false,
					width: _width
				}).hide();

				$('.bt-cancel', this).click(function(){
					$(popup.popId).dialog('close');
					return false;
				});

				$(popup.popId).dialog('open');
			});

			return false;
		});

    }
};


/**
 * manage the subject selection in the contact form
 */
var contact = {
	init: function(){
		this.cli = '#frm-contact #client';
		this.par = '#frm-contact #sujetParticulier';
		this.pro = '#frm-contact #sujetProfessionel';

		this.hide();

		$(this.cli).change(function(){

			contact.hide();

			if($(this).val() == 'particulier'){
				$(contact.par).attr('disabled', '').parent().show();
			}
			
			if($(this).val() == 'professionel'){
				$(contact.pro).attr('disabled', '').parent().show();
			}
		});
	},
	hide: function(){
		$(contact.par + ',' + contact.pro).attr('disabled', 'disabled').parent().hide();
	}
};


/**
 * Fonction to relaunch after an ajax refresh
 */
var slider = {
    init: function() {
		this.list = '#bloc-title.acc ul';
		this.dist = parseInt($('#bloc-title.acc .xtra').height(), 10);
		this.x = 0;
		this.count = $(this.list + ' li').length - 1;

		this.inter = setInterval(slider.move, 12000);


    },
	move: function(){

		if(slider.x >= slider.count){

			$(slider.list).stop(1, 0).fadeOut(1500, function(){
				$(slider.list).animate({
					marginTop: 0
				}, 10, function(){
					$(slider.list).fadeIn(1500);
				});
			});
			slider.x = 0;

		}else{

			$(slider.list).fadeOut(1500, function(){

				$(slider.list).animate({
					marginTop: parseInt($(slider.list).css('marginTop'),10) - slider.dist + 'px'
				}, 10, function(){
					$(slider.list).fadeIn(1500);
				});
			});

		}

		slider.x++;
	}
};


/**
 * Fonction to relaunch after an ajax refresh
 */
var ajax = {
    init: function() {
		JSTarget.init("rel", "external", "");
    }
};


/**
 * Fonctions to call on DOM ready
 */

$(document).ready(function() {

	if($('#frm-contact')[0]){
		contact.init();
	}

	$('form.validate').zdgForm({
		//_urlSelector: '.hUrl',
		//_loader: '/img/ajax-loader.gif',
		//_before: function(){},
		//_success: function(){},
		//_complete: function(){},
		//_error: function(){},
		//_dataType: 'json',
		_container: 'div'
		//_onBlur: true
	});

	$('.ls-pages.nav-ajax').zdgPaginAjax({
		//_container: '',
		//_loader: '/img/ajax-loader.gif'
	});

	if($('#bloc-title.acc')[0]){
		slider.init();
	}

});
