var ThumbsView	= function(num_per_page, show)
{
	
	switch(show) {
		case 2: 
			show = 2;
		break;
	
		case 3:
			show = 3;
		break;
		
		default: show = 1;
	}
	
	this.data_all	= [];
	this.data_show	= [];
	this.num_per_page	= parseInt(num_per_page, 10);
	this.num_pages	= 0;
	this.current_page	= 1;
	this.current_type	= show;
	this.sorted_abc	= "";
	this.sorted_airdate = "";
	this.sorted_cat	= "";
	this.sorted_city	= "";
	this.d	= document;
	this.w	= window;
	this.DIV	= null;
};

ThumbsView.prototype.load_data	= function(id, thumb, title, city, cat, link, airdate)
{
	this.data_all[this.data_all.length]	= ({
		'id':		id,
		'thumb':	thumb,
		'title':	title,
		'city':	city,
		'cat':	cat,
		'link':	link,
		'airdate': airdate
	});
};

ThumbsView.prototype.INIT	= function()
{
	this.DIV	= this.d.getElementById("category_videos_list");
	if( ! this.DIV ) {
		return false;
	}
	for( var i=0; i<this.data_all.length; i++ ) {
		this.data_show[i]	= this.data_all[i];
	}
	this._set_pager();
};

ThumbsView.prototype.show_type	= function(tp)
{
	
	
	if( ! this.DIV ) {
		return false;
	}
	if( tp == this.current_type ) {
		return true;
	}
	
	
	tp	= tp==2 ? 2 : 1;
	this.current_type	= tp;
	var tp1	= tp;
	var tp2	= tp==2 ? 1 : 2;
	this.d.getElementById("TView_tb"+tp1).className	= "active-tab";
	this.d.getElementById("TView_tb"+tp2).className	= "";
	this._show_thumbs();
};

ThumbsView.prototype.sort_airdate	= function(airdate)
{
	if( ! this.DIV ) {
		return false;
	}
	if( this.sorted_airdate == airdate ) {
		return true;
	}
	this.sorted_airdate	= airdate;
	this._set_filters();
	this._set_pager();
};

ThumbsView.prototype.sort_abc	= function(ltr)
{
	if( ! this.DIV ) {
		return false;
	}
	ltr	= ltr.toLowerCase();
	if( this.sorted_abc == ltr ) {
		return true;
	}
	this.sorted_abc	= ltr;
	this._set_filters();
	this._set_pager();
};

ThumbsView.prototype.sort_cat	= function(cat)
{
	if( ! this.DIV ) {
		return false;
	}
	if( this.sorted_cat == cat ) {
		return true;
	}
	this.sorted_cat	= cat;
	this._set_filters();
	this._set_pager();
};

ThumbsView.prototype.sort_city	= function(city)
{
	if( ! this.DIV ) {
		return false;
	}
	if( this.sorted_city == city ) {
		return true;
	}
	this.sorted_city	= city;
	this._set_filters();
	this._set_pager();
};

ThumbsView.prototype.show_pg	= function(pg)
{
	if( ! this.DIV ) {
		return false;
	}
	if( this.current_page == pg ) {
		return true;
	}
	pg	= parseInt(pg, 10);
	if( isNaN(pg) ) { pg = 1; }
	if( pg < 1 ) { pg = 1; }
	if( pg > this.num_pages ) { pg = this.num_pages; } 
	this.current_page	= pg;
	this._sync_pager();
	this._show_thumbs();
};

ThumbsView.prototype._set_filters	= function()
{
	if( ! this.DIV ) {
		return false;
	}
	this.data_show	= [];
	var tmp;
	for(var i=0; i<this.data_all.length; i++)
	{
		tmp	= this.data_all[i];
		if( this.sorted_airdate!=="" && tmp.airdate!=this.sorted_airdate ) {
			continue;
		}
		if( this.sorted_cat!=="" && tmp.cat!=this.sorted_cat ) {
			continue;
		}
		if( this.sorted_city!=="" && tmp.city!=this.sorted_city ) {
			continue;
		}
		if( this.sorted_abc !== "" ) {
			if( tmp.title.charAt(0).toLowerCase() < this.sorted_abc.charAt(0) ) {
				continue;
			}
			if( tmp.title.charAt(0).toLowerCase() > this.sorted_abc.charAt(1) ) {
				continue;
			}
		}
		this.data_show[this.data_show.length]	= tmp;
	}
	var ttl	= this.d.getElementById("TView_sorted");
	if( ! ttl ) {
		return;
	}
	while( ttl.firstChild ) {
		ttl.removeChild( ttl.firstChild );
	}
	var txt	= [];
	if( this.sorted_airdate != "" ) { 
		sortiran_airdate = this.sorted_airdate;
		sortiran_airdate = sortiran_airdate.split("-");
		var month = sortiran_airdate[1];
		var all_months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
		month--;
		month = all_months[month];	
		curr_date = month+" "+sortiran_airdate[2]+","+sortiran_airdate[0];
		txt[txt.length] = "" + curr_date; }
	if( this.sorted_abc != "" ) { 
		var sortiran_abc = this.sorted_abc; 
		var first_a = sortiran_abc.charAt(0);
		first_a = first_a.toUpperCase();
		var last_a = sortiran_abc.charAt(1);
		last_a = last_a.toUpperCase();
		sortiran_abc = first_a + " to " + last_a;
		txt[txt.length] = "" + sortiran_abc; 
		}
	if( this.sorted_cat != "" ) { txt[txt.length] = "" + this.sorted_cat; }
	if( this.sorted_city != "" ) { txt[txt.length] = "" + this.sorted_city; }
	if( txt.length > 0 ) {
		txt	= " - "+txt.join(" - ");
		ttl.appendChild( this.d.createTextNode(txt) );
	}
};

