/*
*
    Name:      jquery.studio.js
    Purpose:   Primary Plugin for Kevlar Concepts STUDIO
*
*/

(function ($) {

    jQuery.fn.Studio = function (options, i) {
        // This handles multiple elements (like a class selector)
        if (this.length > 1) {
            var a = new Array();
            this.each(
                function (i) {
                    a.push($(this).Studio(options, i));
                });
            return a;
        }
        var opts = $.extend({}, $().Studio.defaults, options);

 
        /* START PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////*/
 
				/// e.g., this.debug();

				this.debug = function () {alert("Studio Alert #2: No operation was performed.");};






        /* END PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////*/



        /* reInit is a flag that you can pass in case you don't
           want to remove everything during the destroy phase. */
        this.Destroy = function (reInit) {
            var container = this;
            var reInit = (reInit != undefined) ? reInit : false;
            $(container).removeData("Studio");
            // this removes the flag so we can re-initialize
        };
 
        this.Update = function (options) {
            opts = null;
            opts = $.extend({}, $().Studio.defaults, options);
            this.Destroy(true);
            return this.Create();
        };
 
        /* iteration will give you the index of the item
           in the selection. If not part of a loop, I'm
           prett sure this will be null. You've been warned. */
        this.Create = function (iteration) {
 
            // this stops double initialization
            if ($(container).data("Studio") == true)
                return this;
 
            // call a function before you do anything
            if (opts.beforeCreateFunction != null && $.isFunction(opts.beforeCreateFunction))
                opts.beforeCreateFunction(targetSection, opts);
 
             // reference to the object you're manipulating. To jQuery it, use $(container).
            var container = this;
            /* Failing that, you could just use 'this' without the var declaration,
               but if you are doing a lot of child looping, you'll be glad to have
               a reference to the target object. Also, performance improvements! */
 
        	/* START WORKFLOW ///////////////////////////////////////////////////////////////////////////////////// */

					// 1. Authenticate domain and retrieve config

					var config = login();
					if (! config ) {

						alert("Studio Alert #4: Login Failed.");
					}

				

					// 2. Load scripts
				
					load_scripts();
							



        	/* END WORKFLOW ////////////////////////////////////////////////////////////////////////////////////////*/
 
            // Set a flag to show that this element has been plugin'd
            $(container).data("Studio", true);
 
            // call a function after you've initialized your plugin
            if (opts.afterCreateFunction != null && $.isFunction(opts.afterCreateFunction))
                opts.afterCreateFunction(targetSection, opts);
 
            return this;
        };
 
        /* START PRIVATE FUNCTIONS */
 
				/// e.g., debug();

        function debug() { alert("Studio Alert #1: No operation was performed.") };
 
				function login() {

					/*
						Authenticates current domain against auth server

					*/

					var domain = window.location.hostname;
			
					return {};
				}; 

				function load_scripts() {

				/*
					Loads all associated javascripts and css

				*/

					$.getScript('http://apps.haverfordpc.com/js/jquery-ui-1.8.11.custom.min.js');
					$.getScript('js/jquery.editable-1.3.3.min.js');
					$.getScript('js/fancybox/jquery.fancybox-1.3.4.pack.js');
					$.getScript('js/fancybox/jquery.easing-1.3.pack.js');
					$.getScript('js/jquery.widget.js');
					$.getScript('js/jquery.contextMenu.js');
					$.getScript('js/fancybox/jquery.fancybox-1.3.4.pack.js');
					$.getScript('js/fancybox/jquery.easing-1.3.pack.js');
					$.getScript('js/jquery.ipweditor-1.2.1.js');
					$.getScript('js/fckeditor/fckeditor.js');
					$.getScript('js/tinymce/jscripts/tiny_mce/tiny_mce_src.js');


	   			if (document.createStyleSheet){
    	      document.createStyleSheet('http://fonts.googleapis.com/css?family=Droid+Sans:regular,bold&v1');
						document.createStyleSheet('http://apps.haverfordpc.com/css/smoothness/jquery-ui-1.8.13.custom.css');
						document.createStyleSheet('css/jquery.contextMenu.css');
						document.createStyleSheet('js/fancybox/jquery.fancybox-1.3.4.css');
      	
					} else {

						$("head").append($("<link rel='stylesheet' href='http://fonts.googleapis.com/css?family=Droid+Sans:regular,bold&v1' type='text/css' media='screen' />"));
						$("head").append($("<link rel='stylesheet' href='http://apps.haverfordpc.com/css/smoothness/jquery-ui-1.8.13.custom.css' type='text/css' media='screen' />"));
						$("head").append($("<link rel='stylesheet' href='css/jquery.contextMenu.css' type='text/css' media='screen' />"));
						$("head").append($("<link rel='stylesheet' href='js/fancybox/jquery.fancybox-1.3.4.css' type='text/css' media='screen' />"));

					}



				}
				

        /* END PRIVATE FUNCTIONS */
 
        return this.Create(i);
    };
 
    // DONT FORGET TO NAME YOUR DEFAULTS WITH THE SAME NAME
    jQuery.fn.Studio.defaults = {
        foo: "bar",
        something: "else",
 
        // Remember: these are function _references_ not function calls
        beforeCreateFunction: null,
        afterCreateFunction: null
    };
})(jQuery);

