﻿/**  Always pack this entire file and copy into StorePage-min.js for deployment. **/
/** Drop this onto \projects\Belcaro\scripts\minify.bat and the script will pack it for you **/
    
    var loadingString = "Please wait loading...";

    function resetStars() {
        //alert("StorePage.js, resetStars()");
        $('.inline-rating').show();        
     }
    
    //The APIUrl value should be on the page.
    function GetReviews(sortBy) //0=date desc, 1=rating % desc
    {
      $('#storereviews').css('cursor','wait');
      $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: isAnon,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=GetReviews&mid='+mid+'&c='+useCachedReviews+'&cnt='+maxReviewCount+'&returnUrl='+storeUrl+'&sort='+sortBy+'&storeReviewUrl='+storeReviewUrl,
            success: function(retval)
                    {         //alert('reviews finishing');               
                        $('#storereviews').html(retval);
                        $('#storereviews').css('cursor',"default");                        
                        resetStars(); tb_reinit(); 
                    },
            error: function(e){  
                //alert("error=" + e.responseText);                  
                }
      });
    }       
    
    function toggExpCoupons(e) { $('#expiredCoupons').toggle();}
    
    //The APIUrl value should be on the page.
    function GetExpiredCoupons()
    {    
      $('#container').css('cursor','wait');
      $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: true,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=GetExpiredCoupons&mid='+mid,
            success: function(retval)
                    {   //alert('exp coupons finishing');
                        $('#expiredCoupons').html(retval).show();
                        $('#aOuterDiv').unbind('click');
                        $('#aOuterDiv').removeAttr('onclick');
                        $('#aOuterDiv').bind("click", toggExpCoupons); 
                        $('#container').css('cursor',"default");
                        //tb_reinit(); 
                        resetStars();
                    },
            error: function(e){  
                //alert("error=" + e.responseText);                  
                }
      });
    }       
    
   function bookmarkMe()
    {
       //netscape="Netscape User's hit CTRL+D to add a bookmark to this site."     
        if(window.external && window.external.AddFavorite)
            window.external.AddFavorite(location.href,document.title);
        else if(window.sidebar != 'undefined')
            window.sidebar.addPanel(document.title,location.href,'http://www.shopathome.com');
        else if(window.opera && window.print){ // opera
            var elem = document.createElement('a');
            elem.setAttribute('href',location.href);
            elem.setAttribute('title',document.title);
            elem.setAttribute('rel','sidebar');
            elem.click();
        }
    }                  
    function toggle_visibility(id) {
        $('#'+id).toggle();
    } 
    
    function ReviewPopupCallBack(newPageUrl)
    {
        window.location.replace( newPageUrl );
    }
    
    //The APIUrl value should be on the page.
    function GetFaveCount() 
    {
        $.ajax
          ({
              dataType: 'html',
              async: true,
              cache: false,
              type: 'GET',
              url: ApiUrl + 'webapi/SAHAPI.aspx',
              data: 'method=StoreFavoriteCount&mid=' + mid + '&c=true',
              success: function (retval) {
                  $('#favecount').html(retval);
                  $('#favecount2').html(retval);
                  resetStars();
              },
              error: function (e) {
              }
          });
    }

    function SetFaveObjects() {

        GetFaveCount();
        ToggleVisibility("addFave1");
        ToggleVisibility("remFave1");
        ToggleVisibility("addFave2");
        ToggleVisibility("remFave2");
        ToggleVisibility("litRemove");
        ToggleVisibility("litAdd");
    }

    function ToggleVisibility(objToToggle) {
        if ($("#" + objToToggle).css('display') == 'none') {
            $("#" + objToToggle).css('display', 'inline');
        } else {
            $("#" + objToToggle).css('display', 'none');
        }
    }
        
  function AddFavorite(showReview) {
      $.ajax
          ({
              dataType: 'json',
              async: true,
              cache: false,
              type: 'GET',
              url: ApiUrl + 'webapi/SAHAPI.aspx',
              data: 'method=AddAlert&cid=' + customerId + '&mid=' + mid,
              success: function (json) {
                 if (json.error != null && json.error.length > 0) {
                      //alert("In StorePage.js, AddFavorite ERROR section");
                      $('#favesActionMsg').html(json.error).show();
                      $('#alerterror2').html(json.error).show();
                  }
                  else //success
                  {
                      //alert("In StorePage.js, AddFavorite SUCCESS section");
                      $('#favesActionMsg').html(json.success).show();
                      $('#alerterror2').html(json.success).show();

                      // Post to facebook if the user allows for it
                      post_to_facebook_stream("favorite_store", mid, null, function (result) {
                          // Now allow user to review
                          bIsFavorite = true;
                          if (showReview) {
                              //this is such a hack but can't append params to thickbox url
                              var surl = unescape(storeReviewUrl).replace("&RatingType=", "&fave=t&RatingType=")
                              tb_show('Review This Store', surl);
                          }
                          resetStars();
                      });

                      SetFaveObjects();
                  }
              }
          });
       }

       function RemoveFavorite() {
           $.ajax
          ({
              dataType: 'json',
              async: true,
              cache: false,
              type: 'GET',
              url: ApiUrl + 'webapi/SAHAPI.aspx',
              data: 'method=RemoveAlert&cid=' + customerId + '&mid=' + mid,
              success: function (json) {
                 if (json.error != null && json.error.length > 0) {
                    //alert("In StorePage.js, RemoveFavorite, ERROR");
                    $('#favesActionMsg').html(json.error).show();
                    $('#alerterror2').html(json.error).show();
                }
                else //success
                {
                    //alert("In StorePage.js, RemoveFavorite, SUCCESS");
                    $('#favesActionMsg').html(json.success).show();
                    $('#alerterror2').html(json.success).show();
                    bIsFavorite = false;
                    SetFaveObjects();
                }
              },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                  $('#favesActionMsg').html(XMLHttpRequest.responseText).show();
              }
          });
       }

    function ToggleCouponComments(SpecialsId, MerchantId, SpecialsPostId, LinkClicked)
    {
        if ($('#CouponComments' + SpecialsId).is(":hidden"))
        {
            $('#CouponComments' + SpecialsId).slideDown('slow');
            LinkClicked.innerHTML=LinkClicked.innerHTML.replace(/View/, 'Close');
            
            // If the comments have not been previously loaded... load them now.  Otherwise show comments from previous load.
            if ($('#CouponCommentsControl' + SpecialsId).html().length == 0)
            {
                $('#CouponCommentsControl' + SpecialsId).html(loadingString);
                
                LoadCouponComments(SpecialsId, MerchantId, SpecialsPostId);
            }
        }
        else
        {
            $('#CouponComments' + SpecialsId).slideUp('slow');
            LinkClicked.innerHTML=LinkClicked.innerHTML.replace(/Close/, 'View');
        }
      
        return false;
    }
    
    function ShowCapcha()
    {
        if ($('#Captcha').is(":hidden"))
        {
            $('#Captcha').slideDown('slow');
        }
        return false;
    }
      
    function RefreshCouponComments(SpecialsId, MerchantId, SpecialsPostId)
    {
        $('#CouponCommentsControl' + SpecialsId).html(loadingString);
        if ($('#CouponComments' + SpecialsId).is(":hidden"))
            $('#CouponComments' + SpecialsId).slideDown('slow');
            
        LoadCouponComments(SpecialsId, MerchantId, SpecialsPostId);
        
    }
    function LoadCouponComments(SpecialsId, MerchantId, SpecialsPostId)
    {
        // Load the coupons comments
        $.ajax
          ({
            dataType: 'html',
            async: true,
            cache: isAnon,
            type: 'GET',
            url: ApiUrl + 'webapi/SAHAPI.aspx',
            data: 'method=GetCouponComments&mid='+MerchantId+'&sid='+SpecialsId+'&postid='+SpecialsPostId,
            success: function(retval)
                {
                    $('#CouponCommentsControl' + SpecialsId).replaceWith('<div id="CouponCommentsControl' + SpecialsId + '">'+retval+'</div>');
                    $('#CouponCommentsControl' + SpecialsId).css('cursor',"default");
                },
            error: function(e)
                {
                    $('#CouponCommentsControl' + SpecialsId).replaceWith('<div id="CouponCommentsControl' + SpecialsId + '">Error loading comments. Please check back later. <br /><br />' + e.responseText + '</div>');
                }
        });
    }
