function search_switcher(id, search_id, zone){
    var tabs = new Array("newstab", "sitetab", "inettab", "shoptab");

    if(id == "shoptab"){
        document.getElementsByName('google_search_form')[0].className = 'hidden_class';
        $('shop_search_form').className = 'block';
        tabs.each(function(item){
            if(item != id){
                $(item).setStyles({
                    'font-weight': 'normal',
                    'color'      : '#bfd9fe'
                });
            }
            else{
                $(item).setStyles({
                    'font-weight': 'bold',
                    'color'      : '#ffffff'
                });
            }
        });
    }
    else{
        $('shop_search_form').className = 'hidden_class';
        document.getElementsByName('google_search_form')[0].className = 'block';
        tabs.each(function(item){
            if(item != id){
                $(item).setStyles({
                    'font-weight': 'normal',
                    'color'      : '#bfd9fe'
                });
            }
            else{
                $(item).setStyles({
                    'font-weight': 'bold',
                    'color'      : '#ffffff'
                });
                document.getElementsByName('google_search_form')[0].id='searchbox_'+search_id;
                document.getElementsByName('cx')[0].value=search_id;
                document.getElementsByName('zone')[0].value=zone;
            }
        });
    }
}

function resizeImg(id)
{
    var size=$(id).getSize();
    var proportion;
    if(size.y<size.x){
        proportion = size.x/size.y;
        $(id).setStyles({
            'margin-top':(84-Math.ceil(84/proportion))/2+'px'
        });
    }
}

function resizeLoveImg(id)
{
    var size=$(id).getSize();
    var proportion;
    if(size.y>size.x){
        proportion = size.y/size.x;
        $(id).setStyles({
            'height':'82px',
            'width':Math.floor(82/proportion)+'px'
        });
    }
    else{
        proportion = size.x/size.y;
        $(id).setStyles({
            'height':Math.floor(82/proportion)+'px',
            'width':'82px',
            'margin-top':(82-Math.ceil(82/proportion))/2+'px'
        });
    }
}

function floatBanner()
{
    var sizeBody = getSize();
    if(sizeBody.x > 1100 && !Browser.Engine.trident && !Browser.Engine.webkit){
        $('floatingBanner').setStyles({'display': 'inline'});
        var fieldOfView = Math.floor((sizeBody.x-1010)/2);
        $('floatingBanner').setStyles({
            'width': fieldOfView-10,
            'height': fieldOfView-10
        });
        $('floatingBannerEmbed').setStyles({
            'width': fieldOfView-10,
            'height': fieldOfView-10
        });
        var bringThatShitInFocus = new Fx.Morph('floatingBanner', {duration: 1000,transition: Fx.Transitions.Sine.easeOut})
        var windowScroll = window.getScroll();
        var bannerPos = $('floatingBanner').getPosition();
        if(windowScroll.y+(fieldOfView-10) > bannerPos.y){
            bringThatShitInFocus.start({
               'top' : windowScroll.y
            });
        }
        else if(bannerPos.y > windowScroll.y){
            bringThatShitInFocus.start({
               'top' : windowScroll.y
            });
        }
    }
    else if(sizeBody.x > 1200 && Browser.Engine.trident){
        $('floatingBanner').setStyles({'display': 'inline'});
        fieldOfView = Math.floor((sizeBody.x-1010)/2);
        $('floatingBanner').setStyles({
            'width': fieldOfView-10,
            'height': fieldOfView-10
        });
        bringThatShitInFocus = new Fx.Morph('floatingBanner', {duration: 1000,transition: Fx.Transitions.Sine.easeOut})
        windowScroll = window.getScroll();
        bannerPos = $('floatingBanner').getPosition();
        if(windowScroll.y+(fieldOfView-10) > bannerPos.y){
            bringThatShitInFocus.start({
               'top' : windowScroll.y
            });
        }
        else if(bannerPos.y > windowScroll.y){
            bringThatShitInFocus.start({
               'top' : windowScroll.y
            });
        }
    }
    else{
        $('floatingBanner').setStyles({
            'display': 'none',
            'width': 0,
            'height':0
        });
    }
}

