﻿
(function($) {
    $.fn.rotate = function(options) {
        // Do your awesome plugin stuff here
        var settings = { delay: 0 };
        $.extend(settings, options);
        var $rotatorcount = 0;
        return this.each(function() {
            var $this = $(this);
            var $imgs = $this.find('img');
            $imgs.fadeOut(0);
            $imgs.first().fadeIn(0);
            var index = 0;
            var t = setTimeout(StartIt, settings.delay);

            function StartIt() {
                t = setInterval(SwitchIt, 4000);
            }
            function SwitchIt() {
                var img1 = $imgs[index];
                index++;
                if (index > $imgs.length - 1) {
                    index = 0;
                }
                var img2 = $imgs[index];
                $(img1).fadeOut(1000);
                $(img2).delay(500).fadeIn(1000);
            }

        });

    };
})(jQuery);

(function($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.fn.preLoadImages = function(fileType) {

    return this.each(function() {
            var s = this.src;
            s = s.substring(0, s.indexOf(fileType, 0)) + '_sel' + fileType;            
            var cacheImage = document.createElement('img');
            cacheImage.src = s;
            cache.push(cacheImage);
        });

    }
})(jQuery);


(function($) {
    // Arguments are image paths relative to the current page.
    $.fn.hoverImages = function(suffix) {
        var file = "." + suffix.split('.')[1];
        var s = suffix;
        return this.each(function() {
            var hoversrc = this.src.replace(file, s);
            var i = new Image();
            i.src = hoversrc;
            var normalsrc = hoversrc.replace(s, file);
            $(this).hover(
                function() { this.src = hoversrc; },
                function() { this.src = normalsrc; }
            );

        });

    }
})(jQuery);

(function($) {

    $.fn.gallery = function() {

        return this.each(function() {
            var speed = 250;
            var $gallery = $(this);
            var $mainPic = $gallery.find(".main-photo");
            var $mainDet = $gallery.find(".main-details");
            var $strip = $gallery.find(".overflow");
            var $prev = $gallery.find("#img_prev");
            var $next = $gallery.find("#img_next");
            var $view = $gallery.find('.view');
            $next.click(function() {
                var $sel = $strip.find(".selected").first();
                var $new = $strip.find("#" + String(Number($sel.attr("id")) + 1));
                if ($new) {
                    $new.trigger('click');
                    if ($new.position().left > 770) {
                        $strip.scrollLeft($strip.scrollLeft() + $new.position().left);

                    }
                }
            });
            $prev.click(function() {
                var $sel = $strip.find(".selected").first();
                var $new = $strip.find("#" + String(Number($sel.attr("id")) - 1));
                if ($new) {
                    $new.trigger('click');
                    if ($new.position().left < 0) {
                        var pos = ($strip.scrollLeft() < 770) ? 0 : $strip.scrollLeft() - 770;
                        $strip.scrollLeft(pos);

                    }
                }
            });
            $gallery.find(".overflow td img").each(function() {
                $(this).click(
                    function() {
                        var $img = $mainPic.find("img");
                        var $thumb = $(this);
                        var $div = $mainPic.find("div");
                        $gallery.find("img.selected").removeClass("selected");
                        $thumb.addClass("selected");
                        //add details
                        var $h = $("<h1>");
                        var $p = $("<p>");
                        var $quote = $("<blockquote>");
                        $h.text($thumb.attr("gTitle"));
                        $p.text($thumb.attr("gDescription"));
                        $quote.text($thumb.attr("gTestimonial"));
                        $mainDet.html($h).append($p).append($quote);
                        //find new source
                        var newSrc = $thumb.attr("src");
                        newSrc = newSrc.replace("_tn", "");
                        //fade out main and replace image
                        var w = $thumb.attr("largeWidth");
                        var h = $thumb.attr("largeHeight");

                        $view.stop().animate({height: Number(h) + 10 }, speed);
                        $div.stop().animate({ width: w, height: h, left: (450 - w) / 2 }, speed);
                        $img.stop().fadeOut(speed).animate({height: h, width: w},speed, function(){
                            $img.unbind('load').load(function(){$img.fadeTo(speed,1.0);}).attr("src",newSrc);
                        });

                        return false;
                    }
                );
            });
            var $thumb2 = $gallery.find("img.selected");
            var w2 = $thumb2.attr("largeWidth");
            var h2 = $thumb2.attr("largeHeight");
            var $div2 = $mainPic.find("div");
            $div2.stop().animate({ width: w2, height: h2, left: (450 - w2) / 2 }, speed);
            $view.stop().animate({height: Number(h2) + 10},speed);
            
        });
    }
})(jQuery);
