<?php
/**
 * @version    $Id$
 * @package    IT_Theme_Framework
 * @author     InnoThemes Team <support@innothemes.com>
 * @copyright  Copyright (C) 2012 InnoThemes.com. All Rights Reserved.
 * @license    GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Websites: http://www.innothemes.com
 */

// Get theme options
global $it_options;

if ( ! defined( 'IT_THEME_COLUMN_SETTINGS_INITIALIZED' ) ) :

define( 'IT_THEME_COLUMN_SETTINGS_INITIALIZED', true );

// Process column width
foreach ( array( 'promo', 'blog', 'single_post', 'page', 'bottom' ) as $row ) {
	// Prepare variables
	$row      = "{$row}_sidebar_settings";
	$columns  = array();
	$visible  = count( $it_options[ $row ] );
	$ordering = 0;

	foreach ( $it_options[ $row ] as $id => $class ) {
		// Add class to indicate the order of this column
		$it_options[ $row ][ $id ] .= ' order' . $ordering++;
	}

	// Sort columns for correct source code ordering
	ksort( $it_options[ $row ] );

	foreach ( $it_options[ $row ] as $id => $class ) {
		// Store data for processing visual ordering later
		$columns[] = array( 'id' => $id, 'class' => str_replace( 'span', 'col-md-', $class ) );
	}

	// Process visual ordering for visible columns
	foreach ( $columns as $ordering => $column ) {
		$visualOrdering = intval( preg_replace( '/^.*order(\d+).*$/', '\\1', $column['class'] ) );
		$offset = 0;

		if ( $ordering < $visualOrdering ) {
			// Calculate offset to shift the column to the right side
			for ( $i = $ordering + 1; $i < $visible; $i++ ) {
				$nextOrdering = intval( preg_replace( '/^.*order(\d+).*$/', '\\1', $columns[ $i ]['class'] ) );

				if ( ( 0 == $ordering || $nextOrdering > 0 ) && $nextOrdering < $visualOrdering ) {
					$offset += intval( preg_replace( '/^.*col-md-(\d+).*$/', '\\1', $columns[ $i ]['class'] ) );
				}
			}
		} elseif ( $ordering > 0 && $ordering == $visualOrdering && $ordering + 1 < $visible ) {
			// Calculate offset for column stay in the middle
			for ( $i = 0; $i < $ordering; $i++ ) {
				if ( preg_match( '/offset\d+/', $columns[ $i ]['class'] ) ) {
					$offset -= intval( preg_replace( '/^.*col-md-(\d+).*$/', '\\1', $columns[ $i ]['class'] ) );
				}
			}

			if ( $offset < 0 ) {
				$offset -= intval( preg_replace( '/^.*col-md-(\d+).*$/', '\\1', $columns[ $ordering ]['class'] ) );
			}
		} elseif ( $ordering > $visualOrdering ) {
			// Calculate offset to shift the column to the left side
			for ( $i = 0; $i < $ordering; $i++ ) {
				$prevOrdering = intval( preg_replace( '/^.*order(\d+).*$/', '\\1', $columns[ $i ]['class'] ) );

				if ( $prevOrdering > $visualOrdering ) {
					if ( preg_match( '/offset\d+/', $columns[ $i ]['class'] ) ) {
						$offset -= intval( preg_replace( '/^.*col-md-(\d+).*$/', '\\1', $columns[ $i ]['class'] ) );
						$offset -= intval( preg_replace( '/^.*offset(\d+).*$/', '\\1', $columns[ $i ]['class'] ) );
					} elseif ( false === strpos( $columns[ $i ]['class'], 'offset-' ) ) {
						$offset -= intval( preg_replace( '/^.*col-md-(\d+).*$/', '\\1', $columns[ $i ]['class'] ) );
					}
				} elseif ( $i > 0 && $prevOrdering < $visualOrdering ) {
					$offset += intval( preg_replace( '/^.*col-md-(\d+).*$/', '\\1', $columns[ $i ]['class'] ) );
				}
			}
		}

		// Set offset so the column display in correct visual ordering
		if ( 0 != $offset ) {
			$columns[ $ordering ]['class'] = preg_replace( '/(order\d+)/', "\\1 offset{$offset}", $columns[ $ordering ]['class'] );
		}
	}

	// Update column IDs and classes
	foreach ( $columns as $column ) {
		unset( $it_options[ $row ][ $column['id'] ] );

		// Re-fine ID
		$realId = preg_replace( '/^\d+:/', '', $column['id'] );

		// Re-fine column order
		$ordering = intval( preg_replace( '/^.*order(\d+).*$/', '\\1', $column['class'] ) ) + 1;

		// Split classes to span, order and offset
		$classes = explode( ' ', preg_replace( '/order\d+/', "order{$ordering}", $column['class'] ) );

		// Reset column data
		$it_options[ $row ][ $realId ] = array(
			'span'   => $classes[0],
			'order'  => isset( $classes[1] ) ? $classes[1] : '',
			'offset' => isset( $classes[2] ) ? $classes[2] : ''
		);
	}
}

endif;
?>
			<div id="it-<?php esc_attr_e( $prefix ); ?>" class="row">
<?php
$param_key = "{$prefix}_sidebar_settings";
$loop_name = ( 'single_post' == $prefix ) ? 'single' : ( ( 'page' == $prefix ) ? 'page' : null );

foreach ( $it_options[ $param_key ] as $column => $classes ) :
?>
				<div id="it-<?php esc_attr_e( $column ); ?>" class="<?php echo '' . trim( implode( ' ', $classes ) ); ?>">
					<?php $column == 'main-content' ? get_template_part( 'loop', $loop_name ) : dynamic_sidebar( $column ); ?>
				</div>
<?php endforeach; ?>
			</div>
