$(document).ready(function() {
   /* $('a[href^=#c]').click(function() {
        var s = $(this).attr('href').replace('#', '');
        plane.key = s.split('-')[0];
        plane.url = s.split('-')[1];
        plane.start();
        return false;
    });

    $('a[href=#teleport]').click(function(){
        plane.teleport();
        return false;
    });

    plane.init();*/
    main_gallery.init();
    tours.resize();
});

var rtimer = null

$(window).bind('resize', function() {
    if(rtimer) clearTimeout(rtimer);
    rtimer = setTimeout(function() {
        main_gallery.resize();
        tours.resize();
    }, 100);
});

tours = {
    resize: function() {
        var cw = $('div.right_2').width();
        var count = Math.floor(cw/150);
        var nw = Math.floor(cw/count) - 11;
        
        var list = $('ul.tours li');
        list.hide();
        for(var i = 0;i < count;i++) {
            $(list[i]).show().width(nw);
        }
    }
}

main_gallery = {
    init: function() {
        /*
        $('a.border').click(function() {
            var o = $(this);
            var big = $('div.big_one');
            // ссылка
            var href = $('a', big).attr('href').split('/');
            href.pop();
            href.push(o.attr('rel').replace('folder-', ''));
            $('a', big).attr('href', href.join('/'));
            // картинка
            $('img', big).attr('src', o.attr('href'));
            // подпись
           var title_s = $('span', o);
           if(title_s.length > 0 && title_s.html()) $('p.bo-title', big).html(title_s.html()).show();
           else $('p.bo-title', big).hide();
            
            return false;
        });
        */
        main_gallery.resize();
    },
    resize: function() {
        var cw = $('div.right_1r').width();
        var count = Math.floor(cw/93);
        
        var list = $('ul.main_gallery_list li');
        list.hide();
        for(var i = 0;i < count;i++) {
            $(list[i]).css('display', 'block');
        }
    }
}
/*
plane = {
    flight: false,
    key: '', 
    countries: {
        c1: 'goa', c2: 'bulgary', c3: 'turkey', c4: 'egypt'
    },
    phase: 'up',
    coords: {x: 0, y: 0},
    speed: 1,
    last_x: 0,
    view: null,
    t: null,
    url: '',
    images: new Array(),
    images_bad: new Array(),
    images_gray: new Array(),
    pl_t: null, 

    init: function() {
        plane.view = $('#i_bg');
        var imgs = ['goa_ground.png', 'bulgary_ground.png', 'turkey_ground.png', 'egypt_ground.png'];
        for(i in imgs) {
            var mi = new Image();
            mi.src = '/img/'+imgs[i].replace('.png', '_gray.gif');
            plane.images_gray.push(mi);
            var bi = new Image();
            bi.src = '/img/'+imgs[i].replace('.png', '_bad.png');
            plane.images_bad.push(bi);
            var gi = new Image();
            gi.src = '/img/'+imgs[i];
            plane.images.push(gi);
        }

        plane.image(0, $('#i_ground'));
    },

    // модный прелоадер
    image: function(i, obj) {
        if(plane.images[i].complete)  {
            obj.css('background', 'url('+plane.images[i].src+') no-repeat bottom left');
            //$('#debug').html('Image: '+plane.images[i].src);
            clearTimeout(plane.pl_t);
        } else {
            if(plane.images_bad[i].complete)  {
                obj.css('background', 'url('+plane.images_bad[i].src+') no-repeat bottom left');
                //$('#debug').html('Image: '+plane.images_bad[i].src);
            } else {
                obj.css('background', 'url('+plane.images_gray[i].src+') no-repeat bottom left');
                //$('#debug').html('Image: '+plane.images_gray[i].src);
            }
            plane.pl_t = setTimeout(function() {plane.image(i, obj)}, 100);
        }
    },

    start: function() {
        if(plane.flight) return false;
        plane.flight = true;

        // прелоадим гигантский фон
        var bg = new Image();
        bg.src = '/img/'+plane.countries[plane.key]+'_ground.png';

        $('#arrows').fadeOut();
        $('#s_magic_button').hide();
        $('#magic_button').fadeIn();

        plane.coords.x = parseFloat(plane.view.css('margin-left').replace('px', ''));
        plane.coords.y = parseFloat(plane.view.css('margin-top').replace('px', ''));

        plane.phase = 'up';

        clouds.start();
        hot.start();
        plane.move();
    },

    teleport: function() {
        plane.flight = false;
        document.location.href = plane.url;
    },

    move:function() {
        if(!plane.flight) {
            clouds.stop();
            hot.stop();
            clearTimeout(plane.t);
            return false;
        }
        if(plane.phase == 'up') {
            // -1235 - правая граница
            plane.speed = 0.75*Math.log(Math.abs(plane.coords.x));
            plane.coords.x -= plane.speed;
            
            if(Math.abs(plane.coords.x) >= 400+1055) plane.coords.y -= 2.25*Math.sin(plane.speed);

            plane.view.css({marginLeft: plane.coords.x, marginTop: plane.coords.y});

            if(Math.abs(plane.coords.x) > 1005+1055) {
                plane.phase = 'flight';
            }
        }
        if(plane.phase == 'flight') {
            // 240 - верхняя граница
            var k = 0.75*Math.log(Math.abs(plane.coords.x));
            plane.speed = Math.pow(Math.E, 2805/Math.abs(plane.coords.x));

            plane.coords.y -= 2.75*Math.sin(k);

            plane.coords.x -= plane.speed;

            plane.view.css({marginLeft: plane.coords.x, marginTop: plane.coords.y});
            if(Math.abs(plane.coords.x) > 1435+1055) {
                plane.phase = 'down';
                plane.last_x = plane.coords.x;
                clearTimeout(plane.pl_t);
                plane.image(parseFloat(plane.key.replace('c', ''))-1, $('#i_ground'));
            }
        }
        if(plane.phase == 'down') {
            plane.speed = 0.75*Math.log(Math.abs(plane.coords.x-85-plane.last_x));

            plane.coords.y -= 1.75*Math.cos(plane.speed);

            plane.coords.x -= plane.speed;

            plane.view.css({marginLeft: plane.coords.x, marginTop: plane.coords.y});
            if(plane.coords.y <= -140) {
                plane.phase = 'stop';
                plane.last_x = plane.coords.x;
                $('#i_ground').css('margin-left', Math.abs(plane.last_x)-150+'px');
            }
        }
        if(plane.phase == 'stop') {
            //var k = 0.75*Math.log(Math.abs(plane.coords.x)-(plane.last_y+2));
            plane.speed = Math.pow(Math.E, 4555/Math.abs(plane.coords.x));

            if(plane.coords.y > -290) plane.coords.y -= 2.5*Math.sin(plane.speed);

            plane.coords.x -= plane.speed;

            plane.view.css({marginLeft: plane.coords.x, marginTop: plane.coords.y});

            if(Math.abs(plane.coords.x) >= Math.abs(plane.last_x)+1159) {
                clouds.stop();
                clearTimeout(plane.t);
                $('#i_bg').css('margin-left', -(Math.abs(plane.coords.x)-(Math.abs(plane.last_x)-150))+'px');
                $('#i_ground').css('margin-left', '0px');
                plane.flight = false;
                hot.stop();
                $('#arrows').fadeIn();
                $('#magic_button').hide();
                $('#s_magic_button').fadeIn();
                return false;
            }
        }

        plane.t = setTimeout(plane.move, 24/1000);
    }
}

clouds = {
    speed: 0,
    x: new Array(),
    self: new Array(),
    t: null,

    start: function() {
        clouds.self.push($('#i_clouds'));
        clouds.self.push($('#i_clouds_b'));
        for(var i in clouds.self) {
            clouds.x.push(parseFloat(clouds.self[i].css('margin-left').replace('px', '')));
        }

        clouds.move();
    },

    move: function() {
        clouds.speed = 0.35;
        clouds.x[0] -= 0.5*clouds.speed;
        clouds.x[1] -= 0.125*clouds.speed;
        clouds.self[0].css({marginLeft: clouds.x[0]});
        clouds.self[1].css({marginLeft: clouds.x[1]});

        clouds.t = setTimeout(clouds.move, 24/1000);
    },

    stop: function() {
        for(var i in clouds.self) {
            clouds.self[i].css('margin-left', 2);
        }
        clearTimeout(clouds.t);
    }
}

hot = {
    tours: {
        c1: ['Горячее предложение!', 'Лети в Гоа!', 'Супер-дешёвые туры!', 'Новый год под пальмой!'],
        c2: ['Горячее предложение!', 'Лети в Болгарию!', 'Болгария из Самары!', 'Новый год в горах!'],
        c3: ['Горячее предложение!', 'Лети в Турцию!', 'Турецкий рынок!', 'Новый год в Тадж-Махале!'],
        c4: ['Горячее предложение!', 'Лети в Египет!', 'Друг, Наташа!', 'Новый год у пирамид!']
    },
    cur: 0,
    o: null,
    t: null,

    start: function() {
        hot.o = $('#hot_tour');

        hot.move();
    },
    stop: function() {
        clearTimeout(hot.t);
        hot.o.fadeOut();
    },
    move: function() {
        hot.o.find('a').html(hot.tours[plane.key][hot.cur]);
        hot.o.animate({marginLeft: "125px"}, 'fast', function() {
            hot.t = setTimeout(function() {
                hot.o.animate({marginLeft: "-225px"}, 'fast', function() {
                    clearTimeout(hot.t);
                    hot.t = setTimeout(function() {
                        hot.cur++;
                        if(hot.cur >= hot.tours[plane.key].length) hot.cur = 0;
                        hot.o.css('margin-left', '450px')
                        hot.move();
                    }, 500);
                });
            }, 3000);
        });
    }
}
*/

function ShowWin(url,x,y,name,isscrollbars) {
cx=screen.width / 2 - (x / 2);
cy=(screen.height/2-(y/2));
isscrollbars=(isscrollbars=="no")?"no":"yes";
window.open(url,name,"toolbar=no,status=no,directories=no,menubar=no,resizable=yes,width="+x+",height="+y+",scrollbars="+isscrollbars+",top="+cy+",left="+cx);
return false;
} 
