var Pager = {currentPage : 0};
var Channel = {currentChannel : "474"}
var Sorting = {currentSort : "upload DESC"};

function setSort(sorttype){
    Sorting.currentSort = sorttype;
    getFiles(Sorting.currentSort, [Channel.currentChannel], Pager.currentPage, 6, "[]", false);
}

function getFiles ( sorting, filters, start, limit, fields, noCache) {
	 
  	var params = {
    	"vhost" :       106,
		"sort" :       	sorting,
		"start" :       start,
		"limit" :       6,
		"noCache" :     noCache
  	};
 
	params['filters[channel]'] = filters[0];
	params['filters[moderationStatus]'] = "approved";
	params['fields[0]'] = "commentcount";
	params['fields[1]'] = "rating";
	params['fields[2]'] = "title";
	params['fields[3]'] = "length";

	jsonRequest('media.getFiles', params, 
    	function(result)     { getFilesResult(result, filters[0], Pager.currentPage); },
    	function(exception)  { getFilesException(exception); }
  	);
}

function showPage(cid, pageNum) {

	//$('channel-' + cid).currentPage = pageNum;
	getFiles('upload DESC', [cid], (pageNum - 1) * 6, 6, null, false);
	return false;
}

function getFilesResult(result, cid, pageNum) {
	if (result.data.length < 1){
		$('channel-' + cid).innerHTML = '<h3 style="text-align:center">No media found in this channel.</h3>';
		Effect.Appear($('channel-' + cid), {duration: 0.4});
		Effect.Fade($('loader'),{duration: 0.6, delay: 0.2});
	} else {
		//clearing out all files in the div
		$('channel-' + cid).innerHTML = '';
		
		var output = '';
		// process files 
		for(var i=0, len=result.data.length; i<len; i++){
			//putting files in the div
			//before that, we have to do a little hack to make the 10-based rating a 5-based rating
			var rating = result.data[i].rating / 2;
			
			output += makeFileThumb(result.data[i], rating, cid);	
		} 
		
		$('channel-' + cid).innerHTML = '<ul id="search_results">' + output + '</ul>';
		
		var pager = document.createElement('div');
		pager.className = 'pager';
		pager.id = 'pager-' + cid;
		$('channel-' + cid).appendChild(pager);
	
		//making a pager
		makePager(result.totalCount, 12, cid, Pager.currentPage);	
		//show channelPage div
		showChannelPage (cid);
	}
}

function getFilesException(exception) {

	alert('an error occured');

  	// Some error occured
	var m = '';
  	for ( x in exception){
		m += x + ':\t' + exception [ x ];
	}
  	
	console.log(m);
	
}

function makePager(totalItems, pageSize, cid, pageNum) { 

		$('pager-' + cid).innerHTML = '<a href="/channel/' + cid + '">view all &raquo;</a>';
	
}

function startShowPage(cid,i) {
	Effect.Appear('loader',{duration: 0.3});
	
	return function () { return showPage(cid,i);}; 
}

function makeFileThumb(data, rating, cid){
	
	var time = data.length;
	var minutes = Math.floor(time / 60);
	var seconds = Math.floor(time % 60);
	
	data.length = minutes + ":" + seconds;
	
	data.title = stripLine(data.title, 90);

	var mediaThumb = new Template('<li style="" class="video mediathumb" id="file-#{id}">'+"\n" +
		'	<a href="/mediadetail/#{id}"><img src="#{thumbUrl}/175" />' + "\n" +
		'			<h3 class="title">#{title}</h3>'+ "\n" +
		'			<span class="duration">#{length}</span><!--<span class="commentcount">#{commentcount}</span>-->' + "\n" +
		'	</a>'+
		'</li>');
	
	var myThumb = mediaThumb.evaluate(data);
	
	return myThumb;

}

function showChannelPage (cid) {

	Effect.Appear($('loader'),{duration: 0.3});

	//hiding all channelPage divs
	var channelPages = $$('div.channelPage');
	for (var i=0, len = channelPages.length; i<len; i++){
		channelPages[i].hide();
	} 	

	//showing the selected channelPage div
	function showIt(){
		Effect.Appear($('channel-' + cid), {duration: 0.4});
		Effect.Fade($('loader'),{duration: 0.6, delay: 0.2});

	}

	var t=setTimeout(showIt,600);
}

function loadChannel (cid, elem) {
	
	Channel.currentChannel = cid;
	//if this tab has'nt been opened yet, proceed
	if (! $('channel-' + cid) ) {

		var channelPage = $$('div.channelPage');
		for(var i=0, len = channelPage.length; i<len; i++){
			channelPage[i].hide();
		}

		$('loader').show();
		
		//create div to hold gallery
		var newDiv = document.createElement('div');
		newDiv.className = 'channelPage';
		newDiv.id = 'channel-' + cid;
		newDiv.currentPage = 0;
		newDiv.currentChannel = cid;
		Pager.currentPage = 0;
		$('channel_pages').appendChild(newDiv);

		getFiles(Sorting.currentSort, [cid], Pager.currentPage, 6, "[]", false);

	} else {
		//we'll just directly open this channelpage
				showChannelPage(cid);
	}

	//handling the tabs
	if(elem){
		var links = $('channel_tabs').getElementsByTagName('a');
		for ( var j = 0, len = links.length; j<len; j++){
			links[j].className = '';
		}
		elem.className = 'active';
		elem.blur();
	}

	//changing RSS feed link
	$('rss_feed').href = "/services/mediarss?channel=" + cid;

	return false;
}

