
var servicesSlide = function(moveTo){
	
	$('.contentTextSlide').stop().animate({
		scrollTop : moveTo
	},  350, 'easeInOutExpo', function(){
		
	});
}


$(document).ready(function(){
						   	
	$('.slider-nav ul li').click(function(){
		$(".slider-nav a").removeClass('selected');
		$(this).find("a").addClass('selected');
	});
	

}); 

function _init_carousel(carousel) {
	$('#next-slide').bind('click', function() {
        carousel.next();	
        return false;
    });

    $('#prev-slide').bind('click', function() {
        carousel.prev();
        return false;
    });
    
	$('#slider-nav li').bind('click', function() {
    	carousel.scroll(jQuery.jcarousel.intval($(this).index()+1));
    	return false;
    });

    
};

function _set_active_slide(carousel, item, idx, state){
	$('#slider-nav ul li a').removeClass('active');
	$('#slider-nav ul li a').eq(idx-1).addClass('active');	
}

function _move_text_with_slide(carousel, item, idx, state){
	servicesSlide(( idx-1 ) * 150 );
}

$(function(){
	
	$('.under-construction').center(); 
	
	// Drop-down navigation
	$('#navigation ul li').hover(function(){
		$(this).find('.dd').show();
		$(this).has('.dd').find('a').eq(0).addClass('hover');
		$(this).has('.dd').find('.dd .dd').hide();
	},
	function(){
		$(this).find('.dd').hide();
		$(this).find('a').eq(0).removeClass('hover');
	});
	
	//Home Carousel
	$('#slider-images ul').jcarousel({
		auto: 8,
		wrap: "last",
		scroll: 1,
		visible: 1,
		initCallback: _init_carousel,
		itemFirstInCallback:  _set_active_slide,
		itemVisibleInCallback: { onBeforeAnimation: _move_text_with_slide },
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
		
	// Fancy Box
	$(".gallery a.images, .services a.images, a.fancy").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'overlayColor'	: '#000'
	});
	
	// Contact Form
	$('.validate-form').submit(function(){
		var form = $(this);
		form.find('.required').parents('.row:eq(0)').removeClass('field-error');
		
		var field, v, id, msg, t, field_holder;
		var alert_msg = $('.msg-alert');
		var error = false;
		form.find('.required').each(function(){
			field = $(this);
			field_holder = field.parents('.row:eq(0)');
			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.addClass('field-error');
				alert_msg.fadeIn();
				//error_fields.push( field );
			}
		});
		
		if( !error ) {
		
			form.find('.required').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(){
				alert_msg.fadeOut();
				form.find('.msg-thanks').fadeIn(function(){
					
					$('#message-field').val( $('#message-field').attr('title') );
					
					window.setTimeout(function(){
						form.find('.msg-thanks').fadeOut();
					}, 5000);
					
				});
			});
		}
		
		return false;
	}); 
	

	//Twitts
	if( $('#twitts').length > 0 ) {
		$('#twitts').tweet({
			count: 2,
			username: TWITTER_USERNAME,
			loading_text: 'loading twitter...'
		});
	};
	
	//Blink Fields 
	$('.blink').
	    focus(function() {
	        if(this.title==this.value) {
	            this.value = '';
	        }
	    }).
	    blur(function(){
	        if(this.value=='') {
	            this.value = this.title;
	        }
	    });
	
    // 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;
	});
	
	//Project Page 
	
	$('.project-gallery a').click(function(){
		var href = $(this).attr('href');
		$('.project-image img').attr('src', href);
		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');
    }
}
