var B81SittaImageframe = new Class({
	frameheight:	null,
	framewidth:		null,
	core:			null,
	container:		null,
	mode:			null,
	flashmovie:		null,
	jsImg:			null,
	
	getOptions: function(){
		return {
			minFlashVersion:8,
			logoMaxHeight:49
		};
	},
	
	initialize: function(c, ifc, options){
		this.setOptions(this.getOptions(), options);
		this.core = c;
		this.container = $(ifc);
		
		//getFrameSize
		this.setFramesize();
		
		//setup LogoSize
		this.setLogoSize();

		//check for flash
		if(this.hasFlash()) {
			this.setupFlashFrame();
		} else {
			this.setupJSFrame();
		}
		
	},
	
	hasFlash: function() {
		var versionObj = swfobject.getFlashPlayerVersion();
		if(versionObj.major >= this.options.minFlashVersion) {
			return true;
		} else {
			return false;	
		}
	},
	
	getAssetValue: function() {
		var dimObj = this.core.getElementMeasures(this.container);
		var value;
		if(dimObj.height > 200) {
			value = '_big';
		} else if(dimObj.height > 150) {
			value = '_mid';
		} else {
			value = '_small';
		}
		return value;
	},
	
	setFramesize: function() {
		this.framewith = this.container.offsetWidth;
		this.frameheight = this.container.offsetHeight;
	},
	
	onResize: function() {
		this.setFramesize();
		this.setLogoSize();
		if(!this.hasFlash()) {
			this.resizeJS();
		}
	},
	
	setLogoSize:function() {
		var tgt = $('logo');
		if(!tgt) return;
		var lh = this.core.getPercentValue(this.frameheight, 30);
		if(lh > this.options.logoMaxHeight) {
			lh = this.options.logoMaxHeight;
		}
		tgt.setStyle('height', lh+'px');
	},

	
	resizeJS:function() {
		var oldH = this.jsImg.offsetHeight;
		var oldW = this.jsImg.offsetWidth;
		
		var newH = this.core.getPercentValue(this.frameheight, 120);
		
		var fact = (newH/oldH);
		var newW = oldW*fact;
		
		this.jsImg.setStyle('height', newH+'px');
		this.jsImg.setStyle('width', newW+'px');
		this.jsImg.setStyle('margin-top', '-'+(newH/8)+'px');
		this.jsImg.setStyle('margin-right', ''+(newW/6)+'px');
	},
	
	setupFlashFrame: function() {
		var swf = this.core.options.templatepath+'aditionals/imageframe/flash/imageframe.swf';
		
		var flashcont = new Element('div', { 'id': 'flashcont'} );
		flashcont.injectInside(this.container);
		
		var flashvars = new Object();
			flashvars.assetpath = this.core.options.templatepath+'aditionals/imageframe/assets/';
			flashvars.assetvalue = this.getAssetValue();
			flashvars.implementMenu = this.core.options.flashmenu;
			flashvars.animationSWF = this.core.options.templatepath+'aditionals/imageframe/flash/animationsframe.swf';
			flashvars.basicsSWF = this.core.options.templatepath+'aditionals/imageframe/flash/basicsframe.swf';
			flashvars.menuSWF = this.core.options.templatepath+'aditionals/imageframe/flash/menuframe.swf';
			flashvars.menuXML = this.core.options.templatepath+'aditionals/imageframe/flash/menu.xml';
			flashvars.menujscall = 'callB81Ajax';
			
		var params = new Object();
		 	params.menu = "false";
			params.wmode = "transparent";
			
		var attributes = new Object();
		  	attributes.id = "ifFlash";
		  	attributes.name = "ifFlash";
			
		swfobject.embedSWF(swf, "flashcont", "100%", "100%", "8.0.0","", flashvars, params, attributes, this.onFlashInit.bind(this));
	},
	
	onFlashInit: function() {
		$('logo').setStyle('display', 'none');
		$('imageframe_mask').setStyle('display', 'none');
		
		if(this.core.options.flashmenu) {
			$('top_outer').setStyle('height', '30px');
			$('topmenucontainer').remove();
		}
	},
	
	setupJSFrame: function(obj) {
		var jsImgs = new Asset.images([this.core.options.templatepath+'aditionals/imageframe/assets/noscript/default_new.jpg'], {
										 onComplete: function() {
											this.resizeJS();
										 }.bind(this)
									 });
		this.jsImg = jsImgs[0];
		this.jsImg.injectInside(this.container);
	},
	
	thisMovie: function(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName]
		}
		else {
			return document[movieName]
		}
	},

	
	//external handling
	onAjaxRequestStart: function() {
		if(this.hasFlash()) {
			try {
				this.thisMovie('ifFlash').onAjaxRequestStart();
			} catch(e) {
				
			}
		}
	},
	
	onAjaxComplete: function(activeId) {
		if(this.hasFlash()) {
			try {
				this.thisMovie('ifFlash').onAjaxComplete(activeId);
			} catch(e) {
			}
		}
	}
	
});
B81SittaImageframe.implement(new Options);
B81SittaImageframe.implement(new Events);
