﻿/*
* 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({
            changesFlag: null,   // object has the flag which indicate the source imgGroups have changed.
            autoplay: true,
            initialtime: 4000,
            playtime: 8000,
            gaPageTracker: null  // google tracker object
        }, 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);

//            if (settings.changesFlag != null && $("#" + settings.changesFlag).html() == "true") {
//                _init_rotator();
//                return;
//            }
            nextInx = getNextIndex(currentInx, increment);
            if (nextInx == currentInx) return;
            currentInx = nextInx;

            // 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("alt", (typeof (srcImg.parent("a").attr("href")) != "undefined") ? srcImg.parent("a").attr("href") : "");
                    targetImg.attr("title", (typeof (srcImg.parent("a").attr("href")) != "undefined") ? srcImg.parent("a").attr("href") : "");
                    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");

                    // for google analytics
                    try {
                        var url = (typeof (srcImg.parent("a").attr("gapage")) != "undefined") ? srcImg.parent("a").attr("gapage") : null;
                        var tracked = (typeof (srcImg.parent("a").attr("tracked")) != "undefined") ? srcImg.parent("a").attr("tracked") : false;
                        if (settings.gaPageTracker != null && url != null && url != "") {
                            targetImg.attr("gapage", url);
                        }
                        if (settings.gaPageTracker != null && url != null && url != "" && tracked == false) {
                            settings.gaPageTracker._trackPageview(url);
                            srcImg.parent("a").attr("tracked", true);
                        }
                    } catch (err) { }

                } else {
                    targetImg.attr("href", "");
                    targetImg.attr("target", "_blank");
                    targetImg.css("cursor", "");
                }

            });

        }

        function _init_rotator() {
            //console.log("init");
            if (initialtimer != null)
                window.clearTimeout(initialtimer);
            if (timer != null)
                window.clearTimeout(timer);

//            if (settings.changesFlag != null)
//                $("#" + settings.changesFlag).html("false");
            timer = null;
            initialtimer = null;
            cntImages = 99999;
            currentInx = 0;

            // 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 += "alt='" + ((typeof ($(this).parent("a").attr("href")) != "undefined") ? $(this).parent("a").attr("href") : "") + "' ";
                    imgHtml += "title='" + ((typeof ($(this).parent("a").attr("href")) != "undefined") ? $(this).parent("a").attr("href") : "") + "' ";

                    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' ";

                    // for google analytics
                    try {
                        var url = (typeof ($(this).parent("a").attr("gapage")) != "undefined") ? $(this).parent("a").attr("gapage") : null;
                        var tracked = (typeof ($(this).parent("a").attr("tracked")) != "undefined") ? $(this).parent("a").attr("tracked") : false;
                        if (settings.gaPageTracker != null && url != null && url != "") {
                            imgHtml += "gapage='" + url + "' ";
                        }
                        if (settings.gaPageTracker != null && url != null && url != "" && tracked == false) {
                            settings.gaPageTracker._trackPageview(url);
                            $(this).parent("a").attr("tracked", true);
                        }
                    } catch (err) { }

                } else
                    imgHtml += "href='' target='_blank' style='' ";
                imgHtml += ">";
            });
            $(target).html(imgHtml);
            $("img", target).click(function() {
                if (typeof ($(this).attr("href")) != "undefined" && $(this).attr("href") != "") {
                    // for google analytics
                    try {
                        var url = (typeof ($(this).attr("gapage")) != "undefined") ? $(this).attr("gapage") : null;
                        if (settings.gaPageTracker != null && url != null && url != "") {
                            settings.gaPageTracker._trackPageview(url + "/visit");
                        }
                    } catch (err) { }
                    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);
                };
                this.ClearTimer = function() {
                    window.clearTimeout(initialtimer);
                    window.clearTimeout(timer);
                    timer = null;
                };
            });
        }


        // main logic
        _init_rotator();
    }

})(jQuery);

