/*
    adslider 1.4
    by Ante Damjanovic´
*/
// default settings
function adslideDefaults() {
    this.pause = false;
    this.last = 0;
    this.now = 0;
    this.max = 0;
    // set user values
    this.container = "thecontainer";
    this.image0 = "thecontainer0";
    this.image1 = "thecontainer1";
    this.images = new Array();
    this.width = 0;
    this.height = 0;
    this.border = 0;
    this.delay = 300;
    this.allowPause = true;
    this.order = "";
    // initialize images
    this.img = new Array();
};
var adslideConf = new adslideDefaults();

function adslide() {
    // if element dont exist return false
    if(jQuery("#"+adslideConf.container).length <= 0) {
        return false;
    }
    // how many images?
    adslideConf.max = adslideConf.images.length;
    // sort images
    if (adslideConf.displayorder=="R") adslideConf.images.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)
    // preload images
    for(var i = 0; i < adslideConf.max; i++){
        adslideConf.img[i]       = new Image();
        adslideConf.img[i].src   = adslideConf.images[i][0];
    }
    // set width and heigth of the container
    jQuery("#"+adslideConf.container).css({
        "position": "relative",
        "width": adslideConf.width+"px",
        "height": adslideConf.height+"px",
        overflow: "hidden"
    });
    // set the firs element
    var div0 = document.createElement("div");
    jQuery(div0).attr({ id: adslideConf.container+'-0' });
    jQuery(div0).css({
        display: "block",
        position: "absolute",
        top: "0",
        left: "0",
        zIndex: "9",
        overflow: "hidden",
        width: adslideConf.width+"px",
        height: adslideConf.height+"px"
    });
    var link0 = document.createElement("a");
    jQuery(link0).attr({
        "id": adslideConf.container+"a",
        "href": adslideConf.images[0][1],
        "target": adslideConf.images[0][2]
    })
    jQuery(link0).css({
        border: "none"
    });
    var img0 = document.createElement("img");
    jQuery(img0).attr({
        "id": adslideConf.container+"0",
        src: adslideConf.img[0].src
    });
    jQuery(img0).css({ border: "none" });
    jQuery(link0).append(jQuery(img0));
    jQuery(div0).append(jQuery(link0));
    jQuery("#"+adslideConf.container).append(jQuery(div0));
    // set the second element
    var div1 = document.createElement("div");
    jQuery(div1).attr({ id: adslideConf.container+'-1' });
    jQuery(div1).css({
        display: "none",
        position: "absolute",
        top: "0",
        left: "0",
        zIndex: "9",
        overflow: "hidden",
        width: adslideConf.width+"px",
        height: adslideConf.height+"px"
    });
    var link1 = document.createElement("a");
    jQuery(link1).attr({
        "id": adslideConf.container+"b",
        "href": adslideConf.images[1][1],
        "target": adslideConf.images[1][2]
    })
    jQuery(link1).css({
        border: "none"
    });
    var img1 = document.createElement("img");
    jQuery(img1).attr({
        "id": adslideConf.container+"1",
        src: adslideConf.img[1].src
    });
    jQuery(img1).css({ border: "none" });
    jQuery(link1).append(jQuery(img1));
    jQuery(div1).append(jQuery(link1));
    jQuery("#"+adslideConf.container).append(jQuery(div1));

    jQuery("#"+adslideConf.container+"-0").show();
    jQuery("#"+adslideConf.container).mouseover(function(){ adslideConf.pause = true; });
    jQuery("#"+adslideConf.container).mouseout(function(){ adslideConf.pause = false; });
    setTimeout("adslideChangeImage()",adslideConf.delay);
}

function adslideShowFirst() {
    jQuery("#"+adslideConf.container+"-1").hide();
    jQuery("#"+adslideConf.container+"-1").css({zIndex: 10});
    jQuery("#"+adslideConf.container+"-0").css({zIndex: 9});
    jQuery("#"+adslideConf.container+"b").attr("href", adslideConf.images[adslideConf.now][1]);
    jQuery("#"+adslideConf.container+"b").attr("target", adslideConf.images[adslideConf.now][2]);
    jQuery("#"+adslideConf.container+"1").attr({src: adslideConf.images[adslideConf.now][0]});
    jQuery("#"+adslideConf.container+"-1").fadeIn("slow");
}
function adslideShowSecond() {
    jQuery("#"+adslideConf.container+"-0").hide();
    jQuery("#"+adslideConf.container+"-0").css({zIndex: 10});
    jQuery("#"+adslideConf.container+"-1").css({zIndex: 9});
    jQuery("#"+adslideConf.container+"a").attr("href", adslideConf.images[adslideConf.now][1]);
    jQuery("#"+adslideConf.container+"a").attr("target", adslideConf.images[adslideConf.now][2]);
    jQuery("#"+adslideConf.container+"0").attr({src: adslideConf.images[adslideConf.now][0]});
    jQuery("#"+adslideConf.container+"-0").fadeIn("slow");
}


function adslideChangeImage() {
    // check for pause
    if(!adslideConf.pause) {
        // choose next image
        adslideConf.now = (adslideConf.now+1 >= adslideConf.max) ? 0 : adslideConf.now+1;
        // swap to new image
        if(adslideConf.last == 0) {
            adslideShowFirst();
            adslideConf.last = 1;
        } else {
            adslideShowSecond();
            adslideConf.last = 0;
        }
    }
    setTimeout("adslideChangeImage()",adslideConf.delay);
}
// for compatibility reason
fadeshow = function(thecontainer, theimages, fadewidth, fadeheight, borderwidth, delay, pause, displayorder) {
    adslideConf['container'] = thecontainer;
    adslideConf['images'] = theimages;
    adslideConf['width'] = fadewidth;
    adslideConf['height'] = fadeheight;
    adslideConf['border'] = borderwidth;
    adslideConf['delay'] = delay;
    adslideConf['allowPause'] = pause;
    adslideConf['displayorder'] = displayorder;
    adslide();
};