<?php

//*************************** Columns ***************************//

function ghostpool_columns($atts, $content = null, $code) {
	extract(shortcode_atts(array(
		'name' => '',
		'type' => 'blank',
		'text_align' => 'text-left',
		'height' => '',
        'padding' => '',
        'margins' => '',
        'background' => ''
	), $atts));
	
	if($code=="one") {
	$class = "one first last";	
	} elseif($code=="two") {
	$class = "two first";	
	} elseif($code=="two_last") {
	$class = "two last";	
	} elseif($code=="three") {
	$class = "three first";	
	} elseif($code=="three_middle") {
	$class = "three middle";
	} elseif($code=="three_last") {
	$class = "three last";	
	} elseif($code=="four") {
	$class = "four first";	
	} elseif($code=="four_middle") {
	$class = "four middle";	
	} elseif($code=="four_last") {
	$class = "four last";	
	} elseif($code=="five") {	
	$class = "five first";	
	} elseif($code=="five_middle") {
	$class = "five middle";	
	} elseif($code=="five_last") {
	$class = "five last";		
	} elseif($code=="onethird") {
	$class = "onethird first";
	} elseif($code=="onethird_last") {
	$class = "onethird last";	
	} elseif($code=="twothirds") {
	$class = "twothirds first";	
	} elseif($code=="twothirds_last") {
	$class = "twothirds last";
	} elseif($code=="onefourth") {
	$class = "onefourth first";	
	} elseif($code=="onefourth_last") {
	$class = "onefourth last";
	} elseif($code=="threefourths") {
	$class = "threefourths";		
	} elseif($code=="threefourths_last") {
	$class = "threefourths last";		
	}
	
    if($type == "blank") {
		$col_type = "blank";
	} elseif($type == "joint") {
		$col_type = "joint";
	} elseif($type == "separate") {
		$col_type = "separate";
	}
	
	$clear = strpos($class,"last");

	// Height
	if($height != "") {
		if(preg_match('/%/', $height) OR preg_match('/em/', $height) OR preg_match('/px/', $height)) {
			$height = 'height: '.$height.'; ';		
		} else {
			$height = 'height: '.$height.'px; ';		
		}
	} else {
		$height = "";
	}

	// Padding
	if($padding != "") {
		if(preg_match('/%/', $padding) OR preg_match('/em/', $padding) OR preg_match('/px/', $padding)) {
			$padding = str_replace(",", " ", $padding);
			$padding = 'padding: '.$padding.'; ';	
		} else {
			$padding = str_replace(",", "px ", $padding);
			$padding = 'padding: '.$padding.'px; ';		
		}
	} else {
		$padding = "";
	}
	
	// Margins
	if($margins != "") {
		if(preg_match('/%/', $margins) OR preg_match('/em/', $margins) OR preg_match('/px/', $margins)) {
			$margins = str_replace(",", " ", $margins);
			$margins = 'margin: '.$margins.'; ';	
		} else {
			$margins = str_replace(",", "px ", $margins);
			$margins = 'margin: '.$margins.'px; ';		
		}
	} else {
		$margins = "";
	}

	// Background
	if($background != "") {
		$background = 'background: '.$background.'; ';
	} else {
		$background = "";
	}
	
	if($clear === false) {
		return '<div class="columns '.$class.' '.$col_type.' '.$name.' '.$text_align.'" style="'.$margins.'"><div style="'.$height.$padding.$background.'">'.do_shortcode($content).'<div class="clear"></div></div></div>';
	} else {
		return '<div class="columns '.$class.' '.$col_type.' '.$name.' '.$text_align.'" style="'.$margins.'"><div style="'.$height.$padding.$background.'">'.do_shortcode($content).'<div class="clear"></div></div></div><div class="clear"></div>';
	}
}

add_shortcode("one", "ghostpool_columns");
add_shortcode("two", "ghostpool_columns");
add_shortcode("two_last", "ghostpool_columns");
add_shortcode("three", "ghostpool_columns");
add_shortcode("three_middle", "ghostpool_columns");
add_shortcode("three_last", "ghostpool_columns");
add_shortcode("four", "ghostpool_columns");
add_shortcode("four_middle", "ghostpool_columns");
add_shortcode("four_last", "ghostpool_columns");
add_shortcode("five", "ghostpool_columns");
add_shortcode("five_middle", "ghostpool_columns");
add_shortcode("five_last", "ghostpool_columns");
add_shortcode("onethird", "ghostpool_columns");
add_shortcode("onethird_last", "ghostpool_columns");
add_shortcode("twothirds", "ghostpool_columns");
add_shortcode("twothirds_last", "ghostpool_columns");
add_shortcode("onefourth", "ghostpool_columns");
add_shortcode("onefourth_last", "ghostpool_columns");
add_shortcode("threefourths", "ghostpool_columns");
add_shortcode("threefourths_last", "ghostpool_columns");

?>