/*
if(!console){
    console={
        log: function(){},
        warn: function(){}
    }
}
*/



window.sounds = ['','do','re','mi','fa','sol','la','si'];
window.swftemplate = '<object type="application/x-shockwave-flash" data="/sounds/XX" width="0" height="0">' +
'</object>';


var IE=false;
Gpx = 'px';
try{
    new ActiveXObject('Microsoft.XMLHttp');
    IE=true;
    Gpx = '';
}catch(e){
    IE=false;
    Gpx = 'px';
}
try{
    window.opera.clearErrorMessages();

    IE=false;
    var OPERA=true;
    Gpx = 'px';
}catch(e){
var OPERA = false;

}

function getItems(){
    //IESound();
    //console.log('enter');
    var list = [];
    var c = document.getElementById('menu_horiz_wrapper');
    var d = c.childNodes;
    for (i in d){
        //console.log(d[);

        if(d[i].tagName=='A'){
            list.push(d[i]);
            //console.log(d[i]);
        }
    }
    return list;
}

//var AA = function(id){ return document.getElementById(id);}

var menu = {
    prefix2: "second_level_menu_item_wrapper_",
    prefix1: "menu_item_",
    mainItems: null,


    prepare: function(){

        //document.getElementById('second_level_menu_item_wrapper_1').style.visibility='visible';

        this.mainItems = getItems();
        for (i in this.mainItems){
            if(this.mainItems[i].tagName=='A'){
                menu.attachSecondItem(this.mainItems[i]);
            }
        }

    },


    attachSecondItem: function(mainItem){
        //console.log(mainItem);
        mainItem.numId = this.getNumber(mainItem);
        addSound(mainItem);

        try{
            mainItem.sublevel = document.getElementById(menu.prefix2 + mainItem.numId);
            if(!mainItem.sublevel){
                return;
            }
            //mainItem.sublevel.style.left = menu.calculateLeft(mainItem)+ Gpx;

        }catch(e){
            return;
        }



//console.log(mainItem.sublevel);

        mainItem.onmouseover = function(event){
            clearTimeout(mainItem.tm);
            var x = menu.calculateLeft(this)
            this.sublevel.style.left = x + Gpx;
            this.sublevel.style.visibility='visible';
            //menu.showShadow(x);
            //console.log(event.target);

            Sound(mainItem.numId);

            };
        mainItem.onmouseout = function(event){
            mainItem.tm = setTimeout(function(){mainItem.sublevel.style.visibility='hidden'; },250);
            };
        mainItem.sublevel.onmouseover = function(event){
                clearTimeout(mainItem.tm);
            };
        mainItem.sublevel.onmouseout = function(event){
                mainItem.tm = setTimeout(function(){mainItem.sublevel.style.visibility='hidden';},250);
            };

    },
    getNumber: function(item){
        return item.getAttribute('id').replace(/[a-zA-Z_]*/,'').replace(/_[pa]/,'');
    },
    calculateLeft: function(item){
        if(IE||OPERA){
            return menu.realCalculateLeft(item);
        }
        return item.offsetLeft;
    },
    realCalculateLeft: function(item){

        var node = document.getElementById(item.getAttribute('id'));
        var left = 0;
        while(node.tagName!='BODY'){
            left += node.offsetLeft;
            node = node.offsetParent;
        }
        return left;
    },
    showShadow: function(x){
        var shadow = document.getElementById('shadow');
        shadow.style.left = (x+10) + Gpx;
        shadow.style.visibility = 'visible';
    },
    hideShadow: function(){
        var shadow = document.getElementById('shadow');
        shadow.style.visibility = 'hidden';
    }
}


function addSound(mainItem){
    var m=mainItem
    m.Sound = function(){
        console.log(m.numId);
        document.body.innerHTML += window.swftemplate.replace(/XX/g,window.sounds[m.numId]+'.swf');
    };
}

function Sound(id){
    if(storageProvider.get('sound')!='on') return;
    if(IE){
        return IESound(id);
    }
    //console.log('id=    '+id);
    //document.body.innerHTML += window.swftemplate.replace(/XX/g,window.sounds[id]+'.swf');
    //document.getElementById('soundplayerhidden').setAttribute('data','/sounds/'+window.sounds[id]+'.swf');
    var x = document.createElement('object');
    x.setAttribute('width','0');
    x.setAttribute('height','0');
    x.setAttribute('style','position:absolute; visibility: hidden; top: -300px; left: -499px;');
    x.setAttribute('type','application/x-shockwave-flash');
    x.setAttribute('data','/sounds/'+window.sounds[id]+'.swf');
    var y = document.createElement('param');
    y.setAttribute('name','movie');
    y.setAttribute('value','/sounds/'+window.sounds[id]+'.swf');
    document.body.appendChild(x);
}

function IESound(id){
    /*var ins = '<object type="application/x-shockwave-flash" data="/sounds/' +
    window.sounds[id] +
    '.swf" width="288" height="128"><param name="movie" value="/sounds/' +
    window.sounds[id] +
    '.swf" /></object>';
    document.body.innerHTML += ins;*/
    /*document.getElementById('soundplayerhidden').setAttribute('data','/sounds/'+window.sounds[id]+'.swf');*/

    var flash = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="0" height="0" id="small_book" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/sounds/'+window.sounds[id]+'.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#fff9f1" /><embed src="/sounds/'+window.sounds[id]+'.swf" quality="high" bgcolor="#fff9f1" width="0" height="0" name="small_book" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';

   var divik = document.createElement('div');
   document.appendChild(divik);
   divik.innerHTML = flash;


}






storageProvider = {
    properties: {},
    init: function(){
        //console.log(document.cookie);
        var splited = document.cookie.split(/\s*;\s*/);
        //console.dir(splited);
        var mass;
        for(i in splited){
            mass = splited[i].split(/=/);
            var index = mass[0];
            storageProvider.properties[index] = mass[1];
        }
        setTimeout('storageProvider.checkSound();',1000);
    },
    get: function(id){
        return storageProvider.properties[id] || false;
    },
    set: function(name,value){
        storageProvider.properties[name]=value;

        storageProvider.setCookie(name,value);
    },
    toggleSound: function(){
        if(storageProvider.get('sound')=='on'){
            storageProvider.set('sound','off');
            document.getElementById('controlsound').src = '/img/sound_off.gif';
        }else{
            storageProvider.set('sound','on');
            document.getElementById('controlsound').src = '/img/sound_on.gif';
        }
        //storageProvider.checkSound();
    },
    setCookie: function(name, value) {

        var curCookie = name + "=" + escape(value)+';path=/';

                document.cookie = curCookie

    },
    checkSound: function(){

        if(!document.getElementById('controlsound')){
            setTimeout(storageProvider.checkSound,800);
        }
        try{
            if(storageProvider.get('sound')!='on'){
                document.getElementById('controlsound').src = '/img/sound_off.gif';
            }else{
                document.getElementById('controlsound').src = '/img/sound_on.gif';
            }
        }catch(e){
            setTimeout(storageProvider.checkSound,800);
        }
       // alert(storageProvider.get('sound'));
    }


}




storageProvider.init();





window.menu = menu;

try{
window.onload = function(){
        menu.prepare();
        try{
            linkZvukRej();
        }catch(e){}
        try{
            makeAjaxFoto();
        }catch(e){}
        try{
            setPlayer();
        }catch(e){}
    };
}catch(e){
    //console.warn(e);
}
