$(function() {
	$('#product .pictures .thumbnails a').lightBox();
	$('#tabs').tabs();
});

$(function() {
	if( $('input:radio[name=checkout_method]').val() )
	{
		var methods = ['member','guest'];
		var method = $('input:radio[name=checkout_method]:checked');
		if(method.length > 0)
		{
			for( var i = 0; i < methods.length; i++ )
			{
				if( methods[i] != method.val() )
				{
					$('#checkout_'+methods[i]+'_form').hide();
				}
			}
		}
		else
		{
			for( var i = 0; i < methods.length; i++ )
			{
				$('#checkout_'+methods[i]+'_form').hide();
			}
		}

		var method_changed = function() {
			for( var i = 0; i < methods.length; i++ )
			{
				if( methods[i] == $(this).val() )
				{
					$('#checkout_'+methods[i]+'_form').slideDown('slow');
				}
				else
				{
					$('#checkout_'+methods[i]+'_form').slideUp('slow');
				}
			}
		}

		var method = $('input:radio[name=checkout_method]').bind('change', method_changed);
	}
});

$(function() {
  if( $('#dogs-form').length > 0 )
  {
    var has_dog1 = !!($('#id_dogname1').val() || $('.dog1-item .error_info').length > 0);
    var has_dog2 = !!($('#id_dogname2').val() || $('.dog2-item .error_info').length > 0);
    var has_dog3 = !!($('#id_dogname3').val() || $('.dog3-item .error_info').length > 0);

    if( !has_dog2 )
    {
      $('.dog2-item').hide();
    }
    if( !has_dog3 )
    {
      $('.dog3-item').hide();
    }

    if( !has_dog2 || !has_dog3 )
    {
      $('#dogs-form table').after("<a id='add-dog-link' href=''>Add another dog</a>");
      $('#add-dog-link').click(function() {
        if( !has_dog2 )
        {
          has_dog2 = true;
          $('.dog2-item').show();
          var replace = has_dog3;
        }
        else if( !has_dog3 )
        {
          has_dog3 = true;
          $('.dog3-item').show();
          var replace = true;
        }

        if( replace )
        {
          $(this).replaceWith("<p id='fake-add-dog-link'>You cannot add anymore dogs</p>");
        }

        return false;
      });
    }
  }
});

$(function() {
  var banner_menus = $('.banner-menu');

  for(var i = 0; i < banner_menus.length; i++) {
    var menu = banner_menus[i];
    var container = $('.banner-image', menu);
    var img = $('img', container)[0];
    var img_num = 0;
    var next_img = null;

    var images = $('.banner-thumbnails a', menu);
    var imgs = [img];
    var num_images = images.length < 1 ? 1 : images.length;
    var timeout = null;

    if(num_images == 1) {
      continue;
    }

    function load_images() {
      var thumbnail_click = function(event)
      {
        advanceToImage(event.data.index);
        return false;
      }

      for( var j = 0; j < num_images; j++ )
      {
        images[j] = $(images[j]);
        images[j].bind('click', {'index': j}, thumbnail_click);
      }

      for( var j = 1; j < num_images; j++ )
      {
        imgs.push(new Image());
        imgs[j].src = $('img',images[j]).attr('src').replace(/_thumbnails\/(.*)_(jpg|png|gif)_\d+x\d+_crop_q85\.jpg$/, '$1.$2');
        imgs[j].width = imgs[0].width;
        imgs[j].height = imgs[0].height;
      }

      for( var j = 0; j < num_images; j++ )
      {
        imgs[j] = $(imgs[j]);
      }
    }

    function advanceToImage(next_img_num) {
      if( !timeout )
      {
        //return;
      }

      clearTimeout(timeout);

      if( next_img_num == img_num )
      {
        timeout = setTimeout(advanceImage, 10000);
        return;
      }

      timeout = null;
      var img_to_remove = imgs[img_num];

      imgs[next_img_num].css('left',imgs[img_num].width());
      container.append(imgs[next_img_num]);
      images[img_num].removeClass('current');
      images[next_img_num].addClass('current');
      container.attr('href', images[next_img_num].attr('href'));
      imgs[next_img_num].animate({'left': 0}, 800, 'linear', function() {
        clearTimeout(timeout);
        timeout = setTimeout(advanceImage, 10000);
      });

      img_num = next_img_num;
    }

    function advanceImage() {
      var next_img_num = img_num+1;
      next_img_num = next_img_num % num_images;
      advanceToImage(next_img_num);
    }

    timeout = setTimeout(advanceImage, 6000);
    load_images();
  }
});

(function() {
 if( $('#charity_form') ) {
   var hidden_charity = $('<input type="hidden" name="charity" id="charity_hidden" />');
   var charity_autocomplete = $('<input type="text" name="charity_autocomplete" id="charity_autocomplete" />');

   $('#id_charity_label').attr('for',charity_autocomplete.id);

   var select_charity = $('#id_charity')

   var options = [];
   var options_els = $('option',select_charity);
   for(var i=0; i < options_els.length; i++) {
    var el = $(options_els[i]);
    options.push({
      'label': el.text(),
      'value': el.attr('value')
      });
    if( el.attr('selected') )
    {
      charity_autocomplete.val(el.text());
      hidden_charity.val(el.attr('value'));
    }
   }

   settings = {
    'matchContains': true,
    'formatItem': function(item) {
      return item.label;
    }
   };
   charity_autocomplete.autocomplete(options, settings).result(function(event, item) {
       console.log("chose " + item.value);
       hidden_charity.val(item.value);
     }).bind('blur', function() {
       var found = null;
       for( var i=0; i < options.length; i++ )
       {
         //console.log($(charity_autocomplete.val() + ' == ' + options[i].label);
         if( charity_autocomplete.val().toLowerCase() == options[i].label.toLowerCase() )
         {
           found = options[i];
           break;
         }
       }
       if( found )
       {
         //console.log('found');
         hidden_charity.val(found.value);
         charity_autocomplete.val(found.label);
       }
       else
       {
         //console.log('not found');
         hidden_charity.val('');
         charity_autocomplete.val('');
       }
     });

   select_charity.after(hidden_charity);
   select_charity.replaceWith(charity_autocomplete);
 }
})();

