$(document).ready(function () {

    main_height_update();
    var currenturl = window.location.href;
    var filter = /cat-.*$/i;
    var currentcat = currenturl.match(filter);
    markers(currentcat);

    //-------------------rollover images + markers--------

    //rollover images
    $('.rolloverimages li').css("opacity", "0");
    $('.items li').hover(
	  function () {
	      $('.rolloverimages li').eq($(this).index()).stop().animate({ opacity: 1 }, 200);
	  },
	  function () {
	      $('.rolloverimages li').eq($(this).index()).stop().animate({ opacity: 0 }, 200);
	  }
	);
    //copy bgcolor from body to rolloverimages
    $('.rolloverimages').css('backgroundColor', $('body').css('backgroundColor'));
    //categories + search markers
    $('.categories li a').click(function () {
        var currentcat = $(this).attr('class');
        markers(currentcat);
    });
    //search marker	
    $('#toolbar .scolumn input').focusin(function () {
        $('.items li a, .categories li a').removeClass('marker');
        $('#toolbar .scolumn span').addClass('marker');
    });
    $('#search').focusout(function () {
        if ($(this).attr('value') == '') {
            $('#toolbar .scolumn span').removeClass('marker');
            $('.items li a').removeClass('marker');
        }
    });
    //--------------inner pages---------------------------	
    //pages
    $('.pagesnav li').not('.back').click(function () {
        $('.pagesnav li a.marker').removeClass('marker');
        $(this).find('a').addClass('marker');
        $('.pages div').css('display', 'none');
        $('.pages div').eq($(this).index() - 1).css('display', 'block');
    });
    //back button
    $('.pagesnav li.back a').click(function () {
        history.go(-1); return false;
    });
    //comments show/hide
    $('.commentslist a').toggle(function () {
        $(this).closest('li').find('.hcomment').css('display', 'block');
    }, function () {
        $(this).closest('li').find('.hcomment').css('display', 'none');
    });

    $(".comments #Message").maxlength(500);
    $(".comments form").submit(function () {
        var f = $(this);
        if (f.valid()) {
            $.post(f.attr("action"), f.serialize(), function () {
                f[0].reset();
                $("#msgSent").show();
            });
        }
        return false;
    });
});
//-------------------functions--------------------------
function markers(currentcat){	
	if(currentcat!=null){
		currentcat='.'+currentcat;
		$('#toolbar .scolumn span, .categories li a, .items li a').removeClass('marker');
		$('.items li a.'+currentcat).addClass('marker');
		$(currentcat).addClass('marker');		
	}
}
function main_height_update(){
	if(($('#bookmarks').height()-45)>430){
		$('#main').css('min-height',$('#bookmarks').height()-45);		
	}
	else
	{
		$('#main').css('min-height',430);
	}
}
//--------------------bookmarks-----------------------
var favorite = (function ($) {
    var holder, fav = "", $$ = {};

    /*init*/
    $(document).ready(function () {
        var bookid = window["bookid"];
        holder = $("<ul/>").appendTo($("#bookmarks").html(''));
        fav = getCookie('fav');
        $.each(fav.split(",,"), function (i, f) {
            var id = f.toIntFilter();
            if (id) {
                render(id);
            }
        });
        if (bookid && !$$.exists(bookid)) {
            $("#addFav").show();
        }
    });

    function render(id) {
        $(holder).prepend($('<li/>')
        .append('<a href="/book/' + id + '"><img src="/content/BookPics/' + id + '.jpg" /></a>')
        .append($('<span class="del"/>').click(function () {// remove
            $$.remove(id);
            $(this).parent("li").remove();
            if (window["bookid"] == id) {
                $("#addFav").show();
            }
            main_height_update();
        })));
    }

    function flush() {
        setCookie('fav', fav, 5);
    }
    function FIFO(str, lim) {
        var count = str.split(',,').length - 1;
        if (count > lim - 1) {
            return str.substr(str.indexOf(',', 1) + 1);
        }
        return str;
    }
    $$.set = function (id) {
        var _id = "," + id + ",";
        if (!fav.contains(_id)) {
            fav += _id;
            var count = fav.split(',,').length;
            if (count > 10) {
                $(holder).find("#bookmarks li:last").remove();
                fav = fav.substr(fav.indexOf(',', 1) + 1);
            }
            flush();
            render(id);
        }
    };
    $$.exists = function (id) {
        return fav.contains("," + id + ",");
    };
    $$.remove = function (id) {
        fav = fav.replace(',' + id + ',', '');
        flush();
    };
    return $$;
})(jQuery);
//---------------------end------------------------------
