$(document).ready(function() {

	// Add body class
	$('body').addClass('js');

	// Slideshow of images
	$article = $('.article div.image');
	
	$('.containimages.multiple').append('<a href="#" class="left pag">Back</a><a href="#" class="right pag">Forward</a>');
	$('.containimages a').click(function() {
		return false;
	});
	
	$('.article div.image').each(function() {
		var $this = $(this);
		$this.cycle({
			 next: $this.parent().find('.left'),
			 prev: $this.parent().find('.right'),
			 timeout:0,
speed:0,
timeout:0,
			 after: onAfter
		});

	});
	
	$('.viewmore .left, .viewmore .right').click(function() {
		return false;
	});

	function onAfter(curr, next, opts, fwd){
		var $ht = $(this).height();
		if($(this).parent().height() > $ht) {
			$(this).parent().animate({ height: $ht }, 500);
		} else {
			$(this).parent().css('height', $ht);
		}
	}


	// arrows stuff
	var $arrows;
	$arrows = $('#section .article .containimages a.left, #section .article .containimages a.right');
	setTimeout(function(){ 
		$arrows.fadeOut(2000);
	}, 2000);
	
	$arrows.css('top', (Math.ceil($arrows.parent().find('.image').children('img:smallest').height()/2)) + 'px');

	$('.article .image, .article .containimages a').hover(function() {
		$(this).parent().find('a.pag', this).stop().fadeTo(500, 1);
	}, function() {
		$(this).parent().find('a.pag', this).stop().fadeTo(2000, 0);
	});
	
});


$.extend($.expr[":"], {
  smaller: function(elem, i, match) {
    var m = /^(\d+)(?:x(\d+))$/.exec(match[3]);
    if (!m) {
      return false;
    }
    var h = m[1];
    var w = m[2] || h;
    var t = $(elem);
    return t.height() <= h && t.width() <= w;
  },
  larger: function(elem, i, match) {
    var m = /^(\d+)(?:x(\d+))$/.exec(match[3]);
    if (!m) {
      return false;
    }
    var h = m[1];
    var w = m[2] || h;
    var t = $(elem);
    return t.height() >= h && t.width() >= w;
  },
  smallest: function(elem, i, match, set) {
    var areas = $.map(set, function(elem, index) {
      return $(elem).height() * $(elem).width();
    });
    for (var j=0; j<areas.length; j++) {
     // console.log("" + j + ": " + areas[i] + " / " + areas[j]);
      if (areas[j] < areas[i]) {
        return false;
      }
    }
    return true;
  },
  largest: function(elem, i, match, set) {
    var areas = $.map(set, function(elem, index) {
      return $(elem).height() * $(elem).width();
    });
    for (var j=0; j<areas.length; j++) {
      if (areas[j] > areas[i]) {
        return false;
      }
    }
    return true;
  }
});




