var timeout;
var menu_array = [];
var menu_proj = [];
var index_proj = [];
var menu_index = [];
var box;
var initMultiBox;

function start(){
    
    menu_array = $$('.act');
    menu_proj = $$('.projects');
    index_proj = $$('.index_text');
    menu_index = $$('.index_p');
    
    correctBrowser();
    if ($$('.toggle')) startSlide();
    if ($$('.menu')) startMenu();
    if ($$('.nn2')) startOver();
    if ($$('.index')) startProj();
    
    box = new multiBox({
        mbClass: '.mbhtml',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
        container: $(document.body),//where to inject multiBox
        descClassName: false,//the class name of the description divs
        path: './Files/',//path to mp3 and flv players
        useOverlay: true,//use a semi-transparent background. default: false;
        addDownload: false,//do you want the files to be downloadable?
        pathToDownloadScript: './Scripts/ForceDownload.asp',//if above is true, specify path to download script (classicASP and ASP.NET versions included)
        addRollover: false,//add rollover fade to each multibox link
        addOverlayIcon: false,//adds overlay icons to images within multibox links
        showControls: false,//adds overlay icons to images within multibox links
        showNumbers: false,
        addChain: false,//cycle through all images fading them out then in
        recalcTop: false,//subtract the height of controls panel from top position
        addTips: false//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
    });
    
    initMultiBox = new multiBox({
        mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
        container: $(document.body),//where to inject multiBox
        descClassName: false,//the class name of the description divs
        path: './Files/',//path to mp3 and flv players
        useOverlay: true,//use a semi-transparent background. default: false;
        addDownload: false,//do you want the files to be downloadable?
        pathToDownloadScript: './Scripts/ForceDownload.asp',//if above is true, specify path to download script (classicASP and ASP.NET versions included)
        addRollover: false,//add rollover fade to each multibox link
        addOverlayIcon: false,//adds overlay icons to images within multibox links
        showControls: true,//adds overlay icons to images within multibox links
        showNumbers: false,
        addChain: false,//cycle through all images fading them out then in
        recalcTop: false,//subtract the height of controls panel from top position
        addTips: false//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
    });
    
    initMultiBox = new multiBox({
        mbClass: '.mbvideo',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
        container: $(document.body),//where to inject multiBox
        descClassName: false,//the class name of the description divs
        path: rootPath,//path to mp3 and flv players
        useOverlay: true,//use a semi-transparent background. default: false;
        addDownload: false,//do you want the files to be downloadable?
        pathToDownloadScript: './Scripts/ForceDownload.asp',//if above is true, specify path to download script (classicASP and ASP.NET versions included)
        addRollover: false,//add rollover fade to each multibox link
        addOverlayIcon: false,//adds overlay icons to images within multibox links
        showControls: true,//adds overlay icons to images within multibox links
        showNumbers: false,
        addChain: false,//cycle through all images fading them out then in
        recalcTop: false,//subtract the height of controls panel from top position
        addTips: false//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
    });
}

function resize(){
    if (document.body.clientWidth < 1024) {
        $$('.container').setStyle('width', '990px');
        $$('.toggler, .toggle, .toggle1, .toggle2, .slider').setStyle('width', '860px');
    }
}

function showTab(url){
    var ob = $('overlay');
    var fla = $(url);
    
    ob.setStyles({
        'display': 'block', 
        'opacity': .7,
        'height': window.getScrollSize().y
    });

    fla.setStyle('display', 'block');
    
    (function(){
    var flaW = fla.getWidth();
    var w = $(window).getWidth();

    var top = (($(window).getHeight() - fla.getHeight()) / 2) + window.getScroll().y;
    var left = ($(window).getWidth() - flaW) / 2;
    
    var tabl = fla.getElement('table');
    var widt;
    var lft;

    if (tabl.hasClass('titl2')){
        widt = '841px';
        tabl.setStyle('width', '841px');
        lft = ($(window).getWidth() - flaW) / 2;
    } else {
        widt = '80%';
        lft = '10%';
    }
    
    fla.setStyles({
        width: widt,
        top: top,
        left: lft
        
    });
    
    ob.addEvent('click', function(){
        ob.setStyles({'display': 'none', 'opacity': 0});
        fla.setStyle('display', 'none');
    });
    
    }).delay(100);
}


