

/***
 	SelectStars
 
 	Library to replace a set of radioboxes that are used to let
 	the user choose a "star"-rating (of 1 to 5). The constructor
 	takes an ID which contains the radioboxes to replace, as well
 	as a name which a generated hidden input field will have the
 	star value in.
*/

function SelectStars(starcontainer, input_name, labelelement)	{

	this.starcontainer = document.getElementById(starcontainer);
	this.input_name = input_name;
	this.initial_value = this.getInitialValue();
	
	/* Remove old content in starcontainer */
	this.hidePrevious();
	
	
	if( typeof labelelement == "undefined" )	{
		this.labelelement = 'span';
	}
	else	{
		this.labelelement = 'div';
	}
	

	/* Paths to images and labels to display next to stars */	
	this.image_star = '/media/icons/gif/star.gif';
	this.image_star_grey = '/media/icons/gif/star_grey.gif';
	this.labels = [
		'Ingen rangering',
		'Meget dårlig',
		'Dårlig',
		'Midt på treet',
		'Bra',
		'Veldig bra',
		'Fantastisk'
	];
	
	this.choices = 6;
	
	
	
	/* Generate the star images with handlers */	
	this.buildStarImages();
	
	
	
	/* Generate a <span> for displaying label next to stars */
	this.buildLabelContainer();
	
	/* Generate a <input type="hidden"> to store rating value */
	this.buildHiddenInput();
	
	if( typeof this.initial_value != 'undefined' )	{
		this.setValue(this.initial_value);
	}
}

SelectStars.prototype.getInitialValue = function()	{

	radioboxes = this.starcontainer.getElementsByTagName('input');
	
	for( var x=0; x < radioboxes.length; x++ )	{
		if (radioboxes[x].checked === true)	{
			return radioboxes[x].value;
		}
	}
};


SelectStars.prototype.setValue = function(value) {

	for( var x=0; x < this.images.length; x++ )	{
		
		console.log('Tried ' + value + ' > ' + x );
		
		if( value > x )	{
			this.images[x].src = this.image_star;
		}
		else	{
			this.images[x].src = this.image_star_grey;
		}
	}
	
	this.updateLabel(value);
	
	this.hidden_input.value = value;
};


SelectStars.prototype.buildHiddenInput = function(){
	var input = document.createElement('input');
	
	input.type = 'hidden';
	input.value = '';
	input.name = this.input_name;
	
	this.hidden_input = input;
	this.starcontainer.appendChild(input);
};

SelectStars.prototype.buildLabelContainer = function(){
	this.labelcontainer = document.createElement(this.labelelement);
	this.labelcontainer.className = 'stars_labelcontainer';
	
	this.labelcontainer.innerHTML = this.labelelement == 'span' ? '&nbsp;&nbsp;' + this.labels[0] : this.labels[0];
	
	this.starcontainer.appendChild(this.labelcontainer);
}

SelectStars.prototype.hidePrevious = function(){
	this.starcontainer.innerHTML = '';
};

SelectStars.prototype.updateLabel = function(value){
	if( this.labelcontainer )	{
	
		if( typeof value == "undefined" )	{
			value = this.hidden_input.value;
		}
		
		if( value == '' ) value = 0;
	
		this.labelcontainer.innerHTML = this.labelelement == 'span' ? '&nbsp;&nbsp;' + this.labels[value] : this.labels[value];
	}
};

SelectStars.onmouseoverfunction = function(){

	for( var y=0; y < this.className; y++ )	{
		this.SelectStars.images[y].src = this.SelectStars.image_star;
	}
	
	if( this.className < this.SelectStars.images.length )	{
		for( var z = this.className; z < this.SelectStars.images.length; z++ )	{
			this.SelectStars.images[z].src = this.SelectStars.image_star_grey;
		}
	}
	
	this.SelectStars.updateLabel(this.className);
};

SelectStars.onmouseoutfunction = function(){
	if( this.SelectStars.hidden_input.value != '0' )	{
		for( var b = 0; b < this.SelectStars.images.length; b++ )	{
			if( b < this.SelectStars.hidden_input.value )	{
				this.SelectStars.images[b].src = this.SelectStars.image_star;
			}
			else	{
				this.SelectStars.images[b].src = this.SelectStars.image_star_grey;
			}
		}
	}
	else	{	
		for( var b = 0; b < this.SelectStars.images.length; b++ )	{
			this.SelectStars.images[b].src = this.SelectStars.image_star_grey;
		}
	}
		
	this.SelectStars.updateLabel();
};

SelectStars.onclickfunction =  function() {
	this.SelectStars.hidden_input.value = this.className;
};

