Module"Clade/gallery

local p = {}

-- display a gallery of cladograms using floating div elements

function p.gallery(frame)
	
	local params = mw.getCurrentFrame():getParent().args

    local outerBorders = false
    if params['caption'] or params['footer'] then outerBorders = true end
    
	-- the gallery container
	local gallery = mw.html.create('div'):addClass('clade-gallery'):css('float',params['align'])

	-- main caption for gallery if there is one
	
	if params['main-caption'] then
		gallery:tag('div'):wikitext(params['main-caption']):addClass('main-caption')
	end

	-- a gallery of floating divs with content

    local shading = *
	if params['shading'] then shading = 'wikitable'	end -- class for shading
	
	local vertical = * -- defaults to horizontal
	if params['vertical'] then vertical = 'vertical'	end -- class for vertical display
	
	local styleAll = params['style'] or * -- style for all elements
	 
    local mainBorders = *
    local elementBorders = *
    if 	not params['main-borders'] or params['main-borders'] == "no" then mainBorders = 'no-border' end
    if 	params['borders'] == "no" then elementBorders = 'no-border' end
    	
    -- the gallery	contents
    local content = gallery:tag('div'):addClass('main-content')
                                      :addClass(mainBorders)
                                      --:css('height',params['height'])  
   	local i=0
	while i<15 do
		i=i+1 
		local caption = params['caption'..i] --or *
		local header = params['header'..i] --or *
		local footer = params['footer'..i] --or *
		local style = params['style'..i] or styleAll
		local headerstyle = params['headerstyle'..i] or params['headerstyle']
		local width = params['width'..i] or params['width']

		-- for each gallery element containing  a cladogram
        if params['cladogram'..i] then
		    local cladogramTable = content:tag('table')
		                  :addClass(elementBorders)
		                  :addClass(shading):addClass('gallery-element')
		                  :addClass(vertical)
		                  :css('width',width)    
		                  :css('height',params['height']) 
		                  :cssText(style)    
            if caption then 
	        	cladogramTable:tag('caption'):wikitext(caption):addClass('caption')
	        end
	        if header then 
	        	local row = cladogramTable:tag('tr'):addClass('header')
	        	row:tag('th'):wikitext(header)
	        	             :cssText(headerstyle)
	        end
	        -- if params['cladogram'..i] already checked
	        local row = cladogramTable:tag('tr')
	        row:tag('td') :wikitext("\n" .. params['cladogram'..i])
	                      :addClass('cladogram')
	                      --:css("padding", "20px")
		                  -- :css('width',params['cladogram-width'])    
		                  --:css('height',params['cladogram-height'])    
		                  :css('height',params['height']) 
	        
	        if footer then
	        	local row = cladogramTable:tag('tr')
	        	row:tag('td'):wikitext(footer):addClass('footer'):addClass(shading)
	        end
		    
		    --[DIV VERSION - different browsers behave differently
		    -- the div containing each element in the gallery
		    local div = content:tag('div'):addClass('gallery-element'):addClass(direction)
		                  :css('width',params['width'])    
		                  :css('height',params['height'])    
		    
		    -- the caption, header, content and footer for each element
		    if caption then div:tag('div'):wikitext(caption):addClass('caption'):addClass(shading) end
		    
		    if header then div:tag('div'):wikitext(header):addClass('header'):addClass(shading) end
		    
		    div:tag('div'):wikitext(params['cladogram'..i]):addClass('cladogram'):addClass(shading)
		                  :css('width',params['cladogram-width'])    
		                  :css('height',params['cladogram-height'])    
		    
		    if footer then div:tag('div'):wikitext(footer):addClass('footer'):addClass(shading) end
		    END DIV VERSION--](https://hif.wikipedia.org/wiki/DIV_VERSION_-_different_browsers_behave_differently%0A%09%09____--_the_div_containing_each_element_in_the_gallery%0A%09%09____local_div_=_content:tag%28%27div%27%29:addClass%28%27gallery-element%27%29:addClass%28direction%29%0A%09%09__________________:css%28%27width%27%2Cparams%5B%27width%27%5D%29____%0A%09%09__________________:css%28%27height%27%2Cparams%5B%27height%27%5D%29____%0A%09%09____%0A%09%09____--_the_caption%2C_header%2C_content_and_footer_for_each_element%0A%09%09____if_caption_then_div:tag%28%27div%27%29:wikitext%28caption%29:addClass%28%27caption%27%29:addClass%28shading%29_end%0A%09%09____%0A%09%09____if_header_then_div:tag%28%27div%27%29:wikitext%28header%29:addClass%28%27header%27%29:addClass%28shading%29_end%0A%09%09____%0A%09%09____div:tag%28%27div%27%29:wikitext%28params%5B%27cladogram%27..i%5D%29:addClass%28%27cladogram%27%29:addClass%28shading%29%0A%09%09__________________:css%28%27width%27%2Cparams%5B%27cladogram-width%27%5D%29____%0A%09%09__________________:css%28%27height%27%2Cparams%5B%27cladogram-height%27%5D%29____%0A%09%09____%0A%09%09____if_footer_then_div:tag%28%27div%27%29:wikitext%28footer%29:addClass%28%27footer%27%29:addClass%28shading%29_end%0A%09%09____END_DIV_VERSION--) 
	    else
	       --break	-- either break or comment out to allow gaps in sequence
	    end
	end
	
	-- main footer
	if params['main-footer'] then
		gallery:tag('div'):wikitext(params['main-footer']):addClass('main-footer'):addClass(noborder)
	end
	-- TemplateStyles file
	local src = "Template:Clade gallery/styles.css"
    	
	return p.templateStyle( frame, src ) .. tostring(gallery)
end
function p.templateStyle( frame, src )
   return frame:extensionTag( 'templatestyles', '', { src = src } );
end

-- this must be at end
return p