$(document).ready(function() {  

$('.bigger, #nav li, div.titledesc').biggerlink({otherstriggermaster:false}); 	//make entire elements clickable

$("h1").prepend("<span></span>");

$('.hasajax').hide();
$('.jsonly').show();
$("#index").tablesorter( {headers: { 4:{sorter: false} }}); 
$('div.styleWrapper li input[checked=checked]').parent().parent().css('backgroundColor', "#a3cbe5");


$('#share a').each(function(){
	$(this).bind('mouseover', function(){
		$(this).fadeTo('fast', 1);
		});
	});

$('#share a').each(function(){
	$(this).bind('mouseout', function(){
		$(this).fadeTo('fast', .3);
		});
	});

$('#embed a.showembed').click(function(){
	$('#embed textarea').toggle();
	$("textarea#embed_code").select();
	});

$('input#id_q').focus(function(){
		$('table.options').fadeIn(300);
	});

$('div.styleWrapper li').each(function(){
		var color = $("input", this).attr('value');
		var bar = $("input", this).parent().parent().css('backgroundImage', 'url(/foobook_site_media/img/colors/' + color + '.png)');

		$(this).bind('click', function(){
			$("input", this).attr("checked", "checked");
			$('div.styleWrapper li').css('backgroundColor', 'transparent');
			$(this).css('backgroundColor', "#a3cbe5");
		});
	});

$('table#index tbody tr a.delIndexedScrap').click(function(){
		$(this).hide();
		$(this).siblings().hide();
		var foo = $(this).next().show();
	});

$('a.jslogin').click(function(){
		$('#inlinesearch').hide();
		$('table#loginform').show();
	});

$('a.jscancellogin').click(function(){
		$('#inlinesearch').show();
		$('table#loginform').hide();
	});


$('table#index tbody tr a.ConfirmDelIndexedScrap').each(function(){
	$(this).bind('click', function(){
		pk = $(this).parent().parent().parent().attr('id');
		pk = pk.replace(/^row/,'');
		ajaxDelScrap(pk);
		});
	});

$('table#index tbody tr a.indexDelFav').each(function(){
	$(this).bind('click', function(){
		pk = $(this).parent().parent().attr('id');
		pk = pk.replace(/^row/,'');
		delFav(pk);
		});
	});

$('table#index tbody tr a.indexAddFav').each(function(){
	$(this).bind('click', function(){
		pk = $(this).parent().parent().attr('id');
		pk = pk.replace(/^row/,'');
		addFav(pk);
		});
	});

$('table#index tbody tr a.CancelDelIndexedScrap').each(function(){
	$(this).bind('click', function(){
		$(this).parent().siblings().show();
		$(this).parent().hide();
		});
	});

if ($('table.panel').length){
	var panelId = $('table.panel').attr('id').replace(/^row/,'');
	};

$('a.delScrap').click(
	function() {
		//$(this).siblings().hide();
		//$(this).hide();
		//$('div.delScrapConfirm').show();
		$('tr#buttons').hide()
		$('tr#confirm').show()
	});



$('a.delScrapConfirm').click(
	function() {
		$.post("/ajaxDelScrap/",
		{ pk: panelId },
			function(response){window.location = "/" + response + "/scraps/" }
		);
	});





$("#id_version").change(function() {
        var foo = $("option:selected", this).attr('value')
		$.post("/backup/",
		{ id: foo },
			function(response){ $('textarea#id_text').text(response); }
		);
    });







$('a.delScrapConfirm').click(
	function() {
		$.post("/ajaxDelScrap/",
		{ pk: panelId },
			function(response){window.location = "/" + response + "/scraps/" }
		);
	});

$('a.delScrapCancel').click(
	function() {
		$('div.delScrapConfirm').hide();
		$('tr#buttons').show()
		$('tr#confirm').hide()
	});


$('a.addFav').click(
	function() {
		$.post("/addFav/",
		{ scrapId: panelId },
		function(){
			$('a.addFav').hide();
			$('a.delFav').show();
		});
	});

$('a.delFav').click(
	function() {
		$.post("/delFav/",
		{ scrapId: panelId },
		function(){
			$('a.delFav').hide();
			$('a.addFav').show();
		});
	});

var set1 = $('td#set1 img')
var set2 = $('td#set2 img')
set1.hide();
set2.hide();
$('td#set1 img:first').show();
$('td#set2 img:first').show();
bindcaptchapages(1);
bindcaptchapages(2);

$('div.scrappanel').fadeTo('fast', '.2');
$('#scrapwrapper').hover(
	function() {
		$('div.scrappanel').fadeTo('fast', '1');
		},
	function() {
		$('div.scrappanel').fadeTo('fast', '.2');
		});


//
if (navigator.appName == "Microsoft Internet Explorer"){ // Hate
	$('table#index tr:even').css('background', '#e4f2f7')
	};

}); // End document.ready

function ajaxDelScrap(id) {
	$.post('/ajaxDelScrap/', { pk: id },
		function(){
			$("tr#row" + id).fadeOut();
	})};

function delFav(pk) {
	$.post("/delFav/", { scrapId: pk },
	function(){
		var parentElement = "tr#row" + pk
		$(parentElement + " .indexAddFav").show()
		$(parentElement + " .indexDelFav").hide()
	})};

function addFav(pk) {
	$.post("/addFav/", { scrapId: pk },
	function(){
		var parentElement = "tr#row" + pk
		$(parentElement + " a.indexAddFav").hide()
		$(parentElement + " a.indexDelFav").show()
	})};

function bindcaptchapages(i) {
	$('table#captchaanimals td.captcha' + i + ' a').each(function(){
		var attribute = $(this).attr('class');
		attribute = attribute.match(/^\d/);
		$(this).bind('click',function(){
				$('td#set' + i + ' img').hide();		
				$('td#set' + i + ' img.capimage' + attribute).show();		
			});
		});
	};

