

var Browser = {

    availWidth: screen.availWidth,
    availHeight: screen.availHeight,
    upWin: null,
    
	type: {
		IE:     !!(window.attachEvent && !window.opera),
		Opera:  !!window.opera,
		WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
		Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
				MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
	},
        
    trim: function(str) {

		if (typeof str != "string") {
			return str;
		}

		var ret = str.replace(/^\s*([\S\s]*)$/,'$1');
		ret = str.replace(/\s+$/,'');
		ret = str.replace(/\s\s+/g,' ');

		return ret;
    },
    
	trimUScore: function (str) {
		var ret = Browser.trim(str);
		ret = ret.replace(/\s/g,'_');
		return ret;
	},

    openWindow: function(url, name, w_width, w_height, args, placeWindow) {

		name = Browser.trim(Browser.trimUScore(name));
		args = Browser.trim(args);


		if(args !== ''){
			args = args + ',';
		}
		
		if (!Browser.upWin || Browser.upWin.closed) {
			var center_of_screen_X = Math.floor(Browser.availWidth/2)-Math.floor(w_width/2);
			var center_of_screen_Y = Math.floor(Browser.availHeight/2)-Math.floor(w_height/2);
			placeWindow = (Browser.type.Opera) ? "" : ",left=" + center_of_screen_X + ",top=" + center_of_screen_Y;
		}
		
		Browser.upWin = window.open(url,name, args + 'width='+w_width+',height='+w_height + placeWindow);

		if(Browser.upWin){
			Browser.upWin.focus();
		} else {
			alert("pop up blocked");
		}
	
    },
	
	getViewportHeight: function()
	{
		var myHeight = 0;
		if( typeof( window.innerHeight ) == 'number' )
		{
			//Non-IE
			myHeight = window.innerHeight;
		}
		else if( document.documentElement && document.documentElement.clientHeight )
		{
			//IE 6+ in 'standards compliant mode'
			myHeight = document.documentElement.clientHeight;
		} else if ( document.body && document.body.clientHeight )
		{
			//IE 4 compatible
			myHeight = document.body.clientHeight;
		}
		return myHeight;
	},
	
	getViewportWidth: function()
	{
		var myWidth = 0;
		if( typeof( window.innerWidth ) == 'number' )
		{
			//Non-IE
			myWidth = window.innerWidth;
		}
		else if( document.documentElement && document.documentElement.clientWidth )
		{
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
		} else if ( document.body && document.body.clientWidth )
		{
			//IE 4 compatible
			myWidth = document.body.clientWidth;
		}
		return myWidth;
	},
	
	getWindowSize: function()
	{
		var myHeight = 0;
		if( typeof( window.outerHeight ) == 'number' )
		{
			//Non-IE
			myWidth = window.innerWidth;
		}
		else 
		{

		}

	},
	
	getBrowserHeight: function() 
	{
	  var myHeight = 0;
	  if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	  }
	  return myHeight;
	},
	
	getBrowserWidth: function() 
	{
	  var myWidth = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
	  } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	  } else if( document.body && ( document.body.clientWidth) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	  }
	  return myWidth;
	},
	
	
	/*
	getBrowserHeight: function()
	{
		
		var myHeight = 0;
		if( typeof( window.innerHeight) == 'number') {
			//Non-IE
			myHeight = window.innerHeight;
		} esle if(document.documentElement) {
			myHeight = document.documentElement.clientHeight;
		} else if{document.body) {
			myHeight = document.body.clientHeight;
		}
		alert("height: " + browserHeight);
		return myHeight;
	},
	*/
	getBrowserScrollOffset: function() {
		var pageYOffset = 0;
		if( typeof(window.pageYOffset) == 'number'){
			//Non-IE
			pageYOffset = window.pageYOffset;
			//alert("Non-IE " + pageYOffset);
		}
		else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE 6+ in 'standards compliant mode'
			pageYOffset = document.documentElement.scrollTop;
			//alert("IE 6+ " + pageYOffset);
		}
		else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//IE 4 compatible
			pageYOffset = document.body.scrollTop;
		}
		return pageYOffset;
	},
	
	setViewportSize: function(width,height)
	{
		var size = Browser.getWindowSize();
		//alert("W:" + size.width +" H:" + size.height);
	}
	
	
};


