/*
 * Some pfister helper js methods  
 */

 function isNumberKey(evt)
  {
     if (evt == null) return false;
     var charCode = (evt.which) ? evt.which : evt.keyCode
     if ((charCode > 31) && (charCode < 48 || charCode > 57)) {
        return false;
     } 
     return true;
  }
  
  // prevent textfield from exeeding defined max length
  function isMaxLength(obj, mlength){
	if (obj.getAttribute && obj.value.length > mlength) {
		obj.value=obj.value.substring(0, mlength)
	}
  }

  //scroll the page on "to top" link
  function toTopLinkSetup() {
		jQuery('a[href=#top]').click(function(){
			jQuery('html, body').scrollTop(0);
				       return false;
		});
  }
 jQuery(document).ready( toTopLinkSetup );
 
 
 function GetBrowserWidth()
 {
         var x = 0;
         if (self.innerHeight) {
                 x = self.innerWidth;
         }
         else if (document.documentElement && document.documentElement.clientHeight) {
                 x = document.documentElement.clientWidth;
         }
         else if (document.body) {
                 x = document.body.clientWidth;
         }
         return x;
 }

 
 function GetBrowserHeight()
 {
         var y = 0;
         if (self.innerHeight) {
                 y = self.innerHeight;
         }
         else if (document.documentElement && document.documentElement.clientHeight) {
                 y = document.documentElement.clientHeight;
         }
         else if (document.body) {
                 y = document.body.clientHeight;
         }
         return y;
 }
 
	var isIE8 = (function()
 			{
 		return !!( (/msie 8./i).test(navigator.appVersion) && window.ActiveXObject && XDomainRequest );
 			});
 	

	
 	// more / less info slider
 	
 	function initMarketingTextPanel() {
 		if (isIE8()) {
 			jQuery(".expand_box").hide();
 			jQuery(".expand_link").toggle(
 					function(){ //executed every even time
 						jQuery(".expand_box").css('display', 'inline');
 						jQuery("#postfix").addClass('hidden');
 					},
 					function(){ //executed every odd time
 						jQuery(".expand_box").css('display', 'none');
 						jQuery("#postfix").removeClass('hidden');
 					}
 					
 			);
 			jQuery(".expand_link").click(function(){ //executed every odd time
 				jQuery(this).children('span').toggleClass('hidden');
 			});
 			
 		} else {
 			jQuery(".expand_box").hide();
 			jQuery(".expand_link").click(function () {
 				jQuery(".expand_box").slideToggle(function(){
 					if(jQuery(this).css('display') == 'block') {
 						jQuery(this).css('display', '');
 					}
 				});
 				jQuery(this).children('span').toggleClass('hidden');
 				jQuery("#postfix").toggleClass('hidden');
 			});
 		}
 	}
 	
 	jQuery(document).ready(function() {
 		initMarketingTextPanel();
 	});
 	
 
 	var BrowserDetect = {
 			init: function () {
 				this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
 				this.OS = this.searchString(this.dataOS) || "an unknown OS";
 			},
 			searchString: function (data) {
 				for (var i=0;i<data.length;i++)	{
 					var dataString = data[i].string;
 					var dataProp = data[i].prop;
 					this.versionSearchString = data[i].versionSearch || data[i].identity;
 					if (dataString) {
 						if (dataString.indexOf(data[i].subString) != -1)
 							return data[i].identity;
 					}
 					else if (dataProp)
 						return data[i].identity;
 				}
 			},
 			dataBrowser: [
 				{
 					string: navigator.vendor,
 					subString: "Apple",
 					identity: "Safari",
 					versionSearch: "Version"
 				}
 			],
 			dataOS : [
 				{
 					string: navigator.platform,
 					subString: "Mac",
 					identity: "Mac"
 				}
 			]

 		};
 		BrowserDetect.init();
 		
	function getCarouselRotationInterval(parent) {
		var rotationInterval = 15000; //ms
		var rotationIntervalSetup = jQuery(parent).find(".carousel-rotation-interval");
		if (rotationIntervalSetup.length > 0) {
			var relAttributeValue = rotationIntervalSetup.attr("rel");
			if (relAttributeValue) {
				rotationInterval = parseInt(relAttributeValue) * 1000;
			}
		}
		
		return rotationInterval;
	}
		
	/**
	 * Finding how far the browser window has been scrolled
	 * @return offset pixes
	 */
	function getScrollY() {
		  var scrOfX = 0, scrOfY = 0;
		  if( typeof( window.pageYOffset ) == 'number' ) {
		    //Netscape compliant
		    scrOfY = window.pageYOffset;
		  } else if( document.body && document.body.scrollTop) {
		    //DOM compliant
		    scrOfY = document.body.scrollTop;
		  } else if( document.documentElement && document.documentElement.scrollTop) {
		    //IE6 standards compliant mode
		    scrOfY = document.documentElement.scrollTop;
		  }
		  return scrOfY ;
		}
	
	/**
	 * Show popup with the specified id. The popup window will be vertically aligned with the current browser view.
	 * @param popupid the id of the popup
	 */
	function invokeRichfacesPopup(popupid) {
		Richfaces.showModalPanel(popupid);
		var heightOfPopup = jQuery('#' + popupid +'Container').find('.rich-mp-container').height();
		var offset = (GetBrowserHeight() - heightOfPopup) / 2;
		if (offset < 0) {
			offset = 0;
		}
		jQuery('.rich-mp-container').css("top", getScrollY() + offset);
	}
	

