﻿

function xAnimation(r) {
    this.res = r || 10;
}
xAnimation.prototype.init = function(e, t, or, ot, oe, a, b) {
    var i = this;
    i.e = xGetElementById(e);
    i.t = t;
    i.or = or; // onRun
    i.ot = ot; // onTarget
    i.oe = oe; // onEnd
    i.a = a || 0; // acceleration type
    i.v = xAnimation.vf[i.a];
    i.qc = 1 + (b || 0); // quarter-cycles
    i.fq = 1 / i.t; // frequency
    //  if (i.a) {              // r4
    if (i.a > 0 && i.a < 4) { // r4
        i.fq *= i.qc * Math.PI;
        if (i.a == 1 || i.a == 2) { i.fq /= 2; }
    }
    //  else { i.qc = 1; } // r4
    // displacements
    i.xd = i.x2 - i.x1;
    i.yd = i.y2 - i.y1;
    i.zd = i.z2 - i.z1;
};
xAnimation.prototype.run = function(r) {
    var i = this;
    if (!r) { i.t1 = new Date().getTime(); }
    if (!i.tmr) i.tmr = setInterval(
    function() {
        i.et = new Date().getTime() - i.t1;
        if (i.et < i.t) {
            i.f = i.v(i.et * i.fq);
            i.x = i.xd * i.f + i.x1;
            i.y = i.yd * i.f + i.y1;
            i.z = i.zd * i.f + i.z1;
            i.or(i);
        }
        else { // target time reached
            clearInterval(i.tmr);
            i.tmr = null;
            if (i.qc % 2) {
                i.x = i.x2;
                i.y = i.y2;
                i.z = i.z2;
            }
            else {
                i.x = i.x1;
                i.y = i.y1;
                i.z = i.z1;
            }
            i.ot(i);
            var rep = false;
            if (typeof i.oe == 'function') { rep = i.oe(i); }
            else if (typeof i.oe == 'string') { rep = eval(i.oe); }
            if (rep) { i.resume(1); }
        }
    }, i.res
  );
};
xAnimation.vf = [
  function(r) { return r; },
  function(r) { return Math.abs(Math.sin(r)); },
  function(r) { return 1 - Math.abs(Math.cos(r)); },
  function(r) { return (1 - Math.cos(r)) / 2; },
  function(r) { return (1.0 - Math.exp(-r * 6)); } // r4
];
xAnimation.prototype.pause = function() {
    clearInterval(this.tmr);
    this.tmr = null;
};
xAnimation.prototype.resume = function(fs) {
    if (typeof this.tmr != 'undefined' && !this.tmr) {
        this.t1 = new Date().getTime();
        if (!fs) { this.t1 -= this.et; }
        this.run(!fs);
    }
};

