$(function() {
    

    $('#slidesContainer').bind("contextmenu", function(e) {
        return false;
    });

    $(window).resize(function() {
//        var $gall = $('#galleryContainer, .simply-scroll-container');
        var windowWidth = $(window).width();
        var gallWidth = 860;
        var leftWidth = 156;
//        console.log(windowWidth);
        if (windowWidth < 1025) {
            return;
        }
        var newLeft = parseInt((windowWidth - gallWidth) / 2);
//        console.log(newLeft);
        $('#galleryContainer').animate({'marginLeft': newLeft + "px"});
        $('.simply-scroll-container').animate({'marginLeft': newLeft + "px"});
    });

    setTimeout("$(window).resize()", 500);

    $('.projectThumb').click(function() {
//                alert("hi");
        var $this = $(this);
        if ($this.hasClass("selected")) {
            return false;     
        }
        $('.selected').removeClass("selected");
        $this.addClass("selected");
        var project_id = $this.attr("data-id");
//        doProjectLoading();
        $.getJSON("project_json", {'id': project_id}, function(data) {
            location.hash = data.slug;
            $('#projectTitle').next().text(data.title);
            $('#projectSizeProgram').next().html(nl2br(data.size_program));
            $('#projectDesignStatement').next().html(nl2br(data.design_statement));
            if (data.extra_text == '') {
                $('#projectExtraText').hide();
                $('#projectExtraText').next().hide();
            } else {
                $('#projectExtraText').show().next().html(nl2br(data.extra_text)).show();
            }
            $('.slidesInner').empty();
            for (var i=0; i<data.images.length; i++) {
                var $a = getA(data.images[i]);
                $('.slidesInner').append($a);
            }
            activateSlides();
        }); 
    });


    var hash = location.hash.replace("#", "");
    if (hash == '') {
        $('.projectThumb').eq(0).click();
    } else {
        var $thumb = $('#thumb_' + hash);
        if ($thumb.length > 0) {
            $thumb.click();
        } else {
            $('.projectThumb').eq(0).click();
        }
    }
});


function getA(image) {
    var $a = $('<a />').attr("href", "#");
    var $img = $('<img />').attr("src", image.url).appendTo($a);
    return $a;
}

function activateSlides() {
	$('#slidesDiv').slides({
		preload: false,
		preloadImage: '/static/images/loading.gif',
		pagination: false, //SANJ NOT WORKING
		play: 5000,
		pause: 4000,
		hoverPause: true
	});
}

function nl2br(str) {
    return str.replace("\n", "<br />");
}
