/*!
 * 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.change
				(
					function()
					{
					  $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: 15});
		$('.home #popular_stores').wantacodePagination({itemsPerPage: 6, listingClass: 'listing2'});
		$('.home #top_products').wantacodePagination({itemsPerPage: 3, listingClass: 'listing2'});
		$('.home #sections').wantacodePagination({itemsPerPage: 5, listingClass: 'listing'});

    // 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);
      var txt = $(this).text().replace(/Copy to clipboard and open site$/,'');
      clip.setText(txt);
      clip.glue(this);
      clip.receiveEvent('mouseover', null);
      clip.addEventListener('mouseup', click_coupon_code);
    }).mouseout(function(){
      try {
        clip.receiveEvent('mouseout',null)
      } catch(err) {}
    });
	}
);

$(document).ready(function() {
  $.get('/blog', function(data) {
    $(data).find('.text-holder h1 a, .text-holder h2 a').slice(0,3).each(function(i, link) {
      $('.recent_post').append('<h3><a href="' + link + '">' + link.text + '</a></h3>');
    });
  });
})