function showFla(url){
    var ob = $('overlay');
    var fla = $(url);
    
    ob.setStyles({
        'display': 'block', 
        'opacity': .7,
        'height': window.getScrollSize().y
    });

    fla.setStyle('display', 'block');

    var flaW = fla.getWidth();
    var w = $(window).getWidth();
    var h = $(window).getHeight();
    
    var we = w-200;
    var he = h-150;
    fla.setStyles({
        'width': we,
        'height': he
    });
    var top = (($(window).getHeight() - fla.getHeight()) / 2) + $(window).getScroll().y;
    var left = ($(window).getWidth() - fla.getWidth()) / 2;
    
    
    var inFlashObj = fla.getElement("object");
    var inFlashEmb = fla.getElement("embed");
    
    fla.setStyles({
        top: top,
        left: left        
    });
    
//    inFlashObj.width = we;
//    inFlashObj.height = he;
//    
//    inFlashEmb.width = we;
//    inFlashEmb.height = he;
    
    var ffW = $(url+"F");
    if(Browser.Engine.trident) {
        ffW.width = we;
        ffW.height = he;
    }else{
        inFlashObj.setProperties({
            width: we,
            height: he
        });
        
        inFlashEmb.setProperties({
            width: we,
            height: he
        });
    }
    
//    inFlashObj.setAttribute('width', we);
//    inFlashObj.setAttribute('height', he);
//    
//    inFlashEmb.setAttribute('width', we);
//    inFlashEmb.setAttribute('height', he);
    
    var ff = url+"F";
    
    ob.addEvent('click', function(){
        ob.setStyles({'display': 'none', 'opacity': 0});
        fla.setStyle('display', 'none');
    });
    
    
    eval('window.document.'+ff+'.SetVariable("fw", '+we+')');
    eval('window.document.'+ff+'.SetVariable("fh", '+he+')');
    
    //var obj = new Swiff($(url+'F'));
    //Swiff.remote(obj, 'echoText', 'Hello Flash, meet Swiff.');
}

function closeBox(url){
    var ob = $('overlay');
    var fla = $(url);
    
    ob.setStyles({'display': 'none', 'opacity': 0});
    fla.setStyle('display', 'none');
}

function startSlide(){
    $$('.show').setStyle('opacity', 0);
    $$('.show_in').setStyle('opacity', 0);
    $$('.main_pic').setStyle('opacity', 0);
    myAccordion = new Accordion($$('.toggler'), $$('.slider'), {
            onActive: function(toggler, slider){
                toggler.setStyle(
                    'cursor', 'default');
                $each($$('.slider'), function(el, z){
                   $('tog'+z).setStyle('opacity', 0);
                    if(el.id == slider.id){
                         (function(){ $('sho'+z).morph({opacity: .73})}).delay(410);
                         (function(){ $('sho_in'+z).morph({opacity: 1})}).delay(410);
                         (function(){ $('mpic'+z).morph({opacity: 1})}).delay(410);
                         
                    } else { 
                        $('sho'+z).morph({opacity: 0});
                        $('tog'+z).setStyle('opacity', 1);
                        $('sho_in'+z).morph({opacity: 0});
                        $('mpic'+z).morph({opacity: 0});
                    }
                });
            },
            onBackground: function(toggler, slider){
                toggler.setStyles({
                    'cursor': 'pointer'
                });                
            },
            onComplete: function(){
                
            },

            opacity: 0,
            display: 0,
            alwaysHide: false
    });
}

function startProj(){
    $each(menu_index, function(element, i){
        element.addEvent('mouseenter', function(){
             this.setStyle('background', 'url(img/'+ilang+'.jpg) left top no-repeat');
             if(timeout) clearTimeout(timeout);
             hideAllI(i);
        });
        element.addEvent('mouseleave', function(){
             
             timeout = setTimeout('hideAllI();', 90);
        });
   });
   
   $each(index_proj, function(element, i){ 
         element.addEvent('mouseenter', function(){
             if(timeout) clearTimeout(timeout);
             hideAllI(i);
         });
         element.addEvent('mouseleave', function(){
             timeout = setTimeout('hideAllI();', 40);
             
         });
             
   });
}
function hideAllI(act){
    $each(menu_index, function(element, i){
        if(act !== false && act==i) {
            index_proj[i].setStyle('display', 'block');
            
        } else {
            index_proj[i].setStyle('display', 'none');
            element.setStyle('background', 'url('+top_bg+') left top no-repeat');
        } 
    });
}

