﻿/***************************
*  Landing Slideshow For Tnemec 
*  requires: jQuery
*  author: abe[at]lanit[dot]com     
****************************/
$.fn.loadImage = function(src, f){
    return this.each(function(){        
        $("<img>").appendTo(this).attr("src", src).load( f );
    });
};
var LandShow = {
    settings: {
        jsonUrl: null,
        slideTimeout: 7000,
        slideFadeOutTime: 2000,
        clickFadeOutTime: 500,
        slideIntervalId: null,
        pcontIntervalId: null,
        pcontOpacity: 0.85,
        pcontFadeInTime: 'slow',
        pcontFadeOutTime: 750,
        pcontFadeOutWait: 1500
    },
    isPaused: false,
    inTransition: false,
    jsonLoaded: false,
    images: null,
    imageLoading: false,
    previousIndex: -1,
    init: function(jsonUrl) {
        //get the href from each li and create an anchor tag
        $("#lb-photo-container li[href]").each(function() {
            var $li = $(this);
            var anchor = "<a onclick=\"javascript:window.open('" + $li.attr("href") + "', null, 'height=600, width=800, resizable=1, scrollbars=yes'); return false;\" href=\"" + $li.attr("href") + "\"></a>";
            if ($li.find("span").length > 0) {
                $li.find("span").wrap(anchor);
            }
            else {
                $li.html(anchor);
            }
            $li.removeAttr("href");
        });

        // LandShow.Init should be called by first image's onload event
        ls$.settings.jsonUrl = jsonUrl;
        $(window).load(function() {
            //setTimeout("$.getJSON(ls$.settings.jsonUrl, ls$.loadJSON)",10000);
            //$.getJSON(ls$.settings.jsonUrl, ls$.loadJSON);
            $.ajax({
                url: ls$.settings.jsonUrl,
                dataType: "text",
                success: ls$.loadJSON
            });
        });
    },
    hidePhotoControl: function() {
        var pcont = $("#lb .photo-control-wrapper");
        if (pcont.css('display') == 'block') {
            pcont.fadeOut(ls$.settings.pcontFadeOutTime);
        }
    },
    showPhotoControl: function() {
        window.clearTimeout(ls$.settings.pcontIntervalId);
        var pcont = $("#lb .photo-control-wrapper");
        if (pcont.css('display') == 'none') {
            //if (pcont[0].style.filter!=null){ // ie                    
            //    pcont[0].style.filter = 'alpha(opacity=0)';                    
            //}
            //pcont.css('opacity',0);

            pcont.fadeIn('slow');
        }
    },
    updatePhotoControls: function() {
        var fIndex = ls$.getNextIndex(ls$.previousIndex, 'forward');
        var bIndex = ls$.getNextIndex(ls$.previousIndex, 'back');
        var fimg = $("#lb-photo" + fIndex + " img");
        var bimg = $("#lb-photo" + bIndex + " img");
        if (!fimg.attr('imgLoaded') && $("#lb a.forward").length > 0) {
            $("#lb a.forward")[0].className = 'forward-dis';
        } else if ($("#lb a.forward-dis").length > 0) {
            $("#lb a.forward-dis")[0].className = 'forward';
        }

        if (!bimg.attr('imgLoaded') && $("#lb a.back").length > 0) {
            $("#lb a.back")[0].className = 'back-dis';
        } else if ($("#lb a.back-dis").length > 0) {
            $("#lb a.back-dis")[0].className = 'back';
        }
    },
    loadJSON: function(text) {
        var json = JSON.parse(text);
        ls$.images = json.images;
        ls$.jsonLoaded = true;
        ls$.loadImages();
    },
    findCurrentIndex: function() {
        var li = $("#lb .photo li");
        // get previous index
        for (var i = 0; i < li.length; i++) {
            var img = $(li[i]).find("img");
            if (img.length > 0) {
                img.attr('imgLoaded', 'true');
                return i;
            }
        }

        //This is a hack to get an image to show up every 5 times
        var imgObj = ls$.images[0];
        $("#lb-photo0 a").loadImage(imgObj.img, function() {
            //console.info('hello', count, ls$.images.length);
            $(this).attr('imgLoaded', 'true');
            ls$.updatePhotoControls();
            ls$.imageLoading = false;

            $("#lb-photo0").show();
        });
        //end of hack

        return 0;
    },
    getNextIndex: function(currentIndex, direction) {
        if (direction == 'back') {
            return (currentIndex == 0) ? ls$.images.length - 1 : currentIndex - 1;
        } else {
            return (currentIndex == ls$.images.length - 1) ? 0 : currentIndex + 1;
        }
    },
    loadNextImage: function(nextIndex, count) {

        if (ls$.imageLoading) {
            setTimeout("ls$.loadNextImage(" + nextIndex + "," + count + ")", 3000);
            return;
        }
        var a = $("#lb-photo" + nextIndex + " a");
        if ($(a).find("img").length == 0 && $(a).attr('imgLoaded') != 'true') {
            var imgObj = ls$.images[nextIndex];
            ls$.imageLoading = true;
            a.loadImage(imgObj.img, function() {
                //console.info('hello', count, ls$.images.length);
                $(this).attr('imgLoaded', 'true');
                ls$.updatePhotoControls();
                ls$.imageLoading = false;

                count++;
                if (count < ls$.images.length - 1) {
                    nextIndex = ls$.getNextIndex(nextIndex);
                    ls$.loadNextImage(nextIndex, count);
                }

            });
        }

    },
    loadImages: function() {
        var currentIndex = ls$.previousIndex = ls$.findCurrentIndex();
        var nextIndex = ls$.getNextIndex(currentIndex);

        ls$.loadNextImage(nextIndex, 0);
        ls$.loadImagesComplete();
    },
    loadImagesComplete: function() {

        ls$.setNextWait();

        /* Bind events */
        $("#lb-photo-container").bind("mouseover", function(e) {
            ls$.showPhotoControl();
        });
        $("#lb-photo-container").bind("mouseout", function(e) {
            ls$.settings.pcontIntervalId = window.setTimeout(ls$.hidePhotoControl, ls$.settings.pcontFadeOutWait);
        });
        $("#lb .photo-control-wrapper").bind("mouseover", function(e) {
            ls$.showPhotoControl();
        });
        $("#lb a.pause").click(function() {
            ls$.togglePlay();
            this.blur();
            return false;
        });
        $("#lb a.forward-dis").click(function() {
            ls$.next('forward', true);
            this.blur();
            return false;
        });
        $("#lb a.back-dis").click(function() {
            ls$.next('back', true);
            this.blur();
            return false;
        });
    },
    setNextWait: function(timeout) {
        if (timeout == null) {
            timeout = ls$.settings.slideTimeout;
        }
        window.clearTimeout(ls$.settings.slideIntervalId);
        if (ls$.isPaused) {
            return;
        }
        ls$.settings.slideIntervalId = window.setTimeout(function() {
            ls$.next('forward');
        }, timeout);
    },
    next: function(direction, isClick) {
        if (ls$.inTransition) { return; }
        // determine current index
        var nextIndex = ls$.getNextIndex(ls$.previousIndex, direction);
        var img = $("#lb-photo" + nextIndex + " img");

        if (img.attr('imgLoaded')) {
            ls$.doTransition(ls$.previousIndex, nextIndex, isClick);
        } else {
            ls$.setNextWait(500);
        }
    },
    doTransition: function(previousIndex, nextIndex, isClick) {
        ls$.updatePhotoControls();
        ls$.inTransition = true;
        var prev = $("#lb-photo" + previousIndex);
        var next = $("#lb-photo" + nextIndex);
        next.attr('style', '');
        prev[0].style.zIndex = 55; // setting values using DOM for ie
        next[0].style.zIndex = 50;
        next[0].style.display = 'block';
        var fadeOutTime = (isClick) ? ls$.settings.clickFadeOutTime : ls$.settings.slideFadeOutTime;
        prev.fadeOut(fadeOutTime, function() {
            ls$.setNextWait();
            ls$.inTransition = false;
        });
        ls$.previousIndex = nextIndex;
    },
    togglePlay: function() {
        //console.log("toggle play");            
        ls$.isPaused = !ls$.isPaused;
        if (!ls$.isPaused) {
            $("#lb a.play")[0].className = 'pause';
        } else {
            $("#lb a.pause")[0].className = 'play';
        }
        ls$.setNextWait();
    }
};
var ls$ = LandShow;