function itemFirstInHandler(carousel, li, idx, state)
{
    if (state != 'init')
        return;
        
    var cr = carousel;
    
    $(".jcarousel-next").bind("click", function() { cr.next(); });
    $(".jcarousel-prev").bind("click", function() { cr.prev(); });
}

function carousel_callback(carousel)
{
    jQuery('.jcarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('.jcarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
}

$(function() {
    /*
    var cr = $(".photoList").jcarousel({
        orientation:'vertical',
        itemVisible:2,
        itemScroll:2,
        scrollAnimation:'slow',
        wrap:true,
        noButtons:true,
        itemFirstInHandler: itemFirstInHandler
    });
    */
    
    /*
    var cr = $(".photoList").jcarousel({
        vertical:true,
        visible:2,
        scroll:2,
        animation:'slow',
        wrap:'last',
        verticalAdjust:2,
        buttonNextHTML: null,
        buttonPrevHTML: null,
        initCallback: carousel_callback
    });
    */
    
    //setup metadata
    $.meta.setType("attr", "data");
    
    //attach to click event of images
    $(".row a").not(".empty").click(function() {
        $(".row a").removeClass("dim")
        $(this).addClass("dim");
        var path = $(this).data().path;
        var aTags = $(this).data().tags.split(",");
        var tags = "";
        $.each(aTags, function( i, tag ){
            if (tags.length > 0)
            {
                tags += " - ";
            }
            tags += '<a href="SearchResults.aspx?q=' + tag + '&t=1">' + tag + '</a>';
        });
        var fileName = $(this).data().fileName;
        var position = $(this).data().pos;
        $(".currentImage").attr("src", "pics/" + path);
        $("#spnTags").html(tags).Highlight(500, '#ffc');
        $(".spnImageFileName").html(fileName).Highlight(500, '#ffc');
        $(".spnPhotoNum").html(position);
        $(".photoNum").Highlight(500, '#ffc');
        return false;
    });
});

function setupSendPortfolio()
{
    isInProgress = false;
    $('IMG.hovernew')
        .bind('mouseover', function() {
            this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_on$2");
        })
        .bind('mouseout', function() {
            this.src = this.src.replace(/^(.+)_on(\.[a-z]+)$/, "$1$2");
        })
        .each(function() {
            this.preloaded = new Image;
            this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_on$2");
        }
    );
    $("#cancelSend").click(function() {
        try {
            tb_remove();
        } catch (e) {}
        return false;
    });
    $("#btnSave").click(function() { $("#EmailFriend").submit(); return false; });
    var options = {
        method: 'POST',
        url: 'Ajax/EmailAlbum.aspx',
        dataType: 'json',
        after: function(r) {
	        var json = r;
	        if (json.result == "success")
	        {
		        processSend(json);		//send result to main page
	        }
	        else if (json.result == "error")
	        {
		        $("div#divMessage").addClass("error").css("display", "").html(json.errorText);
	        }
        },
        before: function(formArray, jqForm) {
	        return !isInProgress;
        }
    };
    //do ajax submit
    $("#EmailFriend").ajaxForm(options);
}

function processSend(json)
{
    $("div.message").removeClass("error").addClass("success").css("display", "").html(json.successText);
    tb_remove();    //close thickbox
}

