$(function(){

    // ajax news
    $('.ajax_news').live('click', function(e){
        var params = $(this).attr('href').substr(1)

        $('.news_content').load('/assets/snippets/news/news.ajax.php?'+params)

        e.preventDefault();
    })


    // tootlips
    var timer = new Array();
    var show = new Array();
    $('.pic').hover(function(){
        var item = $(this).attr('class').substr(7);
        if(timer['pic'+item]) clearTimeout(timer['pic'+item])
        $('.tt_pic'+item).css({
            'top': $('.pic'+item).offset().top+230,
            'left': $('.pic'+item).offset().left-25
        }).filter(':hidden').slideToggle('fast')//.animate({height: 'toggle'}, 'fast');

        var date = new Date;
        var ut = date.getTime();
        show['pic'+item] = 1
        var imageUrl = '/assets/templates/default/images/pic'+item+'_a.gif?ut='+ut;
        $('<img />')
            .attr('src', imageUrl)
            .load(function(){
                if(show['pic'+item])
                    $('.pic'+item+' .icon').css('background-image','url('+imageUrl+')')
            });
    },function(){
        var item = $(this).attr('class').substr(7);
        timer['pic'+item]=setTimeout(function(){
            show['pic'+item] = 0
            $('.pic'+item+' .icon').css('background-image','url(/assets/templates/default/images/pic'+item+'.gif)')
            $('.tt_pic'+item+':visible').slideToggle('fast')//.animate({height: 'toggle'}, 'fast');
        },100)
    })
    $('.tooltip').hover(function(){
        var item = $(this).attr('class').substr(14);
        if(timer['pic'+item]) clearTimeout(timer['pic'+item])
    },function(){
        var item = $(this).attr('class').substr(14);
        timer['pic'+item]=setTimeout(function(){
            show['pic'+item] = 0
            $('.pic'+item+' .icon').css('background-image','url(/assets/templates/default/images/pic'+item+'.gif)')
            $('.tt_pic'+item+':visible').slideToggle('fast')//.animate({height: 'toggle'}, 'fast');
        },100)
    })

    // phone
    $('.head3 .contacts').hover(function(){
        var date = new Date;
        var ut = date.getTime();
        var imageUrl = '/assets/templates/default/images/phone.gif?ut='+ut;
        var $target = $(this);
        $('<img />')
            .attr('src', imageUrl)
            .load(function(){
                $target.css('background-image','url('+imageUrl+')')
            });
    },function(){
        
    })

    // audio player
    $('#music_toggle').click(function(e){
        if(typeof audiofile != 'undefined')
        {
            var playing = ($(this).html()=='Выключить музыку' ? 1 : 0)
            if(playing)
            {
                stop()
                document.getElementById("music_toggle").innerHTML = 'Включить музыку'
            }
            else
                play()
        }
        e.preventDefault()
    })

})



var myListener = new Object();
/**
 * Initialisation
 */
myListener.onInit = function()
{
    this.position = 0;
};
/**
 * Update
 */
myListener.onUpdate = function()
{
    var isPlaying = (this.isPlaying == "true");
    if(!isPlaying && document.getElementById("music_toggle").innerHTML=='Выключить музыку')
        play()
    else {
        if(isPlaying)
            document.getElementById("music_toggle").innerHTML = 'Выключить музыку'
        else
            document.getElementById("music_toggle").innerHTML = 'Включить музыку'
    }

};

function getFlashObject()
{
    return document.getElementById("myFlash");
}
function play()
{
    if (myListener.position == 0) {
        if(typeof audiofile != 'undefined')
            getFlashObject().SetVariable("method:setUrl", audiofile);
    }
    getFlashObject().SetVariable("method:play", "");
    getFlashObject().SetVariable("enabled", "true");
}
function pause()
{
    getFlashObject().SetVariable("method:pause", "");
}
function stop()
{
    getFlashObject().SetVariable("method:stop", "");
}
function setPosition()
{
    var position = document.getElementById("inputPosition").value;
    getFlashObject().SetVariable("method:setPosition", position);
}
function setVolume()
{
    var volume = document.getElementById("inputVolume").value;
    getFlashObject().SetVariable("method:setVolume", volume);
}
