$(function(){
	slideCompare();
	$(window).resize(function(){
		setTimeout(slideCompare, 100)
	});
	$('#compareBox table td').hover(function(){
		thisTable = $(this).parents('#compareBox table');
		indexTR = thisTable.find('tr').index($(this).parents('#compareBox tr'));
		for(i = 0; i < 2; i++){
			iTable = $('#compareBox table').eq(i);
			iTable.find('tr').eq(indexTR).addClass('greyRow');
		}
	},
	function(){
		$('#compareBox table tr.greyRow').removeClass('greyRow');
	});
});
$('div.filter-list a').live('mouseup', function(){ slideCompare(); });
function slideCompare(){
	heightTR();
	var slide;
	var speedSlide;
	$arsL = $('a.ars-L');
	$arsR = $('a.ars-R');
	$arsL.click(function(){return false});
	$arsR.click(function(){return false});

	$grL = $('#grWidth-L');
	$grR = $('#grWidth-R');

	$arsL.hide();
	$arsR.hide();
	$grL.hide();
	$grR.hide();

	$tableBox = $('#compareTableBox');
	$tableBox.css({marginLeft: 0});

	widthBox = $('#compareSlideHidden').width();
	widthTable = $tableBox.find('table').width();
	//alert('widthBox '+widthBox+' widthTable '+widthTable);
	if( widthTable > widthBox ){
		//alert($tableBox.height());
		$arsR.show();
		$grR.height($tableBox.height()).show();

		slide = widthTable - widthBox;
		speedSlide = 300; //slide*2;
		
		$arsR.click(function(){
			stop = 0;
			step = 100;
			mLeft = $tableBox.css('margin-left').replace('px','')*1;
//			console.log(slide+' : '+mLeft+' / '+step);
			if(-mLeft+step>=slide) { step = slide+mLeft; stop = 1; }
			$tableBox.stop().animate({marginLeft: mLeft-step}, speedSlide, function(){
				if(stop) {
					$arsR.hide();
					$grR.hide();
				}
			});
			grHeight = parseInt($tableBox.find('th').height()); //+20;
			$arsL.show();
			$grL.height(grHeight).show();
		});
		
		$arsL.click(function(){
			stop = 0;
			step = 100;
			mLeft = - $tableBox.css('margin-left').replace('px','')*1;
//			console.log(slide+' : '+mLeft+' / '+step);
			if(step>=mLeft) { step = mLeft; stop = 1; }
			$tableBox.stop().animate({marginLeft: - mLeft+step}, speedSlide, function(){
				if(stop) {
					$arsL.hide();
					$grL.hide();
				}
			});
			grHeight = parseInt($tableBox.find('th').height()); //+20;
			$arsR.show();
			$grR.height(grHeight).show();
		});
		
/*		$arsR.mouseup(function(){
			$tableBox.stop();
			mw = parseInt($tableBox.css('marginLeft'));
			dist = slide - mw;
			if(dist == 0) {
				$arsR.hide();
				$grR.hide();
				}
		});*/
		
/*		$arsR.mousedown(function(){
			$tableBox.stop().animate({marginLeft: -slide}, speedSlide, function(){
				$arsR.hide();
				$grR.hide();
			});
			$arsL.show();
			grHeight = parseInt($tableBox.find('th').height()); //+20;
			$grL.height(grHeight).show();
		});
		$arsR.mouseup(function(){
			$tableBox.stop();
			mw = parseInt($tableBox.css('marginLeft'));
			dist = slide - mw;
			if(dist == 0) {
				$arsR.hide();
				$grR.hide();
				}
		});*/
/*
		$arsL.mousedown(function(){
			$tableBox.stop().animate ({marginLeft: 0}, speedSlide, function(){
				$arsL.hide();
				$grL.hide();
			});
			$arsR.show();
			$grR.height($tableBox.height()).show();
		});
		$arsL.mouseup(function(){
			$tableBox.stop();
			mw = parseInt($tableBox.css('marginLeft'));
			if(mw == 0) {
				$arsL.hide();
				$grL.hide();
			}
		});
*/
	} else {
		$arsR.unbind("mousedown");
		$arsR.unbind("mouseup");
		$arsL.unbind("mousedown");
		$arsL.unbind("mouseup");
	}
};
function heightTR(){
	var numTR = $('#compareStatic table tr').length;
	for(n = 0; n < numTR; n++) {
		$stTR = $('#compareStatic table tr').eq(n);
		$slTR = $('#compareSlide table tr').eq(n);
		stH = parseInt($stTR.height(),10);
		slH = parseInt($slTR.height(),10);
		//console.log(stH+" "+slH);
		if(stH !== slH) {
			if(stH > slH) {
				$stTR.height($stTR.height());
				$slTR.height($stTR.height());
			}
			else {
				$stTR.height($slTR.height());
					$slTR.height($slTR.height());
			}
		};
	}
};

