/* lightbox style */
function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div class="fancybox-title"><div class="imagecount">Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</div>' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '</div>';
}

jQuery(document).ready(function() {
	jQuery(".fancybox").fancybox({
		'type':'image',
	        fitToView   : true,
		'overlayShow'	: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'showCloseButton': true,
		'titlePosition' : 'inside',
		'titleFormat'	: formatTitle
	});
    jQuery("a.virtualtour").fancybox({
        'padding' : 0,
        'type':'ajax',
        'showNavArrows' : false,
        'arrows': false,
        'scrolling' : 'no',
        'overlayShow':true,
	    beforeLoad: function () {
    	    var url = $(this.element).attr("href");
    	    this.href = '/video/fancyvideo.php?vid='+Base64.encode(url)
    	}
    });

    jQuery(".fancypage").fancybox({
        'padding' : 0,
        'type':'ajax',
        'showNavArrows' : false,
        'arrows': false,
        'scrolling' : 'no',
        'overlayShow':true,
        'afterShow' : function() {
            jQuery(".toolbox").hide();
    
            // re-initialize any cms editor box
            $('.cms_editor,.cms_editor_mini').each(function() {
                cmsid = $(this).attr('id');
                if (cmsid) {
//                    tinyMCE.execCommand("mceAddControl", true, cmsid);
                    tinyMCE.execCommand("mceAddEditor", true, cmsid);
                }
            });
        },
        'beforeClose' : function() { 
            jQuery(".toolbox").show();
    
            // get id of any cms editor box
            $('.cms_editor,.cms_editor_mini').each(function() {
                cmsid = $(this).attr('id');
                if (cmsid) {
  //                  tinyMCE.execCommand("mceRemoveControl", false, cmsid);
                    tinyMCE.execCommand("mceRemoveEditor", false, cmsid);
                }
            });
        }

    });

    // Refresh original page after closing.  Used for photo gallery editing (builder page)
    jQuery(".fancypage_refresh").fancybox({
        'padding' : 0,
        'type':'iframe',
        'showNavArrows' : false,
        'arrows': false,
        fitToView: false,
            autoSize: false,
            autoDimensions: false,
	width:1000,
        height: 610,
        'scrolling' : 'no',
        'overlayShow':true,
        'afterShow' : function() {
            jQuery(".toolbox").hide();
    
            // re-initialize any cms editor box
            $('.cms_editor, .cms_editor_mini').each(function() {
                cmsid = $(this).attr('id');
                if (cmsid) {
                    tinyMCE.execCommand("mceAddControl", true, cmsid);
                    tinyMCE.execCommand("mceAddEditor", true, cmsid);
                }
            });
        },
        'beforeClose' : function() { 
            window.location.href=window.location.href;

            jQuery(".toolbox").show();
    
            // get id of any cms editor box
            $('.cms_editor, .cms_editor_mini').each(function() {
                cmsid = $(this).attr('id');
                if (cmsid) {
                    tinyMCE.execCommand("mceRemoveControl", false, cmsid);
                    tinyMCE.execCommand("mceRemoveEditor", false, cmsid);
                }
            });
        }
  
    });   

});