var style; //make it global
window.addEvent('domready', function(){
    var headliner = {
        counter: 0
    };

    var switch_headliner = function(){
        var switcher_from = new Fx.Morph('news_head'+this.counter, {
            duration: 'normal',
            transition: Fx.Transitions.Sine.easeOut
        });
        if(this.counter==4) this.counter=-1;//to start again from first...
        var switcher_to = new Fx.Morph('news_head'+(this.counter+1), {
            duration: 'normal',
            transition: Fx.Transitions.Sine.easeIn
        });
        this.counter++;
        switcher_from.start({
            'opacity': [1,0]
        });
        switcher_to.start({
            'opacity': [0,1]
        });
    };

    var switcher = switch_headliner.periodical(5000, headliner);

    $('news_ff').addEvent('click', function(){
        switcher = $clear(switcher);
        $('news_pause').setProperty('title','возобновить');
        var i=-1;
        do{
            i++;
        }
        while($('news_head'+i).getStyle('opacity').toInt()==0 || $('news_head'+i).getStyle('visibility')=='hidden');
        var switcher_from = new Fx.Morph('news_head'+i, {
            duration: 'normal',
            transition: Fx.Transitions.Sine.easeOut
        });
        if(i==4) i=-1;//to start again from first...
        headliner.counter++;
        if(headliner.counter==4) headliner.counter=-1;//to start again from first...
        var switcher_to = new Fx.Morph('news_head'+(i+1), {
            duration: 'normal',
            transition: Fx.Transitions.Sine.easeIn
        });
        switcher_from.start({
            'opacity': [1,0]
        });
        switcher_to.start({
            'opacity': [0,1]
        });
    });
    $('news_rewind').addEvent('click', function(){
        switcher = $clear(switcher);
        $('news_pause').setProperty('title','возобновить');
        var i=-1;
        do{
            i++;
        }
        while($('news_head'+i).getStyle('opacity').toInt()==0 || $('news_head'+i).getStyle('visibility')=='hidden');
        var switcher_from = new Fx.Morph('news_head'+i, {
            duration: 'normal',
            transition: Fx.Transitions.Sine.easeOut
        });
        if(i==0) i=5;//to start again from last...
        headliner.counter--;
        if(headliner.counter==0) headliner.counter=5;//to start again from first...
        var switcher_to = new Fx.Morph('news_head'+(i-1), {
            duration: 'normal',
            transition: Fx.Transitions.Sine.easeIn
        });
        switcher_from.start({
            'opacity': [1,0]
        });
        switcher_to.start({
            'opacity': [0,1]
        });
    });
    $('news_pause').addEvent('click', function(){
        if(switcher){
            switcher = $clear(switcher);
            this.setProperty('title','возобновить');
        }
        else{
            switcher = switch_headliner.periodical(5000, headliner);
            this.setProperty('title','остановить');
        }
    });
    /*
     * muting non-current color schemes
     */
    if (document.cookie.length>0)
    {
        var style_start = document.cookie.indexOf("stylez=")+7;
        if (document.cookie.indexOf("stylez=") > -1)
        {
            var style_end = document.cookie.indexOf(";",style_start);
            if (style_end == -1) style_end=document.cookie.length;
            style=unescape(document.cookie.substring(style_start,style_end));
        }
        else style="blue";
    }
    $('switcherz').getElements('img[id!='+style+']').each(function(item){
        var mute = new Fx.Morph(item, {
            link: 'chain',
            transition: Fx.Transitions.Sine.easeOut
        });
        mute.start({
            'opacity': [1, 0.4]
        });
        item.addEvent('mouseover', function(){
            mute.start({
                'opacity': 1
            });
        });
        item.addEvent('mouseout', function(){
            mute.start({
                'opacity': 0.4
            });
        });
    });

    //captcha change
    var captchaMorph = new Fx.Morph('captcha_image', {
        duration: 'short',
        link: 'chain',
        transition: Fx.Transitions.Sine.easeOut
    });
    var ajaxRequest = new Request({
        url:'/?controller=chat&action=captcha',
        method: 'get',
        onSuccess: function(txt){
            $('captcha_image').onload = function(){
                captchaMorph.start({
                    'opacity': [0, 1]
                });
            };
            $('chat_id').value=txt;
            $('captcha_image').src='http://chat.gala.net/security.fcgi?id='+txt;
        }
    });
    $('change_captcha').addEvent('click', function(e){
        e.stop();
        captchaMorph.start({
            'opacity': 0
        }).chain(function(){
            ajaxRequest.send();
        });
    });
    //injecting bannerz codez
    $('top_banner').set('html', $('top_banner_injector').innerHTML);
    $('right_banner').set('html', $('right_banner_injector').innerHTML);
    //floating banner...
    //var floatingBanner = floatBanner.periodical(2000);
});

