function hl() {
    this.hotlistCount = 0;
    this.car = {};
}

hl.prototype.itemLoadCallback = function(carousel, state) {
    if (carousel.prevFirst != null) {
        // Remove the last visible items to keep the list small
        for (var i = carousel.prevFirst; i <= carousel.prevLast; i++) {
            // jCarousel takes care not to remove visible items
            //carousel.remove(i);
        }
    }

    var per_page = carousel.last - carousel.first + 1;
    var currPage = 0;
    var f,l;
    var cr = carousel;


    for (var i = carousel.first; i <= carousel.last; i++) {
        var page = Math.ceil(i / per_page);

        if (currPage != page) {
            currPage = page;

            f = ((page - 1) * per_page) + 1;
            l = f + per_page - 1;

            f = f < carousel.first ? carousel.first : f;
            l = l > carousel.last ? carousel.last : l;

            if (carousel.has(f, l)) {
                continue;
            }

            this.makeRequest(carousel, f, l, per_page, page);
        }
    }
};

hl.prototype.makeRequest = function(carousel, first, last, per_page, page)
{
    // Lock carousel until request has been made
    carousel.lock();
    var that = this;
	jQuery.ajax({
		type: "GET",
		    url: "AjaxRequest.aspx?wci=hotlist",
		data: {"page": page, "per_page": per_page},
		dataType : "json",
		success : function(data){
		    that.itemAddCallback(carousel, first, last, data, page);
		}
	});
};

hl.prototype.itemAddCallback = function(carousel, first, last, data, page)
{
    // Unlock
    carousel.unlock();

    // Set size
    carousel.size(hotlist.hotlistCount);

    var photos = data;
    var per_page = carousel.last - carousel.first + 1;

    for (var i = first; i <= last; i++) {
        var pos = i - 1;
        var idx = Math.round(((pos / per_page) - Math.floor(pos / per_page)) * per_page);
	if (photos[idx]){
        	carousel.add(i, this.getItemHTML(photos[idx], i));
        }else{
        	carousel.add(i, "");
        }
    }
    jQuery(".hotliststar").unblock();

    this.car=carousel;
};

/**
 * Global item html creation helper.
 */
hl.prototype.getItemHTML = function(photo, index)
{
	var gallery_item = '<div class="hotlist_gallery"> <a title="'+ photo.title + '" href="member.aspx?wci=viewprofile&member_key='+photo.memberKey+'"> <img height="100" width="80" src="' + photo.url + '" alt="" /> </a> <div class="removefromhotlist"><a title="Remove from Hotlist" href="#" onClick="hotlist.removeFromHotlist(' + photo.memberKey + ', '+ index +'); return false;">x</a></div> </div>'

    return gallery_item;
};

hl.prototype.removeFromHotlist = function(memberKey, index) {
    var self = this;
    jQuery("div.hotlistbar").block({ message: null, fadeIn: 0, fadeOut: 0, overlayCSS:
			{
			    backgroundColor: '#000',
			    opacity: '0'
			}
    });
    jQuery.ajax({
        type: "GET",
        url: "AjaxRequest.aspx?wci=hotlistremove",
        data: { "fav_member_key": memberKey },
        success: function(data) {
            self.decrementHotlistCount()
            self.car.removeVisibleItem(index);
            jQuery("a[href='member.aspx?wci=viewprofile&member_key=" + memberKey + "']")
					.parent()
					.find(".hotliststar")
					.attr("title", "Add to my hotlist")
					.hide()
					.empty()
					.append("<a class='hotliststarlink' onclick='return hotlist.addToHotlist_search(this, " + memberKey + ")' href='#'/>");
            jQuery("a[href='member.aspx?wci=memberimages&member_key=" + memberKey + "']")
					.parent()
					.find(".hotliststar")
					.hide();
            self.setAddToHotlistProfileLink(memberKey);
            jQuery("div.hotlistbar").unblock();
        }
    });
}

jQuery.jcarousel.fn.extend({
	removeVisibleItem: function(i){
		var e = this.get(i);

		var d = this.dimension(e);

		if (i < this.first)
			this.list.css(this.lt, jQuery.jcarousel.intval(this.list.css(this.lt)) + d + 'px');

		e.remove();
		
        this.list.css(this.wh, jQuery.jcarousel.intval(this.list.css(this.wh)) - d + 'px');
        
        this.reload();
	}
});

hl.prototype.showHotlist = function() {
    jQuery(".showlink").hide();
    jQuery(".hotlistbar").show();
    var that = this;
    if (this.car.reset) {

        this.car.options.size = this.hotlistCount;
        this.car.reset();
    }
    jQuery(".hideViewEdit").show();
    jQuery(".hidelink").show();
    jQuery(".hotlistbar").animate({ height: "154px" }, 400,function() {

        if (!hotlist.car.reset) {
            jQuery('#hotlist_scroll').jcarousel({
                itemLoadCallback: that.itemLoadCallback.bind(that),
                scroll: 9,
                size: that.hotlistCount
            });
        }
    });
};

hl.prototype.hideHotlist = function () {
    jQuery(".hotlistbar").show();
    jQuery(".showlink").show();
    jQuery(".hideViewEdit").fadeOut();
    jQuery(".hidelink").hide();
    jQuery(".hotlistbar").animate({ height: "34px" }, 400);
}