SelectStars.prototype.buildStarImages = function(){	

	this.images = []

	/* Create the image elements */
	for( var x=0; x < this.choices; x++ )	{
		
		var image = document.createElement('img');
		
		image.className = x + 1;
		image.src = this.image_star_grey;
	
		this.starcontainer.appendChild(image);
		this.images.push(image);
	}
	
	/* Create the event handlers */
	var parentObject = this;
	
	for( var x = 0; x < this.images.length; x++ )	{
	
		this.images[x].SelectStars = parentObject;
	
		this.images[x].onmouseover = SelectStars.onmouseoverfunction;
		this.images[x].onmouseout = SelectStars.onmouseoutfunction;
		this.images[x].onclick = SelectStars.onclickfunction;
	}
};






/* 	FeaturedListManager

	Class for toggling display of featured and
	not featured items in a <li>.
*/


function FeaturedListManager(id)
{
	this.id = id;
	this.ul = document.getElementById(id);
	this.li = this.ul.getElementsByTagName('li');
	this.not_featured = []
	
	for( var x=0; x < this.li.length; x++ )	{
		if( this.li[x].className.replace(' ','') == 'not-featured' )	{
			this.not_featured.push(this.li[x]);
		}
	}
	
	if( this.not_featured.length > 0 )
		this.hide(true);
	
	
	
}

FeaturedListManager.prototype.show = function()	{
	for( var x=0; x < this.not_featured.length; x++ )	{
		showElement(this.not_featured[x]);
	}
	
	this.removeLink();
	this.createHideLink();
}

FeaturedListManager.prototype.hide = function(remove_link)	{
	for( var x=0; x < this.not_featured.length; x++ )	{
		hideElement(this.not_featured[x]);
	}
	
	this.removeLink();	
	this.createShowLink();
}

FeaturedListManager.prototype.removeLink = function()	{
	/*this.ul.removeChild(this.li[this.li.length]);*/
	this.ul.removeChild(this.ul.lastChild);
}

FeaturedListManager.prototype.createShowLink = function()	{

	var li = document.createElement('li');
	var a = document.createElement('a');
	a.appendChild(document.createTextNode('Vis alle'));
	a.href = 'javascript:void(0);';
		
	var thisobject = this;
	
	a.onclick = function(){
		thisobject.show();
	};
	
	li.appendChild(a);	
	this.ul.appendChild(li);
}

FeaturedListManager.prototype.createHideLink = function()	{
	var li = document.createElement('li');
	var a = document.createElement('a');
	a.appendChild(document.createTextNode('Vis færre'));
	a.href = 'javascript:void(0);';
		
	var thisobject = this;
	
	a.onclick = function(){
		thisobject.hide();
	};
	
	/*a.onclick = (function(show){show()})(show);*/
	
	li.appendChild(a);
	
	this.ul.appendChild(li);
	this.hide_link = li;
	
}



function confirmDelete(url, question)	{
	if (typeof question == "undefined")
		question = 'Er du sikker på at du ønsker å slette dette objektet?';
		
	var con = confirm(question);
	
	if( con )
		window.location=url;
}




function hideNotFeatured(id)	{
	var ul = document.getElementById(id);
	var li = ul.getElementsByTagName('li');
	
	for( var x=0; x < li.length; x++ )	{
		if( li[x].className == 'not-featured' )	{
			hideElement(li[x]);
		}
	}
}

function hideElement(e)	{
	e.style.display = 'none';
}

function showElement(e) {
	e.style.display = '';
}





/*

window.onload = function(){
	
	//hideSubmenus();
	
	var menus = document.getElementById('navigation').getElementsByTagName('li');
	
	for(x=0; x < menus.length; x++)	{
		var item = menus[x];
		
		if ( item.id != '' )	{
				
			item.onmouseover = function(){
			
				item_id = this.id;
				hideSubmenus();
				resetMainNavigation();
				document.getElementById('submenu_' + item_id).style.display = '';
				
				this.className = 'active';
			};
		}
	}
}

function hideSubmenus() {
	var submenus = document.getElementById('subnavigation').getElementsByTagName('ul');
	for(x=0; x < submenus.length; x++)	{
		submenus[x].style.display = 'none';
	}
}

function resetMainNavigation()	{
	var menus = document.getElementById('navigation').getElementsByTagName('li');
	for(x=0; x < menus.length; x++)	{
		menus[x].className = menus[x].className.replace('active','');
	}
}
*/












