    document.write('<div id="cmsTemplateGallery"></div>');
    
    var embeddedGalleryWrapperId = "#cmsTemplateGallery"
    
    function filterResultsByHash(){
        if((!window.location.hash) || window.location.hash == '' || true){
            //alert($("#galleryKeywordChooser").attr("defaultKeyword"));
            filterResultsByClass($("#galleryKeywordChooser").attr("defaultKeyword"));
            return;
        }
        filterResultsByClass('kw'+window.location.hash.replace("#", "").toLowerCase());
    }
    
    function filterResultsByClass(clsName){
        //$('#divGalleryThumbnails a').not('.' + clsName).fadeOut(600, function(){
        var tnailItems = $('#divGalleryThumbnails .divGalleryItem');
        var counter = 0;
        $('#divGalleryThumbnails').fadeOut(300, function(){
            tnailItems.hide();
            
            $('#divGalleryThumbnails a').removeClass("lightboxInclude");
            $('#divGalleryThumbnails .' + clsName + ' a').not(".noLightbox").addClass("lightboxInclude");
            
            $('#divGalleryThumbnails .' + clsName).show();
            $('#divGalleryThumbnails').fadeIn();
            
            initModal();
        })
        /*
        tnailItems.fadeOut(600, function(){
            counter++;
            if(counter == tnailItems.length){
                $('#divGalleryThumbnails a.' + clsName).fadeIn(600, function(){
                    initModal();
                });
            }
        });*/
        
    }
    
    
    function initGalleryInto(opts){
    
        if(!opts){
            opts = {};
        }
        
        opts.container = opts.container || '#cmsTemplateGallery';
        opts.showchooser = opts.showchooser || false;
        
        embeddedGalleryWrapperId = opts.container;
    
        $.getScript('/lib/js/jquery.nyroModal-1.5.2.min.js', function(){
            
            $.ajax({
                url : '/lib/ajax/cms_templategallery_ajax.asp',
                data : {
                    callmethod : "renderGallery",
                    showchooser : opts.showchooser
                },
                success : function(strResponse){
                    
                    $.nyroModalSettings({
                        galleryCounts : function(){return '';},
                            
                        showTransition : function showTransition(elts, settings, callback) {
                            // Put the loading with the same dimensions of the current content
                            /*elts.loading
	                            .css({
		                            marginTop: elts.contentWrapper.css('marginTop'),
		                            marginLeft: elts.contentWrapper.css('marginLeft'),
		                            height: elts.contentWrapper.css('height'),
		                            width: elts.contentWrapper.css('width'),
		                            opacity: 0
	                            })
	                            .show()
	                            .fadeTo(400, 1, function() {
			                            //elts.contentWrapper.fadeOut(300);
			                            callback();
		                            });
		                            */
		                    elts.contentWrapper.fadeOut(600, function(){
		                        callback();
		                    });
                        },
                        
                        hideTransition : function(elts, settings, callback) {
		                    // Place the content wrapper underneath the the loading with the right dimensions
		                    /*elts.contentWrapper
			                    .hide()
			                    .css({
				                    width: settings.width+'px',
				                    height: settings.height+'px',
				                    marginLeft: settings.marginLeft+'px',
				                    marginTop: settings.marginTop+'px',
				                    opacity: 1
			                    });
		                    elts.loading
			                    .animate({
				                    width: settings.width+'px',
				                    height: settings.height+'px',
				                    marginLeft: settings.marginLeft+'px',
				                    marginTop: settings.marginTop+'px',
				                    opacity : '1'
			                    }, {complete: function() {
					                    elts.contentWrapper.show();
					                    elts.loading.fadeOut(400, function() {
						                    elts.loading.hide();
						                    callback();
					                    });
				                    }, duration: 350});*/
    				                
				            elts.contentWrapper.fadeIn(600, function(){
		                        callback();
		                    });
	                    }
                    });
                    
                    vResponse = strResponse.split("||");
                    if(vResponse[0] == 1){
                        $(opts.container).html(vResponse[1]);
                        //initModal();
                        filterResultsByHash(opts.container);
                        //filterResultsByClass("kwshowall");
                        
                    } else {
                        alert(vResponse[1]);
                    }
                }
            });
        });
    }
    
    function initModal(){
        $.nyroModalRemove();
        $(embeddedGalleryWrapperId + ' a.galleryLightboxItem').attr("rel", "disabled_templateGal");
        //$('#cmsTemplateGallery a.galleryLightboxItem').filter(":visible").not(".noLightbox").attr("rel", "templateGal").nyroModal();
        $(embeddedGalleryWrapperId + ' a.galleryLightboxItem.lightboxInclude').attr("rel", "templateGal").nyroModal();
        //$('#cmsTemplateGallery a.lightboxInclude').attr("rel", "templateGal").nyroModal();
        //$('#cmsTemplateGallery a.lightboxInclude').nyroModal();
    }
    
    if($("#delayDesignGalleryInit").length == 0){
        initGalleryInto();
    }
