﻿function Banner(bannerdiv, numberdiv, objname) {//timeoutinms,
    this.banneradedi = 0;
    this.timerId = 0;
    this.mouseover = false;
    this.bannerArray = new Array();
    this.selectedbanner = 0;
    this.timeoutinms = new Array();
    this.bannerdiv = bannerdiv;
    this.numberdiv = numberdiv;
    this.objname = objname;
    //methods
    this.startTimer = startTimer;
    this.showBanner = showBanner;
    this.startBannerTimer = startBannerTimer;
    this.stopBannerTimer = stopBannerTimer;
    this.stopBannerTimerEnd = stopBannerTimerEnd;
    this.startBannerTimerEnd = startBannerTimerEnd;
    this.addBanner = addBanner;
    this.nextBanner = nextBanner;
    this.prevBanner = prevBanner;

    var that = this;
    document.getElementById(bannerdiv).onmouseover = function () { that.stopBannerTimer(); };
    document.getElementById(bannerdiv).onmouseout = function () { that.startBannerTimer(); };


}


function startTimer(count) {

    if (!this.mouseover && count == 'next')
        this.nextBanner();
    else if (!this.mouseover)
        this.showBanner(count);

    if (this.banneradedi > 1) {
        this.timerId = setTimeout(this.objname + ".startTimer('next')", this.timeoutinms[this.selectedbanner]);
    }
}


function showBanner(number) {
    if (this.banneradedi <= 0 || number < 0 || number >= this.banneradedi)
        return false;

    try {
        document.getElementById(this.bannerdiv).innerHTML = this.bannerArray[number];
        /*Tek banner var ise gösterme*/
        if (this.banneradedi > 1) {
            this.selectedbanner = number;
            return true;
        }
        else {
            return false;
        }
    }
    catch (err) {
        return false;
    }

}

function stopBannerTimer() {
    this.mouseover = true;
}

function startBannerTimer() {
    this.mouseover = false;
}

function stopBannerTimerEnd() {
    this.mouseover = false;
}

function startBannerTimerEnd() {
    this.mouseover = true;
    for (var i = 1; i <= this.banneradedi; i++) {
        document.getElementById(this.numberdiv + i).style.background = '#3957A1';
    }
}

function escapeHTML(str) {
    var div = document.createElement('div');
    var text = document.createTextNode(str);
    div.appendChild(text);
    return div.innerText;
};

function addBanner(code, sure) {
    this.bannerArray[this.banneradedi] = unescape(code);
    this.timeoutinms[this.banneradedi] = sure;
    var that = this;
    var container = document.getElementById(this.numberdiv);

    if (container.hasChildNodes()) {
        while (container.childNodes.length >= 1) {
            container.removeChild(container.firstChild);
        }
    }


    if (this.banneradedi > 0) {

        //sayı linkleri
        for (var i = 0; i <= this.banneradedi; i++) {
            var new_element = document.createElement('span');
            //			new_element.innerHTML = i + 1;
            new_element.setAttribute('id', this.numberdiv + i);
            container.insertBefore(new_element, container.firstChild);
        }

        //geri linki
        var new_element = document.createElement('li');
        //		new_element.innerHTML = '&lt;';
        new_element.setAttribute('id', this.numberdiv + 'prevbanner');
        new_element.onselectstart = new Function('return false');
        new_element.onclick = function () { that.prevBanner(); };
        new_element.onmouseover = function () { that.stopBannerTimer(); };
        new_element.onmouseout = function () { that.startBannerTimer(); };
        new_element.className = 'sol';
        container.insertBefore(new_element, container.firstChild);

        //ayrac
        var new_element = document.createElement('li');
        new_element.setAttribute('id', this.numberdiv + 'ayrac');
        new_element.className = 'orta';
        container.insertBefore(new_element, container.firstChild);

        //ileri linki
        var new_element = document.createElement('li');
        //		new_element.innerHTML = '&gt;';
        new_element.setAttribute('id', this.numberdiv + 'nextbanner');
        new_element.onselectstart = new Function('return false');
        new_element.onclick = function () { that.nextBanner(); };
        new_element.onmouseover = function () { that.stopBannerTimer(); };
        new_element.onmouseout = function () { that.startBannerTimer(); };
        new_element.className = 'sag';
        container.insertBefore(new_element, container.firstChild);
    }

    this.banneradedi++;
}

function nextBanner() {
    this.showBanner((this.selectedbanner + 1) % this.banneradedi);
}

function prevBanner() {
    this.showBanner((this.selectedbanner - 1 + this.banneradedi) % this.banneradedi);
}

