//Opens links with class EXTERNAL in new window
function External(){
    $('a.external').click(function(){
        window.open(this.href);
        return false;
    }); 
}

//FADES THE SHARE BUTTONS BENEATH THE BLOG POSTS
function animShare(){
	$('#social-icons li a').hover(function(){
		$(this).stop().fadeTo(300, 1);
	},
	function(){
		$(this).stop().fadeTo(300, 0.5)
	})	
	
}

function setMiddle(){
    var halfsc = $(window).height()/2;
    var contentHeight = $('#video-content').height()/2;
    var nav = $('#menu').outerHeight(true);
    $('#video-content').css({'position' : 'relative' , 'top' : halfsc-contentHeight-nav});
}

function imgHeight(){
     var screenHeight = $(window).height();
     $('#image').css('height', screenHeight);
     $('#content').css('height', screenHeight);
     $('#inner-content').css('height', screenHeight);
}

function checkInputs(){
	var n = 0;
	$('input').each(function(){
                inputVal = $(this).val();
		if(inputVal == ""){
			$(this).addClass('input-error');
			n++;
		}else{
            $(this).removeClass('input-error');
		}
	});
        $('textarea').each(function(){
                inputVal = $(this).val();
		if(inputVal == ""){
			$(this).addClass('input-error');
			n++;
		}else{
            $(this).removeClass('input-error');
		}
	});
        
	if (n !== 0){
		return false;
	} else {
		return true;
	}
}

function is_email(email, emailID) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var is_valid = reg.test(email);
	if (!is_valid){
        $(emailID).addClass('input-error');
		return false;
		} else {
        $(emailID).removeClass('input-error');
			return true;
		}
}

function formValidate(formID, emailID){
	var completed = checkInputs();
	var email = $(emailID).val();
	var validEmail = is_email(email, emailID);
	if( validEmail && completed ){
		$('#mesaj-returnat').stop().slideUp(500, function(){$(this).remove()});
		sendForm(formID);
	}else{
		if($('#mesaj-returnat').length < 1){
			$('ul.form').append('<li id="mesaj-returnat"><p id="mesaj-erroare">Va rugam sa completati corespunzator toate campurile necesare.</p></li>').children('#mesaj-returnat').stop().slideDown(500);
		}
	}
}

function sendForm(formID){
	sentData = $(formID).serialize();
	$.ajax({
		type: 'POST',
		url: 'inc/contact.php',
		data: sentData,
		success: function(data) {
			$('ul.form').append('<li id="mesaj-returnat">'+ data +'</li>').children('#mesaj-returnat').slideDown(500).delay(7000).slideUp(500, function(){$('#mesaj-returnat').remove();});
			$(formID)[0].reset();
			}
		});
} 

$(document).ready(function(){
    $('#social-icons li a').fadeTo(300, 0.5);
    animShare();
    External();
    setMiddle();
    imgHeight();
   
    $(window).resize(function(){
        setMiddle();
        imgHeight();
    });

    $.ajax({
        url: 'inc/video.php',
        method: 'POST',
        success: function(data) {
            var link = 'http://www.youtube.com/embed/';
            var html = $(data);
            var title = html.find('.video-overview > span.title').attr('title');
            title = title.split(' - ');
            var songTitle = title[1].split('(');
            // alert(artist);
            var video = html.find('.playlist-video-item > a').attr('href');
            video = video.split('/watch?v=');
            video = video[1].split('&');
            $('#video').html('<iframe width="441" height="276" src="'+ link + video[0] +'" frameborder="0" allowfullscreen></iframe>');
            $('#artist').text(title[0]);
            $('#song-title').text(songTitle[0]);
        }
    });
    
    $('.scroll').jScrollPane({autoReinitialise: true});
    
    $('#contact-form').submit(function(e){
        e.preventDefault();
        formValidate(this, '#email');
    })
    
});


