/**
 * Samsund Card CMS Flash Module v1.0.0
 *
 * author : 이용욱 (oneweb@vi-nyl.com)
 * update : 2010-04-07
 *
 * ------------------------------------------------ *
 * FlashModuleCore Javascript Guide
 * ------------------------------------------------ *

	coreURL : 플래시 복합 모듈 경로

	embedModule( "모듈고유ID값", "플래시소스경로", 너비, 높이 ) : 플래시 스크립트 생성

	addLink( "자바스크립트삽입" ) : 복합 링크 경로 삽입 / 링크 적용 순서대로 반복

	getObjectID() : 모듈 고유ID값 return

	returnSource() : 플래시 스크립트 return

	log( e ) : 오류 메세지 Alert / fmoduleCore 개체의 useDebug 가 false 이면 오류 메세지 무시


	예제)

		var f = new fmoduleCore();
		f.embedModule( "CMS_FlashModule", "./test_modules/module1.swf", 230, 145 );
		try
		{
			f.addLink( "goTabPage('M001');" );
			f.addLink( "goTabPageWithParam('M002','aaa=bbb&ccc=ddd');" );
			f.addLink( "goMultiTabPage('M001,M002');" );
		}
		catch (e)
		{
			f.log( e );
		}
		embedModules[ f.getObjectID() ] = f;
		document.write( f.returnSource() );


 * ------------------------------------------------ *
 */

var coreURL = "/flash/fmodulecore.swf"; // 실서버 적용시 수정 필수!!
var embedModules = {};
var embedCount = 0;

function embedFlashModuleURL( source_url, width, height, linkArr )
{
	var f = new fmoduleCore();
	f.embedModule( "CMS_FlashModule_" + embedCount, source_url, width, height );

	for( var i = 0; i < linkArr.length; i ++ )
	{
		f.addLink( linkArr[i] );
	}

	embedModules[ f.getObjectID() ] = f;
	embedCount ++;

	document.write( f.returnSource() );
}

function fn_FlashModuleLink( objectID, index )
{
	if ( ! embedModules[ objectID ] )
	{
		alert( "모듈을 찾을 수 없습니다" );
		return;
	}

	embedModules[ objectID ].executeLink( index );
}

var fmoduleCore = function() {
	var attributesObj = {},
		paramsObj = { allowScriptAccess:"always", allowFullscreen:"false", wmode:"transparent" },
		optionsObj = {},
		linkObj = [],
		linkCount = 0,
		width = 230,
		height = 145,
		objectID = "CMS_FlashModule",
		linkFunction = "fn_FlashModuleLink";
		sourceURL = "",
		embedObj = "",
		useDebug = true;

	function urlEncodeIfNecessary( s ) {
		var regex = /[\\\"<>\.;]/;
		var hasBadChars = regex.exec(s) != null;
		return hasBadChars ? encodeURIComponent(s) : s;
	}

	function log( e )
	{
		if ( useDebug == true )
		{
			alert( e );
		}
	}

	function addLink( url ) {
		linkObj[ linkCount ] = url;
		linkCount ++;
	}

	function createSource() {
		if ( sourceURL == "" ) {
			log( '소스경로가 설정되지 않았습니다!' );
			return;
		}

		attributesObj.id = objectID;
		optionsObj.sourceURL = escape( sourceURL );
		optionsObj.funcLink = linkFunction;
		optionsObj.linkCount = linkCount;

		var prop = '';
		var flashVars = '';
		var moduleSource = '';

		for( prop in optionsObj )
		{
			var arg = optionsObj[prop];

			if ( flashVars == "" )
			{
				flashVars = prop+"="+arg;
			}
			else
			{
				flashVars = flashVars+"&"+prop+"="+arg;
			}
		}

		attributesObj["width"] = width;
		attributesObj["height"] = height;

		paramsObj["quality"] = 'high';
		paramsObj["flashvars"] = flashVars;

		var protocol = ( location.protocol == 'https:' ) ? 'https://' : 'http://';

		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
			attributesObj["type"] = 'application/x-shockwave-flash';
			attributesObj["pluginspage"] = protocol + 'www.adobe.com/go/getflashplayer';
			attributesObj["name"] = attributesObj["id"];

			paramsObj["src"] = coreURL;

			moduleSource = '<embed ';
			for( prop in attributesObj ) { moduleSource += prop + '="' + attributesObj[prop] + '" '; }
			for( prop in paramsObj ) { moduleSource += prop + '="' + paramsObj[prop] + '" '; }
			moduleSource += '/>';
		} else {
			attributesObj["classid"] = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
			attributesObj["codebase"] = protocol + 'fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0';

			paramsObj["movie"] = coreURL;

			moduleSource = '<object ';
			for( prop in attributesObj ) { moduleSource += prop + '="' + attributesObj[prop] + '" '; }
			moduleSource += '>';
			for( prop in paramsObj ) { moduleSource += '<param name="' + prop + '" value="' + paramsObj[prop] + '"/>'; }
			moduleSource += '</object>';
		}

		embedObj = moduleSource;

		return moduleSource;
	}

	return {
		embedModule: function ( id, url, w, h )
		{
			objectID = id;
			sourceURL = url;
			width = w;
			height = h;
			return createSource();
		},
		addLink: function ( url )
		{
			addLink( url );
			createSource();
		},
		returnSource: function ()
		{
			return embedObj;
		},
		getObjectID: function()
		{
			return objectID;
		},
		executeLink: function ( idx )
		{
			if ( ! linkObj[ idx ] )
			{
				log( "링크 데이타를 찾을 수 없습니다" );
				return;
			}

			eval( linkObj[ idx ] );
		}
	};
};

