<?php
/**
 * The template for displaying the header
 *
 * Displays all of the head element and everything up until the "site-content" div.
 *
 * @package WordPress
 * @subpackage GiveASAP
 * @since GiveASAP 1.0
 */

global $post;

if( ! defined( 'GASAP_URI') ){
	define( 'GASAP_URI', plugin_dir_url( __FILE__ ) );
}

$giveaway = sg_get_giveaway( $post );
$prize    = $giveaway->get_prize_settings();
$display  = $giveaway->get_display_settings();
$text     = $giveaway->get_text_settings();
$general  = $giveaway->get_general_settings();
$giveasap_settings = giveasap_get_settings();
$giveasap_front = new GiveASAP_Front( $post );
$rules_button_text = isset( $giveasap_settings['rules_button'] ) ? $giveasap_settings['rules_button'] : __( 'Show Rules', 'giveasap' );
$ga_schedule = $giveaway->get_schedule();
if( $prize["winners"] == '' ) {
	$prize["winners"] = 1;
}

$giveasap_ended = false;
$giveaway_started = false;

if ( $giveaway->has_started() ) {
    $giveaway_started = true;
}

if( $giveaway->has_ended() ) {
	$giveasap_ended = true;
}

$template_name = $giveaway->get_template_name();
$template_file = $giveaway->get_template_file();

$twitterImage = false;
if( isset( $display['twitter_image'] ) ) {
	$twitterImage = giveasap_get_image_url( $display['twitter_image'] ); 
}

$twitterAccount = '@wpgiveaways';
if( isset( $display['twitter_account'] ) ) {
	$twitterAccount =$display['twitter_account'];
}

$facebookImage = false;
if( isset( $display['facebook_image'] ) ) {
	$facebookImage = giveasap_get_image_url( $display['facebook_image'] ); 
}


$timezone = get_option('gmt_offset');
$timezone_short = giveasap_get_timezone_short( $timezone );
$style_href = $giveaway->get_style_href();
$use_theme = isset( $display['page_layout'] ) && '1' === $display['page_layout'] ? true : false;

