function validate_email(email){
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (email.length < 1) {
        alert('Please enter an e-mail address');
        return false;
    }
    if (reg.test(email) == false) {
        alert('Invalid e-mail address');
        return false;
    }
    return true
}

function confirm_signup(email){
    var str = "We will add ";
    str += email;
    str += " to our mailing list..";
    return confirm(str)
}

function confirm_signout(email){
    var str = "Proceed to remove  ";
    str += email;
    str += " from our mailing list..";
    return confirm(str)
}

function mailing_list_subscribe(){
    var email = document.mailing_list.email.value;
    if (validate_email(email) && confirm_signup(email)) {
        $.ajax({
            url: "/mailing_lists",
            type: "POST",
            data: {
                "email": email
            },
            success: function(){
                alert("thanks for joining up");
				document.mailing_list.email.value = "";
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert("sorry.. for some reason we could not add your e-mail this time round.");
            }
        });
    }
}

function mailing_list_unsubscribe(){
    var email = document.leave_mailing_list.email.value;
    if (validate_email(email) && confirm_signout(email)) {
        $.ajax({
            url: "/mailing_lists/remove",
            type: "POST",
            data: {
                "email": email
            },
            success: function(){
                alert("we have sent you an email with a link to remove you from the mailing list");
				document.leave_mailing_list.email.value = "";
				window.location = "/";
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert("sorry.. for some reason something went wrong this time round. Is the email address correct?");
            }
        });
    }
}

function play_music(title, source){
    txt = "";
    txt += '<object type="application/x-shockwave-flash" data="/swf/emff_lila_info.swf" width="200" height="55">'
    txt += '<param name="movie" value="/swf/emff_lila_info.swf"/>'
    txt += '<param name="bgcolor" value="#8C9BCF"/>'
    txt += '<param name="FlashVars" value="src=' + source + '&amp;autostart=yes"/>'
    txt += '</object>'
	txt += "<p>" + title + "</p>";
    $("#player").html(txt);
}


jQuery(document).ready(function(){
    jQuery("div.song_item a").click(function(e){
        var target = jQuery(e.target);
        var speed = "fast"
        if (target.hasClass("active")) {
            target.removeClass("active");
            target.next().toggle(speed);
        }
        else {
            $("#songs a.active").next().toggle(speed);
            $("#songs a.active").removeClass("active");
            target.addClass("active");
            target.next().toggle(speed);
        }
    });
    
});

