﻿/**
 * Title:      BRP Tools
 * Author:     Alexandre paquette, alexandre.paquette@nurun.com, alexandre.paquette@nadrox.com
 *
 * This file contains functionnalities used accross all BRP WebSites. The usage of this script 
 * requires that you add the following to the html head 
 * <script type="text/javascript">
 *         BRPTools = {};
 *         BRPTools.defer = array();
 * </script>
 *
 * The following line must be included after jquery script
 * <script type="text/javascript" src="[path-to-js]/BRPTools.js">
 * 
 * 
**/

/**
 * BRPTools.clickableMenus
 * Handles menus that opens on click (fake dropdown)
 * Each call registers an object into "instances" array
 * so we can handle closing other menus while opening one.
 * When javascript is disabled, the menus are still showing 
 * using CSS hover
 * @param target		represents the element where the Click is binded. 
 * 						the Click event closes all children of @target with 
 * 						.show-on-click classname
 * @param otherToggles 	Array of jQuery selectors which targets elements to toggle,
 * 						other than children of target that uses .show-on-click
 *
 * Css to add to your css file
    .show-on-hover,
    .show-on-click{
	    display:none;
    }
    *:hover > .show-on-hover,
    *:hover > .show-on-click{
	    display:block;
    }
 
 * Css to add to css file loaded by javascript (often call InitialState.css or Script-Enabled.css)
 
    *:hover > .show-on-click{
        display:none;
    }
    
 * Usage Example                      Menu To Open      Other element to toggle
 *   BRPTools.clickableMenus.register("#BRP-Products", $('#menu-overlay'));
 *
 */
BRPTools.clickableMenus = {
	instances : [],
	instance : function(target, otherToggles){
	    //console.log("new instance for "+ target);
		var target = target;
		var inst = this;
		if (typeof otherToggles == "undefined"){
			var otherToggles = $([]);
		}else{
			var otherToggles = $(otherToggles);
		}
		var isOpen;
		
		var hideOverlay = function(e){
			/** e.which is bugged in jquery 1.5 http://bugs.jquery.com/ticket/7183 
			 * So we use keyCode and button instead **/
			
			if (isOpen && ((typeof e == "undefined") || 
							( typeof e != "undefined" && 
									(e.type == "click" && (e.which == 1 || e.which == 0) ) || (e.type == "keyup" && e.which == 27)) &&
									e.currentTarget != $(target)[0])){
				otherToggles.css("display","none");
				$(".show-on-click",$(target)).css("display","none");
				isOpen = false;
			}
			
		};
		var showOverlay = function(e){
			if(!isOpen && e.currentTarget == $(target)[0]){
				e.stopPropagation();
				e.preventDefault();
				$(BRPTools.clickableMenus.instances).each(function(){
					this.hideOverlay(e);
				});
				
			}else if(isOpen && $(e.target).parents(".show-on-click",e.currentTarget).length == 0 ){
				e.preventDefault();
				hideOverlay();
			}
			isOpen = true;
			otherToggles.css("display","block");
			//console.log($(".show-on-click",$(target)));
			$(".show-on-click",$(target)).css("display","block");
		};
		var init = function(){
			if (typeof otherToggles == "undefined"){
				otherToggles = $([]);
			}
			//console.log(target);
			$(target).click(showOverlay);
			$(document.documentElement).keyup(hideOverlay)
			$(document.body).click(hideOverlay);
			
		};
		init();
	return { "hideOverlay" : hideOverlay}},
	register : function(target, otherToggles){
		BRPTools.clickableMenus.instances.push(new BRPTools.clickableMenus.instance(target,otherToggles));
	}
};
BRPTools.youtubeExtractId = function (youtubeUrl){
    var origUrl = youtubeUrl;
    youtubeUrl = youtubeUrl.replace(/(http\:\/\/www.youtube.com\/watch.*?(&|\?)v\=(.*)?(&|$)?)|(http\:\/\/www.youtube.com\/embed\/(.*))|(http:\/\/youtu.be\/(.*))/, "$3$6$8");
    if (youtubeUrl != origUrl) {
        //filter the extra parameters that can be found in the id                        
        youtubeUrl = youtubeUrl.replace(/(.*)?&.*/, "$1");
        return youtubeUrl;
    }else{
        return false;
    }
}
$(document).ready(function(){
    var isEnhanceEnabled = (typeof $.enhance !== "undefined") ;
    if (isEnhanceEnabled){
	    $(BRPTools.defer).each(function(){
	        $.enhance(this.call,{title:"Executing defer script : " + this.title});
	    });
	    $(document).enhance();
	 }else{
	    $(BRPTools.defer).each(function(){
	        this.call();
	    });
	 }
	//This contains an array of function to execute that were added inline 
});