if ( ! $use_theme ) {
	setup_postdata( $post );

	$description = get_the_excerpt();
	$description = strip_tags( $description );
	$title = get_the_title();
?>
    <!DOCTYPE html>
    <html <?php language_attributes(); ?> class="no-js">
    <head>
        <meta charset="<?php bloginfo( 'charset' ); ?>">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="profile" href="http://gmpg.org/xfn/11">
        <title><?php echo wp_title(); ?></title>
        <?php
        if ( function_exists( 'wp_site_icon' ) ) {
	        wp_site_icon();
        }
        ?>
        <?php if( $description ) { ?>
            <meta name="description" content="<?php echo $description; ?>" />
        <?php } ?>
        <!-- Schema.org markup for Google+ -->
        <meta itemprop="name" content="<?php echo  $title; ?>">
        <?php if( $description ) { ?>
            <meta itemprop="description" content="<?php echo $description; ?>">
        <?php } ?>

        <!-- Twitter Card data -->
        <meta name="twitter:card" content="summary_large_image">
        <meta name="twitter:site" content="<?php echo esc_attr( $twitterAccount ); ?>">
        <meta name="twitter:title" content="<?php echo $title; ?>">

        <?php if( $description ) { ?>
            <meta name="twitter:description" content="<?php echo $description; ?>">
        <?php } ?>

        <meta name="twitter:creator" content="<?php echo esc_attr( $twitterAccount ); ?>">

        <?php if( $twitterImage ) { ?>

            <!-- Twitter summary card with large image must be at least 280x150px -->
            <meta name="twitter:image:src" content="<?php echo $twitterImage; ?>">

        <?php } ?>

        <!-- Open Graph data -->
        <meta property="og:title" content="<?php  echo $title; ?>" />
        <meta property="og:type" content="article" />
        <meta property="og:url" content="<?php the_permalink(); ?>" />
        <?php if( $facebookImage ) { ?>
            <meta property="og:image" content="<?php echo $facebookImage; ?>" />
        <?php } ?>
        <?php if( $description ) { ?>
            <meta property="og:description" content="<?php echo $description; ?>" />
        <?php } ?>
        <meta property="og:site_name" content="" />
        <link rel="stylesheet" href="<?php echo $style_href; ?>">

        <?php if( $giveasap_front->get_step() == 1 && $giveasap_settings['google_site_key'] ) { ?>
            <script src='https://www.google.com/recaptcha/api.js'></script>
        <?php
            }

            do_action('sg_header' );

            if( isset( $giveasap_settings['head_scripts'] ) && '' != $giveasap_settings['head_scripts'] ) {
                echo wp_unslash( $giveasap_settings['head_scripts'] );
            }

            if( isset( $giveasap_settings['head_styles'] ) && '' != $giveasap_settings['head_styles'] ) {
                echo '<style>' . wp_unslash( $giveasap_settings['head_styles'] ) . '</style>';
            }

            $url = false;

            if( isset( $display['background_image'] ) && absint( $display['background_image'] ) > 0 ) {
                $url = giveasap_get_image_url( $display['background_image'] );
            }

        ?>
        <style>
            body, html {
                height:100%;
                max-height: 100%;
            }

            body {

                <?php if( $url ): ?>
                    background: url('<?php echo $url; ?>') no-repeat center center;
                    background-size:cover;
                    background-attachment: fixed;
                <?php endif; ?>

                <?php if( isset( $display['background_color'] ) && $display['background_color'] != '' ){
                    echo 'background-color: ' . $display['background_color'] . ';';
                } ?>
            }
        </style>
    </head>

    <body <?php body_class(); ?>>
<?php } else {
    $giveasap_front->set_giveaway( $giveaway );
    $giveasap_front->add_wp_header();
    $giveasap_front->enqueue();
    get_header( 'giveaway' );
    setup_postdata( $post );
} ?>

<div class="giveasap sg-step-<?php echo $giveasap_front->get_step(); ?> <?php echo $template_name; ?> <?php if ( $use_theme ) { ?>sg-theme-layout<?php } ?>">

	<?php
		require_once $template_file; 
	?>

</div>
<script type="text/javascript">
    var sg = <?php echo json_encode( gasap()->get_localized_array() ); ?>
</script>
<?php


 if ( ! $use_theme ) {
	 wp_reset_postdata();
 ?>
     <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="   crossorigin="anonymous"></script>
     <script src="<?php echo GASAP_URI . 'assets/js/jquery.plugin.min.js'; ?>"></script>
     <script src="<?php echo GASAP_URI . 'assets/js/jquery.countdown.min.js'; ?>"></script>

     <?php
     $locale = get_locale();

     if( 'en_US' !== $locale && 'en_EN' !== $locale ) {
         if( file_exists( GASAP_ROOT . 'assets/js/l10n/' . $locale . '.js' ) ) {
         ?>

             <script src="<?php echo GASAP_URI . 'assets/js/l10n/' . $locale . '.js'; ?>"></script>

         <?php
         } elseif ( isset( $giveasap_settings['countdown_lang_url'] ) && $giveasap_settings['countdown_lang_url'] ) {
             ?>
            <script src="<?php echo $giveasap_settings['countdown_lang_url']; ?>"></script>
            <?php
	     }
     }

     ?>
     <?php

     if( isset( $giveasap_settings['footer_scripts'] ) ) {
         echo wp_unslash( $giveasap_settings['footer_scripts'] );
     }

     ?>
     <script src="<?php echo GASAP_URI . 'assets/dist/js/giveasap.js'; ?>"></script>
     <?php do_action( 'sg_footer' ); ?>
    </body>
    </html>
<?php } else {
    wp_reset_postdata();
    get_footer( 'giveaway' );
 }