/*
 * jQuery CareWorks Media Gallery Plugin v 1.0.0
 * http://www.caretech.com
 *
 * Requires:	Nivo Plugin http://nivo.dev7studios.com
 *
 * April 2011
 */

(function ($)
{
  $.fn.CWMediaGallery = function (options)
  {

    //Defaults are below
    var settings = $.extend({}, $.fn.CWMediaGallery.defaults, options);

    return this.each(function ()
    {
      var cwgallery = $(this);
      var mgid = cwgallery.attr("data-mgid");
      if (mgid === undefined || mgid == "")
      {
        mgid = cwgallery.attr("mgid");
      }
      trace("Gallery ID: " + mgid);

      if (mgid == undefined || mgid <= 0)		//get out if we don't have an id
      {
        trace("Gallery ID not Found");
        return this;
      }

      //we have a gallery id - load the xml and attempt to retrieve the data
      //download XML file
      $.ajax({
        type: "GET",
        url: "/upload/MediaGallery/Xml/MediaGallery" + mgid + ".xml", 	//url: "/Upload/MediaGallery/Xml/MediaGallery" + mgid + ".xml",
        dataType: "xml",
        success: function (xml)
        {
          trace("XML Downloaded");
          //Currently just use support for a single image

          $(xml).find("album").each(function ()
          {
            var imgpath = $(this).attr("lgPath");
            trace("Loading images from: " + imgpath);

            //loop through each image and form html to add to slider
            //$("#sliderMG-" + mgid).html("");		//maybe remove this
            $(this).find("img").each(function ()
            {
              //combine these into an object
              var imgsrc = $(this).attr("src");
              var imglink = $(this).attr("link");
              var imgtarget = $(this).attr("target") || "_self";
              var imgpause = $(this).attr("pause");
              var imgtitle = $(this).attr("title");

              trace("Loading Image: " + imgsrc);

              if (imglink != "")
              {
                cwgallery.append("<a href=\"" + imglink + "\" target=\"" + imgtarget + "\"><img src=\"" + imgpath + imgsrc + "\" /></a>");
              } else
              {
                cwgallery.append("<img src=\"" + imgpath + imgsrc + "\" />");
              }
            });
          });

          cwgallery.nivoSlider(settings);

          $(".ppnav").toggle(
					function ()
					{
					  $(this).html(settings.playText);
					  cwgallery.data('nivoslider').stop();
					},
					function ()
					{
					  $(this).html(settings.pauseText);
					  cwgallery.data('nivoslider').start();
					}
				);

        }
      });
    });
  };

  // For debugging
  var trace = function (msg)
  {
    if (this.console && typeof console.log != "undefined")
      console.log(msg);
  }

  //Default settings for nivo slider - can be overriden when loading media gallery
  $.fn.CWMediaGallery.defaults = {
    effect: 'fade',
    slices: 15,
    boxCols: 8,
    boxRows: 4,
    animSpeed: 500,
    pauseTime: 6000,
    startSlide: 0,
    directionNav: false,
    directionNavHide: true,
    controlNav: true,
    controlNavThumbs: false,
    controlNavThumbsFromRel: false,
    controlNavThumbsSearch: '.jpg',
    controlNavThumbsReplace: '_thumb.jpg',
    keyboardNav: false,
    pauseOnHover: true,
    manualAdvance: false,
    captionOpacity: 0.8,
    prevText: 'Prev',
    nextText: 'Next',
    playPauseNav: true,
    playText: 'PLAY',
    pauseText: 'PAUSE',
    beforeChange: function () { },
    afterChange: function () { },
    slideshowEnd: function () { },
    lastSlide: function () { },
    afterLoad: function () { }
  };

})(jQuery);
