$(document).ready(function() {

	$('.scroll_link').click(function(){
		$(window).scrollTo($(this).attr('href'), 1000);
	});
	
	$('.link_top').click(function(){
		$(window).scrollTo('#header', 500);
		return false;
	});

	$('#phone_deals th a').click(function(){$(this).blur();})

	$('#phone_deals > table').tablesorter({
		//	sortingDirection: currentColumnOrder,
			textExtraction: function(node) {
					if(node.getElementsByTagName('span')[0]){
							return node.getElementsByTagName('span')[0].innerHTML;
							}
					return node.innerHTML;
			},
			sortInitialOrder: "asc",
			headers: { 0: { sorter: false}, 1: {sorter: false}, 2: {sorter: false}}
	});

	// add hover state to tr
	$('#phone_deals tbody tr').hover(function () {
		$(this).addClass('hover');
	}, function () {
		$(this).removeClass('hover');
	});
	//add hover state to gallery view box
	
	$('.gallery_view li.handset ').mouseover(function(){$(this).addClass('hovered_handset')});
	$('.gallery_view li.handset ').mouseout(function(){$(this).removeClass('hovered_handset')});
	$('.gallery_view li.slim ').mouseover(function(){$(this).addClass('hovered_slim_handset')});
	$('.gallery_view li.slim ').mouseout(function(){$(this).removeClass('hovered_slim_handset')});
	

	$('.gallery_view li.handset ').click(function () {
		window.location=$(this).find('.new_button').attr('href');
	});

	$('#phone_deals th').click(function(){
		$('#phone_deals tbody tr.spacer').hide();
		$('#phone_deals tbody tr').animate({opacity: 0.5}, 'fast');
		
		$('th').removeClass('selected');
		$(this).addClass('selected');
		$('td').removeClass('ordered');
		if ($(this).hasClass('column_1')){
			$('td.column_1').addClass('ordered');
			$('table').removeClass().addClass('column_1');}
		if ($(this).hasClass('column_2')){ 
			$('td.column_2').addClass('ordered');
			$('table').removeClass().addClass('column_2');
		}
		if ($(this).hasClass('column_3')){ $('td.column_3').addClass('ordered');
		$('table').removeClass().addClass('column_2');}
		if ($(this).hasClass('column_4')){ $('td.column_4').addClass('ordered');}
		if ($(this).hasClass('column_5')){ $('td.column_5').addClass('ordered');}

		$('#phone_deals .first').removeClass('first');
		$('#wrapper').animate({opacity: '+='+1}, "fast", function(){
			$('#phone_deals tr.spacer').empty();
			$('#phone_deals tr.spacer').remove();
			for (i=0;i<=$('#phone_deals tr').size();i++) {
				$('#phone_deals td.rank').eq(i).html(i+1);
			}
			$('#phone_deals tbody tr').eq(0).addClass('first');			
			$('#phone_deals tbody tr').after('<tr class="spacer" style="display:none"><td></td></tr>');
			$('.spacer').slideDown('fast');
			$('#phone_deals tbody tr').animate({opacity: 1}, 'fast');
		});
	});

	$('#hardware_tables > div').addClass('js_tabbed');	
/* Expandable tabbing system */
	var js_tabbeds=$('.js_tabbed').size();
	var js_tabbed_header = "Other";
	
	$('.js_tabbed h2').hide();	
		
	$('<ul id="tabs_list"></ul>').appendTo($('#hardware_tables'));

	for (i=0;i<js_tabbeds;i++) {
		$('.js_tabbed').eq(i).attr('id','js_tabbed_'+i);
		js_tabbed_header = $('.js_tabbed').eq(i).find('h2').html();
		$('<li><a></a></li>').appendTo('#tabs_list');
		$('#tabs_list li a').eq(i).attr('href','#js_tabbed_'+i).html(js_tabbed_header);
		$('<span></span>').appendTo('#tabs_list li a');
	}

/* Assign id to tab headers */

	$('#tabs_list li').eq(0).attr('id','gallery_tab');
	$('#tabs_list li').eq(1).attr('id','compare_tab');
	
/* Generate tabs */
	$('#hardware_tables').tabs();

	if($('.compare_view').hasClass('selected')) {
		$('#compare_tab a').trigger('click');
	}
});