/**
 * @author Josh Bennett
 * @copywrite 2011 Hirtle Callaghan & co
 */
//TODO add some simple extend object functionality. probably just shallow for now.

(function(window, undefined){
    
    /**
    * @namespace hirtle callaghan name space.
    */
    hc = window.hc || {};
    
    /**
    * @namespace hirtle callaghan utility collection.
    */
    hc.Utils = (function(){
        return new function()
        {
            var StringTools = function(){};
            /**
            *@class
            */
            this.StringTools = StringTools;
            
            /** 
             *<p>Quick string format function. Usage:</p> <code>format(inputString, arg0 [,arg1, arg2 ....])</code>
             *
             *@memberOf hc.Utils.StringTools 
             *@param {string} inputStr this is the string you would like to apply the arguments to.
             *@param {string} arg0 This is will replace {0} in the input string. subsequent arguments increment by one. {1} {2} ...
             *@example <p><code>
             * format("hi {0}", foo)
             *</code>
             *output: "hi foo"</p>
             */
            var format = function(inpString, arg0){
                var outString = inpString;
                
                var curArg=1;
                var curArgContext=0;
                
                var argLen = arguments.length - 1;
                for(;curArg<=argLen; curArg++){
                    var rx = new RegExp('\\{' + curArgContext++ + '\\}', 'g');
                    outString = outString.replace(rx, arguments[curArg]);
                }
                
                return outString;
            };
            StringTools.format = format;
            
            /** 
             *This is a quick way to do double whisker/mustache templating. (:-{0)
             *
             *@memberOf hc.Utils.StringTools
             *@param {string} inputStr this is the string you would like to apply the context to.
             *@param {object} context a map of values relating to what shoudl be replaced in the string. anything between {{}} will be parsed.
             */
            var quickTemplate = function(inputStr, context){
                if(!context){return inputStr;}
                
                var outString = inputStr;
                
                var currentObject;
                for(currentObject in context){
                    if(context.hasOwnProperty(currentObject)){
                        var replaceString = format('\\{\\{{0}\\}\\}', currentObject);
                        var regEx = new RegExp(replaceString, 'g');
                        outString = outString.replace(regEx, context[currentObject]);
                    }
                }
                
                return outString;
            }
            StringTools.quickTemplate = quickTemplate;
			
			// #hc.Utils.dom
			this.dom = {};
			
			// **createEl(tagName, attributes)**  
			// This will create an elemnet of the given tag name, with the provided attributes.  
			// Example:  
			//      createEl('a', {  
			//          'href': 'http://www.google.com',
			//          'target': '_blank'
			//      });
			function createEl(tagName, attributes){
				var newEl = document.createElement(tagName),
					curAttr = {};
				if(attributes !== null && typeof attributes !== 'undefined'){
					for(curAttr in attributes){
						if (attributes.hasOwnProperty(curAttr)) {
							var val = attributes[curAttr];
							newEl.setAttribute(curAttr, val );
						}
					}
				}
				
				return newEl;
			}
			
			//#hc.Utils.mail
			this.mail = {};
			
			// **prepareEmail(to, subject, body)**  
			// This comes in handy when attempting to obfuscate email address to prevent scrapers from grabbing them.  
			// This will take an email address, subject and a body and submit a form to the mailto: protocol, which should open up the default email client and populate the to, subject and body.  
			// For more information about what this does see http://support.microsoft.com/kb/279460.
			// - *to* {String} - 'TO' e-mail address 
			// - *subject* {String} - subject line
			// - *body* {String} - e-mail's body
			function prepareEmail(to, subject, bodyText) {
				var form = document.createElement('form');
				
				//Set the form attributes 
				form.setAttribute('method', 'post');
				form.setAttribute('enctype', 'text/plain');
				form.setAttribute('action', 'mailto:' + escape(to) + '?Subject=' + escape(subject) + '&Body=' + escape(bodyText ? bodyText : ' ') );
				form.setAttribute('style', 'display:none');
				
				//Append the form to the body
				document.body.appendChild(form);

				//Submit the form
				form.submit();
				
				//Clean up
				document.body.removeChild(form);
			}
			
			this.mail.prepareEmail = prepareEmail;
        };
    }());
    
    /**
    *@namespace For the hc videoplayer
    *@name hc.Video
    */
    hc.Video = function(){
        var StringTools = hc.Utils.StringTools;
        /**
         *@inner
         */
        var VideoMarkup =
            //'<!-- Using the Video for Everybody Embed Code http://camendesign.com/code/video_for_everybody -->' + '\n' +
            '<video id="hcVideo" class="video-js vjs-default-skin" width="{{videoWidth}}" height="{{videoHeight}}" poster="{{posterImage}}" controls preload>' + '\n' +
            '    <source src=\"{{videoURL_ogg}}\" type="video/ogg" />' + '\n' +
			'    <source src=\"{{videoURL_mp4}}\" type="video/mp4" />' + '\n' +
            '</video>';
            
        return new function(){
            /**
             *create VideoJS player based on url.
             *@param {string} url the url of the video.
             *@param {object} options to pass to video
             *@example var vidOpts = {
             *    videoWidth: "",
             *    videoHeight:"",
             *    videoURL_webm: "",
             *    videoURL_mp4: "",
             *    videoURL_ogg: "",
             *    posterImage: ""
             * };
             */
            var createVideoPlayer = this.createVideoPlayer = function(options){
                
                var defaultOptions = {
                    videoWidth: "",
                    videoHeight:"",
                    videoURL_webm: "",
                    videoURL_mp4: "",
                    videoURL_ogg: "",
                    posterImage: ""
                };
                
                options = options || defaultOptions;
				
				var ret = StringTools.quickTemplate(VideoMarkup, options)
				
                return ret;
            }    
        }
    }();
        

}(window))