function setHighLighting() {
    mouseOver = function () {
        this.className += " linktr_hl"
    }

    mouseOut = function () {
        this.className = this.className.replace(new RegExp(" linktr_hl\\b"), "")
    }


    elems = $('left_content').getElementsByTagName('div');
    for (i=0; i<elems.length; i++) {
        if (elems[i].className=='linktr') {
            elems[i].onmouseover = mouseOver;
            elems[i].onmouseout = mouseOut;
        }
    }

}

function loadObserver() {
    setHighLighting();
}
var c_tab = 'weathertab';
window.onload = loadObserver;

function shCities() {
    if ($('wcities').style.display=='block') {
        var weatherMorphIn = new Fx.Morph('wcities', {
            duration: 'normal',
            transition: Fx.Transitions.Sine.easeOut
        });
        weatherMorphIn.start({
            'opacity': [1,0]
        }).chain(function(){
            $('wcities').style.display = 'none'
        });
    }else {
        var weatherMorphOut = new Fx.Morph('wcities', {
            duration: 'normal',
            transition: Fx.Transitions.Sine.easeIn
        });
        $('wcities').style.display = 'block';
        $('wcities').setStyle('opacity',0);
        weatherMorphOut.start({
            'opacity': [0,1]
        });
    }
}

function chCity(id,name) {
    $('wcontent').innerHTML = '<img src="/images/load.gif" align="center" style="margin-left:65px;" />';
    shCities();
    $('weather_link').href = 'http://gala.net/weather/?city='+id;
    $('cityName').innerHTML = name;
    var url    = 'index.php';
    var params = {
        controller: 'weather',
        id: id
    };
    var ajax   = new Request.HTML(
    {
        update: 'wcontent',
        url: url
    }).get(params);
}

function chHoro(id,name) {
    $('wcontent').innerHTML = '<img src="/images/load.gif" align="center" style="margin-left:65px;" />';
    shCities();
    $('weather_link').href = 'http://horo.gala.net/?lang=ru&sign='+id;
    $('cityName').innerHTML = name;
    var url    = 'index.php';
    var params = {
        controller: 'horo',
        id: id
    };
    var ajax   = new Request.HTML(
    {
        update: 'wcontent',
        url: url
    }).get(params);
}
function chFinance(id,name) {
    $('wcontent').innerHTML = '<img src="/images/load.gif" align="center" style="margin-left:65px;" />';
    shCities();
    $('weather_link').href = 'http://finance.gala.net/?bank='+id;
    $('cityName').innerHTML = name;
    var url    = 'index.php';
    var params = {
        controller: 'finance',
        id: id
    };
    var ajax   = new Request.HTML(
    {
        update: 'wcontent',
        url: url
    }).get(params);
}

function chTab(tab) {
    $(c_tab).className = $(c_tab).className.replace(new RegExp(" active\\b"), "")
    $(tab).className += ' active';
    $('wcontent').innerHTML = '<div style="position:absolute;background-color:#FFFFFF;padding:15px 69px;margin-top:-25px;margin-left:-10px"><img src="/images/load.gif" /></div>';
    var url    = 'index.php';
    var params = {
        controller: tab
    };
    var ajax   = new Request.HTML(
    {
        update: 'tcontent',
        url: url
    }).get(params);
    c_tab = tab;
    switch(tab)
    {
        case 'horotab':
            $('weather_link').href = 'http://horo.gala.net/';
            break;

        case 'financetab':
            $('weather_link').href = 'http://finance.gala.net/';
            break;

        default:
            var city;
            if (document.cookie.length>0)
            {
                var city_start = document.cookie.indexOf("weathercity=")+12;
                if (document.cookie.indexOf("weathercity=") > -1)
                {
                    var city_end = document.cookie.indexOf(";",city_start);
                    if (city_end == -1) city_end=document.cookie.length;
                    city=unescape(document.cookie.substring(city_start,city_end));
                }
                else city=null;
            }
            if (city != null){
                $('weather_link').href = 'http://gala.net/weather/?city='+city;
            }
            else{$('weather_link').href = 'http://gala.net/weather/'}
    }
}