function startOver(){
    $$('.nn2 img').setStyle('opacity', .52);
    $each($$('.nn2 img'), function(element, i){
        element.addEvent('mouseenter', function(){
            this.setStyle('opacity', 1);
        });
        element.addEvent('mouseleave', function(){
            this.setStyle('opacity', .52);
        });
    });
}

function startMenu(){
   $each(menu_array, function(element, i){
        element.addEvent('mouseenter', function(){
             if(timeout) clearTimeout(timeout);
             hideAllS(i);
        });
        element.addEvent('mouseleave', function(){
             timeout = setTimeout('hideAllS();', 90);
        });
   });
   
   $each(menu_proj, function(element, i){ 
         element.addEvent('mouseenter', function(){
             if(timeout) clearTimeout(timeout);
             hideAllS(i);
         });
         element.addEvent('mouseleave', function(){
             timeout = setTimeout('hideAllS();', 40);
         });
             
   });
   
}
function hideAllS(act){
    $each(menu_array, function(element, i){
        if(act !== false && act==i) {
            menu_proj[i].setStyle('display', 'block');
        } else {
            menu_proj[i].setStyle('display', 'none');
        } 
    });
}

function startOverClose(){
    $('close').addEvent('mouseenter', function(){
        var reg = /(.*?)\.gif/;
        this.src = this.src.replace(reg, "$1_.gif");
    });
    $('close').addEvent('mouseleave', function(){
        var reg = /(.*?)\_.gif/;
        this.src = this.src.replace(reg, "$1.gif");
    });
}

function correctBrowser(){
   var ob = $('container');
   var ob2 = $('container2');
   var style = '0 0 0 1px';
   if(Browser.Engine.trident) style = '0 0 0 1px'; 
   if(Browser.Engine.gecko) style = '0 0 0 1px'; 
   if(Browser.Engine.webkit || Browser.Engine.presto) style = '0 0 0 3px'; 
   
   if(style) ob.setStyle('padding',style); 
   if(style) ob2.setStyle('padding',style); 
}


/* 
# Print function 
# clas - content to print                    
*/
var frame = false;

function printdoc(clas){ 
    scroll(0,0); 
    var conthtml = '';
    $each($$('.'+clas), function(element, i){
           conthtml += element.innerHTML + '<br />';  
        });
        
     var html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
                + '<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="ru-ru" xml:lang="ru-ru">'
                +'<head>'
                +'<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> '
                + '<style type="text/css">'
                + ' body{ font-family: Times New Roman; font-size: 12pt; color: #000; background: #fff; padding: 0;} '
                + ' h1{ font-size: 18pt;} '
                + ' td{ padding: 4px;} '
                + ' #top{ width: 98%; background: #585858; padding: 5px 1%; } '
                + ' #top a{ font-size: 14pt; color: #FFF; text-decoration: none;} '
                +'</style>'
                + '<script language="Javascript">'
                + ' function closeP(){ window.parent.closePrintDoc(); }'
                + ' function printP(){ document.getElementById("top").style.display = "none"; window.print(); closeP(); }'
                + ' </script> '
                + '</head><body>'
                + '<div id="top">'
                + '<a href="javascript: void(0);" onclick="javascript: printP(); ">Печать</a> | '
                + '<a href="javascript: void(0);" onclick="javascript: closeP(); ">Закрыть</a>'
                + '</div>'
                + conthtml
                + '</body></html>';
      
    var ws = $(window).getCoordinates();
    var wss = $(window).getScrollSize();
    
    
    $('frame').setStyles({
        'left': 0,
        'top': 0,
        'z-index': 29000,
        'display': 'block'
      
    });
            
    frame = new Element('iframe', {'id': 'iprint', 'width': ws.width+'px', 'height': ws.height+wss.y+'px'}).injectInside($('frame'));
    
        var testFrame = frame;
        var doc = testFrame.contentDocument;
        if (doc == undefined || doc == null)
            doc = testFrame.contentWindow.document;
       
        doc.open();
        doc.write(html);
        doc.close(); 

}

function closePrintDoc(){
    $('frame').setStyles({
        'display': 'none'
       });
    $('frame').innerHTML = '';
    frame.remove();
    
}

