﻿      jQuery(document).ready(function ($) {
          $('a[rel*=facebox]').facebox({
              loading_image: '/images/loading.gif',
              close_image: '/images/but_Close.gif'
          });

          $('.mostRecentTabs a').click(function () {
              switch_tabs($(this));
          });

          switch_tabs($('.defaulttab'));

                           
      })

      function switch_tabs(obj) {
          $('.tab-content').hide();
          $('.mostRecentTabs a').removeClass("selected");
          var id = obj.attr("rel");

          $('#' + id).show();
          obj.addClass("selected");
      }
  

    
      function loadUser() {  
          if ($.cookie("finestKindUser") != null) { 
              var UserName = $.cookie("finestKindUser");
              var Pass = $.cookie("finestKindPass");
              $(document).bind('reveal.facebox', function () {
                  $('#facebox #UserName').val(UserName);
                  $('#facebox #Password').val(Pass);
                  $('#facebox #RememberMe').attr('checked', true);
              })
            }
      }

      function KillUserCookie() {
          $.cookie('finestKindUser', null);
          $.cookie('finestKindPass', null);
      }


      function doLogin() { 
          $.ajax({
              type: "POST",
              contentType: "application/json; charset=utf-8",
              url: "/loginUser.asmx/btnLogin_Click",
              data: "{'UserName': '" + $('#facebox').find('#UserName').val() + "', 'password': '" + $('#facebox').find('#Password').val() + "', 'rememberMe': '" + $('#facebox').find('#RememberMe').val() + "'}",
              dataType: "json",
              success: function(msg) {
                  if (msg.d == 'True') {
                      jQuery.facebox('<h4>Success!</h4><br /><div id=loginContainer>Welcome to FinestKind - you are now logged in.</div><div class="footer"><a href="Javascript:void(0)" class="close"><img src="/images/but_Close.gif" onclick="completeSignIn()" style="cursor:pointer;" /></a></div>')
                   } else if (msg.d == 'False') {
                      jQuery.facebox('<h4>Error!</h4><br /><div id=loginContainer>Your username or password was incorrect - please try again.</div><div class="footer"><a href="Javascript:void(0)" class="close"><img src="/images/but_TryAgain.gif" onClick="closepopup()" class="close_image" style="cursor:pointer;" id="retry" /></a></div>')
                  } else if (msg.d == 'No Password Entered') {
                      jQuery.facebox('<h4>Error!</h4><br /><div id=loginContainer>You must enter a password to login!</div><div class="footer"><a href="Javascript:void(0)" class="close"><img src="/images/but_Close.gif" onClick="closepopup()" class="close_image" style="cursor:pointer;" /></a></div>')
                   } else if (msg.d == 'No User name Entered') {
                      jQuery.facebox('<h4>Error!</h4><br /><div id=loginContainer> You must enter a username to login!</div><div class="footer"><a href="Javascript:void(0)" class="close"><img src="/images/but_Close.gif" onClick="closepopup()" class="close_image" style="cursor:pointer;" /></a></div>')
               }
              }
       })

       if ($('#facebox').find('#RememberMe').is(':checked') ){          
           $.cookie("finestKindUser", $('#facebox').find('#UserName').val(), { path: '/' });
           $.cookie("finestKindPass", $('#facebox').find('#Password').val(), { path: '/' });
       }
      }

      function completeSignIn() {
          window.location.reload()
      }

      function closepopup() {    
          $(document).trigger('close.facebox')      
      }
      
  
      function RegisterUser() {
          $.ajax({
              type: "POST",
              url: "/RegisterUser.asmx/createUser",
              data: "{'FirstName': '" + $('#facebox').find('#txt_FirstName').val() + "','LastName':'" + $('#facebox').find('#txt_LastName').val() + "', 'UserName': '" + $('#facebox').find('#txt_UserName').val() + "', 'userPassword': '" + $('#facebox').find('#pwd_password').val() + "', 'Email':'" + $('#facebox').find('#txt_Email').val() + "',  'Phone':'" + $('#facebox').find('#txt_Phone').val() + "'  }",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                  if (msg.d == 'True') {
                      jQuery.facebox('<h4>Success!</h4><br /><div id=loginContainer>You have successfully registered!.<br /><br /><b>Welcome to Finestkind.co.nz!</b><p>Close this dialog and click on "Login" in the top right of the page to login.</p></div><div class="footer"><a href="Javascript:void(0)" class="close"><img src="/images/but_Close.gif" onClick="closepopup()" class="close_image" style="cursor:pointer;" /></a></div>')
                    } else {
                      jQuery.facebox('<h4>Error!</h4><br /><div id=loginContainer>Ops - we hit a problem trying to create your account. Please try again.<br /><br /> <b>The error message returned:</b> ' + msg.d + '</div><div class="footer"><a href="Javascript:void(0)" class="close"><img src="/images/but_Close.gif" onClick="closepopup()" class="close_image" style="cursor:pointer;" /></a></div>')
                  }
              }
          });
      }


      function postComment() {

//          if ($('#PostComment #ctl00_MainContent_control_postComment_commentBody').val() == '') {
//          alert($('#facebox #PostComment textarea#ctl00_MainContent_control_postComment_commentBody').val())
//          }

          if ($('#facebox #PostComment textarea#ctl00_MainContent_control_postComment_commentBody').val() == '') {
              $.facebox('<h4>Error!</h4><br /><div id=loginContainer>Ops - to post your comment you must enter a... well...  comment!<br />Please try again.<br /></div><div class="footer"><a href="Javascript:void(0)" class="close"><img src="/images/but_Close.gif" onClick="closepopup()" class="close_image" style="cursor:pointer;" /></a></div>');
         } else {           
          $.ajax({           
              type: "POST",
              url: "/userComment.asmx/saveUserComment",
                        data: "{'PostId': '" + $('#PostComment').find('#ctl00_MainContent_control_postComment_hid_postID').val() + "','PosterID': '" + $('#PostComment').find('#ctl00_MainContent_control_postComment_hid_PosterID').val() + "','ArticalID':'" + $('#PostComment').find('#ctl00_MainContent_control_postComment_hid_ArticalID').val() + "', 'ArticalType': '" + $('#PostComment').find('#ctl00_MainContent_control_postComment_hid_ArticalType').val() + "','DatePosted': '" + $('#PostComment').find('#ctl00_MainContent_control_postComment_hid_DatePosted').val() + "', 'Comments': '" + $('#facebox #PostComment textarea#ctl00_MainContent_control_postComment_commentBody').val() + "' }",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                  if (msg.d == 'Post saved') {
                      jQuery.facebox('<h4>Success!</h4><br /><div id=loginContainer>Your message has been posted.<br /><p>All comments are subject to moderation and may be removed at Finestkinds discretion.</p></div><div class="footer"><a href="Javascript:void(0)" class="close"><img src="/images/but_Close.gif" onClick="completeSignIn()" class="close_image" style="cursor:pointer;" /></a></div>')
                  } else {
                      jQuery.facebox('<h4>Error!</h4><br /><div id=loginContainer>Ops - we hit a problem while posting your comment. Please try again.<br /><br /> <b>The error message returned:</b> ' + msg.d + '</div><div class="footer"><a href="Javascript:void(0)" class="close"><img src="/images/but_Close.gif" onClick="closepopup()" class="close_image" style="cursor:pointer;" /></a></div>')
                  }
              }
          });
          }
  }

  
      function loadComment(postID, PosterID, ArticalID, ArticalType, DatePosted) {
          $.ajax({
              type: "POST",
              url: "/userComment.asmx/getUserComment",
              data: "{'iUserCommentsId': '" + postID + "'}", 
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (msg) {
                  $('#PostComment').find('#ctl00_MainContent_control_postComment_commentBody').text(msg.d);
                  $('#PostComment').find('#ctl00_MainContent_control_postComment_postID').val(postID);
                  $('#PostComment').find('#ctl00_MainContent_control_postComment_hid_postID').val(postID);
                  $('#PostComment').find('#ctl00_MainContent_control_postComment_hid_ArticalID').val(ArticalID);
                  $('#PostComment').find('#ctl00_MainContent_control_postComment_hid_ArticalType').val(ArticalType);
                  $('#PostComment').find('#ctl00_MainContent_control_postComment_hid_PosterID').val(PosterID);
                  $('#PostComment').find('#ctl00_MainContent_control_postComment_hid_DatePosted').val(DatePosted);
          
                  jQuery.facebox({ div: '#PostComment' });          
              }
          });    


      }


