<?php
error_reporting(0);
?>		
<?php
function wpremix_tab_seo() {
	$enabled = '';
	$disabled = '';
	if(get_option('wpr_seo_enable')) {
		$enabled = 'checked="checked"';
	} else {
		$disabled = 'disabled';
	}

	?>
	<script type="text/javascript">
		function toggleSEOForm() {
			$j = jQuery;
			if($j('#wpr_seo_enable').is(':checked')) {
				$j('#seo :input').removeAttr('disabled');	
			} else {
				$j('#seo :input').attr('disabled', true);
				$j('#seo .ignore :input').removeAttr('disabled');
			}
		}
	</script>
	
	
	<div id="seo" class="tabbertab">
		<h2>SEO Options</h2>

		<div class="inputline">
			<div class="inputtitle">WP Remix SEO Options</div>
		</div>
		<div class="inputline">
			<div class="inputtext">Enable SEO Features?</div>
			<div class="inputvalue ignore">
				<input type="checkbox" id="wpr_seo_enable" name="wpr_seo_enable" value="1" <?php	 	 echo $enabled;?> onchange="toggleSEOForm()"/> This is the master on/off switch for SEO features. 
			</div>
		</div>

		<div class="inputlinegap"></div>
		<div class="inputline">
			<div class="inputtitle">General SEO Options</div>
		</div>
	
		<div class="inputline">
			<div class="inputtext">Blog Title</div>
			<div class="inputvalue">
				<input type="text" id="wpr_seo_home_title" name="wpr_seo_home_title" value="<?php	 	 echo get_option('wpr_seo_home_title');?>" <?php	 	 echo $disabled;?>/>
				<a href="#" onclick="jQuery('#help_wpr_seo_home_title').slideToggle('fast'); return false;" title="Help!?">?</a>
				<div id="help_wpr_seo_home_title" class="helpbox">Title of your blog</div>
			</div>
		</div>	
	
		<div class="inputline">
			<div class="inputtext">Home Description</div>
			<div class="inputvalue">
				<input type="text" id="wpr_seo_home_desc" name="wpr_seo_home_desc" value="<?php	 	 echo get_option('wpr_seo_home_desc');?>" <?php	 	 echo $disabled;?>/>
				<a href="#" onclick="jQuery('#help_wpr_seo_home_desc').slideToggle('fast'); return false;" title="Help!?">?</a>
				<div id="help_wpr_seo_home_desc" class="helpbox">META description of your home page</div>
			</div>
		</div>	
	
		<div class="inputline">
			<div class="inputtext">Home Keywords</div>
			<div class="inputvalue">
				<input type="text" id="wpr_seo_home_key" name="wpr_seo_home_key" value="<?php	 	 echo get_option('wpr_seo_home_key');?>" <?php	 	 echo $disabled;?>/>
				<a href="#" onclick="jQuery('#help_wpr_seo_home_key').slideToggle('fast'); return false;" title="Help!?">?</a>
				<div id="help_wpr_seo_home_key" class="helpbox">META keywords of your home page</div>
			</div>
		</div>	
	
		<div class="inputlinegap"></div>
		<div class="inputline">
			<div class="inputtitle">Title Formats</div>
		</div>
	
		<div class="inputline">
			<div class="inputtext">Home Title</div>
			<div class="inputvalue">
				<input type="text" class="large" id="wpr_seo_format_home" name="wpr_seo_format_home" value="<?php	 	 echo get_option('wpr_seo_format_home');?>" <?php	 	 echo $disabled;?>/>
				<a href="#" onclick="jQuery('#help_wpr_seo_format_home').slideToggle('fast'); return false;" title="Help!?">?</a>
				<div id="help_wpr_seo_format_home" class="helpbox">Title format for your homepage.<br/>Allowed Tags: %BLOG_TITLE%, %BLOG_TAGLINE%</div>
			</div>
		</div>	
	
		<div class="inputline">
			<div class="inputtext">Post Title</div>
			<div class="inputvalue">
				<input type="text" class="large" id="wpr_seo_format_post" name="wpr_seo_format_post" value="<?php	 	 echo get_option('wpr_seo_format_post');?>" <?php	 	 echo $disabled;?>/>
				<a href="#" onclick="jQuery('#help_wpr_seo_format_post').slideToggle('fast'); return false;" title="Help!?">?</a>
				<div id="help_wpr_seo_format_post" class="helpbox">Title format for your posts.<br/>Allowed Tags: %BLOG_TITLE%, %BLOG_TAGLINE%, %CATEGORY%, %POST_TITLE%</div>
			</div>
		</div>	
	
		<div class="inputline">
			<div class="inputtext">Page Title</div>
			<div class="inputvalue">
				<input type="text" class="large" id="wpr_seo_format_pages" name="wpr_seo_format_pages" value="<?php	 	 echo get_option('wpr_seo_format_pages');?>" <?php	 	 echo $disabled;?>/>
				<a href="#" onclick="jQuery('#help_wpr_seo_format_pages').slideToggle('fast'); return false;" title="Help!?">?</a>
				<div id="help_wpr_seo_format_pages" class="helpbox">Title format for your pages.<br/>Allowed Tags: %BLOG_TITLE%, %BLOG_TAGLINE%, %CATEGORY%, %PAGE_TITLE%</div>
			</div>
		</div>	
	
		<div class="inputline">
			<div class="inputtext">Category Title</div>
			<div class="inputvalue">
				<input type="text" class="large" id="wpr_seo_format_category" name="wpr_seo_format_category" value="<?php	 	 echo get_option('wpr_seo_format_category');?>" <?php	 	 echo $disabled;?>/>
				<a href="#" onclick="jQuery('#help_wpr_seo_format_category').slideToggle('fast'); return false;" title="Help!?">?</a>
				<div id="help_wpr_seo_format_category" class="helpbox">Title format for your categories.<br/>Allowed Tags: %BLOG_TITLE%, %BLOG_TAGLINE%, %CATEGORY_TITLE%, %CATEGORY_DESCRIPTION%</div>
			</div>
		</div>	
	
		<div class="inputline">
			<div class="inputtext">Archive Title</div>
			<div class="inputvalue">
				<input type="text" class="large" id="wpr_seo_format_archive" name="wpr_seo_format_archive" value="<?php	 	 echo get_option('wpr_seo_format_archive');?>" <?php	 	 echo $disabled;?>/>
				<a href="#" onclick="jQuery('#help_wpr_seo_format_archive').slideToggle('fast'); return false;" title="Help!?">?</a>
				<div id="help_wpr_seo_format_archive" class="helpbox">Title format for your archives.<br/>Allowed Tags: %BLOG_TITLE%, %BLOG_TAGLINE%, %ARCHIVE_DATE%</div>
			</div>
		</div>	
	
		<div class="inputline">
			<div class="inputtext">Search Title</div>
			<div class="inputvalue">
				<input type="text" class="large" id="wpr_seo_format_search" name="wpr_seo_format_search" value="<?php	 	 echo get_option('wpr_seo_format_search');?>" <?php	 	 echo $disabled;?>/>
				<a href="#" onclick="jQuery('#help_wpr_seo_format_search').slideToggle('fast'); return false;" title="Help!?">?</a>
				<div id="help_wpr_seo_format_search" class="helpbox">Title format for your search results.<br/>Allowed Tags: %BLOG_TITLE%, %BLOG_TAGLINE%, %SEARCH_QUERY%</div>
			</div>
		</div>	
	
		<div class="inputlinegap"></div>
		<div class="inputline">
			<div class="inputtitle">NoIndex Options</div>
		</div>
	
		<div class="inputline">
			<div class="inputtext">Noindex Categories</div>
			<div class="inputvalue">
				<input type="checkbox" id="wpr_seo_noindex_category" name="wpr_seo_noindex_category" value="1" <?php	 	 if(get_option('wpr_seo_noindex_category')) echo ' checked="checked"';?> <?php	 	 echo $disabled;?>/> Exclude categories from being crawled to avoid duplicate contents.
			</div>
		</div>	
	
		<div class="inputline">
			<div class="inputtext">Noindex Archives</div>
			<div class="inputvalue">
				<input type="checkbox" id="wpr_seo_noindex_archives" name="wpr_seo_noindex_archives" value="1" <?php	 	 if(get_option('wpr_seo_noindex_archives')) echo ' checked="checked"';?> <?php	 	 echo $disabled;?>/> Exclude archives from being crawled to avoid duplicate contents.
			</div>
		</div>	
	
		<div class="inputline">
			<div class="inputtext">Noindex Tags</div>
			<div class="inputvalue">
				<input type="checkbox" id="wpr_seo_noindex_tags" name="wpr_seo_noindex_tags" value="1" <?php	 	 if(get_option('wpr_seo_noindex_tags')) echo 'checked="checked"';?> <?php	 	 echo $disabled;?>/> Exclude tags archive from being crawled to avoid duplicate contents.
			</div>
		</div>

		<div class="inputlinegap"></div>
		<div class="inputline inputtitle">
			<div class="inputsubmit ignore">
				<input name="saveseo" type="submit" class="button" value="Save SEO Options" />
				<input name="resetseo" type="submit" class="button" value="Reset SEO Options" />
			</div>
			<div class="inputsubmitr">
				<input name="saveall" type="submit" class="button" value="Save All" />
				<input name="resetall" type="submit" class="button" value="Reset All" onclick="return wpr_reset_all();" />
			</div>
		</div>
	</div>
<?php	 	
}

