<?php

/* Load plugin text domain (localization) */
add_action('init', 'wp_superlike_load_textdomain');
function wp_superlike_load_textdomain() {
	load_plugin_textdomain( 'wp_superlike', false,'/wp-superlike/l10n');
}

add_action('wp_enqueue_scripts','wp_superlike_scripts_styles');

	/* Register and enqueue frontend styles/scripts */
	function wp_superlike_scripts_styles(){
	
		global $wp_superlike;

		wp_enqueue_script( 'jquery');

		/* plugin css */
		wp_register_style( 'wp_superlike',get_template_directory_uri() . '/includes/plugins/wp-superlike/css/wp-superlike.css');
		wp_enqueue_style('wp_superlike');
		
		/* rtl */
		if ($wp_superlike->get_option('rtl') == 1) {
			wp_register_style( 'wp_superlike_rtl',get_template_directory_uri() . '/includes/plugins/wp-superlike/css/wp-superlike-rtl.css');
			wp_enqueue_style('wp_superlike_rtl');
		}
		
		/* google fonts */
		wp_register_style( 'wp_superlike_googlefont', 'http://fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700&subset=latin,latin-ext');
		wp_enqueue_style('wp_superlike_googlefont');

	}
	
/* add dynamic plugin styles */
add_action('wp_head', 'wp_superlike_styles');
add_action('admin_footer', 'wp_superlike_styles'); // for preview

function wp_superlike_styles() {

	global $wp_superlike;
	
	/* load skin or custom colors */
	if ($wp_superlike->get_option('button_style') == 'builtin') {
		$get_style = $wp_superlike->load_skin();
	} else {
		$get_style = $wp_superlike->load_custom_skin();
		$get_style['slug'] = 'default';
	}
	
	/* load font for rtl */
	$font = 'Open Sans';
	$arrow_direction = null;
	if ($wp_superlike->get_option('rtl') == 1) {
		$font = 'Tahoma';
		$arrow_direction = '-rtl';
	}

	print "<style type=\"text/css\">
	
		.wp-superlike {
			font-family: ".$font.";
		}
	
		.wp-superlike-button, .wp-superlike-disabled .wp-superlike-button:hover {
			color: ".$get_style['button_text_color'].";
			border: 1px solid ".$get_style['button_border_color'].";
			background-color: ".$get_style['button_bg_color'].";
			background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(".$get_style['button_bg_color']."), to(".$get_style['button_bg_color2']."));
			background-image: -webkit-linear-gradient(top, ".$get_style['button_bg_color'].", ".$get_style['button_bg_color2']."); 
			background-image: -moz-linear-gradient(top, ".$get_style['button_bg_color'].", ".$get_style['button_bg_color2'].");
			background-image: -ms-linear-gradient(top, ".$get_style['button_bg_color'].", ".$get_style['button_bg_color2'].");
			background-image: -o-linear-gradient(top, ".$get_style['button_bg_color'].", ".$get_style['button_bg_color2'].");
		}
		
		.wp-superlike-button i {
			background-image: url(".get_template_directory_uri() . "/includes/plugins/wp-superlike/images/".$get_style['slug']."/icon.png);
		}
		
		.wp-superlike-button:hover {
			border: 1px solid ".$get_style['button_border_color_hover'].";
			background-color: ".$get_style['button_bg_color_hover'].";
			background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(".$get_style['button_bg_color_hover']."), to(".$get_style['button_bg_color_hover2']."));
			background-image: -webkit-linear-gradient(top, ".$get_style['button_bg_color_hover'].", ".$get_style['button_bg_color_hover2']."); 
			background-image: -moz-linear-gradient(top, ".$get_style['button_bg_color_hover'].", ".$get_style['button_bg_color_hover2'].");
			background-image: -ms-linear-gradient(top, ".$get_style['button_bg_color_hover'].", ".$get_style['button_bg_color_hover2'].");
			background-image: -o-linear-gradient(top, ".$get_style['button_bg_color_hover'].", ".$get_style['button_bg_color_hover2'].");
		}
		
		.wp-superlike-count, .wp-superlike-count-box {
			background-color: ".$get_style['count_bg_color'].";
			color: ".$get_style['count_text_color'].";
			border: 1px solid ".$get_style['count_border_color'].";
		}
		
		.wp-superlike-arrow {
			background: url(".get_template_directory_uri() . "/includes/plugins/wp-superlike/images/".$get_style['slug']."/arrow".$arrow_direction.".png) no-repeat center;
		}
		
		.wp-superlike-arrow-box {
			background: url(".get_template_directory_uri() . "/includes/plugins/wp-superlike/images/".$get_style['slug']."/arrow-down.png) no-repeat center;
		}

		.wp-superlike-disabled .wp-superlike-button {
			opacity: ".$wp_superlike->get_option('disabled_opacity').";
		}
	
	</style>";

}
	
/* add dynamic plugin scripts */
add_action('wp_head', 'wp_superlike_script');
	
	function wp_superlike_script() {
		?>

			<script type="text/javascript">		
			jQuery(document).ready(function($) {
			
				/**
				* @Should fire on every like button click
				* @check IP (previously liked) optional
				* @check registration/login required optional
				*/
				$('.wp-superlike-button').click(function(){
					var button = $(this).parent();
					if ($(this).parent().hasClass('wp-superlike-disabled') == false) {
					post_id = $(this).parent().data('id');
					$.ajax({
						url: '<?php echo PLUGIN_WP_SUPERLIKE_URL.'ajax/like.php'; ?>',
						type: 'post',
						data: {post_id: post_id},
						dataType: 'json',
						success: function(data){
							if (data.show_likes) {
								$('*[data-id="' + post_id + '"]').find('.wp-superlike-count').html(data.show_likes);
								if (data.grey_out) {
									$('*[data-id="' + post_id + '"]').addClass('wp-superlike-disabled');
								}
							} else {
								if (data.alert) {
									button.append(data.alert_text);
									setTimeout(function(){ button.find('.wp-superlike-alert').hide() }, 2000);
								}
								if (data.grey_out) {
									$('*[data-id="' + post_id + '"]').addClass('wp-superlike-disabled');
								}
							}
						},
						error: function(data){
							alert('Ajax could not be loaded.');
						}
					});
					}
				});

			});
			</script>
		
		<?php
	
	}