/*Name:       newsBoxVersion:    0.0.2 (Januar 8 2010)Company:    stadt.werk	Author:     Finn RudolphSupport:	rudolph@stadtwerk.orgLicence:    This code is licensed under a Creative Commons             Attribution-Noncommercial 3.0 Unported License             (http://creativecommons.org/licenses/by-nc/3.0/).            You are free:                + to Share - to copy, distribute and transmit the work                + to Remix - to adapt the work            Under the following conditions:                + Attribution. You must attribute the work in the manner specified by the author or licensor                   (but not in any way that suggests that they endorse you or your use of the work).                 + Noncommercial. You may not use this work for commercial purposes.             + For any reuse or distribution, you must make clear to others the license terms of this work.            + Any of the above conditions can be waived if you get permission from the copyright holder.            + Nothing in this license impairs or restricts the author's moral rights.*/


/* newsBox constructor */
function newsBox()
{
	/* Closure for this */
	var my = this;

	/* Initiate expandBox */
	this.init = function(boxId)
	{
		this.boxId = boxId;
		this.minHeight = 80.0;

		/* Try to get box element */
		var boxExists = document.getElementById(my.boxId);
		if(boxExists)
		{
			this.box = boxExists;						/* Store url in the box object */			my.box.url = my.getSecondAnchor(my.box);			/* Link expandBox to that url */			my.addEvent(my.box,'click',function() { document.location = my.box.url; });
			
			/* Add resize event and run scaler onload */
			my.addResizeEvent();
			my.Scaler.start();
		}
	};		/* Returns href attribute of the second anchor element inside the newsBox */	this.getSecondAnchor = function(element)	{		anchors = element.getElementsByTagName('a');		if(anchors.length > 0)		{			var url = anchors[1].getAttribute('href');			return url;		}	};
	
	/* Set scale in relation to the viewport height */
	this.Scaler =
	{
		start: function()
		{
			var viewport = my.Scaler.getViewport();
			my.Scaler.setScale(viewport[1]);
		},
		
		setScale: function(viewportHeight)
		{
			var classname = 'newsBox';
			if(viewportHeight < 600)
			{
				classname = 'newsBox small';
				my.minHeight = 35.0;
			}
			else{
				my.minHeight = 80.0;
			}
			my.box.setAttribute('class',classname);
			my.box.setAttribute('className',classname);
			my.box.style.height = my.minHeight + 'px';
		},
		
		getViewport: function()
		{
			var viewport = [];
			if (typeof window.innerWidth != 'undefined')
			{
				viewport[0] = window.innerWidth;
				viewport[1] = window.innerHeight;
			}
			else
			{
				viewport[0] = document.documentElement.clientWidth;
				viewport[1] = document.documentElement.clientHeight;
			}
			return viewport;
		}
	};
	
	
	this.addResizeEvent = function()
	{
		var otherFunctions = window.onresize;
		if(typeof window.onresize != 'function')
		{
			window.onresize = function()
			{
				my.Scaler.start();
			};
		}
		else
		{
			window.onresize = function(){
				if (otherFunctions)
				{
					otherFunctions();
				}
				my.Scaler.start();
			};
		}
	};		/* Add events */	this.addEvent = function(obj, type, fn)	{		if(obj.addEventListener)		{			obj.addEventListener(type, fn, false);		}		else if(obj.attachEvent)		{			obj["e"+type+fn] = fn;			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); };			obj.attachEvent( "on"+type, obj[type+fn] );		}	};
}

/* Create newsBox instance when the DOM structure has been loaded */
domReady(function()
{
	newsBox = new newsBox();
	newsBox.init('newsBox');
});
