test=$;
var dialog_open=false;

$(document).ready(function() {

  $('a[rel=popup]').each(function(){
        var me=$(this);
        me.attr('popup_href',me.attr('href'));
        me.removeAttr('href');
        me.css('cursor','pointer');
        $.preLoadImages(me.attr('href'));
        
    });
    
    $('a[rel=popup]').click(function(){
        if(dialog_open) return;
        var img=$('<img />').attr('src',$(this).attr('popup_href'))
        var timg = new Image();
        timg.onload = function() {
            img.dialog({
                  title: $('.product_info h1').html(),
                  close:function(){dialog_open=false;},
                  resizable:false,
                  width:this.width,
                  //height:this.height+30,
                  modal:true,
                  position:['center'],
                  open:function(){
                        $(this).css('padding',0);
                        $(this).parent().css('text-align','center');
                        $(this).parent().find('.ui-dialog-title').css({'text-align':'left','overflow':'hidden','height':'1.2em'});
                  }
            });
            dialog_open=true;
        }
        timg.src = $(this).attr('popup_href');
    });
    
    $('img[popup_src]').click(function(){
        if(dialog_open) return;
        $(this).parent('a').removeAttr('href');
        var img=$('<img />').attr('src',$(this).attr('popup_src'));
        
        img.dialog({
            title: $(this).attr('title'),
            close:function(){dialog_open=false;},
            position:['center',50],
            resizable:false,
            width:'auto',
            modal:true
        });
        dialog_open=true;
    })
    .each(function(){
        //preload image
        $.preLoadImages($(this).attr('popup_src'));
    });
});
        
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
      return;
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      console.log('caching image',arguments[i]);
      cache.push(cacheImage);
    }
  }
})(jQuery)


