/*** ---------- CHANGE THIS TO YOUR TWITTER USERNAME ------------- ***/
var TWITTER_USERNAME = 'cafeBrazilUK';
/*** ---------------------------------------------------------- ***/


$( document ).ready( function(){
	$('.blink')
		.focus(function(){
			if( $(this).attr('value') == $(this).attr('title') ) {
				$(this).attr({ 'value': '' });
			}
		})
		.blur(function(){
			if( $(this).attr('value') == '' ) {
				$(this).attr({ 'value': $(this).attr('title') })
			}
		});
	
	$('#navigation li').each(function(){
		var sub = $(this).find('.dd:eq(0)');
		if( sub.length > 0 ) {
			$(this).find('a:eq(0)').addClass('has-dd');
		}
	});
	
	$('ul.fixlast').each(function(){
		$(this).find('> li:last').addClass('last');
	});
	
	$('#navigation li').hover(
		function(){
			var sub = $(this).find('.dd:eq(0)');
			if(sub.length == 0 ) return;
			sub.fadeIn('fast');
			$(this).find('a:eq(0)').addClass('hover');
		
		},
		function(){
			var	sub = $(this).find('.dd:eq(0)');
			if(sub.length == 0 ) return;
			
			sub.hide();
			$(this).find('a:eq(0)').removeClass('hover');
		}
	);
	
	// Toggle footer
	var footer_height = $('#footer .cols').height();
	
	$('#footer .toggle').click(function(){
		var div = $('#footer .cols');
		
		if( div.height() == 0 ) {
			div.animate({ 'height': footer_height + 'px' })
			$(this).removeClass('toggle-show');
		}else {
			div.animate({ 'height': '0px' })
			$(this).addClass('toggle-show');
		}
		
		return false;
	});
	
	// Accoridion
	$('.accordion').accordion({
		'autoHeight' : false,
		'collapsible' : true
	});
	
	// Carousels
	$('.slider2 .slider-nav ul').jcarousel({
		scroll: 1,
		wrap: 'both',
		initCallback: _init_slider2_nav,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	$('.slider2 ul:eq(0)').jcarousel({
		auto: 5,
		scroll: 1,
		wrap: 'both',
		initCallback: _init_slider2,
		itemFirstInCallback:  _set_active_slider2,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	$('.slider1 ul:eq(0)').jcarousel({
		auto: 5,
		scroll: 1,
		wrap: 'both',
		initCallback: _init_slider1,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	// Project Detailed Slider
	$('.project-detailed-image ul:eq(0)').jcarousel({
		scroll: 1,
		wrap: 'both',
		initCallback: _init_project_slider,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	
	// Fancy Box
	$(".gallery a, .project-detailed-image a").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack'
	});
	
	
	// Login Popup
	
	$('#screen').click(function(){
		$('.login').fadeOut(function(){ $('#screen').hide(); });
	});
	
	$('.login-link').click(function(){
		
		var h = $( 'body' ).height() > $( window ).height() ? $( 'body' ).height() : $( window ).height();
		$('#screen').css({ 'height': h });
		
		$('#screen').show();
		$('.login').center();
		$('.login').fadeIn();
		return false;
	});
	
	// Twitter
	if( $('#twitts').length > 0 ) {
		$('#twitts').tweet({
			count: 2,
			username: TWITTER_USERNAME,
			loading_text: 'loading twitter...'
		});
	}

	
	// Contact Form
	$('.validate-form').submit(function(){
		var form = $(this);
		form.find('.required').parent().parent().removeClass('field-error');
		
		var field, v, id, msg, t;
		var error = false;
		form.find('.required').each(function(){
			field = $(this);
			v = $(this).val();
			t = $(this).attr('title');
			id = $(this).attr('id');
			
			if( $(this).hasClass('valid-email') ){
				if( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(v) == false ) {
					error = true;
				}
			}else {
				if( v == '' || v == t ){
					error = true;
				}
			}
			
			if( error ) {
				field.parent().parent().addClass('field-error');
				//error_fields.push( field );
			}
		});
		
		
		if( !error ) {
		
			form.find('.required').parent().parent().removeClass('field-error');
			var data = {}
			
			form.find('.text-field').each(function(){
				data[ $(this).attr('name') ] = $(this).val();
			});
			
			form.find('.text-field').each(function(){
				$(this).val( $(this).attr('title') );
			});
			
			$('#message-field').val( 'Loading...' );
			
			$.post('send.php', data, function(){
				form.find('.msg-thanks').fadeIn(function(){
					
					$('#message-field').val( $('#message-field').attr('title') );
					
					window.setTimeout(function(){
						form.find('.msg-thanks').fadeOut();
					}, 7000);
					
				});
			});
		}
		
		return false;
	});

	
});

var slider2_nav_carousel;

function _init_slider2( carousel ){
	$('.slider2 .slider-nav ul a').bind('click', function() {
		var index = $(this).parent().parent().find('a').index(this) + 1;
		carousel.scroll( index );
		return false;
	});
}

function _set_active_slider2(carousel, item, idx, state) {
	var index = idx - 1;
  	$('.slider2 .slider-nav ul a').removeClass('active');
  	$('.slider2 .slider-nav ul a').eq(index).addClass('active');
  	slider2_nav_carousel.scroll( index );
};

function _init_slider2_nav ( carousel ) {
	slider2_nav_carousel = carousel;
	
	$('.slider2 .slider-nav-direction .next').bind('click', function() {
		carousel.next();
		return false;
	});
	
	$('.slider2 .slider-nav-direction .prev').bind('click', function() {
		carousel.prev();
		return false;
	});
}

function _init_slider1( carousel ){
	$('.slider1 .slider-arrows .next').bind('click', function() {
		carousel.next();
		return false;
	});
	
	$('.slider1 .slider-arrows .prev').bind('click', function() {
		carousel.prev();
		return false;
	});
}

function _init_project_slider( carousel ){
	$('.project-thumbs ul a').bind('click', function() {
		var index = $(this).parent().parent().find('a').index(this) + 1;
		carousel.scroll( index );
		return false;
	});
	$('.project-nav .next').bind('click', function() {
		carousel.next();
		return false;
	});
	
	$('.project-nav .prev').bind('click', function() {
		carousel.prev();
		return false;
	});
}

jQuery.fn.center = function(loaded) {
    var obj = this;
    body_width = parseInt($(window).width());
    body_height = parseInt($(window).height());
    block_width = parseInt(obj.width());
    block_height = parseInt(obj.height());
    
    left_position = parseInt((body_width/2) - (block_width/2)  + $(window).scrollLeft());
    if (body_width<block_width) { left_position = 0 + $(window).scrollLeft(); };
    
    top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
    if (body_height<block_height) { top_position = 0 + $(window).scrollTop(); };
    
    if(!loaded) {
        
        obj.css({'position': 'absolute'});
        obj.css({ 'top': top_position });
        $(window).bind('resize', function() { 
            obj.center(!loaded);
        });
        $(window).bind('scroll', function() { 
            obj.center(!loaded);
        });
        
    } else {
        obj.stop();
        obj.css({'position': 'absolute'});
        obj.animate({ 'top': top_position }, 200, 'linear');
    }
}
