$(function() {
	
	readyFunctions();
	
	$('#header .contact').live('click', function() {
		$('#bottom-contact-form').hide();
		$('#top-contact-form').toggle();
		return false;
	});
	
	$('#footer .contact').live('click', function() {
		$('#top-contact-form').hide();
		$('#bottom-contact-form').toggle();
		return false;
	});
	
	$('.contact-form .closelabel').live('click', function() {
		$('#top-contact-form').hide();
		$('#bottom-contact-form').hide();
    });
	
	$('#testimonials .paging a').live('click', function() {
	    $('#testimonials .paging a').removeClass('active');
	    $(this).addClass('active');
		$('#testimonials .testimonial').hide();
		$($(this).attr('href')).show();
		return false;
	});
	
	$('.speakers-list .buttons a').live('click', function() {
        var slider = $(this).parents('.speakers-list:eq(0)');
        var fragments = slider.find('.content li:visible');
        var container = slider.find('.content ul');
        
        var currentPage = slider.data('position');
        if (!currentPage) {
            currentPage = 1;
        }
        var fragments_count = fragments.length;
        var fragmet_width = 123;
        var perPage = 7;
        var numPages = Math.ceil(fragments_count/perPage);
        var stepMove = fragmet_width*perPage;
        var firstPosition = 0;
        var lastPosition = -1*((numPages-1)*stepMove);
        
        if ($(this).hasClass('next')) {
            currentPage ++;
            if (currentPage > numPages) {
                slider.data('position', 1);
                container.animate({'left': firstPosition});
                return false;
            };
            container.animate({'left': -((currentPage - 1)*stepMove)});
        };
        if ($(this).hasClass('prev')) {
            currentPage --;
            if (currentPage < 1) {
                slider.data('position', numPages);
                container.animate({'left': lastPosition});
                return false;
            };
            container.animate({'left': -((currentPage-1)*stepMove)});
        };
        slider.data('position', currentPage);
        
        return false;
    });
    
    feePopup();
    
    $('a[rel*=facebox]').facebox();
    
    $('#page a[href=/contact/]').bind('click', function(){return open_contact();})
    
    prep_speaker_detail_tabs();
});

function prep_speaker_detail_tabs() {
    var one_shown = false;
    $('#detailed .side-a .content').hide().each(function(){
        if (!one_shown) {
            $(this).show();
            one_shown = true;
        };
    });
    
    $('#detailed .side-a .tabs a').click(function(e){
        e.preventDefault();
        $('#detailed .side-a .tabs a').removeClass('active');
        $(this).addClass('active');
        $('#detailed .side-a .content').hide();
        $($(this).attr('href')).show();
    });
};

function readyFunctions() {
	$('.field').focus(function() {
		$(this).addClass('field-focus');
        if(this.title==this.value) {
            this.value = '';
        }
    }).blur(function(){
        if(this.value=='') {
            this.value = this.title;
        }
        if (this.value == this.title) {
        	$(this).removeClass('field-focus');
        } else {
        	$(this).addClass('field-focus');
        };
    });
    
    $('form input.button').hover(function() {
    	$(this).addClass('button-hover');
    }, function() {
    	$(this).removeClass('button-hover');
    });
}

this.feePopup = function(){	
	xOffset = 92;
	yOffset = 10;
	$(".fee-icons").hover(function(e){
		$("#fee-popup").css({
			"top"	: (e.pageY - xOffset) + "px",
			"left"	: (e.pageX + yOffset) + "px"
		}).fadeIn("fast")
    }, function(){	
		$("#fee-popup").hide();
    });
	$(".fee-icons").mousemove(function(e){
		$("#fee-popup").css({
			"top"	: (e.pageY - xOffset) + "px",
			"left"	: (e.pageX + yOffset) + "px"
		})
	});
};

updateSpeakersList = function() {
    var program = $('#program-select').val();
    var fee = $('#fee-select').val();
    var locale = $('#locale-select').val();
    var speakers = $('.speakers-list li');
        
    speakers.show();
    
    speakers.each(function(){
        var speaker = $(this);
        if (program != "false" && !speaker.hasClass('program-'+program)) {
            speaker.hide();
        };
        if (fee != "false" && !speaker.hasClass('fee-'+fee)) {
            speaker.hide();
        };
        if (locale != "false" && !speaker.hasClass('locale-'+locale)) {
            speaker.hide();
        };
    });
    $('.speakers-list .content ul').animate({'left': 0});
};

function open_contact() {
    $('#footer .contact a').click();
    return false;
};

