
//imageCycler v.1.0

var imagecyclers = [];

window.onload = init; 

if (!Array.indexOf) {
    Array.prototype.indexOf = function (si, start) {
        for (var i = (start || 0); i < this.length; i++) {
            if (this[i] == si) {
                return i;
            }
        }
        return -1;
    }
}

function init() {
    if (typeof jQuery != 'undefined') {
        imageCyclerInit();
    }
    else {
        a = document.getElementsByTagName("*");
        for (i=0; i < a.length; i++) {
            if (a[i].className == "imagecycler") {
                a[i].innerHTML = '<p style="margin:20px;font-size:16px;font-weight:bold">jQuery required</p>';
            }
        }
    }
}


function imageCyclerInit() {
    var yt_elements = [];
    $(".imagecycler").each(function(ix){
        var box = $(this);
        var box_scroll_elements = [];
        var cap;
        yt_elements.push([box]);
        imagecyclers.push(box);
        box.data({"ic_width":box.width(),"ic_ms":8000});
        box.css({"position":"relative","overflow":"hidden"});
        box.children().each(function(i){
            var child = $(this);
            var se;
            if (child.is("div") && child.hasClass("imagecycler_youtube")) {
                if (child.find("a").attr("href").indexOf("v=") == -1) {
                    return;
                }
                se = child;
                se.data("href","");
                se.css("position","relative");
                yt_elements[yt_elements.length-1].push(se);
            }
            else if (child.is("a") && child.children().length && child.children().slice(0,1).hasClass("imagecycler_image")) {
                se = $(child.children().slice(0,1));
                se.data("href",child.attr("href"));
            }
            else if (child.hasClass("imagecycler_image")) {
                se = child;
                se.data("href","");
            }
            else {
                return;
            }
            
            if (child.next().hasClass("imagecycler_caption") && child.next().is("img")) {
                cap = child.next();
                cap.remove();
                cap.addClass("imagecycler_caption_img");
            }
            else if (se.attr("title")) {
                cap = $("<p>"+se.attr("title")+"</p>");
                cap.addClass("imagecycler_caption_text");
            }
            else {
                cap = $("<p></p>");
            }
            
            child.detach();
            
            se.css({"display":"block", "position":"absolute"});
            if (i > 0) {
                se.css({"left":"-"+box.width()+"px"});
            }

            se.attr("title","");
            
            cap.css({"display":"block", "position":"absolute"});
            if (i > 0) {
                cap.css({"left":"-"+box.width()+"px"});
            }
            
            se.data("caption",cap);
            
            box_scroll_elements.push(se);

        });

        box.data("box_scroll_elements",box_scroll_elements);

        box.empty();
        
        for (i = 0; i < box_scroll_elements.length; i++) {
            box.append(box_scroll_elements[i]);
            box.append(box_scroll_elements[i].data("caption"));
        }

        c = box.attr("class");
        c = c.split(" ");
        for (i = 0; i < c.length; i++) {
            if (c[i].slice(c[i].length-2) == 'ms') {
                n = parseInt(c[i].slice(0,c[i].length-2));
                if (n != NaN) {
                    box.data({"ic_ms":n});
                }
            }
        }
        
        if (box.children().length > 1) {
            var t = setTimeout("imageCyclerCycle("+ix+")",box.data("ic_ms"));
        }
        else {
            var t = null;
        }
        box.data({"ic_timeout":t});
        box.data({"ic_counter":0});
        box.data("active_element",box_scroll_elements[0]);
        
        if (box_scroll_elements[0].data("href")) {
            box.css({"cursor":"pointer"});
            box.click(function(){
                location.href = box_scroll_elements[0].data("href");
            });
        }
        
    });
    
    initYoutubeVideos(yt_elements);
}

