//																			//
		//	This is a simple helper file pulled from a project						//
		//	(built on Laravel 4.1).  Is it not fancy (just static methods), 		//
		//	but can be easily understood out of context.  As well, it contains 		//
		//	various functionality types that can give you an idea of my				//
		//	coding style.															//
		//__________________________________________________________________________//



		/****************************************************************************|
		|	 																		 |
		|	 Hlp																	 |
		|																			 |
		|	 Misc helpers:															 |
		|	 		- Resources														 |
		|	 		- Browser														 |
		|	 		- Utility														 |
		|	 																		 |
		|	 (Yes, these are static)												 |
		|	 																		 |
		|****************************************************************************/

		class Hlp
		{

			/****************************************************************************|
			|****************************************************************************|
			|	 RESOURCE HELPERS														 |
			|****************************************************************************|
			|****************************************************************************/


			/***************************************************************************|
			|																			|
			|	 resourceUrl															|
			|																			|
			|	 Returns secure resource URL for filename or id (faux overloading)		|
			|																			|
			|	 @param  int/string $resource - id or name of resource					|
			|	 @param  int		$expire	- min link is valid							|
			|	 @param  int		$groupId - if checking for group rather than 		|
			|									logged user								|
			|	 @return string		url for resource 									|
			|																			|
			****************************************************************************/
			public static function resourceUrl($resource, $expire = 30, $groupId = NULL)
			{
				if (!self::resourceRights($resource, $groupId)) {
					return App::abort(401, 'You are not authorized to view this resource.');
				}

				$fileName = self::resourceBuildName($resource);

				return Cdn::getSignedURL(Path::resource().$fileName, $expire);
			}


			/***************************************************************************|
			|																			|
			|	 resourceRights															|
			|																			|
			|	 Determines rights for a resource id or name  (faux overloading)		|
			|																			|
			|	 @param  int/string $resource - id or name of resource					|
			|	 @param  int		$groupId - if checking for group rather than 		|
			|									logged user								|
			|	 @return bool		Whether rights exist for resource 					|
			|																			|
			|	NOTE: If groupId is provided, this method assumes user has 				|
			|			rights to the group												|
			|																			|
			****************************************************************************/
			public static function resourceRights($resource, $groupId = NULL)
			{
				$hasRights = false;
				$groupId = ($groupId) ? $groupId : Auth::user()->group_id;
				$resourceId = is_numeric($resource) ? $resource : Resource::where('file_name', $resource)->pluck('id');
				$resGroupRight = Resource::group($groupId, $resourceId);

				// If group doesn't have rights, see if the org group is in has rights
				if (!$resGroupRight) {
					$groupOrg = Group::find($groupId)->pluck('organization_id');
					$resOrg = Resource::find($resourceId)->pluck('organization_id');

					$hasRights = ($groupOrg == $resOrg) ? true : false;
				}
				else {
					$hasRights = true;
				}

				return $hasRights;
			}



			/***************************************************************************|
			|																			|
			|	 resourceBuildName														|
			|																			|
			|	 Build resource file name from id or filename (faux overloading)		|
			|																			|
			|	 @param  int/string $resource - id or name of resource					|
			|	 @param  string		$ext - file extension						 		|
			|	 @return string		Resource filename					 				|
			|																			|
			****************************************************************************/
			public static function resourceBuildName($res, $ext = NULL)
			{		
				$resource = (is_numeric($res)) ? Resource::findOrFail($res) : Resource::where('file_name', $res)->firstOrFail();
		
				if(!isset($resource)) {
					return App::abort(404, 'Resource does not exist.');
				}

				if (!$ext) {
					switch ($resource->type) {
						case 'video':
							$ext = IVID; break;
						case 'pdf':
							$ext = IPDF; break;
						default:
							$ext = IIMG; break; //assume we are trying to get an image
					}
				}

				return $resource->file_name.$ext;
			}



			/***************************************************************************|
			|																			|
			|	 isSigned																|
			|																			|
			|	 Determines if url is signed for access									|
			|																			|
			|	 @param  string		$url - resource url									|
			|	 @return bool		Whether url is signed					 			|
			|																			|
			****************************************************************************/
			public static function isSigned($url)
			{
				return strpos($url, SIGNATURE) !== false;
			}



			/****************************************************************************|
			|****************************************************************************|
			|	 BROWSER HELPERS														 |
			|****************************************************************************|
			|****************************************************************************/

			/***************************************************************************|
			|																			|
			|	 browserValid															|
			|																			|
			|	 Determines if browser meets min version requirements (from config)		|
			|																			|
			|	 Note: based on current session and config settings						|
			|																			|
			****************************************************************************/
			public static function browserValid()
			{
				$browser = new Browser\valBrowser();
				$browserErrMsg = 'cust.browser.incompatible';

				try {
					if ($browser->Detect()->isDetected()) {

						$validBrowsers = Config::get('settings.valid_browser');
						$userBrowserType = $browser->getBrowser();
						$userBrowserVersion = floor($browser->getVersion());

						if (array_key_exists($userBrowserType, $validBrowsers)) {

							$topVersion = $validBrowsers[$userBrowserType];

							if ($userBrowserVersion < (intval($topVersion)-1)) {
								return $browserErrMsg;
							} else {
								return false;
							}
						} else {
							return $browserErrMsg;
						}
					} else {
						return $browserErrMsg;
					}
				} catch(Exception $ex) {
					return $browserErrMsg;
				}
			}


			/***************************************************************************|
			|***************************************************************************|
			|	 UTILITIES																|
			|***************************************************************************|
			|***************************************************************************/

			/***************************************************************************|
			|																			|
			|	 getGroup																|
			|																			|
			|	 Returns current group as object (collection)							|
			|																			|
			|	 Note:  Based on current session or URL context							|
			|																			|
			****************************************************************************/
			public static function getGroup()
			{
				return Auth::check() ? Auth::user()->group : Group::fromUrl();
			}


			/***************************************************************************|
			|																			|
			|	 isMarkup																|
			|																			|
			|	 Simple test to determines if string is markup							|
			|																			|
			|	 @param  string		$str - string to check								|
			|	 @return bool		Whether string is markup					 		|
			|																			|
			|	 Note: Assumes string is not null										|
			|																			|
			****************************************************************************/

			public static function isMarkup($str)
			{
				return strlen($str) != strlen(strip_tags($str) ? true : false;
			}


			/***************************************************************************|
			|																			|
			|	 removeItemsByKey														|
			|																			|
			|	 Removes array items by key												|
			|																			|
			|	 @param  array		$arrArray - Array from which to remove items		|
			|	 @param  array		$arrKeys - Keys of items to remove					|
			|	 @return array		Cleaned array								 		|
			|																			|
			****************************************************************************/
			public static function removeItemsByKey($arrArray, $arrKeys)
			{
				foreach ($arrKeys as $arrKey) {
					unset($arrArray[$arrKey]);
				}

				return  $arrArray;
			}


			/***************************************************************************|
			|																			|
			|	 removeItemsByValue														|
			|																			|
			|	 Removes array items by key												|
			|																			|
			|	 @param  array		$arrArray - Array from which to remove items		|
			|	 @param  array		$arrValues - Values of items to remove				|
			|	 @return array		Cleaned array								 		|
			|																			|
			****************************************************************************/
			public static function removeItemsByValue($arrArray, $arrValues)
			{

				foreach ($arrValues as $arrValue) {
					while (($key = array_search($arrValue, $arrArray)) !== false) {
						unset($arrArray[$key]);
					}
				}

				return  $arrArray;
			}
		}