function setAutoRemove(id, val) {
    if (!$(id)) return false;
    $(id).addEvent('focus', function() {
        if (this.get('value') == val) {
            this.set('value', '');
        }
    });
    $(id).addEvent('blur', function() {
        if (this.get('value') == '') {
            this.set('value', val);
        }
    });
    if ($(id).get('value') == '') $(id).set('value',val);

    return $(id);
}
function setbackground(i, count) {
    if(count == 1){
        document.body.setStyle('background', '#'+backgrnd.bcolor+' url("/'+backgrnd.backgrd1+'") no-repeat fixed '+backgrnd.width_percent+'% '+backgrnd.height_percent+'%');
    } else if(count == 2) {
        if(i<2) {
            i++;
        } else {
            i=1;
        }
        window.setTimeout( "setbackground("+i+", "+count+")", 15000);
        if(i == 1) {
            document.body.setStyle('background', '#'+backgrnd.bcolor+' url("/'+backgrnd.backgrd1+'") no-repeat fixed '+backgrnd.width_percent+'% '+backgrnd.height_percent+'%');
        } else if(i == 2) {
            document.body.setStyle('background', '#'+backgrnd.bcolor+' url("/'+backgrnd.backgrd2+'") no-repeat fixed '+backgrnd.width_percent+'% '+backgrnd.height_percent+'%');
        }
    } else if(count == 3) {

        if(i<3) {
            i++;
        } else {
            i=1;
        }
        window.setTimeout( "setbackground("+i+", "+count+")", 15000);
        if(i == 1) {
            document.body.setStyle('background', '#'+backgrnd.bcolor+' url("/'+backgrnd.backgrd1+'") no-repeat fixed '+backgrnd.width_percent+'% '+backgrnd.height_percent+'%');
        } else if(i == 2) {
            document.body.setStyle('background', '#'+backgrnd.bcolor+' url("/'+backgrnd.backgrd2+'") no-repeat fixed '+backgrnd.width_percent+'% '+backgrnd.height_percent+'%');
        } else if(i == 3) {
            document.body.setStyle('background', '#'+backgrnd.bcolor+' url("/'+backgrnd.backgrd3+'") no-repeat fixed '+backgrnd.width_percent+'% '+backgrnd.height_percent+'%');
        }
    }
}
function setrandbackground() {
        $(document.body).setStyle('background', '#'+backgrnd.bcolor+' url("/'+backgrnd.backgrd+'") no-repeat fixed '+backgrnd.width_percent+'% '+backgrnd.height_percent+'%');
}
window.addEvent('load',function(){
    setAutoRemove('header-search',$('header-search').get('value'));
    $$('.video_player').each(function(item) {
        if (item.get('id')) {
            if (item.get('id').split('-').pop() == 'autostart'){
                var autostart = true;
            } else {
                var autostart = false;
            }
            var is_sound = item.get('title').indexOf('mp3') != -1;
            swfobject.embedSWF(
                    '/flash/player.swf',
                    item.get('id'),
                    '578',
                    item.get('title').indexOf('mp3') == -1 ? 410 : 32,
                    '9.0.0',
                    '/flash/expressInstall.swf',
            {
                'file':			item.get('title'),
                'type':			is_sound ? 'sound' : 'video',
                'icons':		is_sound ? false : true,
                backcolor: '444444',
                frontcolor: 'cccccc',
                lightcolor: 'f8fb01',
                //'screencolor':	'#030303',
                'volume':		'60',
                'skin':			'/flash/default.swf',
                'bufferlength':	'5',
                'autostart': autostart
            },
            {
                'allowScriptAccess':	'always',
                'allowFullScreen':		'true',
                'scale':				'noscale',
                'salign':				'tl',
                'bgcolor':				'#030303',
                'wmode':                'opaque'
            },
            {
                'id':					'index-videoplayer',
                'name':					'index-videoplayer',
                'align':				'middle'
            }
                    );
        }
    });


    if (document.getElementById('voting-video')) {
        $$('.star').each(function(item) {
            $(item).addEvent('click', function(evt) {
                var vote = evt.target.get('id').split('-').pop();
                var id = evt.target.getParent('.voting_stars').get('id').split('-').pop();
                new Request({
                    'url':	'/video/vote',
                    onComplete: function(res) {
                        evt.target.getParent('.voting_stars').set('html','<div class="vote_message">'+res+'</div>');
                    }
                }).post({
                    'vote':	vote,
                    'id': id
                });
                evt.stop();
            });
        });
    }

    if (document.getElementById('voting-topic')) {
        $$('.star').each(function(item) {
            $(item).addEvent('click', function(evt) {
                var vote = evt.target.get('id').split('-').pop();
                var id = evt.target.getParent('.topic_voting_stars').get('id').split('-').pop();
                new Request({
                    'url':	'/fanzone/vote',
                    onComplete: function(res) {
                        evt.target.getParent('.topic_voting_stars').set('html','<div class="vote_message">'+res+'</div>');
                    }
                }).post({
                    'vote':	vote,
                    'id': id
                });
                evt.stop();
            });
        });
    }

    if ($('video-comments-block')) {
        $('video-comments-block').slide('hide');
        $('video-show-comments').addEvent('click',function(evt){
            evt.stop();
            if ($('video-comments-block').hasClass('is_hidden')) {
                $('video-comments-block').removeClass('is_hidden');
                $('video-comments-block').slide('in');
            } else {
                $('video-comments-block').addClass('is_hidden');
                $('video-comments-block').slide('out');
            }
        });
    }

    if ($('video-post-comment-form')) {
        $('video-post-comment-button').addEvent('click',function(){
            $('video-post-comment-form').set('action','/video/post_comment?token=07485645711077755');
            $('video-post-comment-form').submit();
        });
    }

    if (document.getElementById('share')) {
        $('share').slide('hide');
    }
    if (document.getElementById('embed')) {
        $('embed').slide('hide');
    }

    if (document.getElementById('link-share')){
        $('link-share').addEvent('click',function(evt){
            evt.stop();
            if (document.getElementById('embed')) {
                $('embed').slide('hide');
            }
            if (document.getElementById('share')) {
                $('share').slide('in');
            }
        });
    }
    if (document.getElementById('link-embed')){
        $('link-embed').addEvent('click',function(evt){
            evt.stop();
            if (document.getElementById('share')) {
                $('share').slide('hide');
            }
            if (document.getElementById('embed')) {
                $('embed').slide('in');
            }
        });
    }

});
window.addEvent('domready',function(){
     if(window.backgrnd != null) {
        var i = 0;
         if(window.backgrnd.bcolor == '0') {
             window.backgrnd.bcolor = '000000';
         }
        setrandbackground();
    }
    if(window.backgrnd != null) {
        Asset.images('/'+backgrnd.backgrd);
    }
    if(document.getElementById('country')) {
        $('country').addEvent('change', function(item) {
            var id_country = this.value;
            new Request.JSON({
                    'url':	'/profile/get_cities.json',
                    onComplete: function(res) {
                        $('city').innerHTML = '';
                        for(var i in res.res) {
                            var option = new Element('option');
                            option.value = i;
                            option.innerHTML = res.res[i];
                            option.inject($('city'), 'bottom');
                        }
                    }
                }).post({
                    'id_country': id_country
                });
        });
    }
});
