/**
 * @name       SBM
 * @version    2.01
 * @updated    2011/08/24
 * @copyright  Copyright (c) 2011, SOFTBANK MOBILE Corp.
 */



sail_ins = 'SBM';
SBM = new SAILClass();
SBM.useCollection('prototype');
SBM.useCollection('func');
SBM.useCollection('env');
SBM.useCollection('img');
SBM.useCollection('win');
SBM.useCollection('flash');
//SBM.useCollection('date');
SBM.useCollection('dom');
//SBM.useCollection('dd');
SBM.useCollection('ajax');
SBM.include('js/sbm/hub.js');
SBM.include('js/sbm/hub/tree.js');
SBM.include('js/sbm/tpl.js');



function SAILClass () {
	this.version     = '2.00';
	this.global_func = false;
	this.included    = [];
	
	
	
	/**
	 * 
	 */
	this.useGlobalFunc = function () {
		if (this.func) { this.func.globalize(); }
		if (this.dom)  { this.dom.globalize(); }
		
		this.global_func = true;
	};
	
	
	
	/**
	 * @param   {string}  name
	 * @return  {boolean}
	 */
	this.useCollection = function (name) {
		var base = 'js/collections/';
		switch (name.toLowerCase()) {
			case 'prototype': return this.include(base + 'prototype.js');
			case 'func'     : return this.include(base + 'func.js');
			case 'env'      : return this.include(base + 'env.js');
			case 'img'      : return this.include(base + 'img.js');
			case 'win'      : return this.include(base + 'win.js');
			case 'flash'    : return this.include(base + 'flash.js');
			case 'date'     : return this.include(base + 'date.js');
			case 'dom'      : return this.include(base + 'dom.js');
			case 'dd'       : return this.include(base + 'dd.js');
			case 'ajax'     : return this.include(base + 'ajax.js');
		}
	};
	
	
	
	/**
	 * @param  {string}  name
	 * @param  {Object}  [_options]
	 */
	this.useModule = function (name, _options) {
		var name    = name.toLowerCase().split('::');
		var options = (typeof(_options) != 'undefined') ? _options : {};
		
		switch (name[0]) {
			case 'lightbox':
				this.include('js/modules/prototype/prototype.js');
				this.include('js/modules/scriptaculous/effects.js');
				this.include('js/modules/lightbox/lightbox.js');
				this.include('js/modules/lightbox/css/lightbox.css');
				switch (name[1]) {
					case 'product':
						switch (name[2]) {
							case 'colors':
								this.include('js/sbm/functions/lightbox/product/colors.js');
								lightbox_hoverNav = (typeof(options.hoverNav) != 'undefined')
								                  ? options.hoverNav
								                  : false;
								break;
						}
					break;
				}
				break;
			
			case 'jquery':
				this.include('js/modules/jquery/jquery.js');
				break;
			
			case 'product':
				this.include('js/sbm/functions/product/prd3g.js');
				break;
		}
	};
	
	
	
	/**
	 * @param   {string}  ns
	 */
	this.use = function (ns) {
		if (ns != 'plugin.enquete')
			return;
		
		this.plugin = {
			extend: function (props) {
				jQuery(function () { props.ready(); });
				return props;
			}
		};
		
		this.include('js/sbm2/plugin/enquete.js');
	};
	
	
	
	/**
	 * @privare
	 * @param   {string}   file
	 * @param   {boolean}  [_use_common]
	 * @return  {boolean}
	 */
	this.include = function (file, _use_common) {
		var use_common = (typeof(_use_common) != 'undefined') ? _use_common : true;
		var file       = (use_common ? this.getCommonDir() : '') + file;
		
		for (var i = 0, ix = this.included.length; i < ix; i++) {
			if (this.included[i] == file) {
				return false;
			}
		}
		
		if (file.indexOf('.css') >= 0) {
			document.write('<link href="' + file + '" rel="stylesheet" type="text/css" media="all" />');
		}
		else {
			document.write('<script src="' + file + '" type="text/javascript"></script>');
		}
		this.included.push(file);
		
		return true;
	};
	
	
	
	/**
	 * @private
	 * @return  {string}
	 */
	this.getCommonDir = function () {
		var common_find    = 'mb/shared/js/sbm.js';
		var common_replace = 'mb/shared/';
		var common;
		var scripts = document.getElementsByTagName('script');
		
		for (var i = 0, ix = scripts.length; i < ix; i++) {
			if (scripts[i].src && scripts[i].src.indexOf(common_find) >= 0) {
				common = scripts[i].src.split(common_find)[0] + common_replace;
				break;
			}
		}
		
		return common;
	};
}
