/*
 * Jaba common library for jQuery 0.5.1
 *
 * Copyright (c) 2007 Jaba Multimedia (www.jaba.com.au)
 * Built upon jQuery 1.1.3.1 (http://jquery.com)
 * 

 * 1. Module : Image Rotator.
 * 2. Usage :
    <script type="text/javascript">
        $(document).ready(function(){
            var setting = {};
            $.JabaImageRotator($(".imgstore"), $("#showhere"), setting);
        });
        
        ... trigger
        $("#imgRotating").get(0).ChangeImages(-1);
    </script>
    <div id="showhere align=left ></div>
    <div class="imgstore" style="display: none;">
            <img src="/images/fordemo/demo1.jpg">
            <img src="/images/fordemo/demo2.jpg">
            <img src="/images/fordemo/demo3.jpg">
    </div>
    <div class="imgstore" style="display: none;">
            <img src="/images/fordemo/demo3.jpg">
            <img src="/images/fordemo/demo1.jpg">
            <img src="/images/fordemo/demo2.jpg">
    </div>    
 */

(function($){

    $.JabaImageRotator = function(imgGroups, target, settings) {  
        var settings = $.extend({
                autoplay : true,
                initialtime : 4000,
                playtime : 8000
            }, settings); 
        
        var timer = null;
        var initialtimer = null;
        var cntImages = 99999;
        var currentInx = 0;
        
        function getNextIndex(current, increment)
        {
            var currentIndex = 0;
            if (typeof(increment) == "undefined" || increment == null) increment = 1;
            if (current + increment < 0)
                currentIndex = cntImages - 1;
            else if (current + increment >= cntImages)
                currentIndex = 0;
            else 
                currentIndex = current + increment;
            return currentIndex;
        }
        
        function changeImages() {
            _changeImages(1);
        }
        
        function _changeImages(increment) {
            if (timer == null)
                timer = window.setInterval(changeImages, settings.playtime);
                
            currentInx = getNextIndex(currentInx, increment);

            // change image
            $(imgGroups).each(function() {
                var targetImg = $("." + target.attr("id") + "_imgs").eq($(imgGroups).index(this));
                var srcImg = $("img, imgmore", $(this)).eq(currentInx);
                if (targetImg.length <= 0) return;
                
                // change imgmore tag to img of next ==> it causes downloading and preparing the next image.
                var nextInx = getNextIndex(currentInx, increment);
                var nextImg = $("img, imgmore", $(this)).eq(nextInx);
//                if (nextImg.get(0).tagName.toLowerCase() == "imgmore")
//                    nextImg.get(0).tagName = "img";
                
                // start animation
                if ($.browser.msie) {
                    targetImg.get(0).style.filter="blendTrans(duration=2)";
                    targetImg.get(0).filters.blendTrans.Apply();
                    targetImg.get(0).src = srcImg.attr("src");
                    targetImg.get(0).filters.blendTrans.Play();
                } else {
                    targetImg.fadeTo("slow", 0.1, function() {
                            targetImg.attr("src", srcImg.attr("src"));
                            targetImg.fadeTo("slow", 1);
                        });
                }
                
                // for hyperlink
                if (srcImg.parent("a").size() > 0) {
                    targetImg.attr("href", (typeof(srcImg.parent("a").attr("href")) != "undefined")? srcImg.parent("a").attr("href") : "");
                    targetImg.attr("target", (typeof(srcImg.parent("a").attr("target")) != "undefined")? srcImg.parent("a").attr("target") : "_blank");
                    targetImg.css("cursor", "pointer");
                } else {
                    targetImg.attr("href", "");
                    targetImg.attr("target", "_blank");
                    targetImg.css("cursor", "");
                }
                
            });
            
        }
        
        // main logic
        
        // set the possible index
        if( $(imgGroups).length == 0) cntImages = 0;
        $(imgGroups).each(function() {
            if ($("img, imgmore", $(this)).length < cntImages) cntImages = $("img, imgmore", $(this)).length;
        });
        
        // place the first images.
        var imgHtml = "";
        $("img, imgmore", $(imgGroups)).eq(0).each(function() {
            imgHtml += "<img class='" + target.attr("id") + "_imgs" + "' src='" + $(this).attr("src") + "' ";
            // for hyperlink
            if ($(this).parent("a").size() > 0) {
                imgHtml += "href='" + ((typeof($(this).parent("a").attr("href")) != "undefined")? $(this).parent("a").attr("href") : "") + "' ";
                imgHtml += "target='" + ((typeof($(this).parent("a").attr("target")) != "undefined")? $(this).parent("a").attr("target") : "_blank") + "' ";
                imgHtml += "style='cursor:pointer' ";
            } else 
                imgHtml += "href='' target='_blank' style='' ";
            imgHtml += ">";
        });
        
        $(target).html(imgHtml);
        $("img", target).click(function() {
            if (typeof($(this).attr("href")) != "undefined" && $(this).attr("href") != "")
            {
                window.open($(this).attr("href"), $(this).attr("target"));
            }
        });
//        $("img", target).mouseover(function() {
//            if (typeof($(this).attr("href")) != "undefined" && $(this).attr("href") != "")
//            {
//                window.status = $(this).attr("href");
//            }
//        });
//        $("img", target).mouseout(function() {
//                window.status = "";
//        });
        
	    initialtimer = window.setTimeout(changeImages, settings.initialtime);

        // set the interface
        $(target).each(function() {
            this.ChangeImages = function(increment) {
                window.clearTimeout(initialtimer);
                window.clearTimeout(timer);
                timer = null;
                _changeImages(increment);
            };
        });
    }
   
})(jQuery);
