<?php

/*	##################################
	SETTINGS
	################################## */

define('TS_THEME', 'deLuxe');
define('TS_DOMAIN', 'deluxe');
define('TS_SHORT', 'dl');
define('TS_VERSION', '0.9.7');
define('TS_IMG', get_bloginfo('template_url').'/img/');
define('TS_ADMIN', get_bloginfo('template_url') .'/lib/admin/');
define('TS_SCRIPTS', get_bloginfo('template_url') .'/lib/scripts/');

        
/*	##################################
   	INIT LOCALIZATION
	################################## */

load_theme_textdomain('deluxe', TEMPLATEPATH . '/lang');


/*	##################################
   	GET THEME OPTIONS
	################################## */
	
function ts_get_option($key) {
	
	global $ts_options;	
	$ts_options = get_option('ts_options');
	
	$ts_defaults = array(
		'ts_color' => 'red',
		'ts_logo' => TS_IMG . 'logo.png',
		'ts_rss' => get_bloginfo('rss2_url')
	);
	
	foreach($ts_defaults as $k=>$v) {
		if (!$ts_options[$k])
			$ts_options[$k] = $ts_defaults[$k];
	}
	
	return $ts_options[$key];
}


/*	##################################
	THEME SUPPORT
	################################## */

add_theme_support('menus');

if (function_exists('register_nav_menu')) {
	register_nav_menu('main', 'Main Menu');
}

if (function_exists('add_theme_support')) {
	add_theme_support('post-thumbnails');
	// Thumbnails for archives
	add_image_size('dl-thumb', 260, 140, true);
	// Post image for single posts
	add_image_size('dl-large', 560, 300, true);
	// Post image for display
	add_image_size('dl-display', 300, 220, true);
}

/*	##################################
   	BODY CLASS
	################################## */

add_filter('body_class','ts_body_classes');
	
function ts_body_classes($classes) {
	$classes[] = ts_get_option('ts_color');
    return $classes;
}


/*	##################################
   	TS THEME HEADER
	################################## */
	
add_action('wp_enqueue_scripts', 'ts_scripts');

function ts_scripts() {
	wp_enqueue_script('jquery');
	
	wp_enqueue_script('superfish', TS_SCRIPTS . 'superfish/superfish.js', array('jquery'), '1.4.8', true);
	wp_enqueue_script('supersubs', TS_SCRIPTS . 'superfish/supersubs.js', array('jquery'), '1.4.8', true);
	wp_enqueue_style('superfish', TS_SCRIPTS . 'superfish/superfish.css', false, '1.4.8', 'all' );
	
	if (is_singular()) wp_enqueue_script('comment-reply');
	
	wp_enqueue_script('pretty', TS_SCRIPTS . 'pretty/js/jquery.prettyPhoto.js', array('jquery'), '2.5.6', true);
	wp_enqueue_style('pretty', TS_SCRIPTS . 'pretty/css/prettyPhoto.css', false, '2.5.6', 'all' );
	
	if(is_active_widget(false,'','ts_flickr'))	
    	wp_enqueue_script('flickrfeed', TS_SCRIPTS . 'jflickrfeed.js', array('jquery'), '0.8', true);
	
}

add_filter('the_generator', 'ts_theme_generator');

function ts_theme_generator($generator) {
	$generator .= "\r\n" . '<meta name="generator" content="ThemeShift - '.TS_THEME.' '.TS_VERSION.'" />';
	return $generator;
}

add_action('wp_head','ts_theme_head');

function ts_theme_head() { ?>

<?php if(ts_get_option('ts_css')) : ?>
<style type="text/css" media="screen">
<?php echo ts_get_option('ts_css'); ?>

</style><?php echo "\n\n"; endif; ?>
<!--[if lt IE 8]>
<link rel="stylesheet" href="<?php echo TS_ADMIN; ?>ie.css" type="text/css" media="screen" />
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" href="<?php echo TS_ADMIN; ?>ie7.css" type="text/css" media="screen" />
<![endif]-->
<!--[if IE 6]>
<script type="text/javascript" src="<?php echo TS_ADMIN; ?>pngfix.js"></script>
<![endif]-->

<?php }

add_action('wp_footer','ts_theme_footer');

function ts_theme_footer() {
	
	if (is_singular()) include(TEMPLATEPATH . '/lib/scripts/validate-comments.php');
	include(TEMPLATEPATH . '/lib/scripts/validate-search.php');
	echo "\n".ts_get_option('ts_tracking')."\n";

}


/*	##################################
   	POST CLASS
	################################## */

add_filter('post_class','ts_post_class');
function ts_post_class($classes) {
	if (is_archive() || is_home()) $classes[] = 'box';
	$classes[] = 'clearfix';
	return $classes;
}
// alt post class in archive pages
function ts_post_class_alt($classes) {
	$classes[] = 'box-alt';
	return $classes;
}


