<?php

/* These are functions specific to the included option settings and this theme */


/*-----------------------------------------------------------------------------------*/
/* Output Custom CSS from theme options
/*-----------------------------------------------------------------------------------*/

function tz_head_css() {

		$shortname =  get_option('tz_shortname'); 
		$output = '';
		
		$custom_css = get_option('tz_custom_css');
		
		if ($custom_css <> '') {
			$output .= $custom_css . "\n";
		}
		
		// Output styles
		if ($output <> '') {
			$output = "<!-- Custom Styling -->\n<style type=\"text/css\">\n" . $output . "</style>\n";
			echo $output;
		}
	
}

add_action('wp_head', 'tz_head_css');


/*-----------------------------------------------------------------------------------*/
/* Add Body Classes for Layout
/*-----------------------------------------------------------------------------------*/

add_filter('body_class','tz_body_class');
 
function tz_body_class($classes) {
	$shortname = get_option('tz_shortname');
	$layout = get_option($shortname .'_layout');
	if ($layout == '') {
		$layout = 'layout-2cr';
	}
	$classes[] = $layout;
	return $classes;
}


/*-----------------------------------------------------------------------------------*/
/* Add Favicon
/*-----------------------------------------------------------------------------------*/

function tz_favicon() {
	$shortname = get_option('tz_shortname');
	if (get_option($shortname . '_custom_favicon') != '') {
	echo '<link rel="shortcut icon" href="'. get_option('tz_custom_favicon') .'"/>'."\n";
	}
	else { ?>
	<link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri() ?>/admin/images/favicon.ico" />
	<?php }
}

add_action('wp_head', 'tz_favicon');


/*-----------------------------------------------------------------------------------*/
/* Show analytics code in footer */
/*-----------------------------------------------------------------------------------*/

function tz_analytics(){
	$shortname =  get_option('tz_shortname');
	$output = get_option($shortname . '_google_analytics');
	if ( $output <> "" ) 
		echo stripslashes($output) . "\n";
}
add_action('wp_footer','tz_analytics');


/*-----------------------------------------------------------------------------------*/
/*	Realtive Time function for Twitter wdiget
/*-----------------------------------------------------------------------------------*/

function relativeTime($time)
{
	define("SECOND", 1);
	define("MINUTE", 60 * SECOND);
	define("HOUR", 60 * MINUTE);
	define("DAY", 24 * HOUR);
	define("MONTH", 30 * DAY);

	$delta = strtotime('+2 hours') - $time;
	if ($delta < 2 * MINUTE) {
		return "1 min ago";
	}
	if ($delta < 45 * MINUTE) {
		return floor($delta / MINUTE) . " min ago";
	}
	if ($delta < 90 * MINUTE) {
		return "1 hour ago";
	}
	if ($delta < 24 * HOUR) {
		return floor($delta / HOUR) . " hours ago";
	}
	if ($delta < 48 * HOUR) {
		return "yesterday";
	}
	if ($delta < 30 * DAY) {
		return floor($delta / DAY) . " days ago";
	}
	if ($delta < 12 * MONTH) {
		$months = floor($delta / DAY / 30);
		return $months <= 1 ? "1 month ago" : $months . " months ago";
	} else {
		$years = floor($delta / DAY / 365);
		return $years <= 1 ? "1 year ago" : $years . " years ago";
	}
}

/*-----------------------------------------------------------------------------------*/
/*	List categories for the gallery
/*-----------------------------------------------------------------------------------*/

