(function($) {
  Shadowbox.loadSkin('tion', '/stylesheets/shadowbox');
  var language = is_defined("CURRENT_LANGUAGE") ? CURRENT_LANGUAGE : 'pl';
  Shadowbox.loadLanguage(language, '/javascripts/shadowbox/lang');
  Shadowbox.loadPlayer(['flv', 'html', 'iframe', 'img', 'qt', 'swf', 'wmp'], '/javascripts/shadowbox/player');
	
 
  $.fn.setLightbox = function()  {
    return $(this);
  }

  $.reloadShadowbox = function() {
            Shadowbox.clearCache();
            Shadowbox.setup();
            Shadowbox.setup($('a[@rel=lightbox], a[@target=_lightbox]'), $.extend($.ShadowboxOptions));
  };

    function setShadowboxDescription(sb) {
      var description;
      if(sb.el) {
        var descriptionElement = $(sb.el).parent().find(".description");
        if(descriptionElement.length > 0) {
          description = descriptionElement.html();
        } else {
          description = $(sb.el).attr("title") || ""
        }
      } else {
        description = sb.title || ""
      }
      $("#shadowbox_description").html(description);
    }

    $.ShadowboxOptions = {
      flvPlayer: '/flvplayer.swf',
      overlayColor: '#000',
      overlayOpacity: 0.9,
      counterType:'skip',
      continuous: true,
      animSequence: 'sync', 
      displayCounter: true,
      viewportPadding: 30,
      slideshowDelay: 5,
      onOpen: function(sb, gallery) {
        Shadowbox.pause(true);
        
        setShadowboxDescription(sb);
        if(gallery.length && gallery.length > 1) {
          $("#shadowbox_current_photo").html((gallery.indexOf(sb) || 0) + 1);
          $("#shadowbox_photos_length").html(gallery.length);
          $("#shadowbox_slideshow_container").css({position: 'static'});
          $("#shadowbox_nav_info, #shadowbox_slideshow_container").css({top: 0, left: 0});
        } else {
          $("#shadowbox_slideshow_container").css({position: 'absolute'});
          $("#shadowbox_nav_info, #shadowbox_slideshow_container").css({top: -2000, left: -2000});
        }
      },
      onChange: function(sb, gallery, current) {
        setShadowboxDescription(sb);
        $("#shadowbox_current_photo").html(current + 1);
      },
      onClose: function(sb) {
        Shadowbox.pause(true);
      }
    };

  function setShadowbox() {
      Shadowbox.init($.ShadowboxOptions);
      Shadowbox.setup($('a[@rel=lightbox], a[@target=_lightbox]'), $.extend($.ShadowboxOptions));
  }

  $(function() {
    setShadowbox();
  });

function decodeHTML(str) {
  return str.replace(/&apos;/g, "'").replace(/&quot;/g, '"').replace(/&gt;/g, ">").replace(/&lt;/g, "<");
}

  $("[rel^=sbCustomHTML]").livequery('click', function() {
    var htmlMatch = $(this).attr("rel").match(/sbCustomHTML;width=(\d+);height=(\d+);html=(.*)/);
    if(!htmlMatch[3])
      return false;

    var html = decodeHTML(htmlMatch[3]);

    var options = {
      player:     'html',
      title: $(this).attr('title'),
      content:    html
    }
    var height = htmlMatch[2];
    var width = htmlMatch[1];
    if(height != "" && height != "0") 
      options['height'] = height;
    if(width != "" && width != "0") 
      options['width'] = width;
    Shadowbox.open(options);

    return false;
  });

})(jQuery);