/*	##################################
   	GET PAGE ID BY TITLE
	################################## */

function get_page_id($page_name){
	$page = get_page_by_title($page_name);
	return $page->ID;
}


/*	##################################
   	CUSTOM MORE LINK
	################################## */

function the_more() {
	global $post;
	if (strpos($post->post_content, '<!--more-->')) :
	        $the_more = '<p class="clearfix more-link"><a href="'.get_permalink().'" class="button" title="'.get_the_title().'">';
	        $the_more .= '<span>'.__('Read more',TS_DOMAIN).'</span>';
	        $the_more .= '</a></p>';
	        echo $the_more;
	endif;
}


/*	##################################
   	CUSTOM COMMENTS FORMAT
	################################## */

function deluxe_comments($comment, $args, $depth) {

	$GLOBALS['comment'] = $comment; ?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
	    <div id="comment-<?php comment_ID() ?>">
	    	<div class="comment-avatar">
	        	<?php echo get_avatar($comment,$size='40'); ?>
	        	<span class="overlay5"><img src="<?php echo TS_IMG; ?>overlay5.png" alt="overlay5" width="60" height="60"/></span>
	    	</div>
	    	<p class="comment-meta"><strong><?php printf(__('%s'), get_comment_author_link()) ?></strong> - <?php printf(__('%1$s'), get_comment_date()) ?></p>
	        <?php if ($comment->comment_approved == '0') : ?>
	        	<p class="moderation"><?php _e('Your comment is awaiting moderation.',TS_DOMAIN) ?></p>
	        <?php endif; ?>
	        <?php comment_text() ?>
	        <p class="reply">
	        	<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
	        </p>
	    </div>
<?php

}

function deluxe_pings($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>"><?php comment_author_link(); ?> - <?php comment_excerpt(); ?>
<?php

}


/*	##################################
	TIMTHUMB (incl. WPMU SUPPORT)
	################################## */

function ts_image($ts_image_url) {
    global $blog_id, $wpmu_version;
    if(isset($wpmu_version)) {
        if (isset($blog_id) && $blog_id > 0) {
            $ts_image_parts = explode('/files/', $ts_image_url);
            if (isset($ts_image_parts[1])) {
                $ts_image_src = '/blogs.dir/' . $blog_id . '/files/' . $ts_image_parts[1];
                return $ts_image_src;
            } else
                return $ts_image_url;
        }
    } else {
        return $ts_image_url;
    }
}


/*	##################################
	MENU FALLBACK
	################################## */
	
function ts_menu() {
	$ts_menu = '<div class="ts-menu"><ul class="sf-menu">';
	if(!ts_get_option('ts_ex_home')) :
		if(is_front_page()) $current = ' class="current"';
		$ts_menu .= '<li'.$current.'><a href="'.get_bloginfo('url').'">'.__('Home', TS_DOMAIN).'</a></li>';
	endif;
	if(is_single()) :
		$category = get_the_category(); 
		$current_cat = $category[0]->cat_ID;
	else :
		$current_cat = '';
	endif;
	$ex_cats = ts_get_option('ts_ex_cats');
	$ts_menu .= wp_list_categories('title_li=&current_category='.$current_cat.'&exclude='.$ex_cats.'&orderby=ID&echo=0');
	$ex_pages = ts_get_option('ts_ex_pages');
	$ts_menu .= wp_list_pages('title_li=&sort_column=menu_order&exclude='.$ex_pages.'&echo=0');
	$ts_menu .= '</ul></div>';
	echo $ts_menu;	
}


/*	##################################
	HIDE 'NO CATEGORIES'
	################################## */

add_filter('wp_list_categories','ts_hide_no_cats');

function ts_hide_no_cats($ts_cats) {
  if (!empty($ts_cats)) {
    $ts_cats = str_replace('<li>' .__( "No categories" ). '</li>', '', $ts_cats);
  }
  return $ts_cats;
}


/*	##################################
	REMOVE STUFF
	################################## */

// #more from more-link
function ts_less($content) {
	global $id;
	return str_replace('#more-'.$id.'"', '"', $content);
}
add_filter('the_content', 'ts_less');

// default css for wp-pagenavi
add_action( 'wp_print_styles', 'ts_deregister_styles', 100 );
function ts_deregister_styles() {
	wp_deregister_style('wp-pagenavi');
	wp_deregister_style('contact-form-7');
}


/*	##################################
   	OPTIONS / ADMIN / META BOX
	################################## */

require_once( TEMPLATEPATH . '/lib/admin/theme-options.php');
require_once( TEMPLATEPATH . '/lib/admin/theme-admin.php');
require_once( TEMPLATEPATH . '/lib/admin/widgets.php');


?>