/**
 * Visszatér az adott swf objektummal
 * @param {Object} movie_name
 */
ml.swf.get = function(movie_name) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movie_name] : document[movie_name];
};

ml.swf.embed = function(src, id, width, height, flashvars) {
	ml.swf.embed_via_swfobject(src, id, width, height, flashvars);
//	ml.swf.embed_via_acflruncontent(src, id, width, height);
};

ml.swf.embed_via_acflruncontent = function(src, id, width, height) {
	AC_FL_RunContent(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
		'width', width,
		'height', height,
		'src', src,
		'quality', 'high',
		'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
		'align', 'middle',
		'play', 'true',
		'loop', 'true',
		'scale', 'showall',
		'wmode', 'window',
		'devicefont', 'false',
		'id', 'ml_messagewall',
		'bgcolor', '#f6f6f6',
		'name', 'ml_messagewall',
		'menu', 'true',
		'allowFullScreen', 'false',
		'allowScriptAccess','always',
		'movie', 'ml_messagewall',
		'salign', '',
		'FlashVars', 'modul_id=Messagewall modul inicializálva!'
	); //end AC code
};

ml.swf.embed_via_swfobject = function(src, id, width, height, flashvars) {
	var attributes = {
		id: id,
		name: id,
		wmode:  'transparent',
		style: 'outline: none;'
	};
	if ( typeof(flashvars)=="undefined") flashvars = {};
	flashvars.modul_id = id;
	var params = {
		wmode:  'transparent'
	};
// swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes, callbackFn)
	swfobject.embedSWF(src, id, width, height, "9.0.0", "/swf/expressinstall.swf", flashvars, params, attributes);
};

ml.swf.cachedata = {};

/**
 * az swf-nek átadandó "initscript" kódot cache-eli
 */
ml.swf.cacheRun = function(id, code) {
	if ( !ml.swf.cachedata[id] ) ml.swf.cachedata[id] = {};
	if ( !ml.swf.cachedata[id].Code ) ml.swf.cachedata[id].Code = [];
	ml.swf.cachedata[id].isCodeLoaded = true;
	ml.swf.cachedata[id].Code[ ml.swf.cachedata[id].Code.length ] = code; 
	ml.swf.cachedata[id].state = 'loaded';
	ml.swf.sendToSwf(id);
};

/**
 * az swf-nek átadandó adatokat cache-eli
 */
ml.swf.cacheStore = function(id, key, val) {
	ml.swf.cachedata[id][key] = val;
};


/**
 * Elkuldi a XML-t a flashnek, ha mar betoltodott es az adat is megerkezett
 */
ml.swf.sendToSwf = function(id) {
	if ( !ml.swf.cachedata[id] ) return;
	if ( !ml.swf.cachedata[id].Code ) return;
	
	if ( ml.swf.cachedata[id].isCodeLoaded && ml.swf.cachedata[id].isSwfLoaded ) {
		try {
//			console.log(id);
//			console.log( typeof(ml.swf.get(id).parseXML) );
			// Lehet még nem töltődött be!
			ml.swf.cachedata[id].state = 'beforesend';
//			console.log(ml.swf.cachedata[id].Code);				
			var i;
			for(i=0; i<ml.swf.cachedata[id].Code.length; i++) {
				try {
					eval(ml.swf.cachedata[id].Code[i]);
				}catch(err) {
					console.log('Catched exception:');
					console.log(err);
				};
			};
			ml.swf.cachedata[id].Code = [];
			ml.swf.cachedata[id].state = 'aftersend';
		}catch (e) {
			alert(e);
		}
	}
};

ml.swf.handleWheelAttach = function(id) {
	var app = ml.swf.get('swf_'+id);
	if(app.attachEvent) app.attachEvent("onmousewheel", ml.swf.handleWheel);
	else app.addEventListener("DOMMouseScroll", ml.swf.handleWheel, false);
};



ml.swf.handleWheel = function(e) {
	var app;
	var event_x;
	var event_y;
	var delta;

	if(e.srcElement)
	{
		app = e.srcElement;
		event_x = e.offsetX;
		event_y = e.offsetY;
		delta = e.wheelDelta / 120;
	}
	else
	{
		app = e.target;
		var offset = j(app).offset();
//		event_x = e.layerX - app.offsetLeft;
//		event_y = e.layerY - app.offsetTop;
		event_x = e.pageX - offset.left;
		event_y = e.pageY - offset.top;
		delta = -e.detail / 3;
	}
	
	
//	delta = (e.wheelDelta)?e.wheelDelta*((!!window.opera)?-1:1) : e.detail*-1;

	var o = {x: event_x, y: event_y, delta: delta, ctrlKey: e.ctrlKey, altKey: e.altKey, shiftKey: e.shiftKey};
//	console.log(e);
//	console.log('x,y: '+event_x+','+event_y);
	try {
   	app.handleWheel(o);
   }catch(e) {
		console.log(e);
	}
	
	var e = e ? e : window.event;
	if(e.stopPropagation)	e.stopPropagation();
	if(e.preventDefault)	e.preventDefault();
	e.cancelBubble = true;
	e.cancel = true;
	e.returnValue = false;
	return false;
	
};


function onSwfLoaded(id){
	if ( !ml.swf.cachedata ) ml.swf.cachedata = {}; 
	if ( !ml.swf.cachedata[id] ) ml.swf.cachedata[id] = {};
	ml.swf.cachedata[id].isSwfLoaded = true;
	ml.swf.sendToSwf(id);
//	console.log('onSwfLoaded: '+id);
};

