/**
 * jQuery jqGalViewII Plugin
 * Examples and documentation at: http://benjaminsterling.com/jquery-jqgalviewii-photo-gallery/
 *
 * @author: Benjamin Sterling
 * @version: 1.0
 * @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
 * 
 * changes:
 *   changed completely from the original.
 */
(function($) {
    $.fn.jqGalViewII = function(options) {
        return this.each(function(i) {
            var el = this;
            el.jqthis = $(this);
            el.jqchildren = el.jqthis.find('img');
            el.opts = $.extend({}, jqGalViewII.defaults, options);
            el.index = i;
            el.totalChildren = el.jqchildren.size();
            el.jqjqviewii = jqGalViewII.swapOut(el);

            el.container = $('<div class="gvIIOuterContainer"/>').appendTo(el.jqjqviewii);
            el.holder = $('<div class="gvIIThumbnailsContainer"/>').appendTo(el.container);

            if (el.totalChildren == 0)
                el.jqjqviewii.css('display', 'none');
            el.jqthis.after(el.jqjqviewii).remove();


            el.imgCw = el.container.width();
            el.imgCh = el.container.height();

            el.jqchildren.each(function(j) {
                var jqimage = $(this);
                var tmpimage = this;

                tmpimage.index = j;

                var jqdiv = $('<div id="gvIIID' + j + '" class="gvIIItem">')
				.appendTo(el.holder); // end : $div
                //.append('<div class="gvIILoaderMini">')

                if (el.opts.getUrlBy == 0) {
                    tmpimage.altImg = jqimage.parent().attr('href');
                }
                else if (el.opts.getUrlBy == 1) {
                    tmpimage.altImg = el.opts.fullSizePath + tmpimage.src.split('/').pop();
                }
                else if (el.opts.getUrlBy == 2) {
                    tmpimage.altImg = tmpimage.src.replace(el.opts.prefix, '');
                };


                tmpimage.fullName = jqimage.attr('fullname');
                tmpimage.f0 = jqimage.attr('f0');
                tmpimage.f1 = jqimage.attr('f1');

                var image = new Image();
                image.onload = function() {
                    image.onload = null;
                    jqdiv.empty().append(jqimage);

                    var margins = jqGalViewII.center({ "w": jqdiv.width(), "h": jqdiv.height() }, { "w": image.width, "h": image.height });

                    //jqimage.css({marginLeft:margins.l,marginTop:margins.t});

                    jqimage.mouseover(
						function(e) {
						    $(".gvIIItem").css("border-color", "#fff");
						    jqdiv.css("border-color", "#D50D5B");
						    $("#userProfile").remove()
						    if (tmpimage.fullName.length > 0) {
						        $("body").append("<div id='userProfile'><span class='top'></span><div class='content'><span class='fullname'>" + tmpimage.fullName + "</span>"
						        + (tmpimage.f0.length > 0 ? "<br/><strong>ik was: </strong>" + tmpimage.f0 : "")
						        + (tmpimage.f1.length > 0 ? "<br/><strong>ik ben nu: </strong>" + tmpimage.f1 : "") + "</div><span class='bottom'></span></div>");
						        $("#userProfile")
			                        .css("top", (e.pageY - 10) + "px")
			                        .css("left", (e.pageX + 15 - 265) + "px")
			                        .fadeIn(400);
						    }
						}
					)
					.click(function() {
					    jqGalViewII.view(this, el);
					}).mouseout(function() {
					    $("#userProfile").fadeOut(400);
					    setTimeout(function() { jqdiv.css("border-color", "#fff"); }, 300);
					}).mousemove(function(e) {
					    $("#userProfile")
	                        .css("top", (e.pageY - 10) + "px")
	                        .css("left", (e.pageX + 15 - 265) + "px")
					});
                }; // end : image.onload 
                image.src = tmpimage.src;
            });
        });
    };

    jqGalViewII = {
        //pDem parent deminsions
        //iDem img deminsions
        resize: function(pDem, iDem) {
            if (iDem.w > pDem.w) {
                iDem.h = iDem.h * (pDem.w / iDem.w);
                iDem.w = pDem.w;
                if (iDem.h > pDem.w) {
                    iDem.w = iDem.w * (pDem.h / iDem.h);
                    iDem.h = pDem.h;
                };
            } else if (iDem.h > pDem.h) {
                iDem.w = iDem.w * (pDem.h / iDem.h);
                iDem.h = pDem.h;
                if (iDem.w > pDem.w) {
                    iDem.h = iDem.h * (pDem.w / iDem.w);
                    iDem.w = pDem.w;
                };
            };

            return iDem;
        },
        center: function(pDem, iDem) {
            return { "l": (pDem.w - iDem.w) * .5, "t": (pDem.h - iDem.h) * .5 };
        },
        swapOut: function(el) {
            return $('<div id="jqgvii' + el.index + '">');
        },
        view: function(img, el) {
            if (typeof img.altImg == 'undefined') return false;
            var url = img.altImg;
            if (/picasa/.test(url)) {
                url = /\?/.test(img.altImg) ? '&imgmax=800' : '?imgmax=800';
            };
            document.location = url;
        },
        defaults: {
            getUrlBy: 0, // 0 == from parent A tag | 1 == the full size resides in another folder
            fullSizePath: null,
            prefix: 'thumbnail.',
            titleOpacity: .60
        }
    };
})(jQuery);
