/**
* main script file
* @author		mac@zestedegenie.com
* @creationDate 2011-03-22
* @editDate		2011-03-22
*/


/** *******************************************************
 * 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","");});


/** *******************************************************
 * browser vars
 */

var isIe = false;
var isIe6 = false;

/**
 * Check if browser is IE
 */
if ($.browser.msie) {
	isIe = true;
}
/**
 * Check if browser is IE6 or less
 */
if (isIe && $.browser.version < 7) {
	isIe6 = true;
}

/** *******************************************************
 * contain text vars
 */
var vars = {
	init: function(){
		if($('body.en')[0]){ // if site EN
			// colobox
			vars.current = "{current} of {total}";
			vars.previous = "previous";
			vars.next = "next";
			vars.close = "close";
		}else{
			// colobox
			vars.current = "{current} de {total}";
			vars.previous = "suivant";
			vars.next = "précédent";
			vars.close = "fermer";
		}
	}
};

/** *******************************************************
 * Put elements at the same height
 */
var equalHeight = {
	/**
	 * init
	 * @param	jQuery $group	jQuery object of the elements to equalize
	 */
	init: function(group) {
		var tallest = 0;
		group.each(function() {
			var thisHeight = jQuery(this).height();
			if (thisHeight > tallest) { // if curr height higher than tallest
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
};

/** *******************************************************
 * add clearers in list
 */
var clearer = {
	/**
	 * init
	 * @param	jQuery $e	jQuery object of to clears
	 * @param	jQuery $nb	nb of elements to clear
	 */
	init: function(e, nb) {
		
		$(e+'>li').each(function(){
			if($(this).index(e+'>li')%nb === 0 ){
				$(this).before('<span class="clearer"></span>');
			}
		});
		
	}
};

/** *******************************************************
 * remove and put back default values in field
 */
var inputLabel = {
	/**
	 * add events on field and set default value;
	 * @param	jQuery $targ	jquery selector of the field
	 */
	init: function(targ){
			
		targ.each(function(){
			var targ = $(this);
			var val1 = '';
			$(this).focus(function(){
				if(val1 === ''){val1 = $(this).val();}
				inputLabel.clear(targ, val1);
			})
			.blur(function(){
				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
	 */
	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
	 */
	blur: function(targ, startValue){
		if(targ.val() !== ""){ // if field not empty
			targ.val();
		}else{
			targ.val(startValue).addClass("label-in");
		}
	}
};

/** *******************************************************
 * Fonction to relaunch after an ajax refresh
 */
var ajax = {
	init: function() {
		JSTarget.init("rel", "external", "");
	}
};


/** *******************************************************
 * Fonctions to call on DOM ready
 */

$(document).ready(function() {
	vars.init();
	clearer.init('#ls-succ', 4);
	
	// gallery photo setup
	if(typeof($.colorbox) !== 'undefined'){
		
		$('#ls-gal a').attr('rel', 'color').colorbox({
			current: vars.current,
			previous: vars.previous,
			next: vars.next,
			close: vars.close
		});
	}
	
	// rounded corners and png fix
	if(typeof(DD_roundies) !== 'undefined' && isIe6 === true){
		//DD_roundies.addRule('');
	}
	
});


/** *******************************************************
 * Fonctions to call on window load
 */
$(window).load(function() {

});
