//
// AirCampus3 client library.
// 
// requires: prototype.js
//
// @copyright 2008 Business Breakthrough,Inc.
// @create 2008/12/08
//

// ac3 scope
var ac3 = {} ;

// IFrame manager.
var IFrameManager = Class.create() ;
IFrameManager.prototype = {
	// constructor
	initialize: function() {
		this.frameMap    = {} ;   // Mapping logicalName -> frameNo.
		this.frameCount  = 0 ; 
		this.frameStatus = [] ;   // Status of each frame [0 .. frameCount-1] , true:busy/false:available  
	} ,
	
	// Find frame by logicalName
	//
	// @param logicalName Logical frame name
	// @return [divName,iframeName] if found. null if not found.
	findFrame: function(logicalName) {
		if (this.frameMap[logicalName] !== undefined) {
			var frameNo = this.frameMap[logicalName] ;
			var divName = 'frameArea_' + frameNo ;
			var ifName = divName + '_i' ;
			return [divName,ifName] ;
		} else {
			return null ;
		}
	},
	
	// Show IFrame
	showIFrame: function(logicalName) {
		var frm = this.findFrame(logicalName) ;
		if (frm != null) {
			var divName = frm[0] ;
			$(divName).style.visibility="visible";
		}
	} ,
	
	// Hide IFrame
	hideIFrame: function(logicalName) {
		var frm = this.findFrame(logicalName) ;
		if (frm != null) {
			var divName = frm[0] ;
			$(divName).style.visibility="hidden";
		}
	} ,
	
	// Move IFrame
	moveIFrame: function(x,y,w,h,logicalName) {
		var frm = this.findFrame(logicalName) ;
		if (frm != null) {
		    var frameRef=$(frm[0]) ;
    		var frameRefI=$(frm[1]) ;
		    frameRef.style.left=x;
    		frameRef.style.top=y;
    		try {
    			frameRef.width=w ;
    			frameRef.height=h ;
    			if (frameRefI) {
    				frameRefI.width=w ;
    				frameRefI.height=h ;
    			}
    		}
    		catch (e) {
    			alert(e.message) ;
    		}
		}
	} ,
	
	// Load IFrame
	//
	// @param url URL for iframe
	// @param logicalName Logical frame name
	// @return [divName,iframeName]
	loadIFrame: function(url,logicalName) {
		var frm = this.findFrame(logicalName) ;
		if (frm != null) {
			var divName = frm[0] ;
			var ifName = frm[1] ;
			var currentSrc = $(ifName).src ;
			if (currentSrc != url) {
				$(ifName).src = url ;			
			} 
		} else {
			// search available frame
			var frameNo = -1 ;
			for (var i = 0 ; i < this.frameCount ; i++) {
				if (!this.frameStatus[i]) {
					frameNo = i ;
					break ; 
				}
			}
			if (frameNo < 0) {
				return false ;
			}
			
			// Create new iframe
			this.frameStatus[frameNo] = true ; // busy
			this.frameMap[logicalName] = frameNo ;
			var divName = 'frameArea_' + frameNo ;
			var ifName = divName + '_i' ;
			$(divName).innerHTML = '<iframe id="' + ifName + '" src="' + url + '" frameborder="0"></iframe>'
			$(divName).style.visibility="visible";
			//alert(logicalName + ' -> ' + frameNo + ' / ' + url) ;
			//alert(divName + ',' + ifName) ;			
		}
	} ,
	
	// Release IFrame for recycle.
	//
	// @param logicalName Logical frame name
	//
	releaseIFrame: function(logicalName) {
		var frm = this.findFrame(logicalName) ;
		if (frm != null) {
		    var divName=$(frm[0]) ;
			try {
				$(divName).innerHTML = '' ;
				var frameNo = this.frameMap[logicalName] ;
				this.frameStatus[frameNo] = false ; // available
			}
    		catch (e) {
    			alert(e.message) ;
    		}
		}		
	} ,
	
	// Put n IFrame area.
	//
	// @param n Number of iframe
	putIFrameArea: function(n) {
		if (n == undefined) {
			n = 10 ;
		}
		this.frameCount = n ; 
		this.frameStatus = [] ;		
		for (var i = 0 ; i < n ; i++) {
			var divName = 'frameArea_' + i ;
			var tag = '<div id="' + divName + '" wmode=windowed style="position:absolute;background-color:transparent;border:0px;visibility:hidden;"></div>' + "\n" ;
			document.write(tag) ;
			this.frameStatus.push(false) ;			
		}
	} ,
	
	_tail: null
} ;

// ReFocus AC flash.
// Fix IE+MS IME probrem.
ac3.postFileReference = function(appId) {
	if (Prototype.Browser.IE) {
		window.focus();
		$(appId).focus() ;
	}
} ;

// Critical region message.
ac3.critical = null ;

// onBeforeUnloadハンドラ
ac3._onBeforeUnload = function(e) {
	if (ac3.critical == null) {
		// nothing todo.
		return ;
	}
	e.returnValue = ac3.critical ;
	return ac3.critical ;
} ;

// Regist beforeUnloadEvent Handler
ac3.registBeforeUnload = function () {
	if (Prototype.Browser.WebKit) {
		window.addEventListener('beforeunload',ac3._onBeforeUnload) ;
	} else {
		Event.observe(window,'beforeunload',ac3._onBeforeUnload) ;
	}
} ;

// Begin critical region.
ac3.beginCritical = function(msg) {
	ac3.critical = msg ;
}

// End critical region.
ac3.endCritical = function() {
	ac3.critical = null ;
} ;

	// タイトルの変更テスト
	function focusWindow(val) {
		window.focus();
		document.title = 'Javascriptでタイトル変更';
		blink();

		return "ac3.js";
	}
	
	function blink() {
		if (!document.all) { return; }
		for (i = 0; i < document.all.length; i++) {
			if (document.title.style.visibility == "visible") {
				document.title.style.visibility = "hidden";
			} else {
				document.title.style.visibility = "visible";
			}
		}
		setTimeout("blink()", 800);
	}
	
	// クッキーの使用テスト
	function getBrowserSetting() {
		if (window.navigator.cookieEnabled) {
			return "クッキーは利用できます。";
		}else {
			return "クッキーは利用できません。";
		}
	}
	
	// 受講環境設定URL
	function getSettingPageUrl() {
		return "http://www.bbt757.com/ac/web/cfgac.asp";
	}
	