ThumbsView.prototype._set_pager	= function()
{
	if( ! this.DIV ) {
		return false;
	}
	this.num_pages	= Math.ceil( this.data_show.length / this.num_per_page );
	this.current_page	= 1;
	this._sync_pager();
	this._show_thumbs();
};

ThumbsView.prototype._sync_pager	= function()
{
	if( ! this.DIV ) {
		return false;
	}
	var pgr	= this.d.getElementById("TView_pager");
	while( pgr.firstChild ) {
		pgr.removeChild( pgr.firstChild );
	}
	var i1	= this.current_page>3 ? this.current_page-2 : 1;
	var i2	= i1+4 > this.num_pages ? (this.num_pages) : i1+4;
	if( i1 == i2 ) {
		return true;
	}
	if( i2-i1<4 && i2>4 ) {
		i1	= i2 - 4;
	}
	var data	= [];
	if( this.current_page > 1 ) {
		data[data.length]	= [this.current_page-1, 'PREV'];
	}
	var i;
	for(i=i1; i<=i2; i++) {
		data[data.length]	= [i, i];
	}
	if( this.current_page < this.num_pages ) {
		data[data.length]	= [this.current_page+1, 'NEXT'];
	}
	var obj	= this;
	var a;
	for(i=0; i<data.length; i++) {
		a	= this.d.createElement("A");
		a.href	= "javascript:;";
		a.onclick	= function() {
			obj.show_pg( this.getAttribute("pg") );
		};
		a.setAttribute("pg",	data[i][0]);
		a.appendChild( this.d.createTextNode( data[i][1] ) );
		if( data[i][0]==this.current_page && data[i][0]==data[i][1] ) {
			a.className	= "active";
		}
		pgr.appendChild(a);
	}
};

ThumbsView.prototype._show_thumbs	= function()
{
	
	if( ! this.DIV ) {
		return false;
	}
	while( this.DIV.firstChild ) {
		this.DIV.removeChild( this.DIV.firstChild );
	}
	var i1	= this.num_per_page * (this.current_page - 1);
	var i2	= i1 + this.num_per_page;
	var i, p = [];
	for(i=i1; i<i2; i++) {
		if( ! this.data_show[i] ) {
			continue;
		}
		
		if( this.current_type == 2 ) {
			this._print_thumb_tp2( this.data_show[i] );
		}
		
		if( this.current_type == 3 ) {
			this._print_thumb_tp3( this.data_show[i] );
		}
		
		if( this.current_type == 1 ) {
			this._print_thumb_tp1( this.data_show[i] );
			p[p.length]	= this.data_show[i].thumb;
		}
	}
	
	var div	= this.d.createElement("DIV");
	div.style.clear	= "both";
	this.DIV.appendChild( div );
	if( p.length > 0 ) {
		var tmp	= [];
		for(i=0; i<p.length; i++) {
			tmp[i]	= new Image();
			tmp[i].src	= p[i];
		}
	}
};

