﻿/// <reference path="~/js/jquery-1.3.2.js" />

(function($)
{
  $.fn.iPreviewPopup = function(o)
  {
    return this.each(function()
    {
      new $jp(this, o);
    });
  };

  var defaults = {
    speed: "slow",
    position: "center",
    initCallback: null
  };

  $.iPreviewPopup = function(e, o)
  {
    this.options = $.extend({}, defaults, o || {});
    this.container = $(e);
    this.blnOpen = false;
    this.pictureID = 0;
    this.previewimg = null;
    this.tab_image = null;
    this.tab_dataentry = null;
    this.tab_ecard = null;
    this.tab_ecardconfirmation = null;
    this.tab_comments = null;
    this.ecardrecipientsName = [];
    this.ecardrecipientsEmail = [];
    this.defaultEmailTo = 'Emailadres';
    this.defaultNameTo = 'Naam';

    var self = this;

    $(".closewindow a", this.container).click(function() { self.close(); });
    $(".closewindowinbar a", this.container).click(function() { self.close(); });
    $("a.commentslink", this.container).click(function() { self.showComments(); });

    $("a.ecardlink", this.container).click(function() { self.showECard(); });
    $("a.buttonecardlink", this.container).click(function() { self.showECard(); });
    $("a.buttonimagelink", this.container).click(function() { self.showImage(); });
    $("a.buttoncloselink", this.container).click(function() { self.close(); });
    $("a.sendecard", this.container).click(function() { self.sendECard(); });
    $("a.buttoncommentslink", this.container).click(function() { self.showComments(); });

    $("#button_ecardaddrecipient", this.container).click(function() { self.addRecipient(); });
    $("#preview_photographer", this.container).click(function() { self.showComments(); });

    this.previewimg = $('.popuppreviewimg', this.container);
    this.photographer = $('#preview_photographer', this.container);
    this.city = $('#preview_city', this.container);
    this.description = $('#comments_description', this.container);
    this.greetings = $('#comments_greetings', this.container);
    this.photographerinfo = $('#comments_photographerinfo', this.container);
    this.ecardrecipientcontainter = $('#ecardrecipientcontainer', this.container);

    this.tab_image = $('#preview_image', this.container);
    this.tab_dataentry = $('#preview_dataentry', this.container);
    this.tab_ecard = $('#preview_ecard', this.container);
    this.tab_comments = $('#preview_comments', this.container);
    this.tab_ecardconfirmation = $('#preview_ecardconfirmation', this.container);

    this.tab_ecardconfirmation.hide();
    this.tab_dataentry.hide();
    this.tab_comments.hide();
    this.tab_ecard.hide();

    // define initial position and be aware that parent can be resized...
    this.funcResize = function() { self.resize(); };
    //this.resize();
    //$(window).unbind('resize', this.funcResize).bind('resize', this.funcResize);

    // reposition window after load of image
    $(this.previewimg).load(function()
    {
      self.resize();
    });

    // tell the world about this new born
    if (this.options.initCallback != null)
      this.options.initCallback(this, 'init');
  };

  // Create shortcut for internal use
  var $jp = $.iPreviewPopup;

  $jp.fn = $jp.prototype = {
    iPreviewPopup: '0.1'
  };

  $jp.fn.extend = $jp.extend = $.extend;

  $jp.fn.extend(
  {
    addRecipient: function()
    {
      var strName = $('#pictureecardform_nameto').val();
      var strEmail = $('#pictureecardform_emailto').val();

      if (strName != this.defaultNameTo && strEmail != this.defaultEmailTo && strEmail.indexOf('@') > 0)
      {
        if (this.ecardrecipientsEmail.length < 5)
        {
          this.ecardrecipientsEmail.push(strEmail);
          this.ecardrecipientsName.push(strName);
        }

        var self = this;
        this.ecardrecipientcontainter.html('');
        for (var intCounter = 0; intCounter < this.ecardrecipientsName.length; intCounter++)
        {
          var strHTML = '<div class="ecardrecipient">' + this.ecardrecipientsName[intCounter] + '&nbsp;<a href="#" class="ecardrecipientlink">x</a></div>';
          this.ecardrecipientcontainter.append(strHTML);
        }
        this.ecardrecipientcontainter.append('<div style="clear:both;"></div>');

        $('#pictureecardform_nameto').val(this.defaultNameTo);
        $('#pictureecardform_emailto').val(this.defaultEmailTo);

        $('.ecardrecipientlink').click(function()
        {
          var intIndex = $(".ecardrecipientlink").index(this);
          self.ecardrecipientsEmail.splice(intIndex, 1);
          self.ecardrecipientsName.splice(intIndex, 1);
          $(this).parent().empty();
        });


      }

    },

    close: function()
    {
      if (this.blnOpen)
      {
        this.blnOpen = false;
        this.container.fadeOut("slow");
      }
    },

    open: function(strID, strOption)
    {
      if (strID != undefined)
      {
        var self = this;

        $.ajax(
        {
          method: "get",
          url: "downloadxmlpicture.pp",
          dataType: "xml",
          data: "option=info&id=" + strID,
          success: function(data)
          {
            self.pictureID = parseInt(strID);
            var intOrientation = parseInt($(data).find('orientation').text());
            var intOwner = parseInt($(data).find('owner').text());
            var dblLat = parseFloat($(data).find('y').text());
            var dblLng = parseFloat($(data).find('x').text());

            $("#popuppreviewlinks_placeholder").show();
            $("#popuppreviewlinks").hide();

            if (strOption == "edit" && intOwner == 1)
            {
              $('#dataentry_pictureid').val(strID);

              self.tab_ecardconfirmation.hide();
              self.tab_ecard.hide();
              self.tab_image.hide();
              self.tab_comments.hide();
              self.tab_dataentry.show();
            }
            else
            {
              self.tab_ecardconfirmation.hide();
              self.tab_ecard.hide();
              self.tab_dataentry.hide();
              self.tab_comments.hide();
              self.tab_image.show();
            }

            self.previewimg.attr("src", $(data).find('preview').text());
            self.photographer.html($(data).find('photographer').text());
            self.city.html($(data).find('city').text());
            self.description.html($(data).find('description').text());
            self.greetings.html("Groeten van<br />" + $(data).find('photographer').text());
            self.photographerinfo.html($(data).find('photographerinfo').text());
            self.ecardrecipientcontainter.html('');
            $("#dataentry_description").html('');

            var intOffset;
            if (intOrientation == 2)
              intOffset = 395;
            else
              intOffset = 195;

            map_moveTo(dblLat, dblLng, intOffset);



          },
          error: function() { /*alert('xml error');*/ }
        });

      }

      if (!this.blnOpen)
      {
        this.blnOpen = true;
        this.container.fadeIn("slow");
      }
    },


    resize: function()
    {
      if (this.options.position == "center")
      {
        var intWindowWidth = document.documentElement.clientWidth;
        var intWindowHeight = document.documentElement.clientHeight;
        var intPopupHeight = this.container.height();
        var intPopupWidth = this.container.width();
        //centering
        this.container.css({
          "position": "absolute",
          "top": intWindowHeight / 2 - intPopupHeight / 2,
          "left": intWindowWidth / 2 - intPopupWidth / 2
        });
      }
      this.tab_comments.height(this.tab_image.height());
      this.tab_ecard.height(this.tab_image.height());
      this.tab_ecardconfirmation.height(this.tab_image.height());


    },

    sendECard: function()
    {
      var self = this;
      var strNameFrom = $('#pictureecardform_namesubmitter').val();
      var strEmailFrom = $('#pictureecardform_emailsubmitter').val();
      var strMessage = $('#pictureecardform_message').val();
      var strNameTo = '';
      var strEmailTo = '';

      if ($('#pictureecardform_nameto').val() != this.defaultNameTo && $('#pictureecardform_emailto').val() != this.defaultEmailTo)
      {
        strNameTo = $('#pictureecardform_nameto').val();
        strEmailTo = $('#pictureecardform_emailto').val();
      }

      for (var intCounter = 0; intCounter < this.ecardrecipientsName.length; intCounter++)
      {
        if (strNameTo != '')
          strNameTo = strNameTo + ';';
        strNameTo = strNameTo + this.ecardrecipientsName[intCounter];

        if (strEmailTo != '')
          strEmailTo = strEmailTo + ';';
        strEmailTo = strEmailTo + this.ecardrecipientsEmail[intCounter];
      }

      if (strNameTo != '')
      {
        var strData = 'namefrom=' + strNameFrom + '&emailfrom=' + strEmailFrom + '&nameto=' + strNameTo + '&emailto=' + strEmailTo + '&message=' + strMessage;

        $.ajax({
          type: "POST",
          url: "downloadxmlpicture.pp?option=sendecard&id=" + self.pictureID,
          data: strData,
          dataType: "xml",
          success: function(data)
          {
            self.showECardConfirmation($(data).find('data').text());
          }
        });
      }
    },

    showComments: function()
    {
      this.tab_ecardconfirmation.hide();
      this.tab_ecard.hide();
      this.tab_dataentry.hide();
      this.tab_image.hide();
      this.tab_comments.show();
      var strID = this.pictureID;

      $.ajax(
        {
          method: "get",
          url: "downloadxmlpicture.pp",
          dataType: "xml",
          data: "option=comments&id=" + strID,
          success: function(data)
          {
            $("#comments_picturecomments").html($(data).find('data').text())
          },
          error: function() { /*alert('xml error');*/ }
        });

    },

    showECardConfirmation: function(strMessage)
    {
      $("#ecardconfirmation_message").html(strMessage);

      this.tab_comments.hide();
      this.tab_dataentry.hide();
      this.tab_image.hide();
      this.tab_ecard.hide();
      this.tab_ecardconfirmation.show();


    },

    showECard: function()
    {
      this.tab_ecardconfirmation.hide();
      this.tab_comments.hide();
      this.tab_dataentry.hide();
      this.tab_image.hide();
      this.tab_ecard.show();
    },

    showImage: function()
    {
      $("#popuppreviewlinks_placeholder").show();
      $("#popuppreviewlinks").hide();

      this.tab_ecardconfirmation.hide();
      this.tab_comments.hide();
      this.tab_dataentry.hide();
      this.tab_ecard.hide();
      this.tab_image.show();
    }

  });
})(jQuery);