$(function(){
	
	$('body').addClass('js-enabled');
	
  // popup login form when the user clicks on the 'my lightboxes'
  $('#nav_lightboxes>a').click(showLightboxes);

  // popup login form when the user clicks on the sign in button
  $('#sign_in').click(loadLoginForm);
  
  // tabs on my account page
  $('#body_account').tabify();
  
  // slide open messages on the enquiry history tab
  $('#enquiry_history div.message').slideMessage();
  
  // bio popup on artist page
  $('div.bio').popupInit();
	
 	$('.add>a').boxy();
 	$('.remove>a').boxy();
 	$('a.boxy').boxy();
  
  // Autocomplete the tags in the image search box and clear default
  $("input#search_query, input#advanced_search_query").autocomplete("/auto_complete_for_tag_name").clearDefault();
});

function showLightboxes() {
 $(this).parent().toggleClass('on');
 return false;
}


function loadLoginForm() {
 if($('#login_form').length<1 || $('#login_form').is(':not(:visible)')) {
   var login_form = $('<div id="login_form"></div>');
   $.get('/login', function(data){
     $(data).appendTo(login_form);
     $('#account').append(login_form);
     $('a.close',login_form).click(function(){
       $('#login_form').remove();
       return false;
     });
   });
 } else {
   $('#login_form').remove();
 }
 return false;
}

$.fn.popupInit = function() {
 return this.each(function(){
   // only set up the boxy if needed
   if ($('>div>*',this).text().length > 100) {
     // clone the content
     $(this).after('<div id="bio" class="bio"></div>');
     $('#bio').html($(this).html()).hide();
   
     // crop out unneeded content
     $('h3,img,>div>*:not(:first)',this).remove();
     $('>div>*',this).text($('>div>*',this).text().slice(0,100) + '…');
   
     // add a show link
     $(this).append('<a href="#bio">Read full biog</a>').children('a').boxy().wrap('<p class="more"></p>');
   
     // add a close button to the boxy
     $('#bio').append('<a class="close"><img src="/images/icon_close_blue.png" alt="Close" /></a>');
   }
 });
}

$.fn.tabify = function() {
 return this.each(function(){
   var sections = $('div.section');
   var tabs = $('<ul class="tabs"></ul>').prependTo('#content_main');
   sections.each(function(){
     var link = $('<a href="#">'+$('h2',this).text()+'</a>').click(function(){
       $(this).parent().addClass('on').siblings().removeClass('on');
       var parentIndex = $(this).parents('ul').children('li').index(this.parentNode);
       sections.hide().eq(parentIndex).show();
       return false;
     });
     $('<li></li>').append(link).appendTo(tabs);
   });
   sections.not(':first').hide();
   tabs.children(':first').addClass('first on');
 });
}

$.fn.slideMessage = function() {
 return this.each(function(){
   var message = $(this)
   message.hide();
   $('<a class="show_comments" href="#">My comments</a>').toggle(function(){
     $(this).addClass('open').next().slideDown();
     return false;
   },function(){
     $(this).removeClass('open').next().slideUp();
     return false;
   }).insertBefore(message);
 });
}

$.fn.clearDefault = function() {
 return this.each(function(){
   $(this).focus(function(){
     if($(this).attr('value') == $(this).attr('title')) $(this).attr('value','');
   }).blur(function(){
     if($(this).attr('value') == '') $(this).attr('value',$(this).attr('title'));
   });
 });
}
