﻿Object.createPackage = function(path) {
    var root = window, parts = path.split('.'), part;

    for (var i = 0; i < parts.length; i++) {
        part = parts[i];
        if (!root[part]) {
            root[part] = {};
        }
        root = root[part];
    }

    root.getInstance = function() {
        return this;
    }

    return root;
}

Object.findObject = function(path) {
    var root = window, parts = path.split('.'), part;

    for (var i = 0; i < parts.length; i++) {
        part = parts[i];
        root = root[part];
        if (!root) {
            return null;
        }
    }

    return root;
}

Function.createCallback = function(method, context) {
    return function(args) {
        return method(args, context);
    }
}


with (Object.createPackage('js')) {
    getInstance().EventContext = function(src, args) {
        this.src = src;
        this.args = args;
    }
}

Object.createPackage('js.vars');
Object.createPackage('js.ui');
Object.createPackage('js.app');

js.ui.Image = new function() {
    this.create = function() {
        var res = [];
        for (var i = 0; i < arguments.length; i++) {
            var img = new Image();
            img.src = arguments[i];
            res[res.length] = img;
        }
        return res;
    }
}

document.findElement = function(id) {
    return document.all ? document.all[id] : document.getElementById(id);
}



js.app.toggleGenreNode = function(img, id) {
    var childBlock = document.findElement('gn' + id);
    if (childBlock == null) return;

    if (img.className == 'minus') {
        img.className = 'plus';
        childBlock.style.display = 'none';
    } else {
        img.className = 'minus';
        childBlock.style.display = 'block';
    }
}


//js.vars.imgs = new js.ui.Image.create('/i/m.gif', '/i/p.gif')

//js.app.toggleGenreNode = function(img,id) {
//    var childBlock = document.findElement('gn' + id);
//    if (childBlock == null) return;

//    if (img.src.indexOf('/i/m.gif') >= 0) {
//    img.src = '/i/p.gif';
//    childBlock.style.display = 'none';
//    } else {
//    img.src = '/i/m.gif';
//    childBlock.style.display = 'block';
//    }
//}

js.app.showLoginForm = function() {
    var authform = document.getElementById('authform');
    if (!authform) return;
    document.getElementById('fadeDiv').style.display = authform.style.display = 'block';

    js.app.setLoginFocus();
    window.scroll(0, 0);
    
    return false;
}

//js.app.showLoginForm = function(submitAuthCallBack) {
//    var authform = document.getElementById('authform');
//    if (!authform) return;
//    document.getElementById('fadeDiv').style.display = authform.style.display = 'block';
//    //document.getElementById('authSubmit').onclick = submitAuthCallBack; // исползуйте jQuery! на IE так нельзя писать
//    $('#authSubmit').click(function() { submitAuthCallBack(); });

//    js.app.setLoginFocus();
//    return false;
//}

js.app.setLoginFocus = function() {
    var un = document.getElementById('unTb');
    if (un.value != '')
        document.getElementById('pwdTb').focus();
    else
        un.focus();
    return false;
}


SortingChanged = function(cookieName, redirect) {
    //FadeFunctionInit();		
    var sorter = document.getElementById('Sorter');
    var selected = document.getElementById('Sorter').selectedIndex;
    var values = new Array("AddDate", "Rating", "ReleaseDate", "Title");
    var cookieValue = values[selected];
    //var cookieValue = sele
    cookieName = cookieName + "Sorting";
    SetCookie(cookieName, cookieValue);
    var cookieNameNum = "AlbumSortingNum";
    SetCookie(cookieNameNum, selected);
    if (redirect) window.location = location.href;
}


function YearAlbumFilter() {
    var filter = new AlbumFilter('year', 'yearFilter', 'year');
    filter.Process();
}

