$(document).ready(function(){
	
	// open external links in new window/tab
	$('a[rel="ext"]').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	// clear search field
	$('.search .input input').autoclear();	
	// scroll to top
	$('.top a').click(function(){
		$.scrollTo('#wrap', 750 );
		return false;
	});
	// change list view
	$('a.card-view').click(function(){
		var loc = $(location).attr('href');
		var loc = loc.replace('-list/', '-card/');
		window.location.href = loc;
		return false;
	});
	$('a.table-view').click(function(){
		var loc = $(location).attr('href');
		var loc = loc.replace('-card/', '-list/');
		window.location.href = loc;
		return false;
	});
	// change alpha view
	$('a.norm-view').click(function(){
		var loc = $(location).attr('href');
		var loc = loc.replace('/alpha/', '/');
		window.location.href = loc;
		return false;
	});
	$('a.alpha-view').click(function(){
		var loc = $(location).attr('href');
		var loc = loc.replace('-store/', '-store/alpha/');
		window.location.href = loc;
		return false;
	});
	// show img in list
	$('a.title-cell').hover(
		function(){
			var id_is = $(this).attr('id');
			$('#'+id_is+' img').removeClass('hide');
		},
		function(){
			var id_is = $(this).attr('id');
			$('#'+id_is+' img').addClass('hide');
		}
	);
	// show/hide store search form
	$('#search-glass').toggle(
		function(){
			$('#search-store').slideDown(300);
			setTimeout("$('#search-store input, #search-store p').fadeIn(1500);",150);
		},
		function(){
			$('#search-store input, #search-store p').fadeOut(1500);
			setTimeout("$('#search-store').slideUp(300);",1000);
		}
	);
	// show/hide set list
	$('#set-list').toggle(
		function(){
			$('#all-sets').slideDown(300);
			setTimeout("$('#all-sets ul').fadeIn(1500);",150);
		},
		function(){
			$('#all-sets ul').fadeOut(1500);
			setTimeout("$('#all-sets').slideUp(300);",1000);
		}
	);
	// deck list img switch
	$('.card-is').hover(
		function(){
			var id_is = $(this).attr('id');
			var rel_is = $(this).attr('rel');
			var image_is = $('#img-wrap img').attr('src');
			var image_is = image_is.replace(image_is,'/content/img/mtg/'+rel_is+'/'+id_is+'.jpg');
			$('#img-wrap img').attr('src',image_is);
			if(rel_is == '2ED' || rel_is == '3ED' || rel_is == '4ED' || rel_is == '5ED' || rel_is == '6ED' || rel_is == '7ED' || rel_is == '8ED' || rel_is == '9ED' || rel_is == 'CHR' || rel_is == 'PTK'){
				$('#img-wrap img').addClass('white-border');
				$('#img-wrap img').removeClass('black-border');
			}else{
				$('#img-wrap img').addClass('black-border');
			}
		},
		function(){
			var id_is = $(this).attr('id');
			var rel_is = $(this).attr('rel');
			var image_is = $('#img-wrap img').attr('src');
			var image_is = image_is.replace(image_is,'/content/img/mtg/'+rel_is+'/'+id_is+'.jpg');
			$('#img-wrap img').attr('src',image_is);
		}
	);
	//search form swap
	$('#ygo-store-search').click(
		function(){
			$('#ygo-search-form').removeClass('hide');
			$('#mtg-search-form,#all-search-form').addClass('hide');
			$('#mtg-store-search,#all-search').removeClass('act');
			$(this).addClass('act');
			return false;
		}
	);
	$('#mtg-store-search').click(
		function(){
			$('#mtg-search-form').removeClass('hide');
			$('#ygo-search-form,#all-search-form').addClass('hide');
			$('#ygo-store-search,#all-search').removeClass('act');
			$(this).addClass('act');
			return false;
		}
	);
	$('#all-search').click(
		function(){
			$('#all-search-form').removeClass('hide');
			$('#mtg-search-form,#ygo-search-form').addClass('hide');
			$('#mtg-store-search,#ygo-store-search').removeClass('act');
			$(this).addClass('act');
			return false;
		}
	);
	// tbl sort
	$('#schedule-table').tablesorter();
});