ThumbsView.prototype._print_thumb_tp1	= function(data)
{
	if( ! this.DIV ) {
		return false;
	}
	var div	= this.d.createElement("DIV");
	var a	= this.d.createElement("A");
	var h4	= this.d.createElement("H4");
	var img	= this.d.createElement("IMG");
	var dv2	= this.d.createElement("DIV");
	//var sp1 = this.d.createElement("span");
	var sp	= this.d.createElement("span");
	var spn = this.d.createElement("span");
	
	//sp1.style = "color: red;";
	div.className	= "profile_thumbs";
	a.href	= data.link;
	a.title	= data.title;
	img.src	= data.thumb;
	img.alt	= data.title;
	img.border = "0"
	h4.appendChild(this.d.createTextNode( this._str_cut(data.title, 17) ));
	//sp1.appendChild(this.d.createTextNode( this._str_cut(data.airdate, 17) ));
	sp.appendChild(this.d.createTextNode( this._str_cut(data.cat, 17) ));
	spn.appendChild(this.d.createTextNode( this._str_cut(data.city, 17) ));
	a.appendChild(img);
	div.appendChild(a);
	div.appendChild(h4);
	div.appendChild(sp);
	//div.appendChild(sp1);
	div.appendChild(spn);
	this.DIV.appendChild(div);
};


ThumbsView.prototype._print_thumb_tp3	= function(data)
{
	if( ! this.DIV ) {
		return false;
	}
	var div	= this.d.createElement("DIV");
	var a	= this.d.createElement("A");
	var h4	= this.d.createElement("H4");
	var img	= this.d.createElement("IMG");
	var dv2	= this.d.createElement("DIV");
	//var sp1 = this.d.createElement("span");
	var sp	= this.d.createElement("span");
	var spn = this.d.createElement("span");
	var spnn = this.d.createElement("span");
	
	//sp1.style = "color: red;";
	div.className	= "profile_thumbs";
	a.href	= data.link;
	a.title	= data.title;
	img.src	= data.thumb;
	img.alt	= data.title;
	img.border = "0"
	h4.appendChild(this.d.createTextNode( this._str_cut(data.title, 17) ));
	
	sp.appendChild(this.d.createTextNode( this._str_cut(data.cat, 17) ));
	spn.appendChild(this.d.createTextNode( this._str_cut(data.city, 17) ));
	if(this._str_cut(data.airdate, 100) != "0000-00-00") {
		str=this._str_cut(data.airdate, 100).split("-");
		datata = str[1] + "-" + str[2] + "-" + str[0];
		spnn.appendChild(this.d.createTextNode( "Air Date: " + this._str_cut(datata, 100) ));
	} else {
		spnn.appendChild(this.d.createTextNode( "Air Date: not added" ));
	}
	a.appendChild(img);
	div.appendChild(a);
	div.appendChild(h4);
	div.appendChild(sp);
	
	div.appendChild(spn);
	div.appendChild(spnn);
	this.DIV.appendChild(div);
};

	

ThumbsView.prototype._print_thumb_tp2	= function(data)
{

//<span>Votes to Date: 25</span>
//<a href="#" class="vote_page">Vote</a>

	if( ! this.DIV ) {
		return false;
	}
	var div	= this.d.createElement("DIV");
	var a	= this.d.createElement("A");
	var a1	= this.d.createElement("A");
	var h4	= this.d.createElement("H4");
	var img	= this.d.createElement("IMG");
	var dv2	= this.d.createElement("DIV");
	//var sp1 = this.d.createElement("span");
	var sp	= this.d.createElement("span");
	var spn = this.d.createElement("span");
	var spnn = this.d.createElement("span");
	var vv = "Vote";
	var vote_num = "Votes to Date: " + data.airdate;
	
	//sp1.style = "color: red;";
	div.className	= "profile_thumbs";
	a.href	= data.link;
	a1.href = "javascript:";
	a1.className = "vote_page";
	idt = 'num'+data.id;
	nums = vote_num;
	nums++;
	more = 'this.style.display="none";change('+idt+', '+nums+')';
	a1.setAttribute("onclick", more);
	a.title	= data.title;
	img.src	= data.thumb;
	img.alt	= data.title;
	img.border = "0"
	h4.appendChild(this.d.createTextNode( this._str_cut(data.title, 17) ));
	//sp1.appendChild(this.d.createTextNode( this._str_cut(data.airdate, 17) ));
	sp.appendChild(this.d.createTextNode( this._str_cut(data.cat, 17) ));
	spn.appendChild(this.d.createTextNode( this._str_cut(data.city, 17) ));
	a.appendChild(img);
	div.appendChild(a);
	div.appendChild(h4);
	div.appendChild(sp);
	//div.appendChild(sp1);
	div.appendChild(spn);
	spnn.setAttribute("id", idt);
	spnn.appendChild(this.d.createTextNode( this._str_cut(vote_num, 50) ));
	div.appendChild(spnn);
	a1.appendChild(this.d.createTextNode( this._str_cut(vv, 5) ));
	div.appendChild(a1);
	
	this.DIV.appendChild(div);
};


ThumbsView.prototype._str_cut	= function(str, len)
{
	return str.length<=len ? str : (str.substring(0, len-1)+"..");
};