var iterface   = new iterface_class();

function iterface_class(){
    this.conteiner        = '#conteiner';
    this.width            = 0;
    this.selector         = '#element_';
    this.select_interface = 'menu';
    this.f_selector       = '.sl_i_';
    this.l                = 1;
    this.s                = 0;
    this.e                = 0;
    this.sa               = 0;
    this.a                = 0;
    this.fg               = 0;
    this.block_hover      = false;
    this.result           = false;
    this.menu_name        = '#interface_bg';
    this.menu_status      = false;
    this.menu_open_close  = false;
    
    this.load = {
        'menu':{
            name:'Меню',
            alt_name:'menu',
            number:7,
            all:1,
            start:1,
            action:'admin/menu.php?name=menu'
        },
        'moduls':{
            name:'Модули',
            alt_name:'moduls',
            number:7,
            all:1,
            start:1,
            action:'admin/moduls.php?name=moduls'
        }
    };
    
    this.create = function(){
        var go = [
        '<div id="open_interface" class="position_f left_50 top" onclick="iterface.menu()"></div>',
        '<div id="interface_bg" class="position_f width height" style="display: none;">',
         '<div class="position_f width height interface_shadow"></div><div class="bg"></div>',
   
         '<div class="position_f width left interface_conteiner" id="conteiner" style="-moz-user-select: none">',
         
         '</div>',
   
         '<div class="position_f width interface_menu" align="center">',
          '<div class="interface_menu_tab">',
           '<div>',
            '<div>',
             '<ul class="ul" id="menu_name">',

             '</ul>',
            '</div>',
           '</div>',
          '</div>',
         '</div>',
   
         '<map name="speed">',
          '<area shape="rect" coords="4,12,40,30" style="cursor: pointer;" onmouseover="iterface.speed(false,1)" onmouseout="iterface.speed_out(1)" />',
          '<area shape="rect" coords="42,6,76,18" style="cursor: pointer;" onmouseover="iterface.speed(false,2)" onmouseout="iterface.speed_out(2)" />',
          '<area shape="rect" coords="78,0,112,10" style="cursor: pointer;" onmouseover="iterface.speed(false,3)" onmouseout="iterface.speed_out(3)" />',
          '<area shape="rect" coords="114,0,194,10" style="cursor: pointer;" onmouseover="iterface.speed(false,4)" onmouseout="iterface.speed_out(4)" />',
          '<area shape="rect" coords="116,0,230,10" style="cursor: pointer;" onmouseover="iterface.speed(true,5)" onmouseout="iterface.speed_out(5)" />',
          '<area shape="rect" coords="232,6,266,18" style="cursor: pointer;" onmouseover="iterface.speed(true,6)" onmouseout="iterface.speed_out(6)" />',
          '<area shape="rect" coords="268,12,303,30" style="cursor: pointer;" onmouseover="iterface.speed(true,7)" onmouseout="iterface.speed_out(7)" />',
         '</map>',
         '<div class="position_f left_50 interface_speed"><img src="',templates_dir,'images/interface/interface_speed.png" usemap="#speed" border="0" /></div>',
        '</div>',
	    ].join('');
        $(go).appendTo('body');
    }
    
    this.document_init = function(){
        start = 1;
        
        $.each(iterface.load, function(index) {
            if(start == 1){
                iterface.init(iterface.load[index]);
                iterface.select_interface = index;
            }
            else{
                if(!$('.i_menu_' + iterface.load[index]['alt_name']).length){
                    $('#menu_name').append('<li onclick="iterface.init(iterface.load[\''+index+'\'])" class="default i_menu_'+index+'"><span>'+iterface.load[index]['name']+'</span></li>');
                }
                else{
                    $('.i_menu_' + iterface.select_interface).removeClass('active');
                    $('.i_menu_' + iterface.load[index]['alt_name']).addClass('active');
                }
                
            }
            start +=1;
        });
        
        $('#menu_name li:first').addClass('left');
        $('#menu_name li:last').addClass('right');

    }
    
    this.menu = function(callback){
        if(!iterface.menu_status){
            $('body').css({overflow:'hidden',marginRight: '15px'});
            $(iterface.menu_name).fadeIn('slow',function(){
                iterface.document_init();
                iterface.menu_status = true;
                iterface.menu_open_close = true;
                
            });
        }
        else{
            if(!iterface.menu_open_close){
                $('body').css({overflow:'hidden',marginRight: '15px'});
                $(iterface.menu_name).fadeIn('slow',function(){
                    if(callback) callback();
                });
                iterface.menu_open_close = true;
            }
            else{
                $(iterface.menu_name).fadeOut('slow',function(){
                    $('body').css({overflow:'visible',marginRight: '0px'});
                    if(callback) callback();
                    $(iterface.conteiner).removeClass('add_mode');
                    
                });
                iterface.menu_open_close = false;
            }
        }
    }
    
    this.init = function(arr){
        arr = $.extend({}, arr);
        
        load_data = function(action){
            conect({
                url:templates_dir + arr.action,
                action:function(data){
                    if(action) action(data)
                    else return data;
                }
            }); 
        }
        if(!$(iterface.selector + arr.alt_name).length){
            load_data(function(data){
                style = (iterface.select_interface == '') ? '' : ' style="display:none" ';
                $(iterface.conteiner).append('<ul class="'+arr.alt_name+'" id="element_'+arr.alt_name+'"'+style+'>'+data+'<ul>');
                show_content(iterface.selector + iterface.select_interface,iterface.selector + arr.alt_name,function(){
                    iterface.load_animate(iterface.load[arr.alt_name]);
                });
            })
        }
        else{
            $(iterface.selector + arr.alt_name).fadeOut('fast',function(){
                    show_content(iterface.selector + iterface.select_interface,iterface.selector + arr.alt_name,function(){
                        iterface.load_animate(iterface.load[arr.alt_name]);
                    });
            })
        }
        
        if(!$('.i_menu_' + arr.alt_name).length){
            $('#menu_name').append('<li onclick="iterface.init(iterface.load[\''+arr.alt_name+'\'])" class="active default i_menu_'+arr.alt_name+'"><span>'+arr.name+'</span></li>');
        }
        else{
            $('.i_menu_' + iterface.select_interface).removeClass('active');
            $('.i_menu_' + arr.alt_name).addClass('active');
        }
        
        
        //$('#menu_name li:first').addClass('left');
        //$('#menu_name li:last').addClass('right');

        
    }
    
    this.add = function(arr){
        arr = $.extend({}, arr);
        if(arr.page && iterface.result){
            c = (Math.floor((arr.number/2).toFixed(1)))+1;
            go = arr.all - c;
            
            if(arr.start >= go){
                conect({
                    url:templates_dir + arr.action + '&page=' + (arr.page + 1),
                    action:function(data){
                        if(data == 'no_result'){
                            iterface.result = false;
                        }
                        else{
                            iterface.load[iterface.select_interface]['page'] += 1; 
                            $(iterface.selector + arr.alt_name).append(data);
                        }
                        
                    }
                });
            }
        }
    }
    
    this.formula = function(arr,type){
        width  = $(iterface.conteiner).width();
        height = $(iterface.conteiner).height();

        A = width / 2;
        h = height / 2;
        o = 0.1;
        
        var B = height, Xo = A, Yo = h;
        var k = 0.7;
        
        d = Math.floor((arr.number/2).toFixed(1));
        c = d + 1;
       
        s = (c >= arr.start) ? c - arr.start : (arr.number >= arr.start) ? arr.start - c : (arr.all >= arr.start) ? (arr.start - arr.number) + d : (arr.all - arr.number) + d;

        for(i=1;arr.number >= i;i++){
            
            x = k*((i-1)/d - 1);
            y = Math.sqrt(1-x*x);
            x = Xo + A*x;
            y = (Yo - B*y)+h;

            n = (c >= arr.start) ? i - s : (arr.number >= arr.start) ? i + s : i + s;
            
            if(n > 0 && n <= arr.all){
                elem = $(iterface.f_selector + arr.alt_name +'_'+ n);
                
                if(type){
                    if(c > i){
                        elem.animate({left:x+'px',top:y+'px',opacity:(((arr.number / c) * i)*o).toFixed(1)},300);
                        elem.attr({opacity:(((arr.number / c) * i)*o).toFixed(1)});
                        elem.removeClass('active');
                    }
                    else if(c == i){
                        elem.animate({left:x+'px',top:y+'px',opacity:'1'},300).addClass('active').attr({opacity:1});
                    }
                    else{
                        elem.animate({left:x+'px',top:y+'px',opacity:(((arr.number / c) * (arr.number-(i-1)))*o).toFixed(1)},300);
                        elem.attr({opacity:(((arr.number / c) * (arr.number-(i-1)))*o).toFixed(1)});
                        elem.removeClass('active');
                    }
                }
                else{
                    if(c > i){
                        elem.css({left:x+'px',top:y+'px',opacity:(((arr.number / c) * i)*o).toFixed(1)});
                        elem.attr({opacity:(((arr.number / c) * i)*o).toFixed(1)});
                    }
                    else if(c == i){
                        elem.css({left:x+'px',top:y+'px',opacity:'1'}).addClass('active');
                    }
                    else{
                        elem.css({left:x+'px',top:y+'px',opacity:(((arr.number / c) * (arr.number-(i-1)))*o).toFixed(1)});
                        elem.attr({opacity:(((arr.number / c) * (arr.number-(i-1)))*o).toFixed(1)});
                    }
                }
            }
        }
    }
    
    this.load_animate = function(arr,type,position){

        iterface.formula(arr,type);
        
        time = 0;        
        d = Math.floor((arr.number/2).toFixed(1));
        c = d + 1;
        s = (c >= arr.start) ? c - arr.start : (arr.number >= arr.start) ? arr.start - c : (arr.all >= arr.start) ? (arr.start - arr.number) + d : (arr.all - arr.number) + d;

        im = function(arr,time,n){
            setTimeout(function(){
                if(type){
                    $(iterface.f_selector + arr.alt_name +'_'+ n).fadeIn('fast');
                }
                else{
                    $(iterface.f_selector + arr.alt_name +'_'+ n).fadeIn('slow');
                }
                
            },time)
        }

        if(position){
            b = (c >= arr.start) ? false : (arr.number >= arr.start) ? arr.start - c : (arr.all >= arr.start) ? (arr.start - arr.number) + d : (arr.all - arr.number) + d;
        }
        else{
            b = (c >= arr.start) ? c +  arr.start: (arr.number >= arr.start) ? c +  arr.start : (arr.all >= arr.start) ? (arr.start + d)+1 : (arr.all - arr.number) + d;
            
        }
  
        if(b){
            $(iterface.f_selector + arr.alt_name +'_'+ b).hide();
        } 

        for(i=1;arr.number >= i;i++){
            
            n = (c >= arr.start) ? i - s : (arr.number >= arr.start) ? i + s : i + s;

            if(n > 0 && n <= arr.all){
                im(arr,time,n);
                time = (type) ? 0 : time+50;
            } 
            
        }
        iterface.select_interface = arr.alt_name;
    }
    
    this.get_animate = function(type,speed,number,sp){
        var ops = 0;
        
        clearInterval(iterface.s);
        
        setTimeout(function(){
            iterface.result = true;
            iterface.block_hover = true;
            
            iterface.l = iterface.load[iterface.select_interface]['start'];
        ti = function(){
            
            ops += 1;
            
            if(number && ops > number){
                iterface.speed_out(sp);
                return false;
            }
            
            iterface.add(iterface.load[iterface.select_interface]);
            
            iterface.l = (type) ? ((iterface.load[iterface.select_interface]['all'] > iterface.l) ? iterface.l +=1 : iterface.l) : ((iterface.l <= iterface.load[iterface.select_interface]['all'] && iterface.l > 1) ? iterface.l -=1 : iterface.l);
            
            iterface.load[iterface.select_interface]['start'] = iterface.l;
            iterface.load_animate(iterface.load[iterface.select_interface],true,type);
            
            
        }
        
        ti();
        
        iterface.s = setInterval(function(){
            ti();
            if(iterface.l >= iterface.load[iterface.select_interface]['all'] || iterface.l <= 1){
                clearInterval(iterface.s);
                iterface.speed_alarm(sp);
            }
        },((speed > 500) ? speed: 500));
        },600);
    }
    
    this.show_element = function(id,type){
        
        if(iterface.block_hover) return false;
        
        op = $('.'+id).attr('opacity');

        if(type) $('.'+id).animate({opacity:1},200).addClass('hover');
        else     $('.'+id).animate({opacity:op},200).removeClass('hover');
    }
    
    this.speed_alarm = function(sp){

        elem = $('.interface_speed');
        
        get_alarm = function(){
            elem.addClass('s'+sp);
            setTimeout(function(){
                elem.removeClass('s'+sp);
            },300)
        }
        get_alarm();
        
        iterface.sa = setInterval(function(){
            get_alarm();
        },600);
        
        setTimeout(function(){
             clearInterval(iterface.sa);
        },2400);
    }
    
    this.speed = function(type,sp,number){
        
        speed_s = {
            1:500,
            2:1000,
            3:1500,
            5:1500,
            6:1000,
            7:500,
        }
        
        start = iterface.load[iterface.select_interface]['start'];
        end   = iterface.load[iterface.select_interface]['all'];
        
        $('.interface_speed').addClass('s'+sp);
        
        clearTimeout(this.e);
        
        this.e = setTimeout(function(){

            if(!type && start == 1){
                iterface.speed_alarm(sp);
            }
            else if(type && start == end){
                iterface.speed_alarm(sp);
            }
            else if(sp == 4){
                iterface.speed_alarm(sp);
            }
            else{                
                iterface.get_animate(type,speed_s[sp],number,sp)
            }
            
        },600)
    }
    
    this.speed_out = function(sp){
        clearInterval(iterface.sa);
        clearInterval(iterface.e);
        clearInterval(iterface.s);
        if(sp){
            $('.interface_speed').removeClass('s'+sp);
        }
        else{
            $('.interface_speed').removeClass('s1 s2 s3 s4 s5 s6 s7');
        }
        iterface.block_hover = false;
    }
    
    this.scroll = function(evt){
        iterface.a = (evt > 0) ? iterface.a += 1: iterface.a -=1;
        iterface.speed_out();
        
        speed_s_p = {
            1:5,
            2:6,
            3:7
         }
         
        speed_s_m = {
            1:3,
            2:2,
            3:1
         }
         
        fot = function(){
            clearTimeout(iterface.fg);
            iterface.fg = setTimeout(function(){
                iterface.a = 0;
            },600);
        }
            
         if(evt > 0){
            s = (iterface.a <=3) ? speed_s_p[iterface.a] : speed_s_p[3];
            iterface.speed(true,s,((iterface.a <= 3) ? iterface.a : 3));
            fot();
         }
         else{
            a = Math.abs(iterface.a);
            s = (a <=3) ? speed_s_m[a] : speed_s_m[3];
            iterface.speed(false,s,((a <= 3) ? a : 3));
            fot();
         }
    }
    
    this.add_mode = function(){
        $(iterface.conteiner).addClass('add_mode');
        iterface.menu();
    }
}


