// JavaScript Document - Global functions

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

var swap_text_boxes = [];
function init_swap_text_boxes(){
  $('input[type=text][value].bmg_swaptextbox').each(function() {
    swap_text_boxes[$(this).attr('id')] = $(this).attr('value');
  });
  $('input[type=text][value].bmg_swaptextbox').bind('focus', function() {
    if($(this).val() == swap_text_boxes[$(this).attr('id')]) {
      $(this).val('');
  	  $(this).select(); 
	  if ($(this).hasClass("bmg_swap_color")){
	  	 $(this).addClass("bmg_swaptextboxMod");
	  }	
    }
  });
  $('input[type=text][value].bmg_swaptextbox').bind('blur', function() {
    if($(this).val() == '') {
      $(this).val(swap_text_boxes[$(this).attr('id')]);
	if ($(this).hasClass("bmg_swap_color")){
	  	 $(this).removeClass("bmg_swaptextboxMod");
	  }	  	  
    }
  });
}

var UTIL = UTIL || {};
UTIL.popup = UTIL.popup || {};
UTIL.popup.open = function (href, params)
{
   var defaultParams = {
       "width":       "600",   
       "height":      "520",   
       "top":         "0",     
       "left":        "0",     
       "directories": "no",   
       "location":    "no",    
       "resizeable":  "yes",   
       "menubar":     "no",    
       "toolbar":     "no",    
       "scrollbars":  "yes",   
       "status":      "no"     
   };
    var windowName = params["windowName"] || "new_window";
    var i, useParams = "";
   for (i in defaultParams)
   {
       useParams += (useParams === "") ? "" : ",";
       useParams += i + "=";
       useParams += params[i] || defaultParams[i];
   }
    return window.open(href, windowName, useParams);
};

$(document).ready(function(){
	$.preloadImages("/qcms/qCmsRepository/Global/largeBusiness/images/loading-circle.gif","/qcms/qCmsRepository/Global/largeBusiness/images/loading-image-bg.gif");						   
  	$("a.bmg_popup").each(function (i){
       $(this).click(function(event) {
           event.preventDefault();
           var params = $(this).data("bmg_popup") || {};            
           if ($(this).attr("target"))
           {
               params.windowName = $(this).attr("target");
           }
           var windowObject = UTIL.popup.open(this.href, params);
           $(this).data("windowObject", windowObject);
       });
   });
			$.fn.alphanumeric = function(p) { 
		p = $.extend({
			ichars: "!@#$%^&*()_-+=[]\\\';,/{}|\":<>?~`. ",
			nchars: "",
			allow: ""
		  }, p);	
		return this.each
			(
				function() 
				{
					if (p.nocaps) p.nchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
					if (p.allcaps) p.nchars += "abcdefghijklmnopqrstuvwxyz";					
					s = p.allow.split('');
					for ( i=0;i<s.length;i++) if (p.ichars.indexOf(s[i]) != -1) s[i] = "\\" + s[i];
					p.allow = s.join('|');		
					var reg = new RegExp(p.allow,'gi');
					var ch = p.ichars + p.nchars;
					ch = ch.replace(reg,'');
					 $(this).unbind("keypress");
					$(this).keypress
						(
							function (e)
								{
									if (!e.charCode) k = String.fromCharCode(e.which);
										else k = String.fromCharCode(e.charCode);
										
									if (ch.indexOf(k) != -1) e.preventDefault();
									if (e.ctrlKey&&k=='v') e.preventDefault();	
								}						
						);
					$(this).bind('contextmenu',function () {return false});				
				}
			);
	};
	$.fn.numeric = function(p) {
	
		var az = "abcdefghijklmnopqrstuvwxyz";
		az += az.toUpperCase();

		p = $.extend({
			nchars: az
		  }, p);
		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
	};
	$.fn.alpha = function(p) {
		var nm = "1234567890";
		p = $.extend({
			nchars: nm
		  }, p);
		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	}; 
	
try { 
	document.execCommand('BackgroundImageCache', false, true);
} 
catch(e) {}
	
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		$('.ieFrame').bgiframe({ opacity: true, top: '-4px',  right: '-1em' });
	}


/*function preloadImages(images) {
	var i,newImage;
	for (i=0;i<images.length;i++){
	newImage = new Image();
        newImage.src = images[i];
}}
preloadImages([    
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_menuBgSides_hover.gif',
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_menuBg.gif',
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_menuBgSides.gif',	
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_menuBg_hover.gif',
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_button_arrow_icon_wt.gif',
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_button_background_wt.gif'	
]);
*/
	init_swap_text_boxes();				   

	$("body").click(function(){
	$("div.bmg_subMenu").slideUp("fast");
});
	$("div.bmg_itemMenu a").focus(function(){
	$(this).parent("div").find("div.bmg_subMenu").slideDown("fast");
	$(this).parent("div").siblings().find("div.bmg_subMenu").slideUp("fast");
});
	$(".bmg_nav_search_icon").focus(function(){
	$("div.bmg_subMenu").slideUp("fast");

});
	$("div.bmg_itemMenu").hoverIntent(function () {
//	$(this).children().addClass("bmg_showHide").slideDown("fast");
    $(this).children().slideDown("fast");
	
	if ($.browser.mozilla) {
	$(".bmg_car_button_all").hide();
	}
}, 
	function () {
//	$(this).children("div").removeClass("bmg_showHide").slideUp("fast"); 
	$(this).children("div").slideUp("fast");
	if ($.browser.mozilla) {
	$(".bmg_car_button_all").show();
	}
});
$("ul.bmg_main_menu ul li").mouseover(function(){
	$(this).addClass("bmg_menu_hover");
}).mouseout(function(){
	$(this).removeClass("bmg_menu_hover");
});
$("ul.bmg_main_menu ul li.last").mouseover(function(){
	$(this).addClass("bmg_menu_hover_last");
	$(this).removeClass("bmg_menu_hover");
}).mouseout(function(){
	$(this).removeClass("bmg_menu_hover_last");
});
$("ul.bmg_main_menu ul li").mouseover(function(){
	$(this).addClass("bmg_menu_hover");
}).mouseout(function(){
	$(this).removeClass("bmg_menu_hover");
});

try {
	if (productsSelected) {
		$("#bmg_products").addClass("bmg_itemMenuActive");
		$("#bmg_resourceCenter").removeClass("bmg_itemMenuActive"); 
	} else if (resourceSelected) {
		$("#bmg_resourceCenter").addClass("bmg_itemMenuActive");
		$("#bmg_products").removeClass("bmg_itemMenuActive");
	}
	else {
		$(".bmg_topMenuAll").children().removeClass("bmg_itemMenuActive"); 
	}
}
catch (err) {
	log.error("menuSelection",""+err);
}

}); 
/*function stopError(errMsg, errScript, errLine){  
	try{
		log.error("GlobalError"+errMsg,"Global Error: " + errMsg +"\n > script: " + errScript + "\n > line: " + errLine);
	} catch(e){}  
	return true;  
}   
window.onerror = stopError; */