xAnimation.prototype.size = function(e, w, h, t, a, b, oe) {
    var i = this;
    i.x1 = xWidth(e); i.y1 = xHeight(e); // start size
    i.x2 = Math.round(w); i.y2 = Math.round(h); // target size
    i.init(e, t, o, o, oe, a, b);
    i.run();
    function o(i) { xWidth(i.e, Math.round(i.x)); xHeight(i.e, Math.round(i.y)); } // onRun and onTarget
};
function xWidth(e, w) {
    var css, pl = 0, pr = 0, bl = 0, br = 0, gcs;
    if (!(e = xGetElementById(e))) return 0;
    if (xNum(w)) {
        if (w < 0) w = 0;
        else w = Math.round(w);
    }
    else w = -1;
    css = xDef(e.style);
    if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
        w = xClientWidth();
    }
    else if (css && xDef(e.offsetWidth) && xStr(e.style.width)) {
        if (w >= 0) {
            if (document.compatMode == 'CSS1Compat') {
                gcs = xGetComputedStyle;
                pl = gcs(e, 'padding-left', 1);
                if (pl !== null) {
                    pr = gcs(e, 'padding-right', 1);
                    bl = gcs(e, 'border-left-width', 1);
                    br = gcs(e, 'border-right-width', 1);
                }
                // Should we try this as a last resort?
                // At this point getComputedStyle and currentStyle do not exist.
                else if (xDef(e.offsetWidth, e.style.width)) {
                    e.style.width = w + 'px';
                    pl = e.offsetWidth - w;
                }
            }
            w -= (pl + pr + bl + br);
            if (isNaN(w) || w < 0) return;
            else e.style.width = w + 'px';
        }
        w = e.offsetWidth;
    }
    else if (css && xDef(e.style.pixelWidth)) {
        if (w >= 0) e.style.pixelWidth = w;
        w = e.style.pixelWidth;
    }
    return w;
}
function xHeight(e, h) {
    var css, pt = 0, pb = 0, bt = 0, bb = 0, gcs;
    if (!(e = xGetElementById(e))) return 0;
    if (xNum(h)) {
        if (h < 0) h = 0;
        else h = Math.round(h);
    }
    else h = -1;
    css = xDef(e.style);
    if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
        h = xClientHeight();
    }
    else if (css && xDef(e.offsetHeight) && xStr(e.style.height)) {
        if (h >= 0) {
            if (document.compatMode == 'CSS1Compat') {
                gcs = xGetComputedStyle;
                pt = gcs(e, 'padding-top', 1);
                if (pt !== null) {
                    pb = gcs(e, 'padding-bottom', 1);
                    bt = gcs(e, 'border-top-width', 1);
                    bb = gcs(e, 'border-bottom-width', 1);
                }
                // Should we try this as a last resort?
                // At this point getComputedStyle and currentStyle do not exist.
                else if (xDef(e.offsetHeight, e.style.height)) {
                    e.style.height = h + 'px';
                    pt = e.offsetHeight - h;
                }
            }
            h -= (pt + pb + bt + bb);
            if (isNaN(h) || h < 0) return;
            else e.style.height = h + 'px';
        }
        h = e.offsetHeight;
    }
    else if (css && xDef(e.style.pixelHeight)) {
        if (h >= 0) e.style.pixelHeight = h;
        h = e.style.pixelHeight;
    }
    return h;
}
function xGetElementById(e) {
    if (typeof (e) == 'string') {
        if (document.getElementById) e = document.getElementById(e);
        else if (document.all) e = document.all[e];
        else e = null;
    }
    return e;
}
function xNum() {
    for (var i = 0; i < arguments.length; ++i) { if (isNaN(arguments[i]) || typeof (arguments[i]) != 'number') return false; }
    return true;
}
function xDef() {
    for (var i = 0; i < arguments.length; ++i) { if (typeof (arguments[i]) == 'undefined') return false; }
    return true;
}
function xStr(s) {
    for (var i = 0; i < arguments.length; ++i) { if (typeof (arguments[i]) != 'string') return false; }
    return true;
}
function xGetComputedStyle(e, p, i) {
    if (!(e = xGetElementById(e))) return null;
    var s, v = 'undefined', dv = document.defaultView;
    if (dv && dv.getComputedStyle) {
        s = dv.getComputedStyle(e, '');
        if (s) v = s.getPropertyValue(p);
    }
    else if (e.currentStyle) {
        v = e.currentStyle[xCamelize(p)];
    }
    else return null;
    return i ? (parseInt(v) || 0) : v;
}
function xGetComputedStyle(e, p, i) {
    if (!(e = xGetElementById(e))) return null;
    var s, v = 'undefined', dv = document.defaultView;
    if (dv && dv.getComputedStyle) {
        s = dv.getComputedStyle(e, '');
        if (s) v = s.getPropertyValue(p);
    }
    else if (e.currentStyle) {
        v = e.currentStyle[xCamelize(p)];
    }
    else return null;
    return i ? (parseInt(v) || 0) : v;
}
function xCamelize(cssPropStr) {
    var i, c, a, s;
    a = cssPropStr.split('-');
    s = a[0];
    for (i = 1; i < a.length; ++i) {
        c = a[i].charAt(0);
        s += a[i].replace(c, c.toUpperCase());
    }
    return s;
}
function xDisplay(e, s) {
    if ((e = xGetElementById(e)) && e.style && xDef(e.style.display)) {
        if (xStr(s)) {
            try { e.style.display = s; }
            catch (ex) { e.style.display = ''; } // Will this make IE use a default value
        }                                      // appropriate for the element?
        return e.style.display;
    }
    return null;
}


