var iChoice, itWork = false, nowChoice, mx, my, i, tmp, direction, element = $('<a></a>'), speed = 210, lastContact, poemOpened = false, last_twitt;

var menuXY = new Array('0:0', '1:0', '2:0', '3:0', /*'2:1', 				'3:1',*/ '0:1', '2:-1');

var menuItems = new Array('studio', 'index', 'portfolio', 'ldemo', /*'allportfolio', 	'rdemo',*/ 'people', 'awards');

var xmlWork = new Object();

var itWork = false;
function getId(item){
    for (i = 0; i < menuItems.length; i++) {
        if (menuItems[i] == item) {
            return i;
            break;
        }
    }
    return -1;
}

function getXY(item){
    for (i = 0; i < menuItems.length; i++) {
        if (menuItems[i] == item) {
            return menuXY[i].split(':');
            break;
        }
    }
}

function loadChoice(){
    if ((iChoice != nowChoice)) {
        tmp = getXY(iChoice);
        var difX = mx - tmp[0];
        if (difX != 0) {
            var wWorkspace = parseInt($('#' + nowChoice + '_').css('width'));
            direction = 1;
            if (difX < 0) {
                direction = -1;
            }
            difX = Math.abs(difX);
            $('#' + iChoice + '_').css('left', ((-1) * direction * wWorkspace) + 'px').css('top', '0px');
            $('#' + iChoice + '_').animate({
                left: '0px'
            }, speed);
            $('#' + nowChoice + '_').animate({
                left: (wWorkspace * direction) + 'px'
            }, speed, function(){
                $('#' + nowChoice + '_').css('left', '-3000px');
                nowChoice = iChoice;
            });
        }
        var difY = my - tmp[1];
        if (difY != 0) {
            var hWorkspace = parseInt($('#' + nowChoice + '_').css('height'));
            direction = 1;
            if (difY < 0) {
                direction = -1;
            }
            difY = Math.abs(difY);
            $('#' + iChoice + '_').css('top', ((-1) * direction * hWorkspace) + 'px').css('left', '0px');
            $('#' + iChoice + '_').animate({
                top: '0px'
            }, speed);
            $('#' + nowChoice + '_').animate({
                top: (hWorkspace * direction) + 'px'
            }, speed, function(){
                $('#' + nowChoice + '_').css('top', '-3000px');
                nowChoice = iChoice;
            });
        }
        mx = tmp[0], my = tmp[1];
    }
}

function menu(){
    $('.link').click(function(){
        linkClicked(this);
    });
}

