/**
 * Title: CPLAT
 * Copyright:	Copyright (c) 2008
 * Company:	BBC Worldwide Ltd
 * Description:	BBCWORLDWIDE_CPLAT namespace
 *
 * @author: NgAW1
 * @version: $Revision: $
 * @date: 10 June 2008 16:51:45
 */


 /**
  * This create a namespace BBCWORLDIWDE_CPLAT
  * also contains lots of generic JS functions that can be used for anything
  */
BBCWORLDWIDE_CPLAT = {};

/**
 * This method allow prototypal inheritance where a new object is create from a base object
 * @param base {@link Object}
 * @return Object {@link Object}
*/
BBCWORLDWIDE_CPLAT.newObject = function (o) {
		function F() {}
		F.prototype = o;
		return new F();
};

/**
 * This method creates an object of the request parameters from a given Form object
 * N.B: object is properly encoded by jQuery Ajax method
 * @param oForm {@link Form}
 * @return Object {@link Object}
 */
BBCWORLDWIDE_CPLAT.createRequestParameters = function (oForm) {
	var sRequestParameters = {};
	sRequestParameters.ajaxRequest = true;

	for(i=0; i<oForm.elements.length; i++){
		if((oForm.elements[i].type !== "checkbox") || ((oForm.elements[i].type === "checkbox") && (oForm.elements[i].checked))) {
			if((oForm.elements[i].type !== undefined) && (oForm.elements[i].name.length > 0)) {
				sRequestParameters[oForm.elements[i].name] = oForm.elements[i].value;
			}
		}
	}
	return sRequestParameters;
};


/**
 * This method retrieve or create a Form tag given an input element
 * @param oContainer {@link overLayWindow}
 * @param oInputElement {@link form input element}
 * @return oForm {@link Form}
 */
BBCWORLDWIDE_CPLAT.retrieveFormElement = function (oContainer, oInputElement) {
	var oForm = null;
	if(oInputElement !== undefined) {
		oForm = oInputElement.form;
		if(oForm === null) {
			oContainer.wrap("<form></form>");
			oForm = oInputElement.form;
		}
	}
	return oForm;
};

/**
 * This method retrieve a list of request parameters from query string
 * @param query {@link String}
 * @return params {@link array}
 */
BBCWORLDWIDE_CPLAT.parseQuery = function ( query ) {
	var Params = {};
	   if ( ! query ) {return Params;}// return empty object
	   var Pairs = query.split(/[;&]/);
	   for ( var i = 0; i < Pairs.length; i++ ) {
	      var KeyVal = Pairs[i].split('=');
	      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
	      var key = unescape( KeyVal[0] );
	      var val = unescape( KeyVal[1] );
	      val = val.replace(/\+/g, ' ');
	      Params[key] = val;
	   }
   	return Params;
};

/**
 * This method retrieve the page width and height
 * @return arrayPageSize {@link array}
 */
BBCWORLDWIDE_CPLAT.getPageSize =  function () {
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
};

/**
 * This method detect whether it is a Mac or FireFox
 * @return boolean {@link boolean}
 */
BBCWORLDWIDE_CPLAT.detectMacXFF = function () {
  	var userAgent = navigator.userAgent.toLowerCase();
  	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    		return true;
  	}
};

/**
 * This methid retrieve the window dimension
 * @return object literal {@link object}
 */
BBCWORLDWIDE_CPLAT.getWindowDimension = function () {
	 var x = 0;
	 var y = 0;
	 if (self.innerHeight) {
		 x = self.innerWidth;
		 y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	} else if (document.body) {
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	return {width:x, height:y};
};


