//compare tables functionality

var Konsumenternas = {};

Konsumenternas.CompareTable = {
	// Adds the company name to the vote links for easy referencing
	setReference: function() {
		$('.comparisonTable tbody tr').each(function(index) {
			var companyName = $(this).find('td:first').text();
			$(this).find('td:last a').attr('rel', companyName).attr('ref', index);
		});
	},
	// Adds a company to one of the list
	addSelected: function($this, $list) {
		$this.closest('table').next('.comparedThumbs').show().find($list).append($('<span></span>').html($this.attr('rel')).addClass($this.attr('ref')));
	},
	clearSelected: function() {
		$('.comparedThumbs span').remove();
	},
	// Removes all the referenced company names
	removeSelected: function($elm) {
		var $d = $elm.closest('table').next('.comparedThumbs');
		$d.find('.' + $elm.attr('ref')).remove();
	},
	// Toggles the className "selected" on the vote thumbs
	toggleSelectedClass: function($elm) {
		$elm.find('span').toggleClass('selected').closest('div').siblings().find('.selected').removeClass();
	},
	// Adds click events to the vote thumbs
	addEvents: function() {
		
		$('.comparisonTable .tabName').click(function(e) {
			e.preventDefault();
			var hasCompanies = $(this).closest('table').find('tbody').html().length > 0;
			if (!hasCompanies) {
				alert("Du måste välja företag först.");
				$(document).scrollTop($('.comapareOptions').offset().top);
				$('.optionsLink').effect('pulsate');
			}
			else {
				$(this).parents(".comparisonTable").toggleClass("selected");
			}
		});
		

		$('.fn_show-childlist').click(function() {
			if ($(this).attr('checked')) {
				$(this).closest('.has-children').find('ul').show();	
			}
			else {
				$(this).closest('.has-children').find('.selected-childcompany').empty().end().find('ul').hide();
			}
		});
		
		$('.companies-selection input[type=radio]').click(function() {
			$(this).closest('li.has-children').find('ul').hide();
			$text = $(this).next().text();
			$(this).closest('ul').closest('li').find('.selected-childcompany').text($text).end()
			.find('input[type=radio]').removeAttr('checked');
		});
		
		$('.fn_close-childlist').click(function(e) {
			e.preventDefault();
				
			if($(this).closest('ul').find('input:checked').length === 0) {
				$(this).closest('li.has-children').find('input:eq(0)').removeAttr('checked');	
			}
			$(this).closest('li.has-children').find('ul').hide();
		});
		
		$('.fn_check-all').click(function() {
			if ($(this).attr('checked')) {
				$('.companies-selection input[type=checkbox]').each(function() {
				    $(this).attr('checked', 'checked');
				    $('.fn_check-allingroup0').attr('checked', 'checked');
				    $('.fn_check-allingroup1').attr('checked', 'checked');
				    $('.fn_check-allingroup2').attr('checked', 'checked');
				    $(this).closest('li').find('ul').show();					
				});
			}
			else {
				$('.companies-selection input[type=checkbox]').each(function() {
				    $(this).removeAttr('checked');
				    $('.fn_check-allingroup0').removeAttr('checked');
				    $('.fn_check-allingroup1').removeAttr('checked');
				    $('.fn_check-allingroup2').removeAttr('checked');
				    $(this).closest('li').find('ul').hide();
				});
			}
		});
		
		$('.companies-selection input[type=checkbox]').click(function() {
		    $('.fn_check-all').removeAttr('checked');
		});

		$('.fn_check-allingroup0').click(function() {
		    if ($(this).attr('checked')) {
		        $('.companies-selectiongroup0 input[type=checkbox]').each(function() {
		            $(this).attr('checked', 'checked');
		            $(this).closest('li').find('ul').show();
		        });
		    }
		    else {
		        $('.companies-selectiongroup0 input[type=checkbox]').each(function() {
		            $(this).removeAttr('checked');
		            $('.fn_check-all').removeAttr('checked');
		            $(this).closest('li').find('ul').hide();
		        });
		    }
		});

		$('.companies-selectiongroup0 input[type=checkbox]').click(function() {
		    $('.fn_check-allingroup0').removeAttr('checked');
		});

		$('.fn_check-allingroup1').click(function() {
		    if ($(this).attr('checked')) {
		        $('.companies-selectiongroup1 input[type=checkbox]').each(function() {
		            $(this).attr('checked', 'checked');
		            $(this).closest('li').find('ul').show();
		        });
		    }
		    else {
		        $('.companies-selectiongroup1 input[type=checkbox]').each(function() {
		            $(this).removeAttr('checked');
		            $('.fn_check-all').removeAttr('checked');
		            $(this).closest('li').find('ul').hide();
		        });
		    }
		});

		$('.companies-selectiongroup1 input[type=checkbox]').click(function() {
		    $('.fn_check-allingroup1').removeAttr('checked');
		});

		$('.fn_check-allingroup2').click(function() {
		    if ($(this).attr('checked')) {
		        $('.companies-selectiongroup2 input[type=checkbox]').each(function() {
		            $(this).attr('checked', 'checked');
		            $(this).closest('li').find('ul').show();
		        });
		    }
		    else {
		        $('.companies-selectiongroup2 input[type=checkbox]').each(function() {
		            $(this).removeAttr('checked');
		            $('.fn_check-all').removeAttr('checked');
		            $(this).closest('li').find('ul').hide();
		        });
		    }
		});

		$('.companies-selectiongroup2 input[type=checkbox]').click(function() {
		    $('.fn_check-allingroup2').removeAttr('checked');
		});
		
		
		$('.fn_calculate-comparison').click(function() {
			$('.comparisonTable').removeClass('selected');
			Konsumenternas.CompareTable.clearSelected();
			$('.comparisonTable a.voting span.selected').each(function() {
				var $a = $(this).closest('a');
				if($a.hasClass('vote-positive')) {
					Konsumenternas.CompareTable.addSelected($a, '.positive');
				}
				else {
					Konsumenternas.CompareTable.addSelected($a, '.negative');
				}
			});
		});
		$('.fn_vote').click(function(e) {
			e.preventDefault();
			Konsumenternas.CompareTable.toggleSelectedClass($(this));
			if($(this).hasClass('vote-positive')) {
				Konsumenternas.CompareTable.removeSelected($(this));
			}
			if($(this).hasClass('vote-negative')) {
				Konsumenternas.CompareTable.removeSelected($(this));
			}
		});
		
		
		var $currentTooltip = null;
		
		$('.fn_view-tooltip')
		.each(function() {
			$(this).attr('rel', $(this).attr('title')).removeAttr('title');
		})
		.live('click', function(e){
			
			e.preventDefault();
			
			var $th = $(this).closest('th');
			var $tc = $('.tooltip-container').appendTo('body');
			var $ta = $('.tooltip-arrow').appendTo('body');
			
			if($(this).hasClass('open')) {
				$tc.hide();
				$ta.hide();
				$(this).removeClass('open');
				return;
			}
			
			$currentTooltip = $(this).addClass('open');
			
			$('.fn_view-tooltip').not($currentTooltip).each(function() {
				$(this).removeClass('open');
			});
			
			$('.tooltip-content').html($(this).attr('rel'));
			
			var $winWidth = $(window).width();
			var $tcRight = (parseInt($tc.outerWidth()) + parseInt($th.offset().left));
			
			if (($tcRight + 26) > $winWidth) {
				$tc.css({
					left: ($th.offset().left + $th.outerWidth()) - $tc.outerWidth() - 6 + 'px',
					top: $th.offset().top + $th.outerHeight() + 6 + 'px'
				}).show();
				$ta.css({
					left: $th.offset().left + $th.outerWidth() - 26 + 'px',
					top: $th.offset().top + $th.outerHeight() + 1 + 'px',
					zIndex: "600"
				}).show();
			}
			else {
				$tc.css({
					left: $th.offset().left + 6 + 'px',
					top: $th.offset().top + $th.outerHeight() + 6 + 'px',
					zIndex: "500"
				}).show();
				$ta.css({
					left: $th.offset().left + 20 + 'px',
					top: $th.offset().top + $th.outerHeight() + 1 + 'px',
					zIndex: "600"
				}).show();
			}
			
		});
		$('.fn_close-tooltip').live('click', function(e) {
			e.preventDefault();
			$('.tooltip-container, .tooltip-arrow').hide();
			$currentTooltip.removeClass('open');
		});
		$('.fn_close-feedback').live('click', function(e) {
			e.preventDefault();
			$('.feedback-container').hide();
		});
		$('.fn_toggle-footnote').click(function() {
			$(this).toggleClass('expanded').next().toggle();
		});
		
		
		// If a table doesn't have any footnotes, remove the link
		$('.comparisonTable').each(function() {
			if($(this).find('.fn_toggle-footnote').length === 0) {
				$(this).find('.more-info').remove();
			}
		});
		// Function for toggling all footnotes.
		$('.fn_toggle-all-footnotes').click(function(e) {
			e.preventDefault();
			if ($(this).hasClass('expanded')) {
				$(this).removeClass('expanded').closest('table').find('.expanded').trigger('click');
			}
			else {
				$(this).addClass('expanded').closest('table').find('.footnote').not('.expanded').trigger('click');
			}
		});
	},
	init: function() {
		Konsumenternas.CompareTable.setReference();
		Konsumenternas.CompareTable.addEvents();
	}
};

$(document).ready(function(){
	Konsumenternas.CompareTable.init();
	
	//set first and last class for cells in comparison tables
	$('.comparisonTable td:first-child , .comparisonTable tr:gt(0) th:first-child').addClass("first");
	$('.comparisonTable td:last-child , .comparisonTable tr:gt(0) th:last-child').addClass("last");
	
	
});
