﻿
accordionClass.prototype.id = "0";
accordionClass.prototype.headers = new Array();
accordionClass.prototype.constructor = accordionClass;
accordionClass.prototype.closeOthers = true;
function accordionClass(id) {
    this.id = id;

}
accordionClass.prototype.SliderInterval = 3000;
accordionClass.prototype.TimerInterval = 0;
accordionClass.prototype.StartInterval = function (slideInterval) {
    var _instance = this;
    this.SliderInterval = slideInterval;
    this.TimerInterval = setInterval(function () {
        if (_instance.currentOpen == null) {
            _instance.currentOpen = _instance.headers.length > 0 ? _instance.headers[_instance.headers.length - 1] : null;
        }
        if (_instance.currentOpen != null && !_instance.currentOpen.MouseOver) {
            var sonraki = _instance.headers.bulSonraki(_instance.currentOpen, true);
            if (sonraki != null) {
                if (sonraki.Kapali) {
                    sonraki.Kapali = false;
                    sonraki.ac();
                }
                else {
                    if (sonraki.Kapanir) {
                        sonraki.Kapali = true;
                        sonraki.kapat();
                    }
                }
            
            }
        }
    }, slideInterval);
}
accordionHeader.prototype.Content = document.createElement("td");
accordionHeader.prototype.Header = document.createElement("td");
accordionHeader.prototype.Kapanir = true;
accordionHeader.prototype.Kapali = false;
accordionHeader.prototype.Varsayilan = false;
accordionHeader.prototype.acmaInterVal = 0;
accordionHeader.prototype.kapamaInterVal = 0;
accordionHeader.prototype.ContainerClass = new accordionClass();
accordionHeader.prototype.OpenOver = false;
accordionHeader.prototype.kapat = function () {

    if (this.Kapanir && (this.Content.nodeName.toLowerCase() == "td" || this.Content.nodeName.toLowerCase() == "div")) {
        if (this.ScrollRoot != null) {
            var api = this.ScrollRoot.data("scrollable");
            api.stop();
        }
        if (this.kapamaInterVal != 0)
            return;
        this.Content.style.overflow = "hidden";
        var esasYukseklik = this.Content.scrollHeight;
        var paddingTop = parseInt(this.Content.style.paddingTop);
        var paddingBottom = parseInt(this.Content.style.paddingBottom);
        paddingBottom = isNaN(paddingBottom) ? 0 : paddingBottom;
        paddingTop = isNaN(paddingTop) ? 0 : paddingTop;

        esasYukseklik = esasYukseklik - (paddingBottom + paddingTop);
        var animObj = this.Content;
        var startHeight = this.acmaInterVal == 0 ? esasYukseklik : this.Content.clientHeight;
        if (this.acmaInterVal != 0) {
            clearInterval(this.acmaInterVal);
            this.acmaInterVal = 0;
        }
        var i = 1;
        var _instace = this;
        var mHeight = this.Content.clientHeight - (paddingBottom + paddingTop);
        this.kapamaInterVal = setInterval(function () {
            i++;

            //mHeight = ((Number(animObj.scrollHeight - animObj.clientHeight) / 6) > 1) ? mHeight - (Number(animObj.scrollHeight - animObj.clientHeight) / 6) : mHeight - 2;
            mHeight = ((Number(esasYukseklik - animObj.clientHeight) / 6) > 1) ? mHeight - (Number(esasYukseklik - animObj.clientHeight) / 6) : mHeight - 2;
            if (0 < mHeight) {
                _instace.Content.style.height = mHeight + "px";
            } else {
                _instace.Content.style.height = "0px";
                clearInterval(_instace.kapamaInterVal);
                _instace.kapamaInterVal = 0;
                if (this.ScrollRoot != null) {
                    var api = this.ScrollRoot.data("scrollable");
                    //api.seekTo(0, undefined, function () { });
                }
            }

        }, 10);
    }
}
accordionHeader.prototype.ac = function () {
    var _instace = this;
    this.ContainerClass.currentOpen = this;
    if (this.ContainerClass.closeOthers) {
        for (var i = 0; i < this.ContainerClass.headers.length; i++) {
            if (this.ContainerClass.headers[i] != this && this.ContainerClass.headers[i].tableName == this.tableName) {
                this.ContainerClass.headers[i].Kapali = true;
                this.ContainerClass.headers[i].kapat();
            }
        }
    }
    if (this.Kapanir && (this.Content.nodeName.toLowerCase() == "td" || this.Content.nodeName.toLowerCase() == "div")) {
        if (_instace.ScrollRoot != null) {
            var api = _instace.ScrollRoot.data("scrollable");
            if (api.getSize() > 0)
                api.seekTo(0, 0, function () { });

        }
        var esasYukseklik = this.Content.scrollHeight;
        var paddingTop = parseInt(this.Content.style.paddingTop);
        var paddingBottom = parseInt(this.Content.style.paddingBottom);
        paddingBottom = isNaN(paddingBottom) ? 0 : paddingBottom;
        paddingTop = isNaN(paddingTop) ? 0 : paddingTop;
        esasYukseklik = esasYukseklik - (paddingBottom + paddingTop);
        if (this.kapamaInterVal != 0) {
            clearInterval(this.kapamaInterVal);
            this.kapamaInterVal = 0;
        }
        var startHeight = this.kapamaInterVal == 0 ? 0 : this.Content.clientHeight;

        var i = startHeight;

        var mHeight = startHeight;
        var opacity = 0;
        this.acmaInterVal = setInterval(function () {

            mHeight = ((Number((esasYukseklik - _instace.Content.clientHeight) / 6) < 3) ? mHeight + 3 : mHeight + (esasYukseklik - _instace.Content.clientHeight) / 6); ;
            opacity = (1 / esasYukseklik) * mHeight;
            if (esasYukseklik > (mHeight)) {
                _instace.Content.style.height = (mHeight) + "px";
                //_instace.Content.style.opacity = opacity;
                //_instace.Content.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + opacity * 100 + ')';


            } else {
                _instace.Content.style.height = "auto"//objectSize(_instace.Content, false, false).Height + "px";
                _instace.Content.style.overflow = "hidden";
                clearInterval(_instace.acmaInterVal);
                _instace.acmaInterVal = 0;
                if (_instace.ScrollRoot != null) {
                    var api = _instace.ScrollRoot.data("scrollable");
                    //api.seekTo(0, undefined, function () { });
                    api.play();
                }
            }

        }, 10);
    }
}
accordionHeader.prototype.tableName = "";
function accordionHeader(header, content, kapanir, kapali, varsayilan, containerClass, openover,tableName,sliderNavigatorDiv) {
    var _instance = this;
    this.MouseOver = false;
    addEvent(header, "mouseover",
        function () {
            _instance.MouseOver = true;
            clearInterval(_instance.ContainerClass.TimerInterval);
        }
    );
    addEvent(content,"mouseover" ,
        function () {
            _instance.MouseOver = true;
            clearInterval(_instance.ContainerClass.TimerInterval);
        }
    );
        addEvent(header, "mouseout",
         function () {
             _instance.MouseOver = false;
             if(_instance.Content.getAttribute("slider") == "navigator")
                _instance.ContainerClass.StartInterval(_instance.ContainerClass.SliderInterval);
         }
    ); 
         addEvent(content, "mouseout",
         function () {
             _instance.MouseOver = false;
             if (_instance.Content.getAttribute("slider") == "navigator")
                _instance.ContainerClass.StartInterval(_instance.ContainerClass.SliderInterval);
         }
    );
    
    this.tableName = tableName;
    this.Kapali = varsayilan == true ? false : kapali;
    this.Kapanir = kapanir;
    this.Content = content;
    this.Header = header;
    this.Varsayilan = varsayilan;
    this.ContainerClass = containerClass;
    this.OpenOver = openover;
    this.onBeforeSeek = function (e, a, time) {
        var currentHeaderIndex = 0;
        if (a == e.currentTarget.getSize()) {
            for (var i = 0; i < _instance.ContainerClass.headers.length; i++) {
                if (_instance.ContainerClass.headers[i] == _instance) {
                    currentHeaderIndex = i;
                    var bulundu = false;
                    for (var j = currentHeaderIndex + 1; j < _instance.ContainerClass.headers.length; j++) {
                        var api = _instance.ContainerClass.headers[j].ScrollRoot.data("scrollable");
                        if (api.getSize() > 0) {
                            _instance.ContainerClass.headers[j].ac();
                            bulundu = true;
                            break;
                        }
                    }
                    if (!bulundu) {
                        for (var j = 0; j < currentHeaderIndex + 1; j++) {
                            var api = _instance.ContainerClass.headers[j].ScrollRoot.data("scrollable");
                            if (_instance.ContainerClass.headers[j] == _instance) {
                                api.seekTo(0);
                                break;
                            } else {
                                if (api.getSize() > 0) {
                                    _instance.ContainerClass.headers[j].ac();
                                    break;
                                }
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
    if (content != null) {

        if (!this.Varsayilan) {
            content.style.overflow = "hidden";
            content.style.height = "0px";
        }
    }
    if (header != null && (header.nodeName.toLowerCase() == "td" || header.nodeName.toLowerCase() == "div")) {
        header.AccordionHeader = _instance;
        if (header.childNodes.length > 0) {
            var childNodeName = header.childNodes[0].nodeName.toLowerCase();
            var childNode = header.childNodes[0];

            if ((childNodeName == "img" || childNodeName == "a") && childNode.onclick == null) {
                //alert(this.Kapali);
                childNode.style.cursor = "pointer";

                childNode.onclick = function() {
                    if (_instance.Kapali) {
                        _instance.Kapali = false;
                        _instance.ac();
                    }
                    else {
                        if (_instance.Kapanir) {
                            _instance.Kapali = true;
                            _instance.kapat();
                        }
                    }
                }
                if (this.OpenOver) {
                    childNode.onmouseover = function() {
                    if (_instance.Kapanir && _instance.Kapali) {
                        _instance.Kapali = false;
                        _instance.ac();
                    }
                    }
                }
            }
        }
    }
}
accordionClass.prototype.currentOpen = null;
function createAccordions(tableID, mAccordion,sliderInterval) {
    var _instance = this;
    $.easing.custom = function (x, t, b, c, d) {
        var s = 1.70158;
        if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
        return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
    }
    var mHeaders = new Array();
    var mTable = document.getElementById(tableID);
    var tdNodes = mTable.getElementsByTagName("td");

    var onlyTableTDs = new Array();

    if (tdNodes != null && tdNodes.length > 0) {
        var startInternalSlider = false;
        for (var i = 0; i < tdNodes.length; i++) {
            if (tdNodes[i].getAttribute("typeEx") == "acHeader") {
                if (checkTDParentTable(tdNodes[i], mTable)) {
                    var mHeaderTd = tdNodes[i];
                    mHeaderTd.style.height = "0px";
                    var mContent = document.getElementById(mHeaderTd.getAttribute("contentid"));
                    var scrollRoot = null;

                    var kapali = mHeaderTd.getAttribute("kapali") == "true" ? true : false;
                    var varsayilan = mHeaderTd.getAttribute("varsayilan") == "true" ? true : false;
                    var kapanir = mHeaderTd.getAttribute("kapanir") == "true" ? true : false;
                    var openover = mHeaderTd.getAttribute("openover") == "true" ? true : false;
                    //alert(mContent.scrollHeight);
                    mContent.style.height = "auto"; //objectSize(mContent,false,false).Height + "px";
                    var mHeader = new accordionHeader(mHeaderTd, mContent, kapanir, kapali, varsayilan, mAccordion, openover, getTableName(mHeaderTd));
                    if (varsayilan)
                        mAccordion.currentOpen = mHeader;
                    mHeader.ScrollRoot = scrollRoot;
                    mHeaders.push(mHeader);

                    if (mContent.getAttribute("slider") != null && mContent.getAttribute("slider") == "auto") {
                        try {
                            sliderInterval = sliderInterval != null ? sliderInterval : 5000
                            mHeader.ScrollRoot = $("#" + mContent.id).scrollable({ easing: 'custom', circular: false, vertical: false, speed: 1000 }).autoscroll({ autoplay: varsayilan, interval: sliderInterval });
                            mHeader.ScrollRoot.data("scrollable").onBeforeSeek(mHeader.onBeforeSeek);

                        } catch (e) {
                            var a = 0;
                        }
                    }
                    else if (mContent.getAttribute("slider") == "navigator") {
                        $("#" + mContent.id).scrollable({ easing: 'custom', circular: false, vertical: false, speed: 1000 }).navigator("#" + mContent.getAttribute("navigatorid"));
                        startInternalSlider = true;
                    }
                   
                }
            }
        }
        if(startInternalSlider)
            mAccordion.StartInterval(sliderInterval);
        mAccordion.headers = mHeaders;
    }
}
function getTableName(td) {
    if (td.parentNode != null) {
        if (td.parentNode.nodeName.toLowerCase() == "table") {
            return td.parentNode.id;
        }
        else {
            return getTableName(td.parentNode);
        }
    }
    else {
        return "";
    }
}
function checkTDParentTable(td,table) {
    if (td.parentNode != null) {
        if (td.parentNode.nodeName.toLowerCase() == "table") {
            if (td.parentNode == table || td.parentNode.getAttribute("subtable") == "true") {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return checkTDParentTable(td.parentNode, table);
        }
    }
    else {
        return false;
    }
}

