/* Author: 
  Pasz.nl. Magic by jQuery.
*/

// Global
window.ie6 = $('html').hasClass('ie6');
window.ie7 = $('html').hasClass('ie7');
window.ie8 = $('html').hasClass('ie8');
language = $('html').attr('lang');

//  Smooth scrolling anchor links
var smoothScroll = function() {
    $('a[href*=#]').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
    		var $target = $(this.hash),
    		    theHash = $target.selector;

    		$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');

    		if ($target.length) {
    			var targetOffset = $target.offset().top;
    			$('html,body').animate({scrollTop: targetOffset}, 500, function() {
    			    location.hash = theHash;
    			});
    			return false;
    		}
    	}
    })
}()

var howIWork = function() {
    // How I work roll effect
    var $btn = $('.how-i-work #roll .btn.more'),
        $content = $('.how-i-work .content'),
        defHeight = $('.how-i-work .content').height(),
        $inner = $(''),
        cHeight,
        speed = 500,
        moreTxt = language == 'en' ? 'Read more' : 'Lees meer',
        lessTxt = language == 'en' ? 'Read less' : 'Lees minder';

    // Measure height of body of text
    // $content.wrapInner('<div class="height-wrapper"/>');
    // $inner = $('#werkwijze .height-wrapper');
    // cHeight = $inner.height() + 80;
    
    cHeight = 1010;
    
    // Event
    $btn.click(function() {
        if ($(this).hasClass('less')) {
            $content.stop().animate({
                height : defHeight +'px'
            }, speed);
        
            $btn.toggleClass('less').html('<span>' + moreTxt + '</span>')
        } else {
            $content.stop().animate({
                height : cHeight +'px'
            }, speed);
        
            $btn.addClass('less').html('<span>' + lessTxt + '</span>')
        }

    })
    
    $('#intro nav a[href=#werkwijze], #intro nav a[href=#how-i-work]').click(function() {
        if (!$btn.hasClass('less')) $btn.trigger('click')
    })
}();

var btnToTop = function() {
    var $btn = $('#back-to-top'),
        speed = 250;
    
    $(window).scroll(function() {
        // Show button
        if ($(window).scrollTop() > 100 && !$btn.is(':visible') && !window.ie6) {
            
            $btn.stop().css({
                display : 'block',
                height  : 0
            }).animate({
                height  : '45px'
            }, speed)
            
        }
        
        // Hide button
        if ($(window).scrollTop() <= 100 && $btn.is(':visible')) {
            $btn.stop().animate({
                height  : '0'
            }, speed, function() {
                // Done animating, hide
                $(this).hide()
            })
        }
    })
}()

$("#portfolio a.frame").fancybox({
	'transitionIn'      : 'fade',
	'transitionOut'     : 'fade',
	'speedIn'           : 150, 
	'speedOut'          : 150, 
	'overlayShow'       : true,
	'autoScale'         : false,
	'overlayColor'      : '#000',
	'overlayOpacity'    : 0.8,
	'titlePosition'     : 'inside',
	'scrolling'         : 'no',
	'cyclic'            : true,
	'padding'           : 0,
	'margin'            : 20
})

$("#testimonials blockquote").each(function() {
    var value = 'rotate(' + Math.floor(Math.random()*3) + 'deg)';
    $(this).css({
        '-webkit-transform' : value,
        '-moz-transform'    : value,
        '-o-transform'      : value,
        '-ms-transform'     : value,
        'transform'         : value
    });
})


var contactForm = function() {
    $("#contact #submit").click(function(){
        var $fb = $("#contact .feedback"),
            emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/,
            message = $('#message').val(),
            emailFromVal = $("#email").val(),
            datastr ='&mail=' + emailFromVal + '&text=' + message + '&language=' + language, 
            emailTxt = language == 'en' ? '<strong>Error!</strong> Email invalid?' : '<strong>Fout!</strong> Klopt e-mail adres?',
            emptyTxt = language == 'en' ? '<strong>Error!</strong> Message empty!' : '<strong>Fout!</strong> Bericht is leeg!';
        
        $fb.text('').hide();
        
        if (!emailReg.test(emailFromVal) || emailFromVal == '' || emailFromVal == $('#email').attr('placeholder')) {
            $fb.html(emailTxt).fadeIn(250);
        } else if (message == '' || message == $('#message').attr('placeholder')) {
             $fb.html(emptyTxt).fadeIn(250);
        } else {
            // Send mail
            $.ajax({
                type: 'POST',
                url: 'mail.php',
                data: datastr,
                cache: false,
                success : function(html){
                    $fb.text(html).fadeIn(250);
                    
                    // Reset fields
                    $('#email, #message').val('')
                    
                    jQuery("input[placeholder], textarea[placeholder]").enablePlaceholder();
                },
                error : function(html){
                    $fb.text(html).fadeIn(250);
                }
            })
        }
        return false
        
    });
}()

/*  Placeholder attribute functionality in older browsers   */
jQuery("input[placeholder], textarea[placeholder]").enablePlaceholder();
