<?php

function intense_get_animation_wrapper_start( $type, $trigger, $scroll_percent, $delay, $trigger_client_id = null, $reset = false, $id = null, $class = null ) {
	$intense_animations = Intense_Animated::get_animations();
	$animation = $intense_animations[ $type ];
	$animation_source = $animation[0];
	$animation_category = $animation[1];
	$starthidden = $animation[2];
	$endhidden = $animation[3];

	intense_load_animation_CSS( $type );	
	intense_add_script( 'intense.animated' );
        
    if ( $trigger == 'scroll' ) intense_add_script( 'jquery.appear' );

	if ( !empty( $trigger_client_id ) ) {
		$trigger_client_id = ' data-client-id="' . $trigger_client_id . '"';
	} else {
		$trigger_client_id = '';
	}

	if ( !empty( $reset ) && $reset ) {
		$reset = ' data-reset="' . $reset . '"';
	} else {
		$reset = '';
	}

	$id = ( !empty ( $id ) ) ? ' id="' . $id . '"' : '';

	return "<div" . $id . " class='intense $trigger-animated" . ( $starthidden ? ' start-hidden' : '') . ( !empty( $class ) ? ' ' . $class : '' ) . "' data-delay='$delay' data-scrollpercent='$scroll_percent' data-starthidden='$starthidden' data-endhidden='$endhidden' data-animation='$type'" . $trigger_client_id . $reset . ">";
}

function intense_get_animation_wrapper_end() {
	return '</div>';
}

function intense_get_animations_start_hidden() {
	$intense_animations = Intense_Animated::get_animations();
	$animations = array();

	foreach ($intense_animations as $animation => $attributes) {
		if ( $attributes[2] ) {
			$animations[ $animation ] = $animation;
		}
	}	

	return $animations;
}

function intense_get_animations_end_hidden() {
	$intense_animations = Intense_Animated::get_animations();

	$animations = array();

	foreach ($intense_animations as $animation => $attributes) {
		if ( $attributes[3] ) {
			$animations[ $animation ] = $animation;
		}
	}	

	return $animations;
}

function intense_get_animation_categories() {
	$intense_animations = Intense_Animated::get_animations();

	$categories = array();

	foreach ($intense_animations as $animation => $attributes) {
		if ( !in_array( $attributes[1], $categories ) ) {
			$categories[] = $attributes[1];			
		}
	}

	natsort( $categories );

	return $categories;
}

function intense_load_animation_CSS( $type ) {
	$intense_animations = Intense_Animated::get_animations();

	$minified = ( INTENSE_DEBUG ? "" : "min." );	

	if ( isset( $intense_animations[ $type ] ) ) {
		$animation = $intense_animations[ $type ];
		$animation_source = $animation[0];
		$animation_category = $animation[1];
		$starthidden = $animation[2];
		$endhidden = $animation[3];
		$css = "";

		switch ( $animation_source ) {
			case 'animate':
				$css = file_get_contents( INTENSE_PLUGIN_FOLDER . '/assets/animations/animate/' . $type . '.' . $minified . 'css' );
				break;
			case 'magic':
				$css = file_get_contents( INTENSE_PLUGIN_FOLDER . '/assets/animations/magic/' . $type . '.' . $minified . 'css' );
				break;		
			case 'intense':
				$css = file_get_contents( INTENSE_PLUGIN_FOLDER . '/assets/animations/intense/' . $type . '.' . $minified . 'css' );
				break;
			default:
				# code...
				break;
		}	

		$GLOBALS['IntensePluginInlineStyle'][ $animation_source . '_animate_' . $type ] = $css;		
	}
}

add_shortcode( 'intense_animated_demo', 'intense_animated_demo_shortcode' );
function intense_animated_demo_shortcode( $atts = null, $content = null ) {
    $intense_animations = Intense_Animated::get_animations();

    $output = '';

    $categories = intense_get_animation_categories();

    foreach ( $categories as $category ) {
        $category_animations = array();

        foreach ( $intense_animations as $animation => $attributes ) {
            $animation_source = $attributes[0];
            $animation_category = $attributes[1];
            $starthidden = $attributes[2];
            $endhidden = $attributes[3];

            if ( $category == $animation_category ) {
                $category_animations[] = $animation;
            }
        }

        switch ( $category ) {
        case 'Repeating':
            $trigger = 'delay';
            $triggerHint = '';
            break;
        case 'Attention Seekers':
            $trigger = 'hover';
            $triggerHint = ' <small>(' . $trigger . ' to activate)</small>';
            break;
        default:
            $trigger = 'click';
            $triggerHint = ' <small>(' . $trigger . ' to activate)</small>';
            break;
        }

        $output .= '<h2>' . $category . $triggerHint . '</h2>';
        //$output .= '<strong>' . $category . ':</strong> ';

        natsort( $category_animations );

        foreach ( $category_animations as $animation ) {
        	$button = intense_run_shortcode( 'intense_button', array( 
        		'size' => 'medium',
        		'link' => 'javascript: return false;',
        		'trigger' => $trigger,
    		), $animation );

        	$output .= intense_run_shortcode( 'intense_animated', array( 
        		'type' => $animation,  
        		'trigger' => $trigger,
    		), $button );
        }       

        $output .= intense_run_shortcode( 'intense_hr', array( 'size' => 'large' ) );
        
        //$output = rtrim($output, ', ');
        //$output .= '<br /><br />';
    }

    return $output;
}