function showPic (whichpic,picId) {
	document.getElementById(picId).src = whichpic.href;
	document.getElementById('link').href = whichpic.title;
	return false;
}	

//overlay labels on certain forms
//must do it this way to display text in password field!

jQuery.fn.labelOver = function(overClass) {
	return this.each(function(){
		var label = jQuery(this);
		var f = label.attr('for');
		if (f) {
			var input = jQuery('#' + f);
			this.hide = function() {
			  label.css({ textIndent: -10000 })
			}
			this.show = function() {
			  if (input.val() == '') label.css({ textIndent: 0 })
			}
			// handlers
			input.focus(this.hide);
			input.blur(this.show);
		  label.addClass(overClass).click(function(){ input.focus() });
			
			if (input.val() != '') this.hide(); 
		}
	})
}

//handle adding to wishlist
		$.fn.wishlist = function(address, theLinkID, itemRel, itemName) {
		  $.get(address,function(message){
		  	//check for 'empty' class on wishlist li, and remove element if present
				if (message == "added")
					{
						if ($("li.empty").length > 0){
							$("li.empty").remove();
						}
			//insert li tag into ul#wishlist with link to toy
						$("#wishlist ul").append("<li><a href='"+itemRel+"'>"+itemName+"</a></li>");
			//remove the original 'add to wishlist' link			
						$("a#"+theLinkID).remove();
					}
			});
			return this;
		}


$(document).ready( function() {

	//overlay label with class 'pre'
	 $('label.pre').labelOver('over');
	//login form display/hide
	  $('#loginbox').hide(); 
	  $('a#login').click(function() {
		$('#loginlinks').hide();
		$('#loginbox').show('fast');
		return false;
	  });
	  $('a#loginhide').click(function() {
		$('#loginbox').hide('fast');
		$('#loginlinks').show();    
		return false;
	  });
 if ($('a#retry_postcode').length > 0) {
	$('a#retry_postcode').click(function() {
		var redir = $('a#retry_postcode').href;
		alert(redir);
		$('#postcodebox').load(redir);
		return false;
	  });
	}
	// Ten reasons panel on homepage
	if ($('#tenreasons').length > 0) {
		$('#tenreasons > ul').tabs();
		$('#tenreasons > ul').tabs('rotate', 5000);
	}
	if ($('#tenreasons_banner').length > 0) {
		$('#tenreasons_banner > ul').tabs();
		$('#tenreasons_banner > ul').tabs('rotate', 5000);
	}

	if ($('#how_it_works').length > 0) {
		$('#how_it_works .kwicks').kwicks({
				max: 390,
				spacing: 0
			});
	}
	if ($('div#edit_user').length > 0) {
		$("div#edit_user").click(function() {
			//$("div#edit_user_details").empty().load($(this).attr('href'));
			$("div#edit_user_details").empty().load('/forms/user_edit/inline');
		return false;
	});
	}
	if ($('a.edit_subscription').length > 0) {
		$("a.edit_subscription").click(function() {
			var whichDiv = ($(this).attr('title'));
			var whichEntry = ($(this).attr('rel'));
			$("div#"+whichDiv).empty().load('/forms/subscription_edit/inline/'+whichEntry);
		return false;
	});
	}
	if ($('#ethics').length > 0) {
		$('#ethics img').tooltip({ 
		    track: true, 
		    delay: 0, 
		    showURL: false, 
		    showBody: " - ", 
		    fade: 250,
		    top: -30, 
		    left: 0 
	});
	}
	if ($('#wishlist').length > 0) {
		$('#wishlist ul li span.threeplus').tooltip({ 
		    track: true, 
		    delay: 0, 
		    showURL: false, 
		    showBody: " - ", 
		    opacity:1,
		    left: -120 
		});
		$('#wishlist ul li span.oversize').tooltip({ 
		    track: true, 
		    delay: 0, 
		    showURL: false, 
		    showBody: " - ", 
		    opacity:1,
		    left: -120 
		});
	}
	if ($('dl#faqs').length > 0) {
		jQuery('dl#faqs').accordion({ 
		header: 'dt', 
		active: false,
		alwaysOpen: false, 
		animated: false,
		autoheight: false 
	});
	}
	if ($('#toyselect').length > 0) {
			$('#toyselect').accordion({
				header: 'dt', 
				active: true, 
				alwaysOpen: false, 
				animated: 'slide', 
				autoheight: false
			});
		}	
	
	//ajax wishlist bits
		$('#wishlist a.deletelink').livequery('click', function() { 
		  var eyed = $(this).attr("id");
		  var pid = $(eyed).parent().attr("id");
		  var confirmDelete = confirm('Do you really want to remove this toy from your list of Toys I\'d Like?'); 
		  if(confirmDelete == true) 
			{ 
			$.get(this.href, { theID: eyed}, function(data) {
				$('a#'+eyed).parent().html(data)
					.css({backgroundColor:"#f00"})
					.css({color:"white"})
					.fadeOut("slow")
			});
			$(eyed).parent().remove();
			if ($("p#p"+eyed).length > 0) {
				$("p#p"+eyed).removeClass("notice");
				$("p#p"+eyed).removeClass("inlist");
				$("p#p"+eyed).empty();	
			}
			var anykids = $("#wishlist ul").children();
			if (anykids.length < 2) {
				$("#wishlist ul").append("<li class='empty'>Your list is currently empty</li>");
				}
			};	
			return false;
		});
		
		
		$('a.addlink').click(function() {
		  var itemName = this.title;
		  var itemRel = this.rel;
		  var theLinkID = $(this).attr("id");
		  var theAddress = this.href;
		  $(this).wishlist(theAddress,theLinkID,itemRel,itemName);
		  $("#wishlist").load("/wishlist/display");
		  return false;
		});
		
		
		
		$('a.confirmlink').click(function() {
		  var itemName = this.title;
		  var itemRel = this.rel;
		  var theLinkID = $(this).attr("id");
		  var theAddress = this.href;
		  var confirmAge = confirm('IMPORTANT: Please note that this toy is unsuitable for under 36 months and you take responsibility for ensuring the safety of children who may come into contact with it who are below that age. Click yes to acknowledge you have recognised it is unsuitable for under 36 months.'); 
		  if(confirmAge == true) 
			{ 
				$(this).wishlist(theAddress,theLinkID,itemRel,itemName);
				$("#wishlist").load("/wishlist/display");
				return false;
			};
		});
	
});