//////// Menu Animation

var Is_Menu_Komakdarsi_Open = false;
var Is_Mot_komakDarsi_Genre_Open = false;
var Is_Mot_komakDarsi_Age_Open = false;
var Is_Menu_Daneshafzayee_Open = false;
var Is_Menu_Olampiad_Open = false;
var Is_Menu_Olampiad_Genre_Open = false;
var Is_Menu_Olampiad_Set_Open = false;
var Is_Menu_Zabanha_Open = false;
var Is_Menu_am_par_Open = false;

function OpenSubmenu(menu, itemsCnt) {
    
    //////////////////////////
    // Amouzesh o Parvaresh
    //////////////////////////

    if (menu == 'am_par' && !Is_Menu_am_par_Open) {//alert('1');
        Is_Menu_am_par_Open = true;        
        var el = xGetElementById(menu);
        xDisplay(el, 'block');
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), itemsCnt * 26, 300, 1, 0);
    }
    else if (menu == 'am_par' && Is_Menu_am_par_Open) {
        Is_Menu_am_par_Open = false;
        var el = xGetElementById(menu);
        xDisplay(el, 'block');
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), 0, 300, 1, 0);
    }
            
    //////////////////////////
    // Komakdarsi Main
    //////////////////////////

    if (menu == 'Menu_Komakdarsi' && !Is_Menu_Komakdarsi_Open) 
    {
        Is_Menu_Komakdarsi_Open = true;
        var el = xGetElementById(menu);        
        xDisplay(el, 'block');        
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), itemsCnt * 26, 300, 1, 0);
    }
    else if (menu == 'Menu_Komakdarsi' && Is_Menu_Komakdarsi_Open) {
        Is_Menu_Komakdarsi_Open = false;
        var el = xGetElementById(menu);
        xDisplay(el, 'block');
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), 0, 300, 1, 0);
    }

    //////////////////////////
    // Komakdarsi Genre
    //////////////////////////

    if (menu == 'Mot_komakDarsi_Genre' && !Is_Mot_komakDarsi_Genre_Open) 
    {
        Is_Mot_komakDarsi_Genre_Open = true;
        var el = xGetElementById(menu);
        var elTop = xGetElementById('Menu_Komakdarsi');
        xDisplay(el, 'block');

        var animTop = new xAnimation(10);
        animTop.size(elTop, xWidth(elTop), xHeight(elTop) + (itemsCnt * 26), 300, 1, 0);

        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), itemsCnt * 26, 300, 1, 0);
    }
    else if (menu == 'Mot_komakDarsi_Genre' && Is_Mot_komakDarsi_Genre_Open) {
        Is_Mot_komakDarsi_Genre_Open = false;
        var el = xGetElementById(menu);
        var elTop = xGetElementById('Menu_Komakdarsi');
        xDisplay(el, 'block');

        var animTop = new xAnimation(10);
        animTop.size(elTop, xWidth(elTop), xHeight(elTop) - (itemsCnt * 26), 300, 1, 0);

        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), 0, 300, 1, 0);
    }

    //////////////////////////
    // Komakdarsi Age
    //////////////////////////

    if (menu == 'Mot_komakDarsi_Age' && !Is_Mot_komakDarsi_Age_Open) 
    {
        Is_Mot_komakDarsi_Age_Open = true;
        var el = xGetElementById(menu);
        var elTop = xGetElementById('Menu_Komakdarsi');
        xDisplay(el, 'block');

        var animTop = new xAnimation(10);
        animTop.size(elTop, xWidth(elTop), xHeight(elTop) + (itemsCnt * 26), 300, 1, 0);

        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), itemsCnt * 26, 300, 1, 0);
    }
    else if (menu == 'Mot_komakDarsi_Age' && Is_Mot_komakDarsi_Age_Open) {
        Is_Mot_komakDarsi_Age_Open = false;
        var el = xGetElementById(menu);
        var elTop = xGetElementById('Menu_Komakdarsi');
        xDisplay(el, 'block');

        var animTop = new xAnimation(10);
        animTop.size(elTop, xWidth(elTop), xHeight(elTop) - (itemsCnt * 26), 300, 1, 0);

        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), 0, 300, 1, 0);
    }

    //////////////////////////
    // Daneshafzayee
    //////////////////////////

    if (menu == 'Menu_Daneshafzayee' && !Is_Menu_Daneshafzayee_Open)
    {
        Is_Menu_Daneshafzayee_Open = true;
        var el = xGetElementById(menu);
        xDisplay(el, 'block');
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), itemsCnt * 26, 300, 1, 0);
    }
    else if (menu == 'Menu_Daneshafzayee' && Is_Menu_Daneshafzayee_Open) {
        Is_Menu_Daneshafzayee_Open = false;
        var el = xGetElementById(menu);
        xDisplay(el, 'block');
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), 0, 300, 1, 0);
    }

    //////////////////////////
    // Olampiad Main
    //////////////////////////

    if (menu == 'Menu_Olampiad' && !Is_Menu_Olampiad_Open) {
        Is_Menu_Olampiad_Open = true;
        var el = xGetElementById(menu);
        xDisplay(el, 'block');
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), itemsCnt * 26, 300, 1, 0);
    }
    else if (menu == 'Menu_Olampiad' && Is_Menu_Olampiad_Open) {
        Is_Menu_Olampiad_Open = false;
        var el = xGetElementById(menu);
        xDisplay(el, 'block');
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), 0, 300, 1, 0);
    }

    //////////////////////////
    // Olampiad Genre
    //////////////////////////
  
    if (menu == 'Menu_Olampiad_Genre' && !Is_Menu_Olampiad_Genre_Open) {
        Is_Menu_Olampiad_Genre_Open = true;
        var el = xGetElementById(menu);
        var elTop = xGetElementById('Menu_Olampiad');
        xDisplay(el, 'block');

        var animTop = new xAnimation(10);
        animTop.size(elTop, xWidth(elTop), xHeight(elTop) + (itemsCnt * 26), 300, 1, 0);

        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), itemsCnt * 26, 300, 1, 0);
    }
    else if (menu == 'Menu_Olampiad_Genre' && Is_Menu_Olampiad_Genre_Open) {
        Is_Menu_Olampiad_Genre_Open = false;
        var el = xGetElementById(menu);
        var elTop = xGetElementById('Menu_Olampiad');
        xDisplay(el, 'block');

        var animTop = new xAnimation(10);
        animTop.size(elTop, xWidth(elTop), xHeight(elTop) - (itemsCnt * 26), 300, 1, 0);

        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), 0, 300, 1, 0);
    }

    //////////////////////////
    // Olampiad Set
    //////////////////////////
  
    if (menu == 'Menu_Olampiad_Set' && !Is_Menu_Olampiad_Set_Open) {
        Is_Menu_Olampiad_Set_Open = true;
        var el = xGetElementById(menu);
        var elTop = xGetElementById('Menu_Olampiad');
        xDisplay(el, 'block');

        var animTop = new xAnimation(10);
        animTop.size(elTop, xWidth(elTop), xHeight(elTop) + (itemsCnt * 26), 300, 1, 0);

        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), itemsCnt * 26, 300, 1, 0);
    }
    else if (menu == 'Menu_Olampiad_Set' && Is_Menu_Olampiad_Set_Open) {
        Is_Menu_Olampiad_Set_Open = false;
        var el = xGetElementById(menu);
        var elTop = xGetElementById('Menu_Olampiad');
        xDisplay(el, 'block');

        var animTop = new xAnimation(10);
        animTop.size(elTop, xWidth(elTop), xHeight(elTop) - (itemsCnt * 26), 300, 1, 0);

        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), 0, 300, 1, 0);
    }

    ///
    /// Zabanha
    ///
    if (menu == 'Menu_Zabanha' && !Is_Menu_Zabanha_Open) {
        Is_Menu_Zabanha_Open = true;
        var el = xGetElementById(menu);
        xDisplay(el, 'block');
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), itemsCnt * 26, 300, 1, 0);
    }
    else if (menu == 'Menu_Zabanha' && Is_Menu_Zabanha_Open) {
        Is_Menu_Zabanha_Open = true;
        var el = xGetElementById(menu);
        xDisplay(el, 'block');
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), 0, 300, 1, 0);
    }

}

