﻿/* jQuery GMCarousel Plugin */
(function($) {
    $.fn.GMCarousel = function(options) {
        return this.each(function(i) {
            // Settings
            var el = this;
            el.acceptClicks = true;
            el.curImage = 0;
            el.jqthis = $(this).css({ position: 'relative' });

            el.opts = $.extend({}, GMCarousel, options);

            el.jqchildren = el.jqthis.children();
            el.totalChildren = el.jqchildren.size();

            el.ImageTitles = new Array();

            if (el.totalChildren == 1) {
                el.opts.type = "none";
                el.opts.displayPlayPause = false;
                el.opts.displayNextPrevious = false;
            };

            el.index = i;

            // Set the width and height
            var width, height;
            switch (el.opts.type) {
                case 'vertical':
                    width = el.opts.width;
                    height = el.opts.height;
                    break;
                case 'fade':
                    width = el.opts.width;
                    height = el.opts.height;
                    break;
                default: //Horizontal
                    width = el.totalChildren * el.opts.width;
                    height = el.opts.height;
                    break;
            };

            // The Container
            el.container = $('<div id="GMC' + i + '" class="GMCContainer">').css({ position: 'relative', width: el.opts.width });
            el.ImgContainer = $('<div class="GMCImgContainer" style="height:' + el.opts.height + 'px;position:relative;overflow:hidden">')
                                .css({ height: el.opts.height, width: el.opts.width, position: 'relative', overflow: 'hidden' });
            el.jqthis.css({ height: height, width: width });

            el.jqthis.wrap(el.container);
            el.jqthis.wrap(el.ImgContainer);


            if (el.opts.displayControlPanel) {
                el.controlPanel = $('<div class="GMCControlPanel">');

                /* PAGINATION */
                el.pagination = $('<div class="GMCPagination">');
                el.pagination.appendTo(el.controlPanel);

                if (el.opts.displayCountLabel) {
                    el.label = $('<p id="GMCLabel' + i + '">' + (el.curImage + 1) + ' of ' + el.totalChildren + '</p>').appendTo(el.pagination);
                };

                if (el.opts.displayNextPrevious) {
                    var jqulNP = $('<ul>').appendTo(el.pagination);
                    /* NEXT PREVIOUS*/
                    el.previousButton = $('<a href="javascript:void(0);" title="Move to previous image">Previous</a>').click(function() {
                        var image = (el.curImage == 0) ? el.totalChildren - 1 : el.curImage - 1;
                        $('div.GMCPagination a#c' + el.index + 'link' + image).click();
                    });
                    $('<li class="prev">').appendTo(jqulNP).append(el.previousButton);

                    el.previousButton.tooltip({
                        bodyHandler: function() {
                            var previous = (el.curImage == 0) ? el.totalChildren - 1 : el.curImage - 1;
                            return el.ImageTitles[previous].toString();
                        },
                        showURL: false
                    });

                    el.nextButton = $('<a href="javascript:void(0);" title="Move to next image">Next</a>').click(function() {
                        el.MoveNext();
                    });
                    $('<li class="next">').appendTo(jqulNP).append(el.nextButton);

                          el.nextButton.tooltip({
                        bodyHandler: function() {
                            var next = (el.curImage == el.totalChildren - 1) ? 0 : el.curImage + 1;
                            return el.ImageTitles[next].toString();
                        },
                        showURL: false
                    });

                };

                if (el.opts.displayPagination) {
                    var jqul = $('<ol class="GMCPages">').appendTo(el.pagination);
                };


                /* PLAY PAUSE */
                if (el.opts.displayPlayPause) {
                    el.playPauseContainer = $('<div class="GMCPlayPause">');

                    el.playLink = $('<li class="play"><a href="javascript:void(0);" id="GMCPlay' + el.index + '" title="Play">Play</a></li>').click(function() {
                        el.MoveNext(true);
                        el.StartRotation();
                    });

                    el.playLink.tooltip({ showURL: false });

                    el.pauseLink = $('<li class="pause"><a href="javascript:void(0);" id="GMCPause' + el.index + '" title="Pause">Pause</a></li>').click(function() {
                        el.StopRotation();
                    });

                    el.pauseLink.tooltip({ showURL: false });

                    var playPauseUl = $('<ul>').appendTo(el.playPauseContainer);

                    playPauseUl.append(el.playLink);
                    playPauseUl.append(el.pauseLink);
                    el.playPauseContainer.insertAfter(el.pagination);

                    el.playPauseContainer.appendTo(el.controlPanel);
                };

                if (el.opts.controlPanelAfterCarousel) {
                    el.jqthis.parent().parent().append(el.controlPanel);
                }
                else {
                    el.jqthis.parent().before(el.controlPanel);
                };
            };


            var pos = { x: 0, y: 0 };

            // for each image
            el.jqchildren.each(function(j) {

                if (el.opts.type == 'horizontal') {
                    pos.x = j * el.opts.width;
                }
                else if (el.opts.type == 'vertical') {
                    pos.y = j * el.opts.height;
                };

                var jqchild = $(this).attr("id", "c" + i + "img" + j).css({ height: el.opts.height, width: el.opts.width, position: 'absolute', left: pos.x, top: pos.y });

                if (el.opts.type == 'fade' && j != 0) {
                    // Hide the subsequent images if faded
                    jqchild.hide();
                };

                var jqimg = jqchild.find('img').hide();

                if (jqimg.parent().is('a')) {

                    var p = jqimg.parent();

                    jqimg.linkHref = p.attr('href');
                    jqimg.linkTarget = p.attr('target');
                    jqimg.linkTitle = p.attr('title');
                    p.remove();

                    var $link = $('<a href="' + jqimg.linkHref + '" target="' + jqimg.linkTarget + '" title="' + jqimg.linkTitle + '"></a>');

                    jqimg.appendTo(jqchild);
                    jqimg.wrap($link);

                };

                /* Add to Pagination */
                if (el.opts.displayControlPanel) {
                    var selected = (j == 0) ? 'selected' : '';

                    var $a = $('<a id="c' + (i) + 'link' + (j) + '" href="#' + (j) + '" class="' + selected + '" title="' + jqimg.linkTitle + '">' + (j + 1) + '</a>').click(function() {
                        el.MoveToImage(this.index, false);
                    });
                    if (jqimg.linkTitle == null || jqimg.linkTitle.length < 1)
                        el.ImageTitles[j] = "Image " + (j + 1);
                    else
                        el.ImageTitles[j] = jqimg.linkTitle;

                    $a.tooltip({ showURL: false });
                    var n = $a.get(0);

                    n.index = j;

                    $('<li>').appendTo(jqul).append($a);
                };


                /* TITLE BAR */
                var $loader = $('<div class="GMCLoader">').appendTo(jqchild);
                var image = new Image();
                image.onload = function() {
                    image.onload = null;
                    $loader.fadeOut();
                    jqimg.css({ marginLeft: -image.width * .5, marginTop: -image.height * .5, position: 'absolute', left: '50%', top: '50%' }).fadeIn();
                };
                image.src = jqimg.attr('src');
            });

            el.MoveNext = function(automatic) {
                var image = (el.curImage == el.totalChildren - 1) ? 0 : el.curImage + 1;
                el.MoveToImage(image, automatic);
            };

            el.MoveToImage = function(image, automatic) {
                if (el.acceptClicks) {
                    // Disable clicks
                    el.acceptClicks = false;

                    if (!automatic) el.StopRotation();

                    var href = image; //href.replace(/^.*#/, '');
                    if (el.pagination) {
                        el.pagination.find('.selected').removeClass('selected');
                        $('div.GMCPagination a#c' + el.index + 'link' + image).addClass('selected');
                    };

                    if (el.opts.type == 'fade') {
                        $('div.GMCImgContainer li#c' + el.index + 'img' + el.curImage).fadeOut(el.opts.speed, function() {
                            $('div.GMCImgContainer li#c' + el.index + 'img' + href).fadeIn(el.opts.speed, function() {
                                el.acceptClicks = true;
                            });
                        });
                    }
                    else {
                        var params = {};

                        var firstToLast = (el.curImage == 0 && href == el.totalChildren - 1) ? true : false;
                        var lastToFirst = (el.curImage == el.totalChildren - 1 && href == 0) ? true : false;

                        if (el.totalChildren < 3) firstToLast = lastToFirst = false;

                        if (el.opts.type == 'vertical') {
                            if (firstToLast || lastToFirst) {
                                var $temp = $("li#c" + i + "img" + href).clone().attr("id", "GMC" + i + "_Temp").attr("class", "GMC" + i + "_Temp");

                                if (firstToLast) {
                                    $temp.css({ top: "-" + el.opts.height });
                                    $temp.prependTo($("div#GMC" + i + " div.GMCImgContainer ul"));
                                    params = { bottom: (0 - el.opts.height) };
                                }
                                else {
                                    $temp.css({ top: (el.opts.height * el.totalChildren) + "px" });
                                    $temp.appendTo($("div#GMC" + i + " div.GMCImgContainer ul"));
                                    params = { bottom: (el.opts.height * (el.totalChildren)) };
                                };
                            }
                            else {
                                params = { bottom: (el.opts.height * href) };
                            };
                        }
                        else if (el.opts.type == 'horizontal') {
                            if (firstToLast || lastToFirst) {
                                var $temp = $("li#c" + i + "img" + href).clone().attr("id", "GMC" + i + "_Temp").attr("class", "GMC" + i + "_Temp");

                                if (firstToLast) {
                                    $temp.css("left", "-" + el.opts.width + "px");
                                    $temp.prependTo($("div#GMC" + i + " div.GMCImgContainer ul"));
                                    params = { right: (0 - el.opts.width) };
                                }
                                else {
                                    $temp.css({ left: el.jqthis.css("width") });
                                    $temp.appendTo($("div#GMC" + i + " div.GMCImgContainer ul"));
                                    params = { right: (el.opts.width * (el.totalChildren)) };
                                };
                            }
                            else {
                                params = { right: (el.opts.width * href) };
                            };
                        };

                        el.jqthis.stop().animate(params, el.opts.speed, function() {

                            if (firstToLast || lastToFirst) {
                                if (el.opts.type == 'horizontal')
                                    el.jqthis.css({ right: (el.opts.width * href) });
                                else
                                    el.jqthis.css({ bottom: (el.opts.height * href) });
                            };
                            // Delete the temp
                            $("li.GMC" + i + "_Temp").remove();
                            el.acceptClicks = true;
                        });
                    };

                    index = href;

                    el.curImage = href;

                    if (el.label) {
                        el.label.text((el.curImage + 1) + ' of ' + el.totalChildren);
                    };
                    return false;

                }
            };


            /* AUTO ROTATION AND PLAY PAUSE */

            var autoRotation = 0;

            el.Rotate = function() {
                el.MoveNext(true);
            };

            el.StopRotation = function() {

                if (autoRotation > 0) {
                    window.clearTimeout(autoRotation);
                    autoRotation = 0;
                    if (el.pauseLink) el.pauseLink.hide();
                    if (el.playLink) el.playLink.css({ display: "" });
                };
            };

            el.StartRotation = function() {
                if (autoRotation == 0) {
                    autoRotation = window.setInterval(function() { el.Rotate(); }, el.opts.delay * 1000);
                    if (el.pauseLink) el.pauseLink.css({ display: "" });
                    if (el.playLink) el.playLink.hide();
                };
            };

            if (el.opts.autoRotate) {
                if (el.playLink) {
                    el.playLink.hide();
                };
                el.StartRotation();
            }
            else {
                if (el.pauseLink) {
                    el.pauseLink.hide();
                };
            };
        }); // End function this.each 

    }; // End function $.fn.GMCarousel
    GMCarousel = {
        height: 500,
        width: 500,
        delay: 6,
        titleOpacity: .90,
        autoRotate: true,
        controlPanelAfterCarousel: true, // Determines whether the control panel is rendered after images to allow the control panel to sit on top if necessary
        displayControlPanel: true,
        displayPagination: true,
        displayPlayPause: true,
        displayNextPrevious: true,
        displayCountLabel: false, // Displays a 1 of 12 label
        speed: 800,
        type: 'horizontal' // horizontal vertical fade
    }; // Initial variables
})(jQuery);               // End of function


$(document).ready(function() {
	$("ul#_carousel").GMCarousel({
		height: 399,
		width: 311,
		delay: 12,
		speed: 400,
		autoRotate: true,
		type: "fade",
		controlPanelAfterCarousel: true,
		displayControlPanel: true,
		displayPagination: true,
		displayPlayPause: true,
		displayNextPrevious: true,
		displayCountLabel: false
	});
});