<?php
function gabfire_call_flv ($parameters){
	global $post, $gab_flv;
	$video_id = $parameters['name'].'_'.$parameters['video_id'].'_'.$post->ID;
	
	echo "
		<span class='cf_gab_video ".$parameters['thumb_align']."'>
		   <div class='flowplayer' data-swf='". get_template_directory_uri() ."/inc/js/flowplayer/flowplayer.swf' data-ratio='0.417'>
			  <video>
				 <gabfire type='video/mp4' src='". esc_url($gab_flv) ."'/>
			  </video>
		   </div>
		</span>";
	
	/* Call skin css only once */
	if(!isset($_SESSION['visitedOnce'])){
		gabfire_flowplayerSkin();
		$_SESSION['visitedOnce'] = true;
	}

	add_action( 'wp_head', '', 5 );
}

function gabfire_call_swf ($parameters){
	global $post, $gab_video;
	$gab_video = get_post_meta($post->ID, 'video', true);
	echo "
		<span class='cf_gab_video ".$parameters['thumb_align']."'>
			<object type='application/x-shockwave-flash' style='width:".$parameters['media_width']."px; height:".$parameters['media_height']."px;' data='". esc_url ( $gab_video ) ."'>
			<param name='wmode' value='opaque' /><param name='movie' value='". esc_url ( $gab_video ) ."' /></object>
		</span>"; 
}

function gabfire_call_iframe ($parameters){
	global $post, $gab_iframe;
	
	/* Remove WWW to support videos for old gabfire theme's framework not to cause any conflict with real embed codes over regular video urls */
	$gab_iframe = str_replace("http://www.", "http://", $gab_iframe);
	$gab_iframe = str_replace("https://www.youtube.com", "http://youtube.com", $gab_iframe);
	
	$orj_value = array("http://youtube.com/watch?v=", "http://vimeo.com/",   "http://dailymotion.com/video/", "http://screenr.com/" );
	$new_value = array("http://youtube.com/embed/", "http://player.vimeo.com/video/", "http://dailymotion.com/embed/video/", "http://screenr.com/embed/");
	$gab_iframe = str_replace($orj_value, $new_value, $gab_iframe);
	
	if(strpos($gab_iframe, "&")) { /* Remove unnecessary part from youtube's URL */
		$gab_iframe = strpos($gab_iframe, "&") ? substr($gab_iframe, 0, strpos($gab_iframe, "&")) : $gab_iframe; 
		
	} else if(strpos($gab_iframe, "_") and strstr($gab_iframe,'dailymotion') ) { /* Remove unnecessary part from dailymotion's URL */
		$gab_iframe = strpos($gab_iframe, "_") ? substr($gab_iframe, 0, strpos($gab_iframe, "_")) : $gab_iframe; 
	}
	
	$orj_value_final = array("http://youtube.com", "http://dailymotion.com", "http://screenr.com" );
	$new_value_final = array("http://www.youtube.com", "http://www.dailymotion.com", "http://www.screenr.com");
	$gab_iframe = str_replace($orj_value_final, $new_value_final, $gab_iframe); 
	
	echo "
		<span class='cf_gab_video ".$parameters['thumb_align']."'>		
			<iframe title='" . get_the_title("") . "' src='". esc_url($gab_iframe) ."?wmode=opaque&amp;showinfo=0&amp;autohide=1' width='".$parameters['media_width']."' height='".$parameters['media_height']."' allowfullscreen></iframe>
		</span>"; 
}

function gabfire_call_post_thumb ($parameters){ /* Get default Post Thumbnail of WordPress */ 
	global $post;
	$image_id = get_post_thumbnail_id();  
	$image_url = wp_get_attachment_image_src($image_id,$parameters['name']);  
	$image_url = $image_url[0]; 
	
	if ($parameters['link'] == 1) {
		echo "<a href='". get_permalink() . "' rel='bookmark'>";	
	}
	
	if ($parameters['imgtag'] == 1) { 
		echo "<img src='";
	}
	
	echo $image_url;
	
	if ($parameters['imgtag'] == 1) {  
		echo "' class='".$parameters['thumb_align']."' alt='" . get_the_title() ."' title='" . get_the_title() ."' />";
	}		
	
	if ($parameters['link'] == 1) {
		echo "</a>"; 
	}	
}

/* Catch default thumbnail (image name is written into gabfire_media array.
 * The image is located in template_url/images/thumbs dir 
 */ 
function gabfire_call_default_thumb ($parameters){ 

	global $post;
	
	if ($parameters['link'] == 1) {
		echo "<a href='". get_permalink() . "' rel='bookmark'>";	
	}

	if ($parameters['imgtag'] == 1) { 
		echo "<img src='";
	}
		echo esc_url(get_template_directory_uri()) . "/images/thumbs/".$parameters['default_name'];
	
	if ($parameters['imgtag'] == 1) {  
		echo "' class='".$parameters['thumb_align']."' alt='" . get_the_title() ."' title='" . get_the_title() ."' />";
	}		
	
	if ($parameters['link'] == 1) {
		echo "</a>"; 
	}		
}

function gabfire_media($parameters) 
{
	if ( !post_password_required() ) {
		global $post, $gab_video, $gab_thumb, $gab_flv, $gab_iframe,$ad_flv;
		$gab_thumb = get_post_meta($post->ID, 'thumbnail', true);
		$gab_video = get_post_meta($post->ID, 'video', true);
		$gab_flv = get_post_meta($post->ID, 'videoflv', true);
		$gab_iframe = get_post_meta($post->ID, 'iframe', true);
		
		if($gab_flv != '' and $parameters['enable_video'] == 1) 
		{ 
			gabfire_call_flv ($parameters);
		}
		
		elseif ($gab_video != '' and $parameters['enable_video'] == 1) 
		{ 
			gabfire_call_swf ($parameters);
		}
		
		elseif ($gab_iframe != '' and $parameters['enable_video'] == 1)
		{ 
			gabfire_call_iframe ($parameters);
		}	
		
		elseif(has_post_thumbnail() and ($parameters['enable_thumb'] == 1))
		{
			gabfire_call_post_thumb ($parameters);
		}
		
		elseif($parameters['enable_default'] == 1) {
			gabfire_call_default_thumb ($parameters);
		}
	}
}