/*
Name:       CardView
Version:    0.0.5 (4. Mai 2011)
Author:     Marc Köster
Support:    koester@stadtwerk.org

License:    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.
*/



/* Constructor */
function View()
{
	/* Closure for this */
	var my = this;
	
	var columns;
	var oldcolumns;
	var cardwith = 230;
	var container;
	var countcards;

	/* Setting option defaults */
	this.defaults =
	{
		containerId:        'cards'            /* Id of the container holding the items */    
	};

	/* Initialize */
	this.init = function(options)
	{
		/* Get a reference to the view buttons and the main container */	
		my.container = document.getElementById(my.defaults.containerId);
		if(my.container !== null)
		{
			my.settings = Content.settings;
			my.countcards = my.settings.max;

			/* set width of cards container */
			my.checkWidth();
			/* Add events */
			my.addResizeListener();
		}
	};
	
	/* adds resize Listener */
	this.addResizeListener = function()
	{
		window.onresize = my.checkWidth;
	};
	
	/* check if the columns change */
	this.checkWidth = function()
	{
		my.oldcolumns = my.columns;
		my.columns = Math.floor(my.windowWidth()/cardwith);
		if(my.columns !== my.oldcolumns && my.columns > 3)
		{
			my.changeCardsWidth(my.columns);
		}
	};
	
	/* changes the width of the cards container */
	this.changeCardsWidth = function(columns)
	{
		cards = my.countcards;
		if(my.countcards < my.settings.projects)
		{
			cards = my.countcards+1;
		}
		else
		{
			cards =  my.settings.projects;
		}
			
		if(cards < columns)
		{
			my.container.style.width = (cards*cardwith) +'px';
		}
		else
		{
			my.container.style.width = (columns*cardwith) +'px';
		}
	};
	
	/* gets the width of the window */
	this.windowWidth = function() 
	{
		if(window.innerWidth) 
		{
			return window.innerWidth-17;
		} 
		else if(document.body && document.body.offsetWidth) 
		{
			return document.body.offsetWidth;
		} 
		else 
		{
			return 0;
		}
	};
	
	/* adds cards */ 
	this.addCards = function()
	{
		this.countcards += this.settings.max;
		this.changeCardsWidth(this.columns);
	};
}

/* Create global Views instance when the DOM structure has been loaded */
var Views;
domReady(function()
{
	Views = new View();
	Views.init();
});