class Gallery_Walker extends Walker_Category {
   function start_el(&$output, $category, $depth, $args) {
      extract($args);
      $cat_name = esc_attr( $category->name);
      $cat_name = apply_filters( 'list_cats', $cat_name, $category );
      $link = '<a href="" data-value="term-'.$category->term_id.'" ';
      if ( $use_desc_for_title == 0 || empty($category->description) )
         $link .= 'title="' . sprintf(__( 'View all items filed under %s' ), $cat_name) . '"';
      else
         $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
      $link .= '>';
      // $link .= $cat_name . '</a>';
      $link .= $cat_name;
      if(!empty($category->description)) {
         $link .= ' <span>'.$category->description.'</span>';
      }
      $link .= '</a>';
      if ( (! empty($feed_image)) || (! empty($feed)) ) {
         $link .= ' ';
         if ( empty($feed_image) )
            $link .= '(';
         $link .= '<a href="' . get_category_feed_link($category->term_id, $feed_type) . '"';
         if ( empty($feed) )
            $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
         else {
            $title = ' title="' . $feed . '"';
            $alt = ' alt="' . $feed . '"';
            $name = $feed;
            $link .= $title;
         }
         $link .= '>';
         if ( empty($feed_image) )
            $link .= $name;
         else
            $link .= "<img src='$feed_image'$alt$title" . ' />';
         $link .= '</a>';
         if ( empty($feed_image) )
            $link .= ')';
      }
      if ( isset($show_count) && $show_count )
         $link .= ' (' . intval($category->count) . ')';
      if ( isset($show_date) && $show_date ) {
         $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
      }
      if ( isset($current_category) && $current_category )
         $_current_category = get_category( $current_category );
      if ( 'list' == $args['style'] ) {
          $output .= '<li ';
          $class = 'cat-item cat-item-'.$category->term_id;
          if ( isset($current_category) && $current_category && ($category->term_id == $current_category) )
             $class .=  ' current-cat';
          elseif ( isset($_current_category) && $_current_category && ($category->term_id == $_current_category->parent) )
             $class .=  ' current-cat-parent';
          $output .=  '';
          $output .= ">$link <span>/</span>\n";
       } else {
          $output .= "\t$link<br />\n";
       }
   }
}

/*-----------------------------------------------------------------------------------*/
/*	Display Exif data for gallery post type
/*-----------------------------------------------------------------------------------*/

function tz_grab_exif_data_from_wp($post_ID, $debug = FALSE) {
	global $id, $post;
	if ( !isset($post_ID) || '' == $post_ID )
		return FALSE;
	$meta = wp_get_attachment_metadata($post_ID, FALSE);
	$return = '';
	if ( $meta['image_meta']['created_timestamp'] )
		$return .= date( "d-M-Y H:i:s", $meta['image_meta']['created_timestamp'] ). '<span class="exif-slash"> / </span> ';
	if ( $meta['image_meta']['copyright'] )
		$return .= 'Copyright: ' .$meta['image_meta']['copyright']. '<span class="exif-slash"> / </span> ';
	if ( $meta['image_meta']['credit'] )
		$return .= 'Credit: ' .$meta['image_meta']['credit']. '<span class="exif-slash"> / </span> ';
	if ( $meta['image_meta']['title'] )
		$return .= 'Title: ' .$meta['image_meta']['title']. '<span class="exif-slash"> / </span> ';
	if ( $meta['image_meta']['caption'] )
		$return .= 'Caption: ' .$meta['image_meta']['caption']. '<span class="exif-slash"> / </span> ';
	if ( $meta['image_meta']['camera'] )
		$return .= 'Camera: ' . $meta['image_meta']['camera']. '<span class="exif-slash"> / </span> ';
	if ( $meta['image_meta']['focal_length'] )
		$return .= 'Focal Length: ' . $meta['image_meta']['focal_length'] .'<span class="exif-slash"> / </span> ';
	if ( $meta['image_meta']['aperture'] )
		$return .= 'Aperture: ' . $meta['image_meta']['aperture']. '<span class="exif-slash"> / </span> ';
	if ( $meta['image_meta']['iso'] )
		$return .= 'ISO: ' . $meta['image_meta']['iso']. '<span class="exif-slash"> / </span> ';
	if ( $meta['image_meta']['shutter_speed'] )
		$return .= 'Speed: ' . number_format($meta['image_meta']['shutter_speed'], 2) . ' ' . __( '', FB_GREYFOTO_TEXTDOMAIN ). '<span class="exif-slash"> / </span> ';
	if ($debug) {
		ob_start();
		var_dump($meta);
		$return = ob_get_clean();
	}
	return $return;
}



?>