function linkClicked(el, timer){
    iChoice = 'index';
    var choice = ($(el).attr('href').replace(/^[^#]*#?/, '')) ? $(el).attr('href').replace(/^[^#]*#?/, '') : 'index';
    for (i = 0; i < menuItems.length; i++) {
        if (menuItems[i] == choice) {
            iChoice = menuItems[i];
            //var newXY = menuXY[i].split(':');
            break;
        }
    }
    if (i == menuItems.length) {
        getWork(el);
        itWork = true;
    }
    else {
        loadChoice();
        itWork = false;
    }
}

/*portfolio*/
function getWork(el){
    var id = $(el).attr('href').replace(/^[^#]*#?/, '');
    element.attr('id', $(el).attr('id'));
    var position = 'r';
    ($(el).attr('id').lastIndexOf('_link') != -1) ? position = 'r' : position = 'l';
    $.ajax({
        url: "works/all.xml",
        cahce: true,
        dataType: "xml",
        success: function(xml){
            loadWork(xml, id, position);
        }
    });
}

function loadWork(xml, id, position){
    // if (!$(xmlWork).find('root'))
    xmlWork = xml;
    
    var work = $(xmlWork).find('root work[id=' + id + ']');
    var div = position + 'demo_';
    $('#' + div + ' h1').text(work.attr('name'));
    $('#' + div + ' .href').text('');
    if (work.attr('link')) 
        $('#' + div + ' .href').attr('href', work.attr('link')).text(work.attr('link'));
    $('#' + div + ' .text').html($(work).find('text').text());
    if ($(work).find('service').text()) {
        $('#' + div + ' .service').html($(work).find('service').text());
    }
    else {
        $('#' + div + ' .service').html('');
    }
    if ($(work).find('cost').text()) {
        $('#' + div + ' .cost').html($(work).find('cost').text());
    }
    else {
        $('#' + div + ' .cost').html('');
    }
    $('#' + div + ' .numbers').html('');
    $('#' + div + ' .image img').css({
        'display': 'none'
    });
    $('#' + div + ' .image .loading').css({
        'display': 'block'
    });
    if (work.attr('img') > 1) {
        $('#' + div + ' .image img').attr('src', '/image.php?id=' + work.attr('id') + '_0&height=' + parseInt($(window).height() - 60) + '&width=' + parseInt($('#ldemo_').width() * 0.6) + '').load(function(){
            $('#' + div + ' .image .loading').css({
                'display': 'none'
            });
            $(this).css({
                'display': 'block'
            });
            
        });
        paintNumbers(work.attr('img'), div);
    }
    else {
        $('#' + div + ' .image img').attr('src', '/image.php?id=' + work.attr('id') + '_0&height=' + parseInt($(window).height() - 60) + '&width=' + parseInt($('#ldemo_').width()*0.6)+ '').load(function(){
            $('#' + div + ' .image .loading').css({
                'display': 'none'
            });
            $(this).css({
                'display': 'block'
            });
        });
    }
    iChoice = div.replace('_', '');
    loadChoice();
}

function paintNumbers(count, div){
    var href = div.split('_');
    for (i = 0; i < count; i++) {
        $('#' + div + ' .numbers').append('<a href="#' + href[0] + '" class="number" id="' + div + '#' + i + '">' + (i + 1) + '</a>');
    }
    $('#' + div + ' .number:first').attr('class', 'number active');
    //$('#' + div + ' .number:first').click();
    numbers();
}

function numbers(){
    $('.number').click(function(){
        var tmp = $(this).attr('id').split('#');
        $('#' + tmp[0] + ' .image .loading').css({
            'display': 'block'
        });
        $('#' + tmp[0] + ' .image img').css({
            'display': 'none'
        });
        var attr = $('#' + tmp[0] + ' .image img').attr('src').split('_');
        $('#' + tmp[0] + ' .image img').attr('src', attr[0] + '_' + tmp[1] + '&height=' + parseInt($(window).height() - 50)+ '&width=' + parseInt($('#ldemo_').width()*0.6) + '').load(function(){
            $('#' + tmp[0] + ' .image .loading').css({
                'display': 'none'
            });
            $('#' + tmp[0] + ' .image img').css({
                'display': 'block'
            });
        });
        $('.number').attr('class', 'number');
        $(this).attr('class', 'number active');
    });
}

/*resize pers*/

function pers(){
    $('.bnet-team li').find('img').each(function(){
        $(this).attr('src', $(this).attr('src') + '&size=' + parseInt($(window).height() / 3.2) + '');
    });
}

/*Вперед назад запилить*/
function refresh(){
    if (element.attr('href') != document.location.href) 
        linkClicked(element.attr('href', document.location.href), true);
}

function buttom(){
    var url = (!itWork) ? document.location.href.replace(/^[^#]*#?/, '') : iChoise;
    if (nowChoice) {
        $('#' + nowChoice + '_').css('left', '-3000px');
        $('#' + nowChoice + '_').css('top', '-3000px');
    }
    if ((getId(url) != -1) || (itWork != false)) {
        nowChoice = url;
        tmp = getXY(url);
        mx = tmp[0], my = tmp[1];
    }
    else {
        url = 'index';
        nowChoice = 'index';
        mx = 1, my = 0;
    }
    $('#' + url + '_').css('left', '0px');
    $('#' + url + '_').css('top', '0px');
}

function contact(){
    $().click(function(e){
        if (e.button == 2) {
            return false;
        }
        $('.contact input[name=' + lastContact + ']').css({
            'display': 'none'
        })
        $('.contact #' + lastContact + '').css({
            'display': 'block'
        });
    });
    $('.contact input').click(function(){
        return false;
    })
    
    $('.contact img').click(function(){
        if (lastContact) {
            $('.contact input[name=' + lastContact + ']').css({
                'display': 'none'
            })
            $('.contact #' + lastContact + '').css({
                'display': 'block'
            });
        }
        $(this).css({
            'display': 'none'
        });
        lastContact = $(this).attr('id');
        $('.contact input[name=' + lastContact + ']').css({
            'display': 'block'
        }).focus().attr('autocomplete', 'off');
        return false;
    });
}

function poem(){
    $('.poemLink').click(function(){
        if (!poemOpened) {
            poemOpened = true;
            $('.poemLink img').attr('src', 'images/linkPoemHover.bfolio.png');
            $('.poem').css('display', 'block');
            $('.aboutText').css('display', 'none');
        }
        else {
            poemOpened = false;
            $('.poemLink img').attr('src', 'images/linkPoem.bfolio.png');
            $('.poem').css('display', 'none');
            $('.aboutText').css('display', 'block');
        }
        return false;
    });
    $().click(function(){
        poemOpened = false;
        $('.poemLink img').attr('src', 'images/linkPoem.bfolio.png');
        $('.poem').css('display', 'none');
        $('.aboutText').css('display', 'block');
    });
}

function info(){
    $('#team-info').css({
        'display': 'none'
    });
}

function portPage(){
    var block = $('#allportfolio_');
    var page = 1;
    var count = $(block).find('ul').length;
    var nextpage;
    var prevpage;
    $('.page_number').click(function(){
        var tmp = $(this).attr('id').split('_');
        page = parseInt(tmp[1]);
        (page == count) ? nextpage = 0 : nextpage = parseInt(page + 1);
        (page == 1) ? prevpage = 0 : prevpage = parseInt(page - 1);
        
        (nextpage) ? $(block).find('#arrow_down').find('a').attr('id', 'page_' + nextpage + '').css('display', 'block') : $(block).find('#arrow_down').find('a').css('display', 'none');
        (prevpage) ? $(block).find('#arrow_up').find('a').attr('id', 'page_' + prevpage + '').css('display', 'block') : $(block).find('#arrow_up').find('a').css('display', 'none');
        
        $(block).find('ul.active').toggleClass('active');
        $(block).find('#list_' + page).toggleClass('active');
        $(block).find('.numbers').find('.page_number.active').toggleClass('active');
        $(block).find('.numbers').find('#page_' + page).toggleClass('active');
    });
}

function twitter(){
    $('#twitter').mouseover(function(){
        $('#last_twitt').css({
            'display': 'block'
        });
        if (last_twitt) {
            $('#last_twitt').html('<p>' + last_twitt + '</p>');
        }
        else {
            $('#last_twitt').html('<p>Загружаю последний твит...</p>');
            $.getJSON('http://search.twitter.com/search.json?q=from%3Abnetteam&rpp=1&callback=?', function(data){
                last_twitt = data.results[0].text;
                last_twitt = last_twitt.replace(/^(#[A-Za-z0-9]+\s)+/, '');
                $('#last_twitt').html('<p>' + last_twitt + '</p>');
            });
        }
    });
    $('#twitter').mouseout(function(){
        $('#last_twitt').css({
            'display': 'none'
        });
    });
}

$(function(){
    info();
    menu();
    pers();
    buttom();
    twitter();
    var navigator = window.setInterval("refresh()", 10);
    poem();
    contact();
    portPage();
    $('.awardsList a').attr('target', '_blank');
 /*   $('.worksLayout').css('top', ((parseInt($('.worksList').css('height')) - parseInt($('.worksLayout').css('height'))) / 2) + 'px');
    $(window).resize(function(){
        $('.worksLayout').css('top', ((parseInt($('.worksList').css('height')) - parseInt($('.worksLayout').css('height'))) / 2) + 'px');
    });*/
});
