var $applications;
var $data;
var $filteredData;
var working=false;

$(document).ready(function(){

	// reset
	$.scrollTo.defaults.axis = 'xy';
	$.scrollTo(0);

	// quicksand
	$applications = $('.projects');
	$data = $applications.clone();

	// fancy
	fancy();

	// view project
	viewPrj();

});

function fancy() {

	$("a.view").fancybox({
		padding: 0,
		frameWidth: 608,
		frameHeight: 500,
		hideOnContentClick: false,
		hideOnOverlayClick: false,
		centerOnScroll: false,
		hideTitle: true,
		overlayColor: '#000',
		overlayOpacity: 0.8
	});

}

function viewPrj() {

	$('.hasprj').each(function () {
        var distance = -10;
        var time = 250;
        var hideDelay = 100;
        var hideDelayTimer = null;
        var beingShown = false;
        var shown = false;
        var trigger = $('.picbg', this);
        var popup = $('.pinkbg', this).css('opacity', 0);
        $([trigger.get(0), popup.get(0)]).mouseover(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                return;
            } else {
                beingShown = true;
                popup.css({
                    display: 'block'
                }).animate({
                    top: '-=' + distance + 'px',
                    opacity: 1
                }, time, 'swing', function () {
                    beingShown = false;
                    shown = true;
                });
            }
        }).mouseout(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                popup.animate({
                    top: '+=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    popup.css({
                        'display': 'none',
                        'top': 106
                    });
                });
            }, hideDelay);
        });
    });

}

function qcksand(id) {

	$.scrollTo(0,800);

	if(!working) {
		working=true;

		$('#menu li').removeClass('sel');
		$('#'+id).addClass('sel');

		if($('ul.projects').is(':hidden')) {
			$('#cv').fadeOut('slow',function(){
				$('ul.projects').fadeIn('slow',function(){
					working=false;
					qcksand(id);
				});
			});
		} else {

			if (id == 'all') {
				$filteredData = $data.find('li');
			} else {
				$filteredData = $data.find('li[data-type*='+id+']');
			}

			$applications.quicksand($filteredData, {
				duration: 500,
				easing: 'easeInOutQuad'
			},function(){
				working=false;
				fancy();
				viewPrj();
			});

		}

	}

};

function cv() {

	$('#menu li').removeClass('sel');
	$('#cv_a').addClass('sel');

	$('ul.projects').fadeOut('slow',function(){
		$('#cv').fadeIn('slow');
		working=false;
	});

}

function contact() {

	var error = false;
	var nume = $('#c_nume').val();
	var mail = $('#c_mail').val();
	var msg  = $('#c_msg').val();
	var captcha = $('#c_captcha').val();

	if(nume.length==0) { error=true; $('#c_nume_txt').html('name <span>please write your name</span>'); }
	else $('#c_nume_txt').text('name');

	if(mail.length==0) { error=true; $('#c_mail_txt').html('email <span>please write your email</span>'); }
	else $('#c_mail_txt').text('email');

	if(msg.length==0) { error=true; $('#c_msg_txt').html('how can i help you <span>please write your message</span>'); }
	else $('#c_msg_txt').text('how can i help you');

	if(captcha.length==0) { error=true; $('#c_captcha_txt').html('human or robot? <span>are you a robot ?</span>'); }
	else $('#c_captcha_txt').text('human or robot?');

	if(!error) {
		$('#contact #form').fadeOut('slow',function(){
			$('#contact #load').fadeIn('slow',function(){

				$.post('index.php',{
					nume: nume,
					mail: mail,
					msg: msg,
					captcha: captcha
				},function(data){

					if(data==1) {
						$('#contact #load').fadeOut('fast',function(){
							$('#contact #load').html('<b>Message send. Thanks!</b>');
							$('#contact #load').fadeIn('fast',function(){
								var timer = setTimeout(function(){
									$('#contact #load').fadeOut('fast',function(){
										$('#c_nume').val('');
										$('#c_mail').val('');
										$('#c_msg').val('');
										$('#c_captcha').val('');

										$('#contact #load').html('<img src="images/ajax-loader.gif" />');
										$('#contact #form').fadeIn('fast');
									});
								},3000);
							});
						});
					} else {
						$('#contact #load').fadeOut('fast',function(){
							$('#contact #form').fadeIn('fast');
						});
					}

				});

			});
		});
	}

}