/*******/

var flag = 0;
$(function(){
	$('div.filter-list a').click(function(){
		a = $(this);
		if (!a.hasClass('filter-list-active')) {
			$('#compareBox table').show();
			if(a.attr('rel') == 'allCards') {
				$('div.filter-list a.filter-list-active').removeClass('filter-list-active');
				a.addClass('filter-list-active');
				$('#compareTableBox table th').show().addClass('showCol');
				$('#compareTableBox table td').show().addClass('showCol');
				flag = 0;
			}
			else {
				$('div.filter-list a[rel="allCards"]').removeClass('filter-list-active');
				a.addClass('filter-list-active');
				type = a.attr('rel');
				if(flag == 0) {
					$('#compareTableBox table th').hide().removeClass('showCol');
					$('#compareTableBox table td').hide().removeClass('showCol');
				};
				$('#compareTableBox table th[rel='+type+']').show().addClass('showCol');
				$('#compareTableBox table td[rel='+type+']').show().addClass('showCol');
				flag = 1;
			}
		}
		else {
			if($('div.filter-list a.filter-list-active').length<2 && a.attr('rel')!='allCards') {
				$('div.filter-list a[rel=allCards]').click();
				return;
			}
			if(a.attr('rel') !== 'allCards') {
				type = a.attr('rel');
				a.removeClass('filter-list-active');
				$('#compareTableBox table th[rel='+type+']').hide().removeClass('showCol');
				$('#compareTableBox table td[rel='+type+']').hide().removeClass('showCol');
				if(!$('#compareTableBox table td.showCol').length) {
					$('#compareBox table').hide();

				}
			};
		};
		slideCompare();
		borderLastCol();
		return false;
	});
	$arsL = $('a.ars-L');
	$arsR = $('a.ars-R');
	
	jQuery('#compareBox').bind('swiperight swiperightup swiperightdown',function(){ 
		$arsL.click(); 
		this.originalEvent.preventDefault();
	});	
	jQuery('#compareBox').bind('swipeleft swipeleftup swipeleftdown',function(){ 
		$arsR.click(); 
		this.originalEvent.preventDefault();
	});
	jQuery('#compareBox').bind('swipeup swipedown',function(){ 
		this.originalEvent.preventDefault();
	});

/*	jQuery('#compareBox').bind('swiperight',function(){ $arsL.click(); this.originalEvent.preventDefault();});
	jQuery('#compareBox').bind('swiperightup',function(){ $arsL.click(); this.originalEvent.preventDefault();});
	jQuery('#compareBox').bind('swiperightdown',function(){ $arsL.click(); this.originalEvent.preventDefault();});
	jQuery('#compareBox').bind('swipeleft',function(){ $arsR.click(); this.originalEvent.preventDefault();});
	jQuery('#compareBox').bind('swipeleftup',function(){ $arsR.click(); this.originalEvent.preventDefault();});
	jQuery('#compareBox').bind('swipeleftdown',function(){ $arsR.click(); this.originalEvent.preventDefault();});
*/	
});

function borderLastCol() {
	nt = $('#compareTableBox table tr').length;
	$('#compareTableBox table tr td.last').removeClass('last');
	for(b = 0; b < nt; b++) {
		thisTR = $('#compareTableBox table tr').eq(b);
		//thisTR.find('th.showCol').filter(':last').addClass('last');
		thisTR.find('td.showCol').filter(':last').addClass('last');
	}
};