
$(document).ready(function(){ 

	//$("#content").hide();

	create_tooltips();
	colorbox_init();
	replace();
	table_sorter_init();
	
	feature_hide_init();
	$(".feature_hide").click(function() { 
		feature_hide($(this).attr("mode"));
		return false;
	});
});

function create_tooltips() {
	$("th.rang").simpletip({ content: $("#content #rang").html(), fixed: false, position: 'bottom', offset: [30, 30], showTime:300 });
	$("th.pic").simpletip({ content: $("#content #pic").html(), fixed: false, position: 'bottom', offset: [30, 30], showTime:300 });
	$("th.price").simpletip({ content: $("#content #price").html(), fixed: false, position: 'bottom', offset: [30, 30], showTime:300 });
	$("th.zoll").simpletip({ content: $("#content #zoll").html(), fixed: false, position: 'bottom', offset: [30, 30], showTime:300 });
	$("th.resolution").simpletip({ content: $("#content #resolution").html(), fixed: false, position: 'bottom', offset: [30, 30], showTime:300 });
	$("th.size").simpletip({ content: $("#content #size").html(), fixed: false, position: 'bottom', offset: [30, 30], showTime:300 });
	$("th.weight").simpletip({ content: $("#content #weight").html(), fixed: false, position: 'bottom', offset: [30, 30], showTime:300 });
	$("th.space").simpletip({ content: $("#content #space").html(), fixed: false, position: 'bottom', offset: [30, 30], showTime:300 });
	$("th.endurance").simpletip({ content: $("#content #endurance").html(), fixed: false, position: 'bottom', offset: [30, 30], showTime:300 });
	$("th.lq").simpletip({ content: $("#content #lq").html(), fixed: false, position: 'bottom', offset: [30, 30], showTime:300 });
}


function colorbox_init(){
	//Examples of how to assign the ColorBox event to elements.
	$("a[rel='kindledx']").colorbox({transition:"fade"});
	$("a[rel='sonyprs505']").colorbox({transition:"fade"});
}


function replace() {
	$(".tablesorter td").each(function(){
		if($(this).text() == 'Ja')
			$(this).html('<div class="ja"></div>');
		else if($(this).text() == 'Nein')
			$(this).html('<div class="nein"></div>');
	});
}

function feature_hide_init() {
	$("th").each(function() {
		if($(this).attr("feature") > 2)
			$(this).hide();	
	});
		
	$("td").each(function() {
		if($(this).attr("feature") > 2)
			$(this).hide();
	});
}

//versteckt oder zeigt Features an, je nach Option
function feature_hide(mode) {
	if (mode == 1) {
		//zeige nur die Haupt-Features an
		$("th").each(function() {
			if($(this).attr("feature") > 1)
				$(this).fadeOut();	
		});
		
		$("td").each(function() {
			if($(this).attr("feature") > 1)
				$(this).fadeOut();
		});
	} else if (mode == 2) {
		//zeige alles ausser die Spezial-Features
		$("th").each(function() {
			if($(this).attr("feature") > 2)
				$(this).fadeOut();
			else	
				$(this).fadeIn();
		});
		
		$("td").each(function() {
			if($(this).attr("feature") > 2)
				$(this).fadeOut();
			else
				$(this).fadeIn();	
		});
	} else {
		//zeige alles an
		$("th").each(function() {
			$(this).fadeIn();
		});
		
		$("td").each(function() {
			$(this).fadeIn();
		});
	}
}


function table_sorter_init() {
	// add new widget called repeatHeaders
		$.tablesorter.addWidget({
			// give the widget a id
			id: "repeatHeaders",
			// format is called when the on init and when a sorting has finished
			format: function(table) {
				// cache and collect all TH headers
				if(!this.headers) {
					var h = this.headers = []; 
					$("thead th",table).each(function() {
						h.push(
							"<th feature=\""+$(this).attr("feature")+"\" class=\""+$(this).attr("class")+"\">" + $(this).text() + "</th>"
						);
						
					});
				}
				
				// remove appended headers by classname.
				$("tr.repeated-header",table).remove();
				
				// loop all tr elements and insert a copy of the "headers"	
				for(var i=0; i < table.tBodies[0].rows.length; i++) {
					// insert a copy of the table head every 10th row
					if((i%5) == 4) {
						$("tbody tr:eq(" + i + ")",table).before(
							$("<tr></tr>").addClass("repeated-header").html(this.headers.join(""))
						
						);	
					}
				}
				
			}
		});
		
		// call the tablesorter plugin and assign widgets with id "zebra" (Default widget in the core) and the newly created "repeatHeaders"
		$("table").tablesorter({
			sortColumn: 'name',			// Integer or String of the name of the column to sort by.
			sortClassAsc: 'headerSortUp',		// Class name for ascending sorting action to header
			sortClassDesc: 'headerSortDown',	// Class name for descending sorting action to header
			highlightClass: 'highlight', 		// class name for sort column highlighting.
			headerClass: 'header',			// Class name for headers (th's)

			//widgets: ['zebra','repeatHeaders']
			widgets: ['zebra']
		});
}
