
/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/
google.load("jquery", "1.4.2");
google.load("jqueryui", "1.8.0");
google.setOnLoadCallback(function () {
  // When everything has loaded, place all polaroids on a random position	


  $(".polaroid").each(function (i) {
    var tempVal = Math.round(Math.random());
    // Internet Explorer doesn't have the "window.innerWidth" and "window.innerHeight" properties
    if (window.innerWidth == undefined) {
      var wiw = 1000;
      var wih = 700;
    } else {
      var wiw = window.innerWidth;
      var wih = window.innerHeight;
    }
    var cssObj = { 'left': Math.random() * (wiw - 200),
      'top': Math.random() * (wih - 200)
    }; // added in case CSS3 is standard
    $(this).css(cssObj);
  });



  // Set the Z-Index (used to display images on top while dragging)
  var zindexnr = 1;

  // boolean to check if the user is dragging
  var dragging = false;

  // Show the polaroid on top when clicked on
  $(".polaroid").mouseup(function (e) {
    if (!dragging) {
      // Bring polaroid to the foreground
      zindexnr++;
      var cssObj = { 'z-index': zindexnr,
        'transform': 'rotate(0deg)',  // added in case CSS3 is standard
        '-webkit-transform': 'rotate(0deg)'
      };  // safari only
      $(this).css(cssObj);
    }
  });

  // Make the polaroid draggable & display a shadow when dragging
  $(".polaroid").draggable({
    cursor: 'crosshair',
    start: function (event, ui) {
      dragging = true;
      zindexnr++;
      var cssObj = { 'box-shadow': '#888 5px 10px 10px', // added in case CSS3 is standard
        '-webkit-box-shadow': '#888 5px 10px 10px', // safari only
        'margin-left': '-10px',
        'margin-top': '-10px',
        'z-index': zindexnr
      };
      $(this).css(cssObj);
    },
    stop: function (event, ui) {
      var tempVal = Math.round(Math.random());
      if (tempVal == 1) {
        var rotDegrees = randomXToY(330, 360); // rotate left
      } else {
        var rotDegrees = randomXToY(0, 30); // rotate right
      }
      var cssObj = { 'box-shadow': '', // added in case CSS3 is standard
        '-webkit-box-shadow': '', // safari only
        'transform': 'rotate(' + rotDegrees + 'deg)', // added in case CSS3 is standard
        '-webkit-transform': 'rotate(' + rotDegrees + 'deg)', // safari only
        'margin-left': '0px',
        'margin-top': '0px'
      };
      $(this).css(cssObj);
      dragging = false;
    }
  });
  $('.polaroid a').click(function () {
    //   alert($(this).attr("id"));
    $(this).parent().fadeTo("fast", 0)
    var BuddyID = $(this).attr("id");
    $(this).fadeTo("fast", 1);
  });


  $('#showAll').click(function () {
    //   alert($(this).attr("id"));
    $('.polaroid').fadeTo("fast", 1)
    //  var BuddyID = $(this).attr("id");
    // $(this).fadeTo("fast", 1);

    //  addToList();


  });

  $('#showPos').click(function () {
    getPosAry()
  });

  $('.delete').click(function () {
    var imageID = $(this).attr("id");
    confirm(imageID);
    hs.close(this)
    __doPostBack('btnDel', '');
  });


  $('#btnSaveIt').click(function () {

    getPosAry();
 //   hs.close();
    __doPostBack('btnSave', '');
  });





  function addToList() {
    // if ($("#drpLists").val() == 0) {
    //  alert('Please select a list');
    // } else {
    var result = "";
    $(".chkRefNos").each(function () {
      if (this.checked == true) {
        alert($(this).val());
        result += $(this).val() + ", ";
      }
    });
    alert(result);
    // };
  }


  function getPos(id) {

    var x = $("#" + id).offset().left;
    var y = $("#" + id).offset().top;

    // alert('x: ' + x + ' y: ' + y);
    return '#t_' + id.replace('d_', '') + '{left:' + x + 'px; top:' + y + "px}"
  }


  function getPosAry() {
    var posArray = ""
    $('.delete').each(function (index) {

      posArray += getPos($(this).attr("id"))

    });

    document.getElementById("preview").innerHTML = posArray;

    $('#hdnCSS').val(posArray);

    // alert(posArray)

  }



  hs.Expander.prototype.onAfterClose = function (sender) {
    var i = this.a.id
    if (i > '') {
      $.ajax({
        type: "GET",
        url: "openClose.ashx",
        data: "id=" + i + "&io=0",
        success: function (i) {
          //alert( "Data Saved: " + i );
        }
      });
    }

  }

  hs.Expander.prototype.onAfterExpand = function (sender) {
    var i = this.a.id
    if (i > '') {
      $.ajax({
        type: "GET",
        url: "openClose.ashx",
        data: "id=" + i + "&io=1",
        success: function (i) {
          //  alert("Data Saved: " + i);
        }
      });
    }

  }



  // Function to get random number upto m
  // http://roshanbh.com.np/2008/09/get-random-number-range-two-numbers-javascript.html
  function randomXToY(minVal, maxVal, floatVal) {
    var randVal = minVal + (Math.random() * (maxVal - minVal));
    return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
  }

});