function wpremix_tab_other() {
?>
	<div id="other" class="tabbertab">
		<h2>More Options...</h2>
		<p>More options coming soon...</p>
	</div>
<?php	 	
}

/**
 * Get the Header
 */
function get_remix_header() {
	// Load all settings.
	global $wpr_default;
	foreach($wpr_default as $group) {
		foreach($group as $key => $value) {
			global $$key;
			$$key = get_option($key);
		}
	}

	//Header
	global $post;
	if (isset($post)) {
		$id = $post->ID;
		$value = get_post_custom_values('_wp_remix_page_header', $id);
		if (isset($value[0])) {
			if (file_exists(TEMPLATEPATH . '/includes/header/' . $value[0] . ".php")) {
				include(TEMPLATEPATH . '/includes/header/' . $value[0] . ".php");
				return;
			}
		}
	}

	$rheader = get_option('remix-global-header');
	if ($rheader) {
		if (file_exists(TEMPLATEPATH . '/includes/header/' . $rheader . ".php")) {
			include(TEMPLATEPATH . '/includes/header/' . $rheader . ".php");
		} else {
			include (TEMPLATEPATH . '/includes/header/header1.php');
		}
	} else {
		include (TEMPLATEPATH . '/includes/header/header1.php');
	}
}

function remix_add_scripts() {
	wp_enqueue_script('jquery');
	wp_enqueue_script('remix_faq', get_bloginfo('template_directory') . '/js/faq.js', array("jquery", ) , 1.0);
}
function mytheme_wp_head() {
?>
<link href="<?php	 	 bloginfo('template_directory'); ?>/style.php" rel="stylesheet" type="text/css" />
<?php	 	 
	mytheme_wp_seo_meta();
}
function mytheme_templateredir() 
{
	ob_start('mytheme_rewritetitle');
}

