// -----------------------------------------------------------------------------------
//
//	Gallerybox 1.0
//	Author: Dinesk Kumar V
//  Created: 01-02-09
//
//	For more information, visit:
//	http://sam-sys.com/gallerybox.html
//
// -----------------------------------------------------------------------------------

(function($) {
    $.fn.galleryBox = function(settings) {

        settings = jQuery.extend({		// Settings to configure the jQuery galleryBox plugin
            olayBgColor: '#000',
            olayOpacity: 0.8,
            imgResizeSpeed: 600,
            initContentWidth: 200,
            initContentHeight: 200,
            contentWidth: 300,
            contentHeight: 400,
            timeoutTimer: false,
            keyToClose: 'c', 	// (string) (c = close) Letter to close the jQuery galleryBox interface.
            keyToPrev: 'p', 	// (string) (p = previous) Letter to show the previous image
            keyToNext: 'n', 	// (string) (n = next) Letter to show the next image.
            imgArray: [],
            galArray: [],
            imgActive: 0,
            galActive: 0
        }, settings);
        var jQueryObj = this;

        function _initialize() {
            _start(this, jQueryObj);
            return false;
        }

        function _start(objClicked, jQueryObj) {
            $('embed, object, select').css({ 'visibility': 'hidden' });
            settings.imgArray.length = 0;
            settings.galArray.length = 0;
            settings.imgActive = 0;
            settings.galActive = 0;
            var thumpath = ''; gallname = '';
            var myRE = /(\[(\w|\.|\/|\-|\:)+\])/g;
            if (jQueryObj.length == 1) {
                //thuminfo = objClicked.rel.split(" ");
                thuminfo = jQueryObj[i].rel.match(myRE);
                if (thuminfo.length > 1) thumpath = thuminfo[1].substring(1, thuminfo[1].length - 1);
                if (thuminfo.length > 0) gallname = thuminfo[0].substring(1, thuminfo[0].length - 1);
                settings.imgArray.push(new Array(new Array(objClicked.getAttribute('href'), thumpath, objClicked.getAttribute('title'))));
                settings.galArray.push(gallname);
            } else {
                for (var i = 0; i < jQueryObj.length; i++) {
                    thumpath = ''; gallname = '';
                    thuminfo = jQueryObj[i].rel.match(myRE);
                    if (thuminfo.length > 1) thumpath = thuminfo[1].substring(1, thuminfo[1].length - 1);
                    if (thuminfo.length > 0) gallname = thuminfo[0].substring(1, thuminfo[0].length - 1);
                    galkey = jQuery.inArray(gallname, settings.galArray);
                    if (galkey < 0) {
                        settings.imgArray.push(new Array(new Array(jQueryObj[i].getAttribute('href'), thumpath, jQueryObj[i].getAttribute('title'))));
                        settings.galArray.push(gallname);
                    }
                    else {
                        settings.imgArray[galkey].push(new Array(jQueryObj[i].getAttribute('href'), thumpath, jQueryObj[i].getAttribute('title')));
                    }

                    if (objClicked.getAttribute('href') == jQueryObj[i].getAttribute('href')) {
                        galkey = jQuery.inArray(gallname, settings.galArray);
                        settings.imgActive = settings.imgArray[galkey].length - 1;
                        settings.galActive = galkey;
                    }

                }

            }
            _buildBox();
            _buildEvents();
            _buildBottomImg();
            _buildContentArea();
            _LoadImage();
        }

        function _buildBox() {

            $('body').append('<div id="gallerybox-overlay"></div><div id="gallerybox-outter"><div id="gallerybox-topbox"><span><a href="#" id="gallerybox-menubox">Category</a><div id="gallerybox-title">' + settings.galArray[settings.galActive] + ' - ' + settings.imgArray[settings.galActive][settings.imgActive][2] + '</div></span></div><div id="gallerybox-menubox-item"></div><div id="gallerybox-content-left"></div><div id="gallerybox-content-right"></div><div id="gallerybox-content"><img id="gallerybox-image"/><div id="gallerybox-image-overlay"></div></div><div id="gallerybox-bottombox"><div id="gallerybox-bottombox-in"></div></div></div>');

            for (var i = 0; i < settings.galArray.length; i++)
                $('#gallerybox-menubox-item').append('<a href="#" rel="' + i + '" title="' + settings.galArray[i] + '">' + settings.galArray[i] + '</a>');

            if ($.browser.msie) {
                $('#gallerybox-overlay').css({
                    backgroundColor: settings.olayBgColor, opacity: settings.olayOpacity, width: $(document.body).width(), height: $(document.body).height()
                }).fadeIn();
                $('#gallerybox-bottombox').css({ width: '98%' });
            } else {
                $('#gallerybox-overlay').css({
                    backgroundColor: settings.olayBgColor, opacity: settings.olayOpacity, width: $(document).width(), height: $(document).height()
                }).fadeIn();
            }

            $('#gallerybox-outter').css({
                width: $(window).width(), height: $(window).height(), top: $(window).scrollTop(), left: $(window).scrollLeft()
            });

            $('#gallerybox-bottombox').css({ top: $('#gallerybox-outter').height() - ($('#gallerybox-bottombox').height() + 20) });

            settings.contentWidth = $('#gallerybox-outter').width() - 380;
            settings.contentHeight = $('#gallerybox-outter').height() - ($('#gallerybox-bottombox').height() + 70);
        }

        function _buildEvents() {
            $('#gallerybox-overlay').click(function() { _closeBox(); });
            $('#gallerybox-outter').click(function(event) { if (event.target.id == 'gallerybox-outter') _closeBox(); else return false; });
            $('#gallerybox-image').load(function(event) { _trasformBox(); });
            $('#gallerybox-bottombox').mousemove(function(event) {
                var position = $('#gallerybox-bottombox').position();
                var gwd = $('#gallerybox-bottombox').width();
                var iwd = $('#gallerybox-bottombox-in').width();

                if (iwd > gwd) {
                    var ratio = (100 / gwd) * (event.clientX - position.left - 10);
                    var leftc = ((iwd - gwd) / 100) * ratio;
                    $('#gallerybox-bottombox').scrollLeft(leftc);
                }
            });
            $(window).resize(function() {
                clearTimeout(settings.timeoutTimer);
                if ($.browser.msie)
                    $('#gallerybox-overlay').css({ width: $(document.body).width(), height: $(document.body).height() });
                else
                    $('#gallerybox-overlay').css({ width: $(document).width(), height: $(document).height() });

                $('#gallerybox-outter').css({ width: $(window).width(), height: $(window).height() });
                $('#gallerybox-bottombox').css({ top: $('#gallerybox-outter').height() - ($('#gallerybox-bottombox').height() + 20) });
                settings.contentWidth = $('#gallerybox-outter').width() - 380;
                settings.contentHeight = $('#gallerybox-outter').height() - ($('#gallerybox-bottombox').height() + 70);
                settings.timeoutTimer = setTimeout(function() { _LoadImage(); }, 200);
            });

            $(window).scroll(function() {
                $('#gallerybox-outter').css({ top: $(window).scrollTop(), left: $(window).scrollLeft() });
            });
            $('#gallerybox-outter').mousewheel(function(event, delta) {
                return false;
            });
            $('#gallerybox-menubox').toggle(function() { $('#gallerybox-menubox-item').fadeIn('normal'); }, function() { $('#gallerybox-menubox-item').fadeOut('fast'); });
            $('#gallerybox-menubox-item a').click(function() { _loadGallery(this); });

            $('#gallerybox-content-left').click(function() {
                if (settings.imgActive > 0) {
                    settings.imgActive--;
                    prv = $('a.gallerybox_b_img_s').prev();
                    $('a.gallerybox_b_img_s').removeClass('gallerybox_b_img_s').addClass('gallerybox_b_img');
                    prv.addClass('gallerybox_b_img_s');
                    _LoadImage();
                }
            });
            $('#gallerybox-content-right').click(function() {
                if (settings.imgActive < settings.imgArray[settings.galActive].length - 1) {
                    settings.imgActive++;
                    nxt = $('a.gallerybox_b_img_s').next();
                    $('a.gallerybox_b_img_s').removeClass('gallerybox_b_img_s').addClass('gallerybox_b_img');
                    nxt.addClass('gallerybox_b_img_s');
                    _LoadImage();
                }
            });
        }
        function _buildBottomImg() {
            gallerybox_bottombox = $('#gallerybox-bottombox-in');
            $('a.gallerybox_b_img, a.gallerybox_b_img_s').remove();
            gallerybox_bottombox.css({ width: 60 * settings.imgArray[settings.galActive].length + 5 });
            for (var i = 0; i < settings.imgArray[settings.galActive].length; i++) {

                if (settings.imgActive == i)
                    gallerybox_bottombox.append('<a href="javascript:void(0)" class="gallerybox_b_img_s" rel="' + i + '"><img width="100%" src="' + settings.imgArray[settings.galActive][i][1] + '" border="0"/></a>');
                else
                    gallerybox_bottombox.append('<a href="javascript:void(0)" class="gallerybox_b_img" rel="' + i + '"><img width="100%" src="' + settings.imgArray[settings.galActive][i][1] + '" border="0"/></a>');
                /*gallerybox_bottombox.append('<a href="javascript:void(0)" class="gallerybox_b_img" rel="'+i+'"><img width="100%" src="'+settings.imgArray[settings.galActive][i][1]+'" border="0"/></a>');*/
            }
            $('a.gallerybox_b_img, a.gallerybox_b_img_s').click(function() {
                settings.imgActive = parseInt(this.getAttribute('rel'));
                $('a.gallerybox_b_img_s').removeClass('gallerybox_b_img_s').addClass('gallerybox_b_img');
                this.className = 'gallerybox_b_img_s';
                _LoadImage();
            });
        }

        function _buildContentArea() {
            topc = ($('#gallerybox-outter').height() - (settings.initContentHeight + $('#gallerybox-bottombox').height())) / 2;
            leftc = ($('#gallerybox-outter').width() - settings.initContentWidth) / 2

            pstop = ((settings.initContentHeight - 150) / 2) + topc;

            $('#gallerybox-content').css({ width: settings.initContentWidth, height: settings.initContentHeight, top: topc, left: leftc });
            $('#gallerybox-content-left').css({ width: 150, height: 150, top: pstop, left: leftc, opacity: 1 });
            $('#gallerybox-content-right').css({ width: 150, height: 150, top: pstop, left: (settings.initContentWidth + leftc - 150), opacity: 1 });
        }


        function _loadGallery(obj) {
            $('#gallerybox-menubox').trigger('click');
            settings.galActive = parseInt(obj.getAttribute('rel'));
            settings.imgActive = 0;
            $('#gallerybox-bottombox-in').empty();
            _buildBottomImg();
            _LoadImage();
        }

        function _LoadImage() {
            $('#gallerybox-content,#gallerybox-image,#gallerybox-content-left,#gallerybox-content-right').stop();
            $('#gallerybox-image-overlay').fadeIn('slow', function() {
                $('#gallerybox-image').css({ width: '', height: '' });
                $('#gallerybox-image').attr('src', settings.imgArray[settings.galActive][settings.imgActive][0]);
            });
            var position = $('#gallerybox-content').position();            
            //pstop = (($('#gallerybox-content').height() - 150) / 2) + position.top;
            psleft = ($('#gallerybox-content').width() / 2);

            ///////////////7
            var hd = $('#gallerybox-image').height();
            xtop = ($('#gallerybox-outter').height() - (hd + $('#gallerybox-bottombox').height())) / 2;
            pstop = ((hd - 150) / 2) + xtop;
            ///////////////
            $('#gallerybox-content-left').animate({ top: pstop, left: psleft - 75 }, 500, function() { $('#gallerybox-content-left').hide() });
            $('#gallerybox-content-right').animate({ top: pstop, left: psleft - 75 }, 500, function() { $('#gallerybox-content-right').hide() });
            $('#gallerybox-title').html(settings.galArray[settings.galActive] + ' - ' + settings.imgArray[settings.galActive][settings.imgActive][2]);
        }



        function _trasformBox() {
            var wd = $('#gallerybox-image').width();
            var hd = $('#gallerybox-image').height();
            var ratio = 100;
            if (settings.contentWidth < wd && settings.contentHeight < hd) {
                ratio1 = (100 / wd) * settings.contentWidth;
                ratio2 = (100 / hd) * settings.contentHeight;
                if (ratio1 < ratio2) ratio = ratio1; else ratio = ratio2;

            }
            else if (settings.contentHeight < hd) ratio = (100 / hd) * settings.contentHeight;
            else if (settings.contentWidth < wd) ratio = (100 / wd) * settings.contentWidth;

            wd = (wd / 100) * ratio;
            hd = (hd / 100) * ratio;
            xtop = ($('#gallerybox-outter').height() - (hd + $('#gallerybox-bottombox').height())) / 2;
            yleft = ($('#gallerybox-outter').width() - wd) / 2;
            pstop = ((hd - 150) / 2) + xtop;
            if (wd < 0) wd = 10; if (hd < 0) hd = 10;
            $('#gallerybox-content').animate({ width: wd, height: hd, top: xtop - 10, left: yleft }, settings.imgResizeSpeed, function() { $('#gallerybox-image-overlay').fadeOut(function() { _trasformSideBox(); }); });

            if (ratio < 100) $('#gallerybox-image').css({ width: wd, height: hd });


        }

        function _trasformSideBox() {
            var position = $('#gallerybox-content').position();
            yleft = $('#gallerybox-content').width() + position.left + 20;

            pstop = (($('#gallerybox-content').height() - 150) / 2) + position.top;
            psleft = ($('#gallerybox-content').width() / 2) + position.left;
            if (settings.imgActive > 0) {
                $('#gallerybox-content-left').css({
                    top: pstop,
                    left: psleft - 75,
                    backgroundImage: "url(" + settings.imgArray[settings.galActive][settings.imgActive - 1][1] + ")"
                }).show();
                $('#gallerybox-content-left').animate({ top: pstop, left: position.left - 170 }, settings.imgResizeSpeed);
            }
            if (settings.imgActive < settings.imgArray[settings.galActive].length - 1) {
                $('#gallerybox-content-right').css({
                    top: pstop,
                    left: psleft - 75,
                    backgroundImage: "url(" + settings.imgArray[settings.galActive][settings.imgActive + 1][1] + ")"
                }).show();
                $('#gallerybox-content-right').animate({ top: pstop, left: yleft + 10 }, settings.imgResizeSpeed);
            }
        }

        function _closeBox() {
            $('#gallerybox-content,#gallerybox-image,#gallerybox-content-left,#gallerybox-content-right').stop();
            $('#gallerybox-outter').remove();
            $('#gallerybox-overlay').fadeOut(function() { $('#gallerybox-overlay').remove(); });
            $('embed, object, select').css({ 'visibility': 'visible' });
        }
        return this.unbind('click').click(_initialize);
    };
})(jQuery);
		  

