function init_pop()
{
   $(document).ready(function(){
      var img = new Image();
      var num_images = $("a.pop").size();
      var cur_index = "";
      $("#media_viewer").append("<div id=mod></div><div id=info><div id=txt></div><div id=ctrl><div id=counter></div><a href=# class=prev title=\"Previous\" alt=\"Previous\"><span>Prev</span></a><a href=# class=next title=\"Next\" alt=\"Next\"><span>Next</span></a></div></div>");
      center_viewer();

      $("a.pop").click(function(e){
         e.preventDefault();
         $("#media_viewer").before("<div id=mask></div><div id=close><a href=#><img src=/images/btn-close.png width=20 height=20 border=0 title=\"Close\" alt=\"Close\"></a></div>").fadeIn(function(){
            $("#close").css({
               left: parseFloat($("#media_viewer").css("left"))+$("#media_viewer").width()+"px",
               top: $("#media_viewer").css("top")
            })
         })
         cur_index = $("a.pop").index(this);
         init_viewer(e,cur_index,$(this).attr("href"));
      })

      $("#media_viewer a.next").live('click',function(e){
         cur_index = (cur_index < num_images-1) ? cur_index+1 : 0;
         init_viewer(e,cur_index,$(this).attr("href"));
      })

      $("#media_viewer a.prev").live('click',function(e){
         cur_index = (cur_index > 0) ? cur_index-1 : num_images-1;
         init_viewer(e,cur_index,$(this).attr("href"));
      })

      $("#mask").live('click',function(){
         close_viewer();
      })

      $("#close").live('click',function(e){
         e.preventDefault();
         close_viewer();
      })

      $(window).resize(function(){
         center_viewer();
      })

      $(window).scroll(function(){
         center_viewer();
      })

      function close_viewer()
      {
         $("#close").fadeOut().remove();
         $("#media_viewer").fadeOut(function(){
            $("#mask").fadeOut().remove();
         });
         $("#media_viewer #mod").html("");
      }

      function center_viewer()
      {
         $("#media_viewer").css("left",($(window).width()-$("#media_viewer").width())/2+$(window).scrollLeft()+"px");
         $("#media_viewer").css("top",($(window).height()-$("#media_viewer").height())/2+$(window).scrollTop()+"px");
         $("#close").css("left",($(window).width()+$("#media_viewer").width())/2+$(window).scrollLeft()+"px");
         $("#close").css("top",($(window).height()-$("#media_viewer").height())/2+$(window).scrollTop()+"px");
      }

      function init_viewer(e,index,src)
      {
         e.preventDefault();

         // Get the next and previous images
         var next = (index < (num_images-1)) ? (index+1) : 0;
         var prev = (index > 0) ? (index-1) : (num_images-1);
         $("#media_viewer a.next").attr("href",$("a.pop:eq("+next+")").attr("href"));
         $("#media_viewer a.prev").attr("href",$("a.pop:eq("+prev+")").attr("href"));
         $("#media_viewer #mod").fadeOut().empty();

         var a_temp = document.createElement('a');
         a_temp.href = src;

         // Check if YouTube video
         if (a_temp.hostname == "www.youtube.com")
         {
            $("#media_viewer #mod").html("<div id=\"yt_api_player\"></div>");
            $("#media_viewer").animate({
               width: "640px",
               height: "480px",
               left: ($(window).width()-640)/2+$(window).scrollLeft()+"px",
               top: ($(window).height()-480)/2+$(window).scrollTop()+"px"
            }, function(){
               $(img).fadeIn();
               $("#media_viewer #mod").fadeIn();
            })

            $("#close").animate({
               left: $(window).width()/2+640/2+$(window).scrollLeft()+"px",
               top: $(window).height()/2-480/2+$(window).scrollTop()+"px"
            }).fadeIn();

            var params = { allowScriptAccess: "always", allowFullscreen: "true", bgcolor: "#cccccc" };
            var atts = { id: "my_yt_player", allowfullscreen: "true" };
            swfobject.embedSWF(src+"&amp;autoplay=1&amp;fs=1&amp;border=0&amp;enablejsapi=1&amp;playerapiid=yt_player", "yt_api_player", "640", "440", "8", null, null, params, atts);

         }
         else if (is_video(src))
         {
            $("#media_viewer #mod").html("<div id=\"player\"></div>");
            $("#media_viewer").animate({
               width: "640px",
               height: "410px",
               left: ($(window).width()-640)/2+$(window).scrollLeft()+"px",
               top: ($(window).height()-410)/2+$(window).scrollTop()+"px"
            }, function(){
               $(img).fadeIn();
               $("#media_viewer #mod").fadeIn();
            })

            $("#close").animate({
               left: $(window).width()/2+640/2+$(window).scrollLeft()+"px",
               top: $(window).height()/2-410/2+$(window).scrollTop()+"px"
            }).fadeIn();

            swfobject.embedSWF("/swf/player.swf", "player", "640", "360", "9.0.0", "/js/expressInstall.swf", {f: src}, {bgcolor: "#000000", menu: "false", scale: "noscale", allowScriptAccess: "always", wmode: "opaque"}, {id: "player"});
         }
         else
         {
            $("#media_viewer").prepend("<img src=/images/preloader.gif width=32 height=32 id=pldr>");
            // Load image
            $(img).hide();
            $(img).load(function(){
               $("#media_viewer #mod").html(this);
               $("#media_viewer").animate({
                  width: this.width+"px",
                  height: this.height+32+"px",
                  left: ($(window).width()-this.width)/2+$(window).scrollLeft()+"px",
                  top: ($(window).height()-this.height)/2+$(window).scrollTop()+"px"
               }, function(){
                  $(img).fadeIn();
                  $("#media_viewer #pldr").remove();
                  $("#media_viewer #mod").fadeIn();
               })

               $("#close").animate({
                  left: $(window).width()/2+this.width/2+$(window).scrollLeft()+"px",
                  top: $(window).height()/2-this.height/2+$(window).scrollTop()+"px"
               }).fadeIn();
            })
            $(img).attr('src',src);
         }

         // Load info
         $("#media_viewer #info #txt").html($(".pop:eq("+index+")").children("img").attr("alt"));
//         $("#media_viewer #info #txt").html($(".pop_info:eq("+index+")").html());
         $("#media_viewer #info #counter").html(pad_zeros((index+1),2)+" out of "+pad_zeros((num_images),2));
      }
   })
}

function pad_zeros(num,digits)
{
   var res = num.toString();
   while(res.length < digits) res = "0"+res;
   return res;
}

function get_file_ext(src)
{
   return (/[.]/.exec(src)) ? /[^.]+$/.exec(src) : "";
}

function is_video(src)
{
   return ((get_file_ext(src) == "flv") || (get_file_ext(src) == "f4v"));
}