function mytheme_rewritetitle($html)
{
	
	if ( get_option('wpr_seo_enable') == 1 )
	{
		if ( preg_match('/<title>/', $html) )
		{
			$html = preg_replace('#<title>(.+)<\/title>#es','mytheme_buildtitle("\\1")', $html);		
		}
	}

	return $html;
}

function mytheme_buildtitle($default)
{
	global $wp_query;

	// %BLOG_TITLE%
	$blog_title = get_option('wpr_seo_home_title');
	if ( $blog_title == '' )
	{
		$blog_title = get_bloginfo('name');
	}
	
	// POST, PAGES, CATEGORIES, ARCHIVES, SEARCH 
		
	//formats: wpr_seo_format_category, wpr_seo_format_archive, wpr_seo_format_search
	if ( is_home() || is_front_page() )
	{
		if ( get_option('wpr_seo_format_home') != '' )
		{
			//macros blog_title & tagline
			$title = get_option('wpr_seo_format_home');
			$title = str_replace('%BLOG_TITLE%',$blog_title, $title);
			$title = str_replace('%BLOG_TAGLINE%', get_bloginfo('description'), $title);
		}
		else
		{
			$title = $default;
		}
	}
	/* POST */
	elseif ( is_single() )
	{
		if ( get_option('wpr_seo_format_post') != '' )
		{
			$post_title = single_post_title('', false);

			$category = '';
			$categories = get_the_category();
			if ( count($categories) > 0 )
			{
				$category	= $categories[0]->cat_name;
			}

			$title = get_option('wpr_seo_format_post');
			$title = str_replace('%BLOG_TITLE%',$blog_title, $title);
			$title = str_replace('%BLOG_TAGLINE%', get_bloginfo('description'), $title);
			$title = str_replace('%CATEGORY%', $category, $title);
			$title = str_replace('%POST_TITLE%',$post_title, $title);
		}
		else
		{
			$title = $default;
		}
	}
	/* PAGES */
	elseif ( is_page() )
	{
		if ( get_option('wpr_seo_format_pages') != '' )
		{
			$page_title = single_post_title('', false);
			
			$category = '';
			$categories = get_the_category();
			if ( count($categories) > 0 )
			{
				$category	= $categories[0]->cat_name;
			}

			$title = get_option('wpr_seo_format_pages');
			$title = str_replace('%BLOG_TITLE%',$blog_title, $title);
			$title = str_replace('%BLOG_TAGLINE%', get_bloginfo('description'), $title);
			$title = str_replace('%CATEGORY%', $category, $title);	
			$title = str_replace('%PAGE_TITLE%',$page_title, $title);
		}
		else
		{
			$title = $default;
		}
	}
	/* CATEGORIES */
	elseif ( is_category() )
	{
		if ( get_option('wpr_seo_format_category') != '' )
		{
			$cat_title	= single_cat_title('', false);
			$cat_desc	= category_description();			

			$title = get_option('wpr_seo_format_category');
			$title = str_replace('%BLOG_TITLE%',$blog_title, $title);
			$title = str_replace('%BLOG_TAGLINE%', get_bloginfo('description'), $title);
			$title = str_replace('%CATEGORY_TITLE%', $cat_title, $title);	
			$title = str_replace('%CATEGORY_DESCRIPTION%',$cat_desc, $title);
		}
		else
		{
			$title = $default;
		}
	}
	/* ARCHIVE */
	elseif ( is_archive() )
	{
		if ( get_option('wpr_seo_format_archive') != '' )
		{
			$thedate = wp_title('', false);
			$title = get_option('wpr_seo_format_archive');
			$title = str_replace('%BLOG_TITLE%',$blog_title, $title);
			$title = str_replace('%BLOG_TAGLINE%', get_bloginfo('description'), $title);
			$title = str_replace('%ARCHIVE_DATE%', $thedate, $title);	
		}
		else
		{
			$title = $default;
		}
	}
	/* SEARCH */
	elseif ( is_search() )
	{
		global $s;

		if ( !empty($s) && get_option('wpr_seo_format_search') )
		{
			$title = get_option('wpr_seo_format_search');
			$title = str_replace('%BLOG_TITLE%',$blog_title, $title);
			$title = str_replace('%BLOG_TAGLINE%', get_bloginfo('description'), $title);
			$title = str_replace('%SEARCH_QUERY%',attribute_escape($s), $title);	
		}
		else
		{
			$title = $default;
		}
	}
	else
	{
		$title = $default;
	}

	
	return '<title>'.$title.'</title>';
}

function mytheme_wp_seo_meta()
{
	// If seo not enabled do nothing...
	if ( get_option('wpr_seo_enable') != 1 ) return;

	// add meta
	$metatag = '<meta name="robots" content="noindex" />'."\n";
	$show = 0;

	if ( get_option('wpr_seo_noindex_category') == 1 && is_category() ) $show = 1;
	if ( get_option('wpr_seo_noindex_archives') == 1 && is_archive() ) $show = 1;
	if ( get_option('wpr_seo_noindex_tags')		== 1 && is_tag() ) $show = 1;

	if ( $show == 1 ) echo $metatag;
}

function mytheme_enqueue_jquery() {
	wp_enqueue_script('jquery');
}


/**
 * Function to add custom link to navigation menu
 */
function wpremix_add_custom_links($links) {
    // Adding custom link to the Navigation bar
    $custom_links = array();
    $custom_links = unserialize(get_option('wpr_custom_links'));

    if(is_array($custom_links)) {
		foreach($custom_links as $link) {
			$url = explode("||", $link);
	        $links .= '<li class="page-item page-item-custom"><a href="http://' . $url['0'] . '" title="' . $url['1'] . '" target="_blank">' . $url['1'] . '</a></li>' . "\n";
		}
    }
    return $links;
}

add_filter('wp_list_pages', 'wpremix_add_custom_links');
add_action('wp_head', 'mytheme_wp_head');
add_action('template_redirect', 'mytheme_templateredir');
add_action('admin_menu', 'mytheme_add_admin'); 
add_action('admin_head', 'mytheme_enqueue_jquery'); 
add_action('wp_print_scripts','remix_add_scripts');

//Remix Editor
include(TEMPLATEPATH . "/editor/remix_advanced_editor.php");
?>