// Ver: $Id: slideshowdirect.js,v 1.5 2011/07/06 13:31:35 martin Exp $

function CuwingSlideshowDirect() {
    this.pagerDiv = 'div.slideshowDirectPager';
    this.useImageDiv = true;
    this.classSuffix = null;
    this.options = null;
    this.debug = false;
}

CuwingSlideshowDirect.prototype.getSource = function(slide) {
    if (this.debug) { cuwing.log("getSource(slide="+slide+")"); }
    if (!slide) {
        return "";
    }
    if (this.useImageDiv) {
        return $("div.image" + this.classSuffix + " img:not([class='transp'])", slide).attr('src');
    }
    return $("img.image" + this.classSuffix + ":not([class='transp'])", slide).attr('src');
};

// --- Functions with external scope ---

CuwingSlideshowDirect.prototype.setTextDivWidth = function(cSlide, nSlide, opt, fwFlag) {
    if (slideshowDirect.debug) { cuwing.log("setTextDivWidth(cSlide="+cSlide+",fwFlag="+fwFlag+")"); }
    try {
        var $sds = $("div.slideshowDirectSlideshow");
        var totW = $sds.width();
        if (slideshowDirect.debug) { cuwing.log(" cSlide="+cSlide+",nSlide="+nSlide); }
        var $s = $(nSlide);
        if (slideshowDirect.debug) { cuwing.log(" totW="+totW+",$s="+$s+",$s.attr('class')="+$s.attr('class')); }
        var $ip = $s;
        if (slideshowDirect.useImageDiv) {
            var c = 'image' + slideshowDirect.classSuffix;
            $ip = $s.children("div[class='"+c+"']").first();
        }
        var $i = $ip.children("img:not([class='transp'])").first();
        var imgW = $i.width();
        if (slideshowDirect.debug) { cuwing.log(" $ip="+$ip+",$i="+$i+",$i.attr('src')="+$i.attr('src')); }
        var maxH = $sds.height();
        if (imgW) {
            $i.width(imgW);
            var textW = totW - imgW;
            if (slideshowDirect.debug) { cuwing.log(" imgW="+imgW+",textW="+textW); }
            var $t = $s.children("div.slideshowDirectSlideText").first();
            $t.width(textW);
            maxH = Math.max(maxH, $t.height() + $("div.slideshowDirectPager").height());
        }
        $sds.height(maxH);
    } catch (e) { if (slideshowDirect.debug) { cuwing.log(e); } }
};

CuwingSlideshowDirect.prototype.pagerAnchorBuilder = function(idx, slide) {
    var src = slideshowDirect.getSource(slide);
    return slideshowDirect.pagerDiv + " a:contains('" + src + "')";
};

CuwingSlideshowDirect.prototype.updateActivePagerLink = function(pager, currSlideIdx, clsName) {
    if (slideshowDirect.debug) { cuwing.log("updateActivePagerLink(currSlideIdx="+currSlideIdx+",clsName="+clsName+")"); }
    $(slideshowDirect.pagerDiv + ' a').each(function() {
        $(this).removeClass(clsName);
    });
    $("a:contains('" + (currSlideIdx+1) + ": ')", pager).addClass(clsName);
};

CuwingSlideshowDirect.prototype.onControlLinkClick = function(evt) {
    evt.preventDefault();
    var $s = $('div.slideshowDirectSlideshow');
    $(this).hasClass("pause") ? $s.cycle('pause') : $s.cycle('resume', 'true');
};

CuwingSlideshowDirect.prototype.onDomReady = function() {
    try {
        var $s = $('div.slideshowDirectSlideshow');
        $s.cycle(slideshowDirect.options);
        $("div.slideshowDirectControls a").click(slideshowDirect.onControlLinkClick);
        if (cuwing.isAdmin()) {
            var $d = $('div.slideshowDirect').parents('div.componentDrag');
            if (cuwing.ie) {
                $d.height('auto').width('100%');
            } else {
                $d.height('auto').width('auto');
            }
            if (typeof(dd) != "undefined") {
                for (i=0; i<dd.elements.length; i++) {
                    dd.elements[i].cuwingRefresh();
                }
                dd.recalc(1);
                setScrollLimits();
            }
        }
    } catch (e) { if (slideshowDirect.debug) { cuwing.log(e); } }
};

var slideshowDirect = window.slideshowDirect || new CuwingSlideshowDirect();

