var initial_texts = {};
var pages = {};
var in_progress = false;
var in_animation = false;
var interval = -1;

$(function() {
    $('div.summaries>ul>li>a').click(function() {
        clearInterval(interval);
        var id = $(this).attr('id');
        var switchTo = 0;
        $('div.summaries>ul>li').each(function(position) {
            if($('a', this).attr('id') == id) {
                switchTo = position;
            }
        });
        switchTabTo(switchTo);
        return false;
    });

    if($('div.summaries>ul>li>a').length) {
        showNext();
        interval = setInterval(showNext, 15000);
    }

    initial_texts = {
        'name': $('#name').val(),
        'company': $('#company').val(),
        'email': $('#email').val(),
        'phone': $('#phone').val()
    };

    $('#name, #company, #email, #phone').focus(onFocus);
    $('#name, #company, #email, #phone').blur(onBlur);

    bind2Menu();

    pages[$('#left-menu>li.selected a').attr('href')] = $('#page-body').html();
});

function bind2Menu() {
    if ($('#left-menu').length) {
        $('#left-menu a').click(changeContent);
    }

    if ($('.left-menu').length) {
        $('.left-menu a').click(changeContent);
    }
}

function getNextTabPosition() {
    var current_number = -1;
    var next_number = 0;
    $('div.summaries>ul>li').each(function(position) {
        if($(this).hasClass('selected')) {
            current_number = position;
        }
    });

    if(current_number < 2 && current_number != -1) {
        next_number = current_number + 1;
    }

    return next_number;
}

function showNext() {
    switchTabTo(getNextTabPosition());
}

function switchTabTo(tabPosition) {
    var next_id = $('div.summaries>ul>li:eq('+tabPosition+')>a').attr('id').split('_')[2];

    var current = $('div.summaries>ul>li[class="selected"]>a');
    var current_id = 0;
    if(current.length) {
        current_id = current.attr('id').split('_')[2];
    }

    if(next_id == current_id) return;

    if(current_id) {
        $('#summary_link_'+current_id).parent().removeClass('selected');
    }
    $('#summary_link_'+next_id).parent().addClass('selected');

    if(current_id) {
        $('div.summaries > #summary' + current_id).fadeOut("slow", function() {
            $('div.summaries > #summary' + next_id).fadeIn("slow");
        });
    } else {
        $('div.summaries > #summary' + next_id).fadeIn("slow");
    }
}

function onFocus() {
    if($(this).val() == initial_texts[this.name]) {
        $(this).val('');
    }
}

function onBlur() {
    if($(this).val().trim() == '') {
        $(this).val(initial_texts[this.name]);
    }
}

function changeContent() {
    if(in_progress) return false;
    in_progress = true;

    var prev = $('#body>.navigation li.selected');
    var next_page = this;
    var next_page_href = $(next_page).attr('href');

    if($('a', prev).attr('href') == next_page_href) {
        in_progress = false;
        return false;
    }

    if(next_page_href.charAt(0) == '#') {
        in_progress = false
        in_animation = false
        return true;
    }

    prev.removeClass('selected');
    $(next_page).parent().parent().addClass('selected');

    if(pages[next_page_href] == undefined) {
        $('html').css('cursor', 'wait');
        $.get(next_page_href, {}, function(data) {
            pages[next_page_href] = data.content;

            if(data.left_text) {
                $('#body .navigation').html(data.left_text);
                bind2Menu();
            }

            setHtmlAndShow(next_page_href);
            $('html').css('cursor', '');
        }, 'json');

        in_animation = true;
        $('#page-body').fadeOut('slow', function() {
            in_animation = false;
            setHtmlAndShow(next_page_href);
        });
    } else {
        in_animation = true;
        $('#page-body').fadeOut('slow', function() {
            in_animation = false;
            setHtmlAndShow(next_page_href);
        });
    }
    return false;
}

function setHtmlAndShow(next_page_href) {
    if(pages[next_page_href] == undefined || in_animation) {
        setTimeout("setHtmlAndShow(\""+next_page_href+"\");",500);
        return;
    }
    $('#page-body').html(pages[next_page_href]);
    $('#page-body').fadeIn('slow', function() {
        in_progress = false;
        in_animation = false;
    });
}

$(function () {
	$('.clients li:last').addClass('last');
	var current = $('.clients li:first'); 
	setInterval(loopClients, 7000);
	function loopClients() {
		current.fadeOut(1000);
		if (current.hasClass('last'))
			current = $('.clients li:first');
		else
			current = current.next();
        current.fadeIn(1000);
	};
});


