Guestbook = {
		
	previewComment : function(e) {
	    e.preventDefault();
		if ($('#isPrivate').attr('checked')) {
			$('#comment').addClass('commentPrivate');
		} else {
			$('#comment').removeClass('commentPrivate');
		}
	    $("#btEdit, #gbPreview a.closeLink").click(function(e) { e.preventDefault(); dimmer("#gbPreview", "#guestbookMessage"); });
	    $("#gbPreview .commentTxt").html(this.commentForPreview($.trim($("#guestbookMessage").val())));
	    dimmer("#gbPreview", "#guestbookMessage");
	},
	
	strnpos : function(str, needle, n, pos) {
	    for (i = 1; i <= n; i++) {
	        pos = str.indexOf(needle, pos) + 1;
	    }
	    return pos - 1;
	},
	
	limitTextarea : function(thearea, limit) {
	    // append a little text note with character limit
	    if (thearea.value.length > limit) {
	        thearea.value = thearea.value.substring(0, limit);
	    }
	},
	
	commentForPreview : function(comment) {
	    if (comment.length > 500)
	        comment = comment.substring(0, 500) + "&hellip;";
	    comment = comment.replace(/<[^>]*>/g, "").replace(/<|>/g, "").replace(/\r?\n/gi, "<br>").replace(/\[(\/?[bui])\]/g, "<$1>");
	    comment = comment.replace(/(https?:\/\/[\w~!@#%&+=,\-\.\/\?]+[A-Za-z0-9\/])/g, '<a href="$1" rel="nofollow noindex external">$1</a>');
	    var linebreaks = comment.match(/<br>/g);
	    if (linebreaks && linebreaks.length > 8) {
	        cutpoint = this.strnpos(comment, "<br>", 8, 0);
	        comment = comment.substring(0, cutpoint) + "&hellip;";
	    }
	    return comment;
	},
	    
	updateCharCount : function() {
	    diff = commentMax - $("#guestbookMessage").val().length;
	    if (diff < 1) diff = "0";
	    $("#caption_limit var").html(diff);
	},
	
	postCommentWithGift : function(e) {
	    e.preventDefault();
	    $("#gbAlert a.closeLink, #gbAlert .cancelLink a").click(function() {
	        dimmer("#gbAlert", "#guestbookMessage");
	    });
	    dimmer("#gbAlert", "#guestbookMessage");    
	    return false;
	},
	
	makeGift : function(obj) {
	    makeGift(obj);
	}
}

Viximo = {
	
	store_div	: '#viximoStore',
	popup_div 	: '#viximoPopup',
	popup_div_anchor : '.formatting #giftsButton a',
	
	init_gift_store : function()
	{
		this.init_categories();
		
    	viximo.init({
			version: 2,
			publisher: 'fotolog',
			user: VIXIMO_USER,
            theme: voStyles,
			features: ['storefront'],
			complete: function() {
    	        Viximo.translations();
				Viximo.load_gift_store(VIXIMO_CAT);
			}
		});
	},
	
	load_gift_store : function(category)
	{
		var category	= category || 'featured';
        var flashMode   = (jQuery.fn.flash.hasFlash("8") ? "" : "preview");
		
		viximo.ui.goodselector("#viximoStore", {
			type: 'preview',
			limit: 25,
			aisle: category,
			sortMethod: 'featured',
			paginatePreview: true,
			showSelectNoneButton: false,
			selector: 'previewbutton',
			wmode: 'transparent',
			media: flashMode,
			select: function(event, goodselector) {
				var good = goodselector.good;
				if (good) {
					// Set the gift_id.
					var the_good_id = good.id;
					var the_good_name = good.name;
					
					var the_good_static_url = good.preview_url;
					
					var the_good_media_url = good.media_url;
					
					//this is the price in Flodos.  take this and convert to USD to give to Viximo...
					var the_good_price = good.price;
					  
					var user	= location.href.indexOf('u=') != -1 ? location.href.substr(location.href.indexOf('u=') + 2) : '';
					location.href = '/gift_store?id=' + the_good_id + '&u=' + user;
				} 
			},
			allowBrowsing: false
		});
	},
	
	load : function(category)
	{
        var category    = category || 'featured';
        var flashMode   = (jQuery.fn.flash.hasFlash("8") ? "" : "preview");
        
	    viximo.ui.goodselector("#viximoStore", {
			type: 'preview',
			limit: 4,
			wmode: 'transparent',
			media: flashMode,
            aisle: category,
			paginatePreview: true,
			showSelectNoneButton: false,
			selector: 'previewbutton',
			select: function(event, goodselector) {
				var good = goodselector.good;
				if (good) {
					// Set the gift_id.
					var the_good_id = good.id;
					var the_good_name = good.name;
					var the_good_static_url = good.preview_url;
					var the_good_media_url = good.media_url;
					//this is the price in Flodos.  take this and convert to USD to give to Viximo...
					var the_good_price = good.price;
					  
					//do something with this data...
					$('#giftValue').val(good.id);
					$('#giftPrice').val(good.price);
					$(".noGift, #gbForm .gbMessage#noGift #guestbookMessage, #gbForm .gbMessage#noGift #gbMessageContainer, #gbForm .gbMessage#noGift #caption_limit").show();
					$("#gbMessageNoGift").hide();
                    $(".gbMessage #bold, .gbMessage #italic").removeClass("disabled");
				    $(".gbMessage #bold, .gbMessage #italic").click(function(e) { enableFotologSyntax(e, this, $("#guestbookMessage")); });
					Viximo.close_mini_store();
					$("#giftPreview").html("").flash({ src: the_good_media_url, width: 100, height: 100 },{ img: the_good_static_url, width: 100, height: 100 });
					//$("#guestbookMessage").css('width', $(".gbMessage")[0].offsetWidth - 150);
					$("#guestbookMessage").addClass("withGift");
					attachGift(good.price, the_good_media_url, the_good_static_url);
					$("#giftPreviewContainer").show();
				} 
			},
			allowBrowsing: false
		});
	},
	
	translations : function() {
	    viximo.translations.add({
            paginator: {
                next: v_next,
                previous: v_previous
            },
            goods: {
                select: v_select,
                free: v_free
            },
            api: {
                loading: v_loading
            }
	    });
	},
	
	init_categories : function() {
		$("#categories a").click(function(e) {
			e.preventDefault();
			Viximo.get_category(this);
		});
	},
	
   init_mini_categories : function() {
        $("#gbTabs a").click(function(e) {
            e.preventDefault();
            Viximo.setTab($(this));
            viximo.ui.goodselector("#viximoStore", "browse", $(this).attr("c"));
        });
    },
    
    setTab : function(tab) {
        $("#gbTabs").children("span.on").removeClass("on");
        tab.parent().addClass("on");
    },
    
    open_mini_store : function(e) {
    	if ($(this.store_div + ' div.viximo-widget').length) {
    		e.preventDefault();
            e.stopPropagation();
            $("#giftsButton a").addClass("on");
            if ($(this.popup_div).css('display') == 'none') {
            	$("body").click(Viximo.close_mini_store);
                popup(Viximo.popup_div, Viximo.popup_div_anchor, {top: -280, left: -87});
            } else {
            	$("body").unbind("click");
            	this.close_mini_store();
            }
    		return;
    	}
    	
        if ($(Viximo.popup_div).css("display") == "none") {
            e.preventDefault();
            e.stopPropagation();

            $("#giftsButton a").addClass("on");
            Viximo.init_mini_categories();

            $(Viximo.popup_div).click(function(e) { e.stopPropagation(); });
            popup(Viximo.popup_div, Viximo.popup_div_anchor, {top: -280, left: -87});
            $("body").click(Viximo.close_mini_store);

            $("#googleAd").addClass("hide");
            
            viximo.init({
                version: 2,
                publisher: 'fotolog',
    			user: VIXIMO_USER,
                theme: voStyles,
                features: ['storefront'],
                complete: function() {
                    Viximo.translations();
                    Viximo.load();
                }
            });
        } else {
            Viximo.close_mini_store();
        }
    },
    
    close_mini_store : function() {
        $(Viximo.popup_div).fadeOut(0);
        $("#giftsButton a").removeClass("on");
        $("#googleAd").removeClass("hide");
        $("body").unbind("click");
    },
    
	get_category : function(link) {
		if (viximo && viximo.ui) {		// bail if viximo.init hasn't completed
			var category	= link.href;
			if (category.indexOf('c=') != -1) { category = category.substr(category.indexOf('c=') + 2); }
			
			viximo.ui.goodselector('#viximoStore', 'browse', category);
			$('#catName').text($(link).text()).attr('class', category.toLowerCase());
		}
	}
}