var FlashBrowserClass = Class.create({
	initialize: function(){
		// this.param = value;

		this.sizeDiffWidth = 0;
		this.sizeDiffHeight = 0;

		this.minimizeDefaultWidth = 400;
		this.minimizeDefaultHeight = 600;

//		this.minimizeDefaultWidth = 400;
//		this.minimizeDefaultHeight = 600;
				
		this.maximizeDefaultWidth = 800;
		this.maximizeDefaultHeight = 600;


		this.maximizeSaveWidth = 0;
		this.maximizeSaveHeight = 0;
		
		this.stageWidth = 300;
		this.stageHeight = 300;
		this.contentId = "flashcontent";
		
		
		hookEvent(window,'mousewheel', this.mouseWheelProxy);
		hookEvent(window,'resize', this.resizeProxy);
		hookEvent(window,'load', this.resizeProxy);

	},	
	onStageResize: function(width,height) {
		if(this.stageWidth != width || this.stageHeight != height)
		{
			this.stageWidth = width;
			this.stageHeight = height;
			this.onDisplayResize(Browser.getViewportWidth(), Browser.getViewportHeight());
		}	
	},

	onDisplayResize: function(width,height) {
//		alert(width);
//		var curretHeight = Browser.getViewportHeight() - 24 ;
//		var currentWidth = Browser.getViewportWidth();
		var currentHeight = this.stageHeight;
//		var currentWidth = this.stageHeight;
		var currentWidth = Browser.getViewportWidth();
		var displayHeight = height;
		var displayWidth = 1000;
		

		var newHeight = displayHeight;	
		var newWidth = displayWidth;	



		if(currentHeight != displayHeight)
		{
			if (currentHeight > displayHeight)
			{
				newHeight = currentHeight + "px";		
			}
			else
			{
				newHeight = displayHeight + "px";	
			}		
	
			this.setDivHeight(newHeight);
		}
		
		if(currentWidth != displayWidth)
		{

			if (currentWidth > displayWidth)
			{
				newWidth = "100%";		
			}
			else
			{
//				alert(width + "::" + displayWidth + "::" + currentWidth )
//				newWidth = displayWidth + "px";	
				newWidth = displayWidth + "px";	
			}
			this.setDivWidth(newWidth);
		}

	},

	setDivSize: function (w,h)
	{
		var elm = document.getElementById(this.contentId);
		
		if(val != "NaNpx")
		{
			if (elm.style.height != val)
			{
				elm.style.height = val;
			}
			
		}
	},

	setDivHeight: function (val)
	{
		var elm = document.getElementById(this.contentId);
		
		if(val != "NaNpx")
		{
			if (elm.style.height != val)
			{
				elm.style.height = val;
			}
		}
	},
	
	setDivWidth: function (val)
	{
		var elm = document.getElementById(this.contentId);
		
		if(val != "NaNpx")
		{
			if (elm.style.width != val)
			{
				elm.style.width = val;
			}
		}
	},
	
	
	/**
	 * Proxy function to call with the right scope
	 */
	mouseWheelProxy: function(event){
	        var delta = 0;
	        if (!event)
			{
                event = window.event;
			}
	        if (event.wheelDelta)
			{
				/* IE/Opera. */
	            delta = event.wheelDelta/120;
	            /* In Opera 9, delta differs in sign as compared to IE. */
	            if (window.opera)
				{
                    delta = -delta;
				}
	        } 
			else if (event.detail)
			{ 
				/** Mozilla case. */
	            delta = -event.detail/3;
	        }


	        /** If delta is nonzero, handle it.
	         * Basically, delta is now positive if wheel was scrolled up,
	         * and negative, if wheel was scrolled down.
	         */
	        if (delta)
	                FlashBrowser.handleWheelScroll(delta);
	        /** Prevent default actions caused by mouse wheel.
	         * That might be ugly, but we handle scrolls somehow
	         * anyway, so don't bother here..
	         */
	        if (event.preventDefault)
	                event.preventDefault();
		event.returnValue = false;
		
		
		
	},
	
	resizeProxy: function(event){
                FlashBrowser.handleResize();
	},
	
	handleWheelScroll: function (delta)
	{
		var elm = document[this.contentId].externalMouseWheelEvent( delta );
	},
	
	handleResize: function ()
	{
		FlashBrowser.onDisplayResize(Browser.getViewportWidth(), Browser.getViewportHeight());
		// var elm = document[this.contentId].externalResizeEvent( Browser.getViewportWidth(), Browser.getViewportHeight()  );
	}
	
	
});

var FlashBrowser = new FlashBrowserClass();


function hookEvent(element, eventName, callback)
{
	if(typeof(element) == "string")
	{
		element = document.getElementById(element);
	}
	
	if(element == null)
	{
		return;
	}
	
	if(element.addEventListener)
	{
		if(eventName == 'mousewheel')
		{
			element.addEventListener('DOMMouseScroll', callback, false);
		}
		
		element.addEventListener(eventName, callback, false);
	}
	else if(element.attachEvent)
    {
    	element.attachEvent("on" + eventName, callback);
	}
}

function unhookEvent(element, eventName, callback)
{
	if(typeof(element) == "string")
	{
		element = document.getElementById(element);
	}
	
	if(element == null)
	{
		return;
	}
	
	if(element.removeEventListener)
	{
		if(eventName == 'mousewheel')
		{
			element.removeEventListener('DOMMouseScroll',callback, false);  
		}
		
		element.removeEventListener(eventName, callback, false);
  	}
	else if(element.detachEvent)
	{
		element.detachEvent("on" + eventName, callback);
	}
}


function trace(str) {
		var elm = document.getElementById("out");

	
	 elm.innerHTML = str; 

}