$(window).load(function() {
    
});

$(document).ready(function(){
	$('.artistSlider').nivoSlider({
        effect:'sliceDown', // Specify sets like: 'fold,fade,sliceDown'
        slices:15, // For slice animations
        boxCols: 8, // For box animations
        boxRows: 4, // For box animations
        animSpeed:500, // Slide transition speed
        pauseTime:4000, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:false, // Next & Prev navigation
        directionNavHide:true, // Only show on hover
        controlNav:false, // 1,2,3... navigation
        controlNavThumbs:false, // Use thumbnails for Control Nav
        pauseOnHover:true, // Stop animation while hovering
    });
	
	
    resizeImg();
    
    $(window).resize(function(){
        resizeImg();
    });
    
    //Apply fixed layout
    applyEnabledLayout();
    
    $('.item').each(function(index){
        var artistName = $(this).find('h1').text();
		var strippedName = artistName.replace(/\s+/g, "");
		strippedName = strippedName.replace(/[^a-zA-Z 0-9]+/g,'');
        var postID = strippedName + "_Post";
        var buttonID = strippedName + "";
        
        $(this).attr("id", postID);
        
        $('.artistbuttons').append("<li id='" + buttonID + "' class='button artistbutton mouseover'><div><h1>" + artistName + "</h1></div><div class='onRollOver'><h1>" + artistName + "</h1></div></li>");
    });
    
    $('.button').each(function(index){
    
        var target = $(this).find(".onRollOver");
        $(this).hover(function(){
            target.stop().animate({
                width: '100%'
            });
        }, function(){
            target.animate({
                width: '0%'
            });
        });
        
    });
    
    
    
    //Rollover artist buttons
    $('.artistbutton').each(function(index){
        $(this).click(function(){
            var target = "#" + $(this).attr('id') + "_Post";
            scrollTo(target);
        });
    });
    
    //Newsletter button
    $('.newsletter').click(function(){
        $("#creditsPop").slideUp("slow", function(){
            $("#newsletterPop").slideToggle("slow");
        });
    });
    
    //Newsletter send button
    $('#newsletterButton').click(function(){
        var emailAddress = $('#emailInput').val();
        $.post("http://www.jospop.com/wp-content/themes/JospopEleven/sendNewsletterMail.php", {
            email: emailAddress,
        }, function(data){
            //alert("Data Loaded: " + data.email);
            $("#newsletterPop").slideUp("slow");
        });
    });
    
    
    $('input[type=text]').focus(function(){
        if ($(this).val() == $(this).attr('defaultValue')) {
            $(this).val('');
        }
    });
    
    $('input[type=text]').blur(function(){
        if ($(this).val() == '') {
            $(this).val($(this).attr('defaultValue'));
        }
    });
    
    //Credits button
    $('.credits').click(function(){
        $("#newsletterPop").slideUp("slow", function(){
            $("#creditsPop").slideToggle("slow");
        });
    });
    
    //Credits button
    $('#icelineCredit').hover(function(){
        $('#icelineCredit .creditButton').animate({
            width: '181px'
        });
    }, function(){
        $('#icelineCredit .creditButton').animate({
            width: '0px'
        });
    });
    
	 $("#icelineCredit").click(function(){
	 	window.open("http://www.iceline.be");
	 });
	 
	 $('#hostingCredit').hover(function(){
        $('#hostingCredit .creditButton').animate({
            width: '181px'
        });
    }, function(){
        $('#hostingCredit .creditButton').animate({
            width: '0px'
        });
    });
    
	 $("#hostingCredit").click(function(){
	 	window.open("http://www.logicinmotion.be");
	 });
});

function resizeImg(){
    var bgImg = $("#background");
    var imgwidth = bgImg.width();
    var imgheight = bgImg.height();
    
    var winwidth = $(window).width();
    var winheight = $(window).height();
    
    var widthratio = winwidth / imgwidth;
    var heightratio = winheight / imgheight;
    
    var widthdiff = heightratio * imgwidth;
    var heightdiff = widthratio * imgheight;
    
    if (heightdiff > winheight) {
        bgImg.css({
            width: winwidth + 'px',
            height: heightdiff + 'px'
        });
    }
    else {
        bgImg.css({
            width: widthdiff + 'px',
            height: winheight + 'px'
        });
    }
}

function applyEnabledLayout(){
    $('.rightbar').addClass('rightbarFixed');
}

function scrollTo(id_to_scroll){
    //alert(id_to_scroll);
    targetOffset = $(id_to_scroll).offset().top - 163;
    $("html,body").animate({
        scrollTop: targetOffset
    }, 500);
}