function MenuControl(main_ul, submenucontainer, current_tab, active_tab_css)
{
	this.main_ul = document.getElementById(main_ul);
	this.submenucontainer = document.getElementById(submenucontainer);	
	
	if( typeof active_tab_css == 'undefined' )	{
		active_tab_css = 'active';
	}
	
	this.active_tab_css = active_tab_css;
	
	if( typeof current_tab == 'undefined' )	{
		this.current_tab = false;
	}
	else	{
		this.current_tab = current_tab;
	}
	
	this.default_tag = this.current_tab;
	
	this.remove_on_mouseout = false;
	this.initialize();
	
	if( this.current_tab != '' && this.current_tab !== false )	{
		this.show(this.current_tab);
	}
	
	
}

MenuControl.prototype.activate_tab = function(tabname){
	e = document.getElementById(tabname);
	e.className = 'active';	
};

MenuControl.prototype.deactivate_tab = function(tabname){
	e = document.getElementById(tabname);
	e.className = e.className.replace('active','');
};

MenuControl.prototype.deactivate_current = function(){
	e = document.getElementById(this.current_tab);
	e.className = e.className.replace('active','');
};

MenuControl.prototype.initialize = function(){

	var tabs = this.main_ul.getElementsByTagName('li');
	
	for(var x=0; x < tabs.length; x++)	{		
		if( tabs[x].id != '' )	{
		
			var link = tabs[x].firstChild;
		
			link.menu_control = this;
			link.onmouseover = function(){
			
				var object = this;
				
				this.timeout = setTimeout(function(){
					if( object.menu_control.current_tab )	{			
						object.menu_control.deactivate_current();
					}
					object.menu_control.show(object.parentNode.id);
					object.menu_control.activate_tab(object.parentNode.id);
				}, 175);
			}
			
			/*if( this.remove_on_mouseout )	{
				link.onmouseout = function(){
					this.menu_control.hide(this.parentNode.id);
				}
			}*/
			
			link.onmouseout = function(){
				if( typeof this.timeout != 'undefined' )	{
					clearTimeout(this.timeout);
				}
			};
		}
	}
};

MenuControl.prototype.show = function(tabname)
{
	this.hideAll();
	
	var submenu = document.getElementById('submenu_'+tabname);
	submenu.style.display = '';
	this.current_tab = tabname;
};

MenuControl.prototype.hide = function(tabname)
{
	var submenu = document.getElementById('submenu_'+tabname);
	submenu.style.display = 'none';
	this.current_tab = false;
	
	if( this.default_tag !== false )	{
		this.show(this.default_tag);
	}
};

MenuControl.prototype.hideAll = function()
{
	var submenus = this.submenucontainer.getElementsByTagName('ul');
	
	for(var x=0; x < submenus.length; x++)	{
		submenus[x].style.display = 'none';
	}
};

MenuControl.prototype.showAll = function()
{
	var submenus = this.submenucontainer.getElementsByTagName('ul');
	
	for(var x=0; x < submenus.length; x++)	{
		submenus[x].style.display = '';
	}
};





function MenuCollapseManager() {
	this.rightcolumn = document.getElementById('rightcolumn');
	this.rightcolumnboxes = YAHOO.util.Dom.getElementsByClassName(
		'rightcolumnbox', 'div', this.rightcolumn); 
			
	this.initialize();
};

MenuCollapseManager.prototype.initialize = function() {
	/* Iterate over rightcolumn boxes and create handlers */
	for(var x=0; x < this.rightcolumnboxes.length; x++)	{
		var box = this.rightcolumnboxes[x];
		var icon_search = YAHOO.util.Dom.getElementsByClassName('toggle-icon', 'img', box);
		var contents_search = YAHOO.util.Dom.getElementsByClassName('contents', 'div', box);
		
		if(icon_search.length == 1 && contents_search.length == 1)	{		
			YAHOO.util.Event.addListener(icon_search[0], 'click', MenuCollapseManager.gethidefunction(
				icon_search[0], contents_search[0]));
		}
	}
};

MenuCollapseManager.gethidefunction = function(icon, contents)	{
		return function(){
			contents.style.display = 'none';				
			icon.src = '/media/icons/gif/maximize.gif';	
			
			YAHOO.util.Event.removeListener(icon, 'click', 
				MenuCollapseManager.gethidefunction(icon,contents));
			
			YAHOO.util.Event.addListener(icon, 'click', 
				MenuCollapseManager.getshowfunction(icon, contents));
		}
};

MenuCollapseManager.getshowfunction = function(icon, contents)	{
		return function(){
			contents.style.display = '';				
			icon.src = '/media/icons/gif/minimize.gif';	
			YAHOO.util.Event.removeListener(icon, 'click', 
				MenuCollapseManager.getshowfunction(icon, contents));
				
			YAHOO.util.Event.addListener(icon, 'click', 
				MenuCollapseManager.gethidefunction(icon, contents));
		}
};