function CloseSubmenu(menu, itemsCnt) {
    //////////////////////////
    // Amouzesh o Parvaresh
    //////////////////////////
    if (menu == 'am_par') {//alert('1');
        Is_Menu_am_par_Open = false;
        var el = xGetElementById(menu);
        xDisplay(el, 'block');
        var anim = new xAnimation(10);
        anim.size(el, xWidth(el), 0, 300, 1, 0);
    }
 }

function CloseAllMenu(menu) {

    if (Is_Menu_Komakdarsi_Open && menu != 'Menu_Komakdarsi') {
        
        Is_Menu_Komakdarsi_Open = false;
        var menu_komakdarsi = xGetElementById('Menu_Komakdarsi');
        var anim_komakdarsi = new xAnimation(10);
        anim_komakdarsi.size(menu_komakdarsi, xWidth(menu_komakdarsi), 0, 300, 1, 0);
    }

    if (Is_Mot_komakDarsi_Genre_Open && menu != 'Mot_komakDarsi_Genre') {
        
        Is_Mot_komakDarsi_Genre_Open = false;
        var menu_komakDarsi_Genre = xGetElementById('Mot_komakDarsi_Genre');
        var anim_komakdarsi_Genre = new xAnimation(10);
        anim_komakdarsi_Genre.size(menu_komakDarsi_Genre, xWidth(menu_komakDarsi_Genre), 0, 300, 1, 0);
    }
    if (Is_Mot_komakDarsi_Age_Open && menu != 'Mot_komakDarsi_Age') {
        
        Is_Mot_komakDarsi_Age_Open = false;
        var menu_komakDarsi_Age = xGetElementById('Mot_komakDarsi_Age');
        var anim_komakdarsi_Age = new xAnimation(10);
        anim_komakdarsi_Age.size(menu_komakDarsi_Age, xWidth(menu_komakDarsi_Age), 0, 300, 1, 0);
    }
    //
    //
    //
    if (Is_Menu_Daneshafzayee_Open && menu != 'Menu_Daneshafzayee') {

        Is_Menu_Daneshafzayee_Open = false; 
        var menu_Daneshafzayee = xGetElementById('Menu_Daneshafzayee');
        var anim_Daneshafzayee = new xAnimation(10);
        anim_Daneshafzayee.size(menu_Daneshafzayee, xWidth(menu_Daneshafzayee), 0, 300, 1, 0);
    }
    //
    //
    //
    if (Is_Menu_Olampiad_Open && menu != 'Menu_Olampiad') {
        
        Is_Menu_Olampiad_Open = false;
        var menu_Olampiad = xGetElementById('Menu_Olampiad');
        var anim_Olampiad = new xAnimation(10);
        anim_Olampiad.size(menu_Olampiad, xWidth(menu_Olampiad), 0, 300, 1, 0);
    }
    if (Is_Menu_Olampiad_Genre_Open && menu != 'Menu_Olampiad_Genre') {
        
        Is_Menu_Olampiad_Genre_Open = false;
        var menu_Olampiad_Genre = xGetElementById('Menu_Olampiad_Genre');
        var anim_Olampiad_Genre = new xAnimation(10);
        anim_Olampiad_Genre.size(menu_Olampiad_Genre, xWidth(menu_Olampiad_Genre), 0, 300, 1, 0);
    }
    if (Is_Menu_Olampiad_Set_Open && menu != 'Menu_Olampiad_Set') {
        
        Is_Menu_Olampiad_Set_Open = false;
        var menu_Olampiad_Set = xGetElementById('Menu_Olampiad_Set');
        var anim_Olampiad_Set = new xAnimation(10);
        anim_Olampiad_Set.size(anim_Olampiad_Set, xWidth(anim_Olampiad_Set), 0, 300, 1, 0);    
    }
}


