/**
 * jQuery jqGalViewII Plugin
 * Examples and documentation at: http://benjaminsterling.com/2007/10/02/jquery-jqgalviewii-photo-gallery/
 *
 * @author: Benjamin Sterling
 * @version: 0.5
 * @copyright (c) 2007 Benjamin Sterling, KenzoMedia
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * @requires jQuery v1.2.1 or later
 *
 *
 * @name jqGalViewII
 * @example $('ul').jqGalViewII();
 *
 */
(function($){
	$.fn.jqGalViewII = function(options){
		return this.each(function(index){
			var el = this, $_ = $(this); $img = $('img', $_);
			el.opts = $.extend({}, $.fn.jqGalViewII.defaults, options);

			var $this = $.fn.jqGalViewII.swapOut($_);

			var $container = $('<div class="gvIIContainer">').appendTo($this);

			el.mainImgContainer = $('<div class="gvIIImgContainer">').appendTo($container);
			el.image = $('<img/>').appendTo(el.mainImgContainer);
			el.loader = $('<div class="gvIILoader"/>').appendTo(el.mainImgContainer);

			var $holder = $('<div class="gvIIHolder"/>').appendTo($container);

			var $arrow = $('<div class="gvIIArrow"/>');


			$(this).after($this).remove();

			$img.each(function(i){
				var $image = $(this);

				var $div = $('<div id="gvIIID'+i+'" class="gvIIItem">')
				.appendTo($holder)
				.append('<div class="gvIILoaderMini">');

				if(el.opts.getUrlBy == 0)
					this.altImg = $image.parent().attr('href');
				else if(el.opts.getUrlBy == 1)
					this.altImg = el.opts.fullSizePath + this.src.split('/').pop();
				else if(el.opts.getUrlBy == 2)
					this.altImg = $this.src.replace(el.opts.prefix,'');

				this.altTxt = $image.attr('alt');

				var image = new Image();
				image.onload = function(){
					image.onload = null;
					$div.empty().append($image);
					$('<div class="">').appendTo($div).css({opacity:".01"})
					.mouseover(
						function(){
							var $f = $(this);
							$f.css({opacity:".75"}).stop().animate({opacity:".01"},500);


						}
					)
					.click(
						function(){
							$image.trigger('click');
						}
					);
					$image.click(function(){
						$.fn.jqGalViewII.view(this,el);
					});
					if(i==0){
						$image.trigger('click');
						$image.siblings().trigger('mouseover');
					}
				};
				image.src = this.src;
			});

			$arrow.appendTo($holder);
		});
	};

	$.fn.jqGalViewII.view = function(img,el){
		if(typeof img.altImg == 'undefined') return false;
		var url = /\?imgmax/.test(img.altImg) ? img.altImg : img.altImg+'?imgmax=800';
		var $i_wh = {}; //
		var $i_whFinal = {}; //
		var wContainer, hContainer;
		var $w, $h, $wOrg, $hOrg, isOver = false;

		el.loader.show();

		wContainer = el.mainImgContainer.width();
		hContainer = el.mainImgContainer.height();
		el.mainImgContainer.show();

		el.image.attr({src:url}).css({top:0,left:0,position:'absolute'}).hide();
		$img = new Image();
		$img.onload = function(){
			$img.onload = null;
			$w = $wOrg = $img.width;
			$h = $hOrg = $img.height;

/*
			if ($w > wContainer) {
				$h = $h * (wContainer / $w);
				$w = wContainer;
				if ($h > wContainer) {
					$w = $w * (hContainer / $h);
					$h = hContainer;
				}
			} else if ($h > hContainer) {
				$w = $w * (hContainer / $h);
				$h = hContainer;
				if ($w > wContainer) {
					$h = $h * (wContainer / $w);
					$w = wContainer;
				}
			}
*/

            // 2009.05.13 start
            if ($w > wContainer) {
                $h = $h * (wContainer / $w);
            }
            $w = wContainer;
            if ($h != hContainer) {
                el.mainImgContainer.animate({
                    height: $h
                }, 500);
            }
            // 2009.05.13 end

			el.image.css({width:$w,height:$h})
			el.loader.fadeOut('fast',function(){el.image.fadeIn();});
		};
		$img.src = url;

	};
	$.fn.jqGalViewII.swapOut = function($el){
		var id = $el.attr('id') ? (' id="'+$el.attr('id')+'"') : '';
		var $this = $('<div' + id + '>');
		return $this;
	};

	$.fn.jqGalViewII.defaults = {
		getUrlBy : 0,
		fullSizePath : null,
		prefix: 'thumbnail.',
		arrowEase:'easeout'
	};
})(jQuery);



    $(document).ready(function(){
        $('.gallery').jqGalViewII();
    });

