function include(script, id) {
    document.write('<script type=\"text\/javascript\" language=\"javascript" src=\"'+script+'\"></script>');
}

/**
 *  Object that manage javascript actions in project
 */
Application = function() {
    var _this = this;
    var _tinyMCE_Defaults = {};

    /**
     *  Include product type cast frames js managment 
     *  on page. 
     *  This is used on product family page 
     *  (controller "product family" action "view")
     *  
     *  @return  void
     */
    this.init_cast_frames = function () {
        include("\/js\/cast_frames.js");
        $(document).ready(function() {
            _this.castFrames = new castFramesManager();
        });
    } /*init_cast_frames function*/
    
    
    
    /**
     *  Include tiny mce script on page.
     *  
     *  @access public
     */
    this.include_tiny_mce = function() {
    	_tinyMCE_Defaults = {
   			script_url : '/js/tiny_mce/tiny_mce.js',
   			convert_urls : false,

			// General options
			theme : "advanced",
			plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist,images",
			// Theme options
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo",
			theme_advanced_buttons3 : "link,unlink,anchor,image,images,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons4 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons5 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
	
			// Example content CSS (should be your site CSS)
			content_css : "css/content.css",
	
			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",
			plugin_insertdate_dateFormat : "%m/%d/%Y",

			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			},
    	 
			theme_advanced_resizing_max_width : '300px',
			width : '300px'
    	}

    	include("\/js\/tiny_mce\/jquery.tinymce.js");
    	$(document).ready(function() {
    		$("textarea.tinymce").tinymce(_tinyMCE_Defaults);
    	});
    } /*include_tiny_mce*/
    
    /**
     *  Include swf upload routings in 
     *  page actions.
     *  
     *  @access public
     *  @retrun void
     */
    this.include_swf_upload = function() {
    	include('\/js\/swfupload\/swfupload.js');
    	include('\/js\/swfupload\/handlers.js');
    } /*include_swf_uplad function*/
    
    /**
     *  Include javascript objets to manage editable 
     *  images.
     *  You have to include this object when ext_html helper
     *  editableimage method is used.
     *  
     *  @access public
     *  @retrun void
     */
    this.include_images_manager = function() {
    	include('\/js\/swfupload\/images.manager.js');
    	$(document).ready(function() {
    		_this.imagesManager = new ImagesManager();
    	});
    } /*include_images_manager function*/
    
    /**
     * 
     */
    this.init_ui_sortable = function (container) {
    	include('\/js\/jquery-ui-1.7.2.custom.min.js');
    	include('\/js\/properties_managers.js');
        $(document).ready(function() {
        	_this.typePropList = new TPropertyListManager(container, null);
        	//_this.propList = new TPropertyListManager();
            $("#" + container).sortable({
                beforeStop: function(event, ui) {
            	    if (typeof(_this.typePropList.beforeStop) == 'function') {
		        _this.typePropList.beforeStop();
                    }
                }
            }).disableSelection();;
        })
    	
    }
    
     /** 
      *  Inititzlize product's table on view product type page 
      *  (this is alt product type view for previous one)  
      *  
      *  @access public;
      *  @return void
      */
     this.init_product_table = function() {
    	 ProductTableManager = function(table_id) {
    	     var _this = this;
    	     var $object = $('#' + table_id); 
    	     var _firstShow = true;
    	     $object.find('tr').mouseover(_on_row_hover);
    		 
    	     
    	     /**
    	      *  Row hover handler. 
    	      *  (Involves product picture change)
    	      *  
    	      *  @access private
    	      */
    	     function _on_row_hover(event) {
    	    	 var row = this;
    	    	 var product_id = $(row).attr('rel');
    	    	 if (product_id) {  
    	    		 _this.hightlightRow(product_id);
    	    	 }
    	     } /*_on_row_hover function*/
    	     
    		 /**
    		  *  Show product table
    		  *  @access public; 
    		  */
    		 this.show  = function() {
    			 $object.show();
    			 if (_firstShow) {
					 var scrollable = $('#scrollable');
					 if (scrollable.height() < parseInt(scrollable.css('max-height'))) {
					 	 scrollable.css({'overflow-y' : 'hidden'});
				 	 } else {
				 		 $('#scrollTableHeader').append('<th style="width:15px;"></th>');
				 	 }
    				 var $headerSet = $('#scrollTableHeader').children();
    				 var $blueprintSet = $('#scrollTableBlueprint').children();
    				 var $firstRowSet = $('#scrollTableBlueprint').next().children();
    				 $blueprintSet.each(function() {
    					 var $this = $(this);
    					 var index  = $blueprintSet.index($this);
    					 $($headerSet[index]).width($this.width());
    					 $($firstRowSet[index]).width($this.width());
    				 });
    				 $('#scrollTableBlueprint').remove();
    			 }
    			 _firstShow = false;
    		 } /*show function*/
    		 
    		 /**
    		  *  Hide product table
    		  *  @acces public
    		  */
    		 this.hide = function() {
    			 $object.hide();
    		 } /*hide function*/
    		 
    		 this.hightlightRow = function(product_id) {
    			 if (product_id == null) {
    				 product_id = $('.prodRow').attr('rel');
    			 }
    	    	 $('.prodimg').hide();
    	    	 $('.prodRow').removeClass('selected');
    	    	 var image_id = "prodImage_" + product_id;
    	    	 var row_id = "prodRow_" + product_id;
	    	     $('#' + image_id).show();
	    	     $('#' + row_id).addClass('selected');
    		 }
    		 
    	 }
         $(document).ready(function() {
             _this.productTabel = new ProductTableManager('productsTable');
             _this.productTabel.hightlightRow();
         });
    	 
     } /*init_product_table fucntion*/
     
     /**
      *  Make functions for news item sliding change performance. 
      */
     this.init_latest_news_action = function() {
    	 include("\/js\/news_slider.js");
    	 
    	 $(document).ready(function() {
    	     _this.newsSlider = new TNewsSlider();
    	 });
    	 
     } /*init_latest_news_action function*/
     
     /**
      *  Files manager 
      */
     this.init_file_manager = function() {
    	 include('\/js\/jquery-ui-1.7.2.custom.min.js');
    	 include('\/js\/filechooser\/filechooser.js');
    	 include('\/js\/file_manager.js');
    	 //include('\/css\/filechooser.css');
    	 $(document).ready(function() {
    		 _this.Files = new FileManager('filesContainer');
             $("#filesContainer").sortable({
                 beforeStop: function(event, ui) {
             	    //_this.typePropList.add(ui);
                 }
             }).disableSelection();;
    		 
    	 });
    	 
     } /*init_file_manager function*/
     
     this.include_agency_search = function() {
    	 include('\/js\/agency_search.js');
    	 $(document).ready(function() {
             _this.Agencies = new AgensiesManager('AgenciesState', 'AgenciesCounty'); 
    	 });
     }
     
     this.init_tool_tips = function() {
    	 include('\/js\/jquery.tooltip.js');
     }
     
     /**
      *  Initialize gallery on main page. 
      */
     this.init_gallery = function(galleryContainer, galleryControl) {
    	 include('\/js\/gallery.js');
    	 $(document).ready(function() {
    	     _this.gallery = new TGallery(galleryContainer, galleryControl);
    	 });
    	 
     } /*init_gallery function*/
     
     /**
      *  
      */
     this.showModal = function($object) {
    	 $.nyroModalManual({url : '#' + $object.attr('id'),  height: 400, width: 500, type : 'swf'});
     }
} /*Application Class*/

App = new Application();
