/*
	Filename:		theme.js
	Author:			David Robinson / Sumo Design Ltd
	Date:			31/08/2007
	Description:	Javascript for themes. 
					Related Items scroller.
		
	Requirements:	jquery.js
*/
function refactor_items(el,chunk){
	// Takes items in
	el=d.getElementById(el);
	var list=el.getElementsByTagName('ul')[0].getElementsByTagName('li');
	var count=list.length;
	var html=d.createElement('div');
	var ul;
	var pagenum=0;//pagenumber
	var itemnum=0;//paged item #
	for(var i=0;i<count;i++){
		if(itemnum<chunk){
			if(itemnum<1){ul=d.createElement('ul');}
			if(i==0)ul.style.display='block';
		}else{
			el.appendChild(ul);
			itemnum=0;pagenum++;
			ul=d.createElement('ul');ul.style.display='none';
		}
		itemnum++;
		ul.appendChild(list[0]);
	}

	// Previous
	var skip=d.createElement('ul');
	var li=d.createElement('li');li.className='previtem';
	var a=d.createElement('a');a.href='';a.innerHTML='&nbsp;';a.onclick=function(){show_items(-1);return false;}

	li.appendChild(a);
	skip.appendChild(li);

	// Feedback
	li=d.createElement('li');li.className='count';li.id='itemcount';
	var firstbatch=(count<chunk)?count:chunk;
	li.innerHTML='Items 1-'+firstbatch+' of '+count;
	skip.appendChild(li);

	// Next
	li=d.createElement('li');li.className='nextitem';
	a=d.createElement('a');a.href='';a.innerHTML='&nbsp;';a.onclick=function(){show_items(1);return false;}

	li.appendChild(a);
	skip.appendChild(li);
	li=null;

	// Clear
	var clear=d.createElement('div');clear.className='clear';

	var footer=d.getElementById('itemsbrowserbtn');
	footer.appendChild(skip);
	footer.appendChild(clear);

	el.appendChild(ul);
	el.appendChild(footer);
	el.removeChild(el.getElementsByTagName('ul')[0]);
}
function show_items(num){
	var uls=d.getElementById('itemsbrowser').getElementsByTagName('ul');
	var count=uls.length;
	count=uls.length-1;

	// Find currently displayed item && hide
	for(var i=0;i<count;i++){
		if(uls[i].style.display=='block'){var current=i;var page=i;}
		uls[i].style.display='none';
		//$(uls[i]).fadeOut(300);
	}

	// Find wanted item (next or prev) && display
	current=current+num;
	if(current>(count-1)){current=0;}
	else if(current<0){current=(count-1);}
	uls[current].style.display='block';
	//setTimeout(function(){$(uls[current]).fadeIn(300);},300);

	var pagenum =current;
	var count_p1=uls[0].getElementsByTagName('li').length;
	var count_cr=$(uls[(current)].getElementsByTagName('li')).length;

	var items_total=$('#itemsbrowser li.itembrowse').length;
	var item_start=(count_p1*pagenum)+1;
	var item_end=(count_p1*pagenum)+count_cr;

	item_end=(item_end>=items_total)?items_total:item_end;

	//var items_total=(d.getElementById('itemsbrowser').getElementsByTagName('li').length-3);


	// Add spare items if current.length<page0.length
	if(count_cr<count_p1){
		var add=count_p1-count_cr;
		for(var j=0;j<add;j++){
			var newLi=d.createElement('li');
			var newA =d.createElement('a');
			newA.innerHTML='&nbsp;';
			newLi.appendChild(newA);
			uls[current].appendChild(newLi);
		}
	}
	d.getElementById('itemcount').innerHTML='Items '+item_start+'-'+item_end+' of '+items_total;

	return null;
}