EtsyNameSpace = typeof(EtsyNameSpace)=="undefined" ? {} : EtsyNameSpace;

EtsyNameSpace.Mini = function(id, itemSource, itemSize, rows, columns, idList, ref)
{
  this.id = id;
  this.itemSource = itemSource;
  this.itemSize = itemSize;
  this.rows = rows;
  this.columns = columns;
  this.idList = idList;
  this.ref    = (typeof ref == 'undefined')?'':ref;
}

EtsyNameSpace.Mini.prototype = {
  
  getHeight: function(){return (EtsyNameSpace.Mini.config.style[this.itemSize].height * this.rows) + (this.itemSource=="list" || this.itemSource=="storque" || this.itemSource=="storque_shop" ? 30 : 80)},
  
  getWidth: function(){return (EtsyNameSpace.Mini.config.style[this.itemSize].width * this.columns)},
  
  getFlashDimensions: function(){
    var h,w, dimension;
    
    if (this.itemSize=="gallery")
    {
        w = 184;
        h = 184;

        dimension = {width:w*cols-14,height:h*rows-6 };
    }
    else
    {
        w = 94;
        h = 94;

        dimension = {width:w*cols-2,height:h*rows-2};
    }

    return dimension;
  },
  
  getFrameSource : function(){
    var host = (/\.etsy\.com$/.test(document.location.host) ?
        document.location.host : 'www.etsy.com');
    if(host === 'community.etsy.com' || host === 'team.etsy.com') {
        // FML
        host = 'www.etsy.com';
    }
    return     'http://' + host + '/' + EtsyNameSpace.Mini.config.baseUrl +
                '?' + 
                'item_source=' + this.itemSource +
                '&item_size=' + this.itemSize +
                '&item_rows=' + this.rows +
                '&item_columns=' + this.columns +
                '&ref=' + this.ref +
                ((this.itemSource=='favorites' || this.itemSource=='shop' || this.itemSource=='storque_shop') ? '&user_id=' + this.id : '') +
                ((this.itemSource=='list' || this.itemSource=='storque') ? '&id_list='+this.idList.join() : '') +
                (this.itemSource=='guide' ? '&guide_id=' + this.id : '')
  },
  
  renderIframe: function(){
    var html = '';
    if (this.itemSource !== 'shop') {
        html += '<iframe ALLOWTRANSPARENCY=true style="width: '+this.getWidth()+'px; ' + 
                    'height: '+this.getHeight()+'px;" ' + 
                    'src="'+this.getFrameSource()+'" scrolling="no" frameborder="0">' +
                    '</iframe>';
    } else {
        html = this.getNewIframeData();
    }
    document.write(html);
  },

  // this moves requests for the etsy mini over to the new controller
  getNewIframeData : function() {
     var url = 'http://www.etsy.com/mini.php'
             + '?user_id='+this.id
             + '&image_type='+this.itemSize
             + '&rows='+this.rows
             + '&columns='+this.columns
             + '&featured=false'
             + '&old_mini=true'
             + '&actual_url='+encodeURIComponent(parent.location.href);
     var height = this.getNewHeight(); 
     var width = this.getNewWidth();
     return '<iframe frameborder="0" src="'+url+'" width='+width+' height='+height+'></iframe>';
  },

    getNewHeight : function() {
        var height = 80;
        if (this.isGallery()) {
            return height + (this.rows*EtsyNameSpace.Mini.config.gallery_height);
        } else {
            return height + (this.rows*EtsyNameSpace.Mini.config.thumbnail_height);
        }
    },

    getNewWidth : function() {
        if (this.isGallery()) {
            return this.columns * EtsyNameSpace.Mini.config.gallery_height;
        } else {
            return this.columns * EtsyNameSpace.Mini.config.thumbnail_height;
        }
    },

    isGallery : function() {
        return this.itemSize == 'gallery';
    }
}

EtsyNameSpace.Mini.config = {
  style: {
    thumbnail: {height: 93, width: 98}, 
    gallery: {height: 180, width: 184}
  },
  gallery_height: 190,
  thumbnail_height: 94,
  baseUrl: "etsy_mini_both.php"
};

