<?php
  
  
  require_once($_SERVER['DOCUMENT_ROOT'] . '/common/constants.php');
  include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/sessionStartGeneral.php");
  
  $autoRedirect = false;
  $redirectLink = "";
  
  if(isset($_SESSION['MyStoreAFS']['store_id_share'])){
    foreach(json_decode(AD_VERSIONS) as $adVersion){
      if(in_array($_SESSION['MyStoreAFS']['store_id_share'], $adVersion->stores)){
      $autoRedirect = true;
      $storeName = isset($_SESSION['MyStoreAFS']['store_name']) ? $_SESSION['MyStoreAFS']['store_name'] : "";
      $redirectLink = $adVersion->link;
      //$redirectLink = "http://www.adviewerdemo.com/V87/maceys-version-1";
      
      if(isset($_SESSION['FS_JWT'])){
        $userToken = "?token=" . $_SESSION['FS_JWT'];
      }else{
        $userToken = "";
      }
      
      $redirectLink = $redirectLink . $userToken;
      
      header("Location: " . $redirectLink);
      }
    }
  }

  $pageTitle = "Weekly Ad";
  $pageDescription = "Check out our weekly print advertisement for deals on your favorite grocery items.";
  $pageKeywords = "Weekly Ad, advertisement, specials, deals";
?>
<?php include_once($_SERVER['DOCUMENT_ROOT'] . "/common/header.php") ?>
<?php include_once($_SERVER['DOCUMENT_ROOT'] . "/common/mainNav.php")?>
    
    
    <section class="container">
      <div id="blankBuffer" class="carousel-buffer"></div>
      
      <section class="container" id="mainContent">
        <div class="span12" style="width:95%;">
          <h1>Weekly Print Advertisement</h1><br>
          <div id="locationSelectContainer" class="hideObject">
            <label for="location" style="display: inline-block;font-weight:bold;font-size:16px;">Choose a location: </label>
            <select id="location">
              <option value="0">Choose location to view ad&nbsp;&nbsp;&nbsp;</option>
            </select>
          </div>
          
		  <div id="ieWarnMsg" class="hideObject">
			<div class="alert alert-error">
				  <a class="close" data-dismiss="alert" href="#">x</a>Our AdViewer works best when viewed in Firefox, Chrome or Safari.
			</div>
		  </div>
		  
		  <br>
		  <p class="btn-group"><a id="weeklyAdPageBtn" class="hideObject btn btn-primary btn-large" href="#">View Ad<span id="locName"></span></a></p>
		  
		  <iframe id="weeklyAdHolder" src="https://afsshareportal.com/siteContent/loading.php" width="100%" height="1200px" class="hideObject"></iframe>
        </div><!-- end right side div -->
        
        
        
      </section>
    </section>
      
    
    <?php include_once($_SERVER['DOCUMENT_ROOT'] . "/common/footer.php"); ?>
  <script>
    
	if ($.browser.msie) {
	  $("#ieWarnMsg").removeClass("hideObject");
	}
	
    function getStoreLocations(){
      $.ajax({
          type: "POST",
          url: "/admin/lookUp.php",
          data: "action=getStoreLocations",
          dataType: "json",
          success: function(theResponse){
              var theHTML = "";
              var theCount = theResponse.length;
              
              if(theCount > 1){
                $.each(theResponse, function(){
                  theHTML += "<option value='" + this.store_id_share + "'>" + this.store_name + "</option>";
                });
                
                $("#location").append(theHTML);
                $("#locationSelectContainer").removeClass("hideObject");
              }else{
                setTimeout(function(){
                  $("#location").closest("div").remove();
                  document.location.href = adVersions['version1']['link'];
				  //$("#weeklyAdHolder").attr("src", adVersions['version1']['link']).removeClass("hideObject");
                }, 750);
              }
          }
      });
    }
    
    $("#location").live("change", function(){
      var theChosenId = $(":selected", this).attr("value");
      theChosenId = parseInt(theChosenId);
      
	  var theStoreName = " for " + $(":selected", this).html();
      var theChosenURL = "";
	  
      
      
      if(theChosenId !=0){
        $.each(adVersions, function(){
          if($.inArray(theChosenId, this['stores']) !== -1){
			document.location.href = this['link'];
          }
        });
        
		
		
        //$("#weeklyAdHolder").removeClass("hideObject");
      }
      
    });
    
    getStoreLocations();
    
    $("#rightSidebarCollapse h4").bind("click", function(){
      $this = $(this).children("i");
      $siblings = $(this).parents(".accordion-group").siblings();
      $("h4", $siblings).children("i").removeClass("icon-minus-sign").addClass("icon-plus-sign");
      if($this.hasClass("icon-plus-sign")){
        $this.removeClass("icon-plus-sign").addClass("icon-minus-sign");
      }else{
        $this.removeClass("icon-minus-sign").addClass("icon-plus-sign");
      }
    });
	
	
  </script>
</body>
</html>
