/*!
 * wantacode
 *
 * @category    wantacode
 * @package     js
 * @copyright   Copyright (c) 2010 Worry Free Labs, LLC. (http://worryfreelabs.com/)
 * @author      Oleksandr Bernatskyi
 */

function click_coupon_code(client) {
  window.location = $(client.domElement).attr('href');
}

$(document).ready
(
	function()
	{
		/**
		 * Forms
		 */
		$('#header form div:has(input[type="text"]), #footer form div:has(input[type="text"])').each
		(
			function()
			{
				var $container = $(this);
				var $label = $('label', $container);
				var $input = $(':input', $container);
				
				if ($input.val().length > 0)
				{
					$label.css('visibility', 'hidden');
				}
				
				$input.focus
				(
					function()
					{
						$label.css('visibility', 'hidden');
					}
				);
				
				$input.blur
				(
					function()
					{
						if ($input.val().length == 0)
						{
							$label.css('visibility', 'visible');
						}
					}
				);
			}
		);
		
		
		/**
		 * Navigation: Categories
		 */
		$('#header ul.nav li.categories')
			.filter('.active')
				.addClass('force-active')
				.end()
			.hover
			(
				function()
				{
					$(this)
						.addClass('active')
						.find('> a')
							.addClass('open')
							.end()
						.find('> div')
							.show();
				},
				function()
				{
					$(this)
						.not('.force-active')
							.removeClass('active')
							.end()
						.find('> a')
							.removeClass('open')
							.end()
						.find('> div')
							.hide();
				}
			);
		
		
		/**
		 * Pagination
		 */
		$('.home #main').wantacodePagination({itemsPerPage: 10});
		$('.home #popular_stores').wantacodePagination({itemsPerPage: 6, listingClass: 'listing2'});
		$('.home #top_products').wantacodePagination({itemsPerPage: 6, listingClass: 'listing2'});

    // Positions top products. Doing via css took too long.
    // Should become unnecessary when paperclip is used to store product images.
    $(window).load(function(){
      var max = 0;
      $(".home #top_products .listing2 li img").each(function(){
        h = $(this).height();
        if (h > max) max = h;
      }).each(function(){
        $(this).css('padding-top', (max - $(this).height()) + 'px');
      });
    });
 
		$('#best-coupons').wantacodePagination({itemsPerPage: 8});
		$('#storewide-coupons').wantacodePagination({itemsPerPage: 8});
		
		$('.search #main').wantacodePagination({itemsPerPage: 10});
		$('.search #sidebar .block').wantacodePagination({itemsPerPage: 8, listingClass: 'listing2'});

    /**
     * Onclicks
     */
    $('.stores .featured .show_more').click(function(){$(this).hide().siblings('.ellip').hide().siblings('.more').fadeIn('fast')});

    /**
     * ZeroClipboard
     */
    var clip = new ZeroClipboard.Client();

    $('a.button.coupon').mouseover(function(){
      clip = null;
      clip = new ZeroClipboard.Client();
      clip.setHandCursor(true);
      clip.setText(this.innerHTML.split('<div')[0]);
      clip.glue(this);
      clip.receiveEvent('mouseover', null);
      clip.addEventListener('mouseup', click_coupon_code);
    }).mouseout(function(){
      try {
        clip.receiveEvent('mouseout',null)
      } catch(err) {}
    });

    /**
     * Subscription
     */
    $("#footer form").ajaxForm({
      target: '#message',
      success: function(){
        $('#footer form').find('input').attr('disabled','').removeClass('disabled');
      }
    }).submit(function(){
      $(this).find('input').attr('disabled','disabled').addClass('disabled');
    });

	}
);