$(document).ready(function(){
    
    iterface.create();
  
    $(iterface.menu_name).bindWheelEvent(function(evt) {
        iterface.scroll(evt);
    });
    
});

$.fn.bindWheelEvent = function(callback) {
    
    if(!$.isFunction(callback)) { return this; }

    if(this.length == 0) { return this; }

    var jsThis = this.get(0);

    var normalizedCallback = function(e) {

        if(!e) { var e = window.event; }

        e.cancelDefaultAction = function() {
            if (this.stopPropagation) {
                this.stopPropagation();
            }
            if (this.preventDefault) {
                this.preventDefault();
            }
            this.cancelBubble = true;
            this.cancel = true;
            this.returnValue = false;
            return false;
        }       
       
        var wheelData = e.detail ? e.detail * -1 : e.wheelDelta / 40;
        if (Math.abs(wheelData)>100) { wheelData=Math.round(wheelData/100); }

        e.wheelData = wheelData;
        //return callback.call(jsThis, e);
        return callback(wheelData);
    }

    if (jsThis.addEventListener) {
        jsThis.addEventListener('DOMMouseScroll', normalizedCallback, false);
        jsThis.addEventListener('mousewheel', normalizedCallback, false);
    }
    else if (jsThis.attachEvent) {
        jsThis.attachEvent('onmousewheel', normalizedCallback);
    }
    return this;
};