/* Copyright (c) 2006 Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * $LastChangedDate$
 * $Rev$
 *
 * Version: @VERSION
 * 
 * Requires: $ 1.2.2+
 */

(function($) {

$.event.special.mousewheel = {
	setup: function() {
		var handler = $.event.special.mousewheel.handler;
		
		// Fix pageX, pageY, clientX and clientY for mozilla
		if ( $.browser.mozilla )
			$(this).bind('mousemove.mousewheel', function(event) {
				$.data(this, 'mwcursorposdata', {
					pageX: event.pageX,
					pageY: event.pageY,
					clientX: event.clientX,
					clientY: event.clientY
				});
			});
	
		if ( this.addEventListener )
			this.addEventListener( ($.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel'), handler, false);
		else
			this.onmousewheel = handler;
	},
	
	teardown: function() {
		var handler = $.event.special.mousewheel.handler;
		
		$(this).unbind('mousemove.mousewheel');
		
		if ( this.removeEventListener )
			this.removeEventListener( ($.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel'), handler, false);
		else
			this.onmousewheel = function(){};
		
		$.removeData(this, 'mwcursorposdata');
	},
	
	handler: function(event) {
		var args = Array.prototype.slice.call( arguments, 1 );
		
		event = $.event.fix(event || window.event);
		// Get correct pageX, pageY, clientX and clientY for mozilla
		$.extend( event, $.data(this, 'mwcursorposdata') || {} );
		var delta = 0, returnValue = true;
		
		if ( event.wheelDelta ) delta = event.wheelDelta/120;
		if ( event.detail     ) delta = -event.detail/3;
		if ( $.browser.opera  ) delta = -event.wheelDelta;
		
		event.data  = event.data || {};
		event.type  = "mousewheel";
		
		// Add delta to the front of the arguments
		args.unshift(delta);
		// Add event to the front of the arguments
		args.unshift(event);

		return $.event.handle.apply(this, args);
	}
};

$.fn.extend({
	mousewheel: function(fn) {
		return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
	},
	
	unmousewheel: function(fn) {
		return this.unbind("mousewheel", fn);
	}
});

})(jQuery);



$(document).ready(function () {							
    $("a[rel^='gallerybox']").galleryBox();
});