function imageCyclerCycle(ix) {
    if (imagecyclers[ix]) {
        var box = imagecyclers[ix];
        var c = box.data("ic_counter");
        var box_scroll_elements = box.data("box_scroll_elements");
        var this_element = box.data("active_element");
        var c = box_scroll_elements.indexOf(this_element);
        var n = c+1;
        if (n >= box_scroll_elements.length) {
            n = 0;
        }
        var next_link = box_scroll_elements[n].data("href");
        
        this_element.data("caption").animate({"left":"-"+box.data("ic_width")+"px"}, 800, function(){
            $(this).prev().animate({"left":box.data("ic_width")+"px"},1000);
            if ($(this).next().length) {
                $(this).next().css({"left":"-"+box.data("ic_width")+"px"}).animate({"left":"0px"}, 1000, function(){
                    $(this).data("caption").animate({"left":"0px"},800);
                    box.unbind('click');
                    box.css("cursor","auto");
                    if (next_link) {
                        box.css("cursor","pointer");
                        box.click(function(){
                            location.href = next_link;
                        });
                    }
                });
                box.data("active_element",box_scroll_elements[n]);
                
            }
            else {
                box_scroll_elements[0].css({"left":"-"+box.data("ic_width")+"px"}).animate({"left":"0px"}, 1000, function(){
                    $(this).data("caption").animate({"left":"0px"},800);
                    box.unbind('click');
                    box.css("cursor","auto");
                    if (next_link) {
                        box.css("cursor","pointer");
                        box.click(function(){
                            location.href = next_link;
                        });
                    }
                });
                box.data("active_element",box_scroll_elements[n]);
                
            }
        });
        
        var t = setTimeout("imageCyclerCycle("+ix+")",box.data("ic_ms"));
        box.data({"ic_timeout":t});
    }
}

/// youtube

function initYoutubeVideos(yt_elements) {
    var count = 0;
    
    var yt_videos_and_their_ic = {};
    
    for (i = 0; i < yt_elements.length; i++) {
        if (swfobject && yt_elements[i].length > 1) {
            //place video with swfobject
            for (ii = 1; ii < yt_elements[i].length; ii++) {
                var box = yt_elements[i][0];
                var ytdiv = yt_elements[i][ii];
                var thisid = "icyt"+count;
                //find url
                var url = ytdiv.find("a").attr("href");
                if (url.indexOf("v=") != -1) {
                    url = url.substr(url.indexOf("v=")+2);
                    if (url.indexOf("&") != -1) {
                        url = url.substr(0,url.indexOf("&"));
                    }
                }
                url = "http://www.youtube.com/e/" + url + "?enablejsapi=1&playerapiid=" + "ytapiplayer"+count + "&rel=0";
                
                //get actual container to fill
                var ytbox = ytdiv.find(".imagecycler_youtube_container");
                var ytrep = ytdiv.find(".imagecycler_youtube_replace");
                ytrep.attr("id",thisid);
                var params = {allowScriptAccess: "always", wmode: "transparent"};
                var atts = {id: "ytapiplayer"+count};
                swfobject.embedSWF(url, thisid, ytbox.width()+"", ytbox.height()+"", "8", null, null, params, atts);
                yt_videos_and_their_ic["ytapiplayer"+count] = box;
                count++;
            }
        }
    }
}

function onYouTubePlayerReady(ytapiplayer_plus_count) {
    var videoplayer = document.getElementById(ytapiplayer_plus_count);
    videoplayer.addEventListener('onStateChange', 'playerStateChange');
}

function playerStateChange(e){

    if (e == -1) {
        return;
    }

    if (e == 0 || e == 2) {
        //restart scrolling
        for (i = 0; i < imagecyclers.length; i++) {
            t = imagecyclers[i].data("ic_timeout");
            if (!t) {
                t = setTimeout("imageCyclerCycle("+i+")",imagecyclers[i].data("ic_ms"));
                imagecyclers[i].data({"ic_timeout":t});
            }
        }
    }
    else if (e == 1) {
        //stop scrolling
        for (i = 0; i < imagecyclers.length; i++) {
            t = imagecyclers[i].data("ic_timeout");
            if (t) {
                t = clearTimeout(t);
                imagecyclers[i].data({"ic_timeout":null});
            }
        }
    }

}