hl.prototype.setAddToHotlistProfileLink = function(memberKey) {
    var self = this;
    jQuery("li.profile_contact_hotlist_remove")
		.removeClass("profile_contact_hotlist_remove")
		.addClass("profile_contact_hotlist_add")
		.find("a")
		//.removeAttr("href")
		.unbind("click")
		.removeAttr("onClick")
		.removeAttr("onclick")
		.click(function(event) {
		    event.preventDefault();
		    self.addToHotlist_profile(memberKey);
		})
		.attr("title", "Add to Hotlist");
};

hl.prototype.setAddToHotlistLinkById = function (memberKey) {
    var self = this;
    self.removeFromHotlist_profile(memberKey); 
    jQuery('#addBuddy' + memberKey).show();
    jQuery('#delBuddy' + memberKey).hide();
};

hl.prototype.setRemoveHotlistLinkById = function (memberKey) {
    var self = this;
    self.addToHotlist_profile(memberKey);  
    jQuery('#addBuddy' + memberKey).hide();
    jQuery('#delBuddy' + memberKey).show();
}

hl.prototype.setRemoveHotlistProfileLink = function(memberKey) {
    var self = this;
	jQuery("li.profile_contact_hotlist_add")
		.removeClass("profile_contact_hotlist_add")
		.addClass("profile_contact_hotlist_remove")
		.find("a")
		//.removeAttr("href")
		.unbind("click")
		.removeAttr("onClick")
		.removeAttr("onclick")
		.click(function(event) {
	        event.preventDefault()
	        self.removeFromHotlist_profile(memberKey);
		})
		.attr("title", "Remove from Hotlist");
};

jQuery(document).ready(function() {
    hotlist = new hl();
    jQuery.ajax({
        type: "GET",
        url: "AjaxRequest.aspx?wci=hotlistcount",
        dataType: "json",
        success: function(data) {
            hotlist.hotlistCount = data.hotlistCount;
            //alert(data.hotlistCount);
            if (hotlist.hotlistCount > 0) {
                jQuery("span#hotlistCount").html("(" + hotlist.hotlistCount + ")");
                jQuery(".hotlistbar").show();

            }
            jQuery(".showlink").click(function(event) {
                event.preventDefault();
                hotlist.showHotlist();
            });
            jQuery(".hidelink").click(function(event) {
                event.preventDefault();
                hotlist.hideHotlist();
            });
        }
    });
});

hl.prototype.addToHotlist_profile = function (memberKey) {
    var self = this;
    jQuery.ajax({
        type: "POST",
        data: { "fav_member_key": memberKey },
        url: "AjaxRequest.aspx?wci=HotlistAdd",
        success: function () {
            self.incrementHotlistCount();
            self.setRemoveHotlistProfileLink(memberKey);
            self.showHotlist();
            jQuery(".hotliststar")
				.show();
            jQuery(".addHotlistBox")
                .hide();
        }
    });
    return false;
};

hl.prototype.removeFromHotlist_profile = function(memberKey) {
    var self = this;
    jQuery.ajax({
        type: "POST",
        data: { "fav_member_key": memberKey },
        url: "AjaxRequest.aspx?wci=HotlistRemove",
        success: function() {
            self.decrementHotlistCount();
            if (self.car.reset) {
                self.car.reset();
            }
            self.setAddToHotlistProfileLink(memberKey)
            jQuery(".hotliststar")
				.hide();
            jQuery(".addHotlistBox")
                .show();
        }
    });
    return false;
};

hl.prototype.addToHotlist_messenger = function(memberKey, pane) {
    var self = this;
    jQuery.ajax({
        type: "POST",
        data: { "fav_member_key": memberKey },
        url: "AjaxRequest.aspx?wci=HotlistAdd",
        success: function() {
            self.incrementHotlistCount();
            if (self.car.reset) {
                self.showHotlist();
            }
            jQuery("#addBuddy" + pane).hide();
            jQuery("#removeBuddy" + pane).show();
        }
    });
    return false;
};

hl.prototype.removeFromHotlist_messenger = function(memberKey, pane) {
    var self = this;
    jQuery.ajax({
        type: "POST",
        data: { "fav_member_key": memberKey },
        url: "AjaxRequest.aspx?wci=HotlistRemove",
        success: function() {
            self.decrementHotlistCount();
            if (self.car.reset) {
                self.car.reset();
            }
            jQuery("#addBuddy" + pane).show();
            jQuery("#removeBuddy" + pane).hide();
        }
    });
    return false;
};

hl.prototype.addToHotlist_search = function(that, memberKey) {
    var self = this;
    jQuery(".hotliststar")
		.block({ message: null, fadeIn: 0, fadeOut: 0, overlayCSS:
			{
			    backgroundColor: '#000',
			    opacity: '0'
			}
		});
    jQuery.ajax({
        type: "POST",
        data: { "fav_member_key": memberKey },
        url: "AjaxRequest.aspx?wci=HotlistAdd",
        success: function() {
        self.incrementHotlistCount();
            jQuery(that)
				.parent()
				.show()
				.empty()
				.attr("title", "In my hotlist");
            self.showHotlist();

        },
        error: function() {
            jQuery(".hotliststar").unblock();
        }
    });
    return false;
};

hl.prototype.incrementHotlistCount = function() {
	this.hotlistCount++;
	jQuery("span#hotlistCount").html("(" + this.hotlistCount + ")");
}

hl.prototype.decrementHotlistCount = function() {
    this.hotlistCount--;
    jQuery("span#hotlistCount").html("(" + this.hotlistCount + ")");
    if (this.hotlistCount == 0) {
	    this.hideHotlist();
	} 
}
