var currentCategory = "";
var currentSubCategory = "";

var iFrameWidth = 600;
var fadeInSpeed = 3000;

$(document).ready(function(){
    try {
        // $("#slideshowButtons").hide();
        $("#infolink").bind("click",loadInfo);
        $("#slideshowlink").bind("click",loadSlides);
        if(cfg.autoplay || window.location.hash) loadSlides();
        $("#containerMenu, #logobanner").fadeIn(fadeInSpeed);
    }
    catch(e) {
        doError(e);
    }
});

function loadInfo(event) {
    try {
        $("#slideshowButtons").hide();
        $("#container").empty();
        // $("#container").append($("#"+targetId).clone().html());
        var ifr = document.createElement("iframe");
        ifr.id = ifr.name = "infodoc";
        ifr.setAttribute("src", this.href);
        ifr.style.border = "none";
        ifr.style.width = "100%";
        ifr.style.height = "100%";
        $("#container").append(ifr);
    }
    catch(e) {
        doError(e);
    }
    finally {
    	this.blur();
    	if(event) event.preventDefault();
    }
}

function displayImages(dat,isSub) {
    try {
        var imgs = [];
        var n = 0;
        var ext = "";
        var path = "";
        var folder = categoriesFolder + "/" + currentCategory + "/";
        var iFrame = false;
        if(isSub) folder += currentSubCategory + "/";
        for(var i in dat) {
            if(typeof dat[i] == "string") {
                path = folder + dat[i];
                ext = getExtension(dat[i]);
                if(dat[i] == "index.txt") {
                    $("div.subcatGroup > div").hide();
                    if(!document.getElementById("textContent"))
                        $("#right").append("<div id=\"textContent\" class=\"subcatGroup\"></div>");
                    if(!document.getElementById(currentCategory + "_Text")) {
                        $("#textContent").append("<div id=\"" + currentCategory + "_Text\" class=\"categoryText\"></div>");
                        $("#" + currentCategory + "_Text").load(path);
                    }
                    document.getElementById(currentCategory + "_Text").style.display = "block";
                }
                else if(ext.toLowerCase() == "jpg" || ext.toLowerCase() == "png") {
                    imgs[n++] = path;
                }
                else if(dat[i] == "index.html") {
                    iFrame = path;
                }
            }
        }
        if(imgs.length || iFrame) {
            $("#container").empty();
            var cont = document.getElementById("container");
                                        // If the is just an image with no
                                        // accompanying index.html
            if(imgs.length === 1 && !iFrame) {
                var img = document.createElement("img");
                img.setAttribute("src", imgs[0]);
                img.style.border = "none";
                cont.appendChild(img);
            }
            else {
                var content, img, ht;
                var scroller = document.createElement("div");
                scroller.setAttribute("id", "scroller");
                if(imgs.length) scroller.style.width = "20000px";
                cont.appendChild(scroller);
                for(i in imgs) {
                    content = document.createElement("div");
                    content.setAttribute("class","content");
                    scroller.appendChild(content);
                    img = document.createElement("img");
                    img.setAttribute("src", imgs[i]);
                    img.style.border = "none";
                    content.appendChild(img);
                }
                if(iFrame) {
                    ht = document.createElement("iframe");
                    ht.setAttribute("src", iFrame);
                    ht.style.border = "none";
                    scroller.appendChild(ht);
                }
                SetScrollerWidth(iFrame);
            }
        }
    }
    catch(e) {
        doError(e);
    }
}

function SetScrollerWidth(iFrame) {
    $("#scroller").css("width",0);
    if(iFrame) $("#scroller").css("width",iFrameWidth);
    $("img", "#scroller").bind("load", function(e){
        var scroller = this.parentNode.parentNode;
        scroller.style.width = (scroller.offsetWidth + this.parentNode.offsetWidth + "px");
    });
}

var slidesWarningDone = false;

function loadSlides(event) {
    try {
        $("#container").empty();
        $("#container").append("<div id=\"flashSlideShow\"></div>");
        var flashVars = {
                showLogo        : "false",
                showVersionInfo    : "true",
                showRegistration: "true",
                dataFile        : "monoslideshow.php?start=" + cfg.focus + "&amp;img=" + window.location.hash.replace("#",""),
                // dataFile: "monoslideshow23.xml",
                callBackFunction: "initializeMonoslideshow"
            };
        $("#debug").empty();
        //$("#debug").append(flashVars.dataFile);
        var flashParams = {allowScriptAccess: "always"};
        var attribs = {
            id: "Monoslideshow",
            name: "Monoslideshow"
        };
        swfobject.embedSWF("monoslideshow.swf", "flashSlideShow", "1024", "532", "7","",flashVars,flashParams,attribs);
    }
    catch(e) {
        doError(e);
    }
    finally {
    	this.blur();
    	if(event) event.preventDefault();
    }
}

var swf;
function initializeMonoslideshow() {
    swf = document.getElementById("Monoslideshow");
    swf.addListener("itemLoaderComplete", "onItemLoaderComplete");
    // if(window.location.hash) setTimeout("swf.togglePause();",3000)
    $("#slideshowButtons").fadeIn(fadeInSpeed);
}

function onItemLoaderComplete(event) {
    window.location.hash = event.id;
    /*
    var replacements = [
           { cont : "Terence Kearns", href : "http://TerenceKearns.com" },
           { cont : "Google", href : "http://www.google.com.au" },
    ];
    var desc = event.description;
    for(var pair in replacements) {
        desc = desc.replace(
            replacements[pair].cont,
            LinkWrap(replacements[pair].cont,replacements[pair].href)
        );
    }
    $("#debug").empty();
    // $("#debug").append(desc);
    */
}

function LinkWrap(cont, href) {
    return "<a href=\"" + href + "\">" + cont + "</a>";
}

function ToggleAudio() {
    try {
        swf.toggleMute();
        var img = document.getElementById("muteButton");
        if(img.src.indexOf("mute") != -1) img.src = img.src.replace("mute","play");
        else img.src = img.src.replace("play","mute");
    }
    catch(e) {
        doError(e);
    }
}

function ToggleSlides() {
    try {
        swf.togglePause();
        var img = document.getElementById("pauseButton");
        if(swf.isPlaying()) img.src = img.src.replace("resume","pause");
        else img.src = img.src.replace("pause","resume");
    }
    catch(e) {
        doError(e);
    }
}

function getExtension(filename) {
    try {
        var myregexp = /.+\.(\w+)$/;
        var match = myregexp.exec(filename);
        if (match != null && match.length > 1) {
            return match[1];
        } else {
            return "";
        }
    }
    catch(e) {
        doError(e);
    }
}

function doError(e) {
    try {
        console.error(e);
    }
    catch(ignore) {
        // assume we don't have the console object from firebug.
        alert(e.fileName + " error: line " + e.lineNumber + "\n\n" + e.message);
    }
}