<?php 
/**
 * JavaScripts In Header
 */
function theme_enqueue_scripts() {
	if((is_admin() && !is_shortcode_preview()) ||'wp-login.php' == basename($_SERVER['PHP_SELF'])){
		return;
	}
	$move_bottom = theme_get_option('advanced','move_bottom');

	wp_enqueue_script('jquery-easing', THEME_JS . '/jquery.easing.js', array('jquery'),'1.3',$move_bottom);
	wp_enqueue_script( 'jquery-tools', THEME_JS .'/jquery.tools.min.js', array('jquery'),'1.2.5',$move_bottom);
	wp_enqueue_script( 'jquery-prettyPhoto',THEME_JS.'/jquery.prettyPhoto.js',array('jquery'),'3.1.3',$move_bottom);
	wp_enqueue_script( 'jquery-swfobject', THEME_JS .'/jquery.swfobject.1-1-1.min.js', array('jquery'),'1.1.1',$move_bottom);
	wp_enqueue_script( 'jquery-spin', THEME_JS .'/spin.js', array('jquery'),'1.2.5',$move_bottom);
	wp_enqueue_script( 'jquery-preload', THEME_JS . '/jquery.preload.js',array('jquery'),'1.0.8',$move_bottom);
	wp_enqueue_script('jquery-cycle', THEME_JS . '/jquery.cycle.all.js', array('jquery'),'2.9998',$move_bottom);
	if(theme_get_option('footer','stricky_footer')){
		wp_enqueue_script( 'jquery-stickyfooter', THEME_JS .'/jquery.stickyfooter.js', array('jquery'),'1.0',$move_bottom);
	}
	wp_enqueue_script( 'main-js', THEME_JS .'/main.js', array('jquery','jquery-preload','jquery-swfobject','jquery-prettyPhoto'),false,$move_bottom);

	wp_register_script( 'slide-nav', THEME_JS .'/slidebar.js', array('jquery'),false,$move_bottom);
	wp_register_script('slider-js', THEME_JS . '/slideshow.js', array('jquery'),false,$move_bottom);
	wp_register_script( 'slider-video', THEME_JS .'/jquery.slider.video.js', array('jquery','mediaelementjs-scripts'),false,$move_bottom);
	wp_register_script('jquery-nivo', THEME_JS . '/jquery.nivo.slider.js', array('jquery'),'2.7.1',$move_bottom);
	wp_register_script('jquery-featureCarousel', THEME_JS . '/jquery.featureCarousel.js', array('jquery'),'1.2.1',$move_bottom);
	wp_register_script('jquery-swfobject', THEME_JS . '/jquery.swfobject.1-1-1.min.js',array('jquery'),'1.1.1',$move_bottom);
	wp_register_script('swfobject', THEME_JS . '/swfobject.js',false,false,$move_bottom);
	wp_register_script('anyInit-js', THEME_JS . '/anythingslide-init.js', array('jquery','slider-js','jquery-cycle','jquery-swfobject'),false,$move_bottom);
	wp_register_script('nivoInit-js', THEME_JS . '/nivoslide-init.js', array('jquery','slider-js','jquery-nivo'),false,$move_bottom);
	wp_register_script('flowInit-js', THEME_JS . '/flowslide-init.js', array('jquery','slider-js','jquery-featureCarousel'),false,$move_bottom);
	wp_register_script( 'mediaelementjs-scripts', THEME_URI .'/mediaelement/mediaelement-and-player.min.js', array('jquery'),'2.7.0',$move_bottom);

	wp_register_script( 'jquery-gallery', THEME_JS .'/jquery.ad-gallery.js', array('jquery'),'1.2.4',$move_bottom);
	wp_register_script( 'jquery-quicksand', THEME_JS .'/jquery.quicksand.js', array('jquery'),'1.2.2',$move_bottom);
	wp_register_script( 'cufon-yui', THEME_JS .'/cufon-yui.js', array('jquery'),'1.09i',$move_bottom);
	wp_register_script( 'jquery-gmap', THEME_JS .'/jquery.gmap.min.js', array('jquery','google-map-api'),'2.1.1');
	wp_register_script( 'google-map-api', 'http://maps.google.com/maps/api/js?sensor=false');
	wp_register_script( 'jquery-tweet', THEME_JS .'/jquery.tweet.js', array('jquery'),false,$move_bottom);
	wp_register_script( 'jquery-tools-validator', THEME_JS .'/jquery.tools.validator.min.js', array('jquery'),'1.2.5',$move_bottom);
	$introduce_type = 'default';
	$slug='';
	if(is_home()){
		$slug='blog';
		$introduce_type = theme_get_option('blog','introduce_type');
	}elseif(class_exists( 'Woocommerce' ) && is_product()){
		$slug='product';
		$introduce_type = theme_get_option('woocommerce','product_introduce_type');
	}elseif(class_exists( 'Woocommerce' ) && is_product_category()){
		$slug='product_cat';
		$introduce_type = theme_get_option('woocommerce','product_cat_introduce_type');
	}elseif( class_exists( 'Woocommerce' ) && is_shop() ){
		$slug = 'shop';
		$introduce_type = theme_get_option('woocommerce','shop_introduce_type');
	}elseif(!is_archive() && !is_search()){
		global $post;
		$introduce_type = get_post_meta($post->ID, '_introduce_text_type', true);
	}
	if($introduce_type =='default'){
		if(is_singular('portfolio')){
			$slug = 'portfolio';
			$introduce_type = theme_get_option('portfolio','introduce_type');
		}elseif(is_single()){
			$slug = 'single';
			$introduce_type = theme_get_option('blog','single_introduce_type');
		}
		if($introduce_type == 'default'){
			$slug = 'general';
			$introduce_type = theme_get_option('general','introduce');
		}
	}
	if('slideshow' == $introduce_type){
		if($slug =='blog'){
			$type = theme_get_option('blog','slideshow_type');
		}elseif($slug=="single"){
			$type = theme_get_option('blog','single_slideshow_type');
		}elseif($slug=="portfolio"){
			$type = theme_get_option('portfolio','slideshow_type');
		}elseif($slug == 'general'){
			$type = theme_get_option('general','general_slideshow_type');
		}elseif($slug == 'product'){
			$type = theme_get_option('woocommerce','product_slideshow_type');
		}elseif($slug == 'product_cat'){
			$type = theme_get_option('woocommerce','product_cat_slideshow_type');
		}elseif($slug == 'shop'){
			$type = theme_get_option('general','shop_introduce_type');
		}else{
			$type =  get_post_meta($post->ID, '_slideshow_type', true);
		}
		
		theme_generator('slideShowHeader_js',$type);
	}
	if ( is_singular() ){
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action('wp_print_scripts', 'theme_enqueue_scripts',105);
function theme_enqueue_styles()
{
	if((is_admin() && !is_shortcode_preview()) || 'wp-login.php' == basename($_SERVER['PHP_SELF'])){
		return;
	}
	wp_enqueue_style('theme-style', THEME_URI.'/css/screen.css', false, false, 'all');
	wp_enqueue_style('css-prettyPhoto',THEME_CSS.'/prettyPhoto.css',false, false, 'all');
	wp_enqueue_style('css-tips', THEME_CSS . '/tips.css',false, false, 'all');
	if(is_multisite()){
		global $blog_id;
		wp_enqueue_style('theme-skin', THEME_CACHE_URI.'/skin_'.$blog_id.'.css', array('theme-style'), false, 'all');
	}else{
		wp_enqueue_style('theme-skin', THEME_CACHE_URI.'/skin.css', array('theme-style'), false, 'all');
	}
	wp_enqueue_style('wp-style', THEME_URI . '/style.css',false, false, 'all');
	wp_register_style('mediaelementjs-styles', THEME_URI.'/mediaelement/mediaelementplayer.css', false, false, 'all');
	wp_register_style('css-feature-carousel', THEME_CSS . '/feature-carousel.css');
	wp_register_style('css-nivo', THEME_CSS . '/nivo-slider.css');
	wp_register_style('css-gallery', THEME_CSS . '/jquery.ad-gallery.css');

$introduce_type = 'default';
	$slug='';
	if(is_home()){
		$slug='blog';
		$introduce_type = theme_get_option('blog','introduce_type');
	}elseif(class_exists( 'Woocommerce' ) && is_product()){
		$slug='product';
		$introduce_type = theme_get_option('woocommerce','product_introduce_type');
	}elseif(class_exists( 'Woocommerce' ) && is_product_category()){
		$slug='product_cat';
		$introduce_type = theme_get_option('woocommerce','product_cat_introduce_type');
	}elseif( class_exists( 'Woocommerce' ) && is_shop() ){
		$slug = 'shop';
		$introduce_type = theme_get_option('woocommerce','shop_introduce_type');
	}elseif(!is_archive() && !is_search()){
		global $post;
		$introduce_type = get_post_meta($post->ID, '_introduce_text_type', true);
	}
	if($introduce_type =='default'){
		if(is_singular('portfolio')){
			$slug = 'portfolio';
			$introduce_type = theme_get_option('portfolio','introduce_type');
		}elseif(is_single()){
			$slug = 'single';
			$introduce_type = theme_get_option('blog','single_introduce_type');
		}
		if($introduce_type == 'default'){
			$slug = 'general';
			$introduce_type = theme_get_option('general','introduce');
		}
	}
	if('slideshow' == $introduce_type){
		if($slug =='blog'){
			$type = theme_get_option('blog','slideshow_type');
		}elseif($slug=="single"){
			$type = theme_get_option('blog','single_slideshow_type');
		}elseif($slug=="portfolio"){
			$type = theme_get_option('portfolio','slideshow_type');
		}elseif($slug == 'general'){
			$type = theme_get_option('general','general_slideshow_type');
		}elseif($slug == 'product'){
			$type = theme_get_option('woocommerce','product_slideshow_type');
		}elseif($slug == 'product_cat'){
			$type = theme_get_option('woocommerce','product_cat_slideshow_type');
		}elseif($slug == 'shop'){
			$type = theme_get_option('general','shop_introduce_type');
		}else{
			$type =  get_post_meta($post->ID, '_slideshow_type', true);
		}
		
		theme_generator('slideShowHeader_css',$type);
	}

}
add_action('wp_print_styles', 'theme_enqueue_styles',105);


/*function theme_add_slide_item(){
	$bar_items	= theme_get_option('slideshow','bar_items');
	$bar_items = empty($bar_items) ? '2' : $bar_items;
	echo "\n<script type='text/javascript'>var slideBarItems = ".$bar_items.";</script>";
}
add_filter('wp_head','theme_add_slide_item');*/
if(theme_get_option('font','font_advance') == 'cufon'){
	function theme_add_cufon_script(){
		$fonts = theme_get_option('cufon','loaded');
		if(is_array($fonts)){
			foreach ($fonts as $file){
				wp_register_script($file, THEME_FONT_URI .'/'.$file, array('cufon-yui'));
				wp_print_scripts($file);
			}
		}
		wp_print_scripts('cufon-yui');
	}
	add_filter('wp_head','theme_add_cufon_script');	
}

if(theme_get_option('font','font_advance') == 'fontface'){
	function theme_add_fontface_css(){
		$fonts = theme_get_option('fontface','loaded');
		if(is_array($fonts)){
			foreach ($fonts as $font=>$folder){
				wp_enqueue_style('font|'.$font, THEME_FONTFACE_URI .'/'.$folder.'/stylesheet.css');
			}
		}
	}
	add_filter('wp_print_styles','theme_add_fontface_css');	
}

if(theme_get_option('font','font_advance') == 'gfont'){
	function theme_add_gfont_lib(){
		$http = (!empty($_SERVER['HTTPS'])) ? "https" : "http";
		$fonts = theme_get_option('gfont','used_gfont');
		if(is_array($fonts)){
			foreach ($fonts as $font){
				wp_enqueue_style('font|'.$font,$http.'://fonts.googleapis.com/css?family='.$font);
			}
		}
	}
	add_action("wp_print_styles", 'theme_add_gfont_lib');
}
function theme_set_lightbox_style(){
	$lightbox_theme = theme_get_option('general','lightbox_theme');
	if(empty($lightbox_theme)){
		$lightbox_theme = 'facebook';
	}
	echo "\n<script type='text/javascript'> var themeParam = '".$lightbox_theme."';</script>\n";
}
add_filter('wp_head','theme_set_lightbox_style');
if(theme_get_option('advanced','combine_js')){
	global $theme_combine_js_enqueued; 
	$theme_combine_js_enqueued = false;
	function theme_combine_js($handles){
		if(is_admin()){
			return;
		}
		global $wp_scripts, $theme_combine_js_enqueued;
		if($theme_combine_js_enqueued) return;
		if (! is_a($wp_scripts, 'WP_Scripts')) return;

		$move_bottom = theme_get_option('advanced','move_bottom');
		$combine_scripts = array();
		$queue_unset = array();
		$wp_scripts->all_deps($wp_scripts->queue); 
		foreach ($wp_scripts->to_do as $key => $handle) {
			$src = $wp_scripts->registered[$handle]->src;
			if (substr($src, 0, 4) != 'http') {
				$src = site_url($src);
				$external = false;
			} else {
				$home = get_option('home');
				if (substr($src, 0, strlen($home)) == $home) {
					$external = false;
				} else	{
					$external = true;
				}
			}
			if(!$external && $handle!='jquery'&& $handle!='woocommerce'&& $handle!='wc_price_slider'){
				$combine_scripts[$handle] = $src;
				unset($wp_scripts->to_do[$key]);
				$queue_unset[$handle] = true;
			}
		}
		foreach ($wp_scripts->queue as $key => $handle) {
			if (isset($queue_unset[$handle])){
				if(!in_array($handle, $wp_scripts->done, true)){
					$wp_scripts->done[] = $handle;
				}
				unset($wp_scripts->queue[$key]);
			}
		}

		$fileId = 0;
		foreach($combine_scripts as $handle => $src){
			$path = ABSPATH . str_replace(get_option('siteurl').'/', '', $src);
			$fileId += @filemtime($path);
		}

		$cache_name = md5(serialize($combine_scripts).$fileId);
		$cache_file_path = THEME_CACHE_DIR . '/' .$cache_name .'.js';
		$cache_file_url = THEME_CACHE_URI . '/' .$cache_name .'.js';

		if(!is_readable($cache_file_path)){
			$content = '';
			foreach($combine_scripts as $handle => $src){
				$content .= "/* $handle: ($src) */\n";
				$content .= @file_get_contents($src). "\n\n\n\n";;
			}
			if (is_writable(THEME_CACHE_DIR)) {
				$fhandle = @fopen($cache_file_path, 'w+');
				if ($fhandle) fwrite($fhandle, $content, strlen($content));
			}
		}
		wp_enqueue_script($cache_name, $cache_file_url,array(),false,$move_bottom);
		$theme_combine_js_enqueued = true;
	}
	add_action('wp_print_scripts', 'theme_combine_js',200);
}

if(theme_get_option('advanced','combine_css')){
	function theme_combine_css($handles){
		if(is_admin()){
			return;
		}
		global $wp_styles;
		if (! is_object($wp_styles)) return;
		$combine_styles = array();
		$queue_unset = array();
		$wp_styles->all_deps($wp_styles->queue); 
		foreach ($wp_styles->to_do as $key => $handle) {
			$media = ($wp_styles->registered[$handle]->args ? $wp_styles->registered[$handle]->args : 'screen');
			$src = $wp_styles->registered[$handle]->src;
			if (substr($src, 0, 4) != 'http') {
				$src = site_url($src);
				$external = false;
			} else {
				$home = get_option('home');
				if (substr($src, 0, strlen($home)) == $home) {
					$external = false;
				} else	{
					$external = true;
				}
			}
			if(!$external){
				$combine_styles[$media][$handle] = $src;
				unset($wp_styles->to_do[$key]);
				$queue_unset[$handle] = true;
			}
		}
		foreach ($wp_styles->queue as $key => $handle) {
			if (isset($queue_unset[$handle])){
				if(!in_array($handle, $wp_styles->done, true)){
					$wp_styles->done[] = $handle;
				}
				unset($wp_styles->queue[$key]);
			}
		}
		foreach ($combine_styles as $media => $styles) {
			$fileId = 0;
			foreach($styles as $handle => $src){
				$path = ABSPATH . str_replace(get_option('siteurl').'/', '', $src);
				$fileId += @filemtime($path);
			}

			$cache_name = md5(serialize($combine_styles).$fileId);
			$cache_file_path = THEME_CACHE_DIR . '/' .$cache_name .'.css';
			$cache_file_url = THEME_CACHE_URI . '/' .$cache_name .'.css';

			if(!is_readable($cache_file_path)){
				$content = '';
				foreach($styles as $handle => $src){
					$content .= "/* $handle: ($src) */\n";
					$content .= @file_get_contents($src). "\n\n";;
				}
				if (is_writable(THEME_CACHE_DIR)) {
					$fhandle = @fopen($cache_file_path, 'w+');
					if ($fhandle) fwrite($fhandle, $content, strlen($content));
				}
			}
			wp_enqueue_style(THEME_SLUG.'-styles-'.$media, $cache_file_url, false, false, $media);
		}
	}
	add_action('wp_print_styles', 'theme_combine_css',200);
}
