var handlers = null;
var flash = null;
jQuery.preloadImages = function() {
//return;
  for(var i = 0; i<arguments.length; i++) jQuery("<img>").attr("src", arguments[i]);
}

$(document).ready(function () {
    jQuery.preloadImages(
        '/img/corners.png',
        '/img/side_t.png',
        '/img/side_r.png',
        '/img/side_b.png',
        '/img/side_l.png'
    );

    flash = swfobject.getFlashPlayerVersion().major>0;
    handlers = new Handlers();

    if ($('body#index').get(0)){
        var flashvars = false;
        var params = { menu: "false", wmode: "transparent" };
        var attributes = false;
        swfobject.embedSWF("/fla/city.swf", "city", "100%", "100%", "9.0.0","/fla/expressInstall.swf", flashvars, params, attributes);
        if (flash) {
            $('#layout').css({
                'height': '100%',
//                'overflow': 'visible',
//                'padding-bottom': '150px',
                'background-image': 'none'
            });
        }
    }

    
    if ($('body#index .plane').get(0)) {
        var planes = [
            null,
            $("#plane1").get(0).style,
            $("#plane2").get(0).style,
            $("#plane3").get(0).style,
            $("#plane4").get(0).style,
            $("#plane5").get(0).style,
            $("#plane6").get(0).style,
            $("#plane7").get(0).style,
            $("#plane8").get(0).style,
            $("#plane9").get(0).style
        ]
        var layout = $('#layout');
        $('.plane').show();
    }

    $(document).mousemove(function(e){
//return false;
        if (!flash) {
            handlers.fit();
            var pCoords = "( "+e.pageX+", "+e.pageY+" )";
            var cCoords = "( "+e.clientX+", "+e.clientY+" )";
            var pivot = -1*e.pageX+layout.innerWidth()/2;

            planes[9].left = Math.floor(pivot/40)+'px';
            planes[8].left = Math.floor(pivot/20)+'px';
            planes[7].left = Math.floor(pivot/18)+'px';
            planes[6].left = Math.floor(pivot/15)+'px';
            planes[5].left = Math.floor(pivot/12)+'px';
            planes[4].left = Math.floor(pivot/10)+'px';
            planes[3].left = Math.floor(pivot/9)+'px';
            planes[2].left = Math.floor(pivot/7.5)+'px';
            planes[1].left = Math.floor(pivot/5)+'px';
        }
//      $("#debug").text($('#layout').css('background-position'));
        
    });

    $("div.handler").hover(
        function () { $('#'+$(this).attr('id').replace('_handler', '')).addClass('hover');}
        ,function () { $('#'+$(this).attr('id').replace('_handler', '')).removeClass('hover');}
    );

    $("div.handler").click(function () {
        display_balloon($(this).attr('id').replace('_glow_handler', ''));
    });


    $('#footer .podhod .logo').hover(
        function(){$(this).addClass('hover')}
        ,function(){$(this).removeClass('hover');}
    );

    $(window).resize(function(){
        fit_height();
        handlers.fit();
    });

//    fit_height();
//    handlers.fit();
    var gazelle = new Gazelle();
    gazelle.animate();
});

window.onload = function () {
    fit_height();
    
}

fit_height = function () {
    flash = swfobject.getFlashPlayerVersion().major>0;
    var dimensions = {width: 0, height: 0};
    if (document.documentElement) { dimensions.width = document.documentElement.offsetWidth; dimensions.height = document.documentElement.offsetHeight; } else if (window.innerWidth && window.innerHeight) { dimensions.width = window.innerWidth; dimensions.height = window.innerHeight; }
    var offset = 0;
    if (dimensions.height<=700) offset = -200;
    else if ((dimensions.height>700)&&(dimensions.height<900)) offset = dimensions.height-900;
    else offset = 0;
    if (!flash) {
        $('.plane').css('top', offset+'px');
        $('#layout').css('background-position', '50% '+offset+'px');
        $('#layout').css('height', 1100+offset+'px');
        $('#background').css('background-position', '50% '+offset+'px');
        $('#balloons').css('top', (offset+300)+'px');
        alert('ok');
    }
    else {
        $('#balloons').css('top', (dimensions.height-400)*0.66+'px');
    }

    $('#technology #body .wire').each(function(){
        $(this).height($(this).parents('.row').innerHeight()-290);
    });

    $('#menu ul').each(function(){
        offset = $(this).find('li:first span').offset();
        var offset2 = $(this).find('li:first span>*').offset();
        $(this).css('margin-left', (offset.left-offset2.left)+'px');
    });

    $('div.icons').parents('a').hover(
        function(){ if (!$(this).hasClass('selected')) $(this).addClass('hover');},
        function(){$(this).removeClass('hover');}
    );

}

Gazelle = function() {
    var self = $('#gazelle');
    var obj = this;
    this.start = function() {
        self.css('left', '-100%');
        self.addClass('moving');
        self.show();
        self.animate({
            left: '16%'
        }, 5000, 'swing', function(){self.removeClass('moving'); }
        );
    };
    this.end = function() {
        self.addClass('moving');
        self.animate({
            left: '200%'
        }, 6000, 'swing', function(){self.hide(); setTimeout(obj.animate, 3000)}
        );
    };

    this.animate = function() {
        obj.start();
        setTimeout(obj.end, 15000);
    };


}

Handlers = function () {
    var handlers = [];
    var glowers = [];
    var obj = this;
    this._reset = function (message) {
//alert('ok');
        handlers = [];
        glowers = [];
//        if (message) alert(message);
        $("div.handler").each(function(){
            $(this).show();
            var id = $(this).attr('id').replace('_handler', '');
            glowers.push($('#'+id));
            handlers.push($('#'+id+'_handler'));
        });
    };
    
    this.fit = function() {
        if (flash) return;
//alert('ok');
        for (var i=0; i<glowers.length; i++) {
            var offset = glowers[i].offset();
            handlers[i].css('top', offset.top+'px');
            handlers[i].css('left', offset.left+'px');
        }
    };

    this._reset();
}

display_balloon = function (name) {
    $('.balloon').hide();
    var id = '';
    switch (name) {
        case 'gazelle': id='monitor'; break;
        case 'shop': id='interactive'; break;
        case 'tower': id='videowall'; break;
        case 'stand': id='poster'; break;
        case 'building': id='photodiode'; break;
        case 'building2': id='photodiode'; break;
        case 'busstop': id='lightbox'; break;
    }
    var balloon = $('#'+id+'_balloon');
    balloon.show();//100, function(){
//    });
        balloon.find('.close').click(function(){balloon.hide();});
        balloon.find('.close').hover(
            function(){$(this).addClass('hover')},
            function(){$(this).removeClass('hover')}
        );
}


