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("Fönstret som skulle öppnas blockerades! Ändra inställningar för svt.se i din popup-blockerare.");
		}
	
    },
	
	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 
		{

/*
			if (document.all) {
		    	offW = document.body.offsetWidth;
		    	offH = document.body.offsetHeight;
		    	window.resizeTo(fixedW, fixedH);
		    	diffW = document.body.offsetWidth  - offW;
		    	diffH = document.body.offsetHeight - offH;
		    	w = fixedW - diffW;
		    	h = fixedH - diffH;
		    	ieDiffWidth  = w - offW;
		    	ieDiffHeight = h - offH;
		    	window.resizeTo(w, h);
		  	}
*/
		}
		
	/*	
		var myWidth = 0;
		if( typeof( window.outerWidth ) == 'number' )
		{
			//Non-IE
			myWidth = window.outerWidth;
		}
		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;
	*/
	},
	
	
	setViewportSize: function(width,height)
	{
		var size = Browser.getWindowSize();
		//alert("W:" + size.width +" H:" + size.height);
	}
	
	
};


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

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

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


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

	},
	
	openPlayNews: function () {
		window.location = 'http://svtplay.se/t/103261/rapport';	
	},

	openPlayWeather: function() {
		Browser.openWindow('http://svt.se/svt/road/Classic/shared/mediacenter/index.jsp?&d=33400&a=362475&lid=puff_678875&lpos=extra_0','largevideoplayer',790,600,'scrolling=no,resizable=no,status=yes')
	},
	
	minimizeWindow: function() {
		this.maximizeSaveWidth = Browser.getViewportWidth();
		this.maximizeSaveHeight = Browser.getViewportHeight();

		window.resizeTo(this.minimizeDefaultWidth,this.minimizeDefaultHeight);

		this.sizeDiffWidth = this.minimizeDefaultWidth - Browser.getViewportWidth();
		this.sizeDiffHeight = this.minimizeDefaultHeight - Browser.getViewportHeight();
	},

	maximizeWindow: function() {
		window.resizeTo( Math.max( (this.maximizeSaveWidth + this.sizeDiffWidth) , this.maximizeDefaultWidth ),
						 Math.max( (this.maximizeSaveHeight + this.sizeDiffHeight) , this.maximizeDefaultHeight )
						);
	},
	
	onStageResize: function(width,height) {
		this.stageWidth = width;
		this.stageHeight = height;
	},

	onDisplayResize: function(width,height) {
		
		var currentHeight = Browser.getViewportHeight();
		var displayHeight = height;
		var newHeight = "100%";	

		if(currentHeight == displayHeight) return;

		if (currentHeight > displayHeight)
		{
			newHeight = "100%";		
		}
		else
		{
			newHeight = displayHeight + "px";	
		}		

		this.setDivHeight(newHeight);
	},

	setDivHeight: function (val)
	{
		var elm = document.getElementById(this.contentId);
		
		if(val != "NaNpx")
		{
			if (elm.style.height != val)
			{
				elm.style.height = 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)
	                Playrapport.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(Playrapport.preventDefault())
		{
	        if (event.preventDefault)
			{
				event.preventDefault();
			}
			
			event.returnValue = false;
	
		}		
		
	},
	
	handleWheelScroll: function (delta)
	{
		var elm = document[this.contentId].externalMouseWheelEvent( delta );
	},
	
	preventDefault : function()
	{
		return document[this.contentId].isFlashScroll();
	}
	
	
});

var Playrapport = new PlayrapportClass();


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() { console.log.apply(console, arguments); }
