/**
 * Copyright 2006 So-net Entertainment Corporation
 */
var sonetphotoUserVars = {};
for (var i = 0; i < SonetPhotoBadgeUtils.KEYS.length; i++) {
  var key = SonetPhotoBadgeUtils.KEYS[i];
  sonetphotoUserVars[key] = SonetPhotoBadgeUtils.getUserSetValue(key);
}

/**
 * So-net Photo Badge class
 */
function SonetPhotoBadge() {
  this.name = 'sonetphotobadge' + SonetPhotoBadgeUtils.getBadgeCount();
  this.userVars = sonetphotoUserVars;
  return this;
}

SonetPhotoBadge.VERSION = '1_0';
SonetPhotoBadge.SWF_FILES = {
  'thumbnail_36' : '/photo/swf/badge/thumbnail_36',
  'thumbnail_80' : '/photo/swf/badge/thumbnail_80',
  'slideshow' : '/photo/swf/badge/slideshow',
  'full_v' : '/photo/swf/badge/full_v',
  'full_h' : '/photo/swf/badge/full_h',
  'full_d1' : '/photo/swf/badge/full_d1',
  'full_d2' : '/photo/swf/badge/full_d2',
  'menu_v' : '/photo/swf/badge/menu_v',
  'menu_h' : '/photo/swf/badge/menu_h'
};
SonetPhotoBadge.SWF_SIZES = {
  'thumbnail_36' : '156*176',
  'thumbnail_80' : '168*188',
  'full_v' : '190*100%',
  'full_h' : '100%*180',
  'full_d1' : '100%*100%',
  'full_d2' : '100%*100%',
  'menu_v' : '61*145',
  'menu_h' : '150*45'
};
SonetPhotoBadge.DEFAULT_HOST = sonetphoto_default_host; // 'sonetphoto_default_host' is defined in 'sonetphoto_badge_X.X.js' file.
SonetPhotoBadge.KEYS = SonetPhotoBadgeUtils.KEYS;

SonetPhotoBadge.prototype.getUserValue = function(key, defaultValue) {
  if (defaultValue == null) {
    defaultValue = '';
  }
  var value = this.userVars[key];
  if (!value) {
    return defaultValue;
  }
  return value;
}
SonetPhotoBadge.prototype.setUserValue = function(key, value) {
  this.userVars[key] = value;
}

SonetPhotoBadge.prototype.getHost = function() {
  return SonetPhotoBadgeUtils.getHost();
}

SonetPhotoBadge.prototype.getSwfUrl = function(key) {
  var host = this.getHost();
  if (!key) {
    var type = this.getUserValue('type');
    key = type;
    if (type == 'thumbnail') {
      var width = this.getUserValue('img_width');
      key += '_' + width;
    } else if (type == 'full') {
      key = this.getKeyForFullType();
    }
  }
  var swfFilePath = SonetPhotoBadge.SWF_FILES[key];

  if (this.isLocalDebug()) { // for local debug
    var paths = swfFilePath.split('/');
    return paths[paths.length-1];
  }
  return host + swfFilePath;
}

SonetPhotoBadge.prototype.isLocalDebug = function() {
  return this.getUserValue('local');
}

SonetPhotoBadge.prototype.getSwfSizes = function(key) {
  if (!key) {
    var type = this.getUserValue('type');
    var width = this.getUserValue('img_width');
    if (type == 'slideshow') {
      var height = parseInt(this.getUserValue('img_height'));
      width = parseInt(width);
      if (height <= 120) {
        return [width+4, height+57];
      } else if (height <= 240) {
        return [width+4, height+59];
      } else {
        return [width+4, height+62];
      }
    }
    var key = type + '_' + width;
  }
  return SonetPhotoBadge.SWF_SIZES[key].split('*');
}

SonetPhotoBadge.prototype.getSwfWidth = function() {
  return this.getSwfSizes()[0];
}

SonetPhotoBadge.prototype.getSwfHeight = function() {
  return this.getSwfSizes()[1];
}

SonetPhotoBadge.prototype.getSwfId = function() {
  return this.name;
}

SonetPhotoBadge.prototype.getFlashVars = function() {
  var sb = '';
  var keys = SonetPhotoBadge.KEYS;
  for (var i = 0; i < keys.length; i++) {
    var value = this.getUserValue(keys[i]);
    if (value != null) {
      if (keys[i] == 'target' && value.split('=')[1] != null) {
        sb += 'target=' + escape(value.split('=')[0]) + '&';
        sb += 'targetValue=' + escape(value.split('=')[1]) + '&';
      } else {
        sb += escape(keys[i]) + '=' + escape(value) + '&';
      }
    }
  }
  if (this.useShade()) {
    sb += "useShade=1&";
  }
  sb += 'lcName=' + escape(this.name);
  return sb;
}

SonetPhotoBadge.prototype.validateVars = function() {
  var valid = this.checkUserVars();
  if (!valid) { this.renderError(); }
  return valid;
}

SonetPhotoBadge.prototype.checkUserVars = function() {
  if (!this.getUserValue('member_name')) { return false; }
  var type = this.getUserValue('type');
  if (type != 'thumbnail' && type != 'slideshow' && type != 'full') {
    this.setUserValue('type', 'thumbnail');
  }
  var target = this.getUserValue('target');
  if (target == null || target.split('=')[0] != 'imagelist' && target.split('=')[0] != 'album' && target.split('=')[0] != 'tag') {
    this.setUserValue('target', 'imagelist');
  } else {
    var tgt = target.split('=')[0];
    if ((tgt == 'album' || tgt == 'tag') && target.split('=')[1] == null) {
      return false;
    }
  }
  var sort = this.getUserValue('sort');
  if (sort != 'new' && sort != 'random') {
    this.setUserValue('sort', 'new');
  }
  var position = this.getUserValue('position');
  if (position != 'absolute' || position != 'relative') {
    this.setUserValue('position', 'absolute');
  }
  return true;
}

SonetPhotoBadge.prototype.renderError = function() {
  alert(SonetPhotoBadgeUtils.MESSAGES['SYNTAX_ERROR']);
}

SonetPhotoBadge.prototype.genSwfTag = function() {
  AC_FL_RunContent(
      'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0',
      'width', this.getSwfWidth(),
      'height', this.getSwfHeight(),
      'src', this.getSwfUrl(),
      'movie', this.getSwfUrl(),
      'id', this.getSwfId(),
      'name', this.getSwfId(),
      'FlashVars', this.getFlashVars(),
      'quality', 'high',
      'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
      'align', 'middle',
      'play', 'true',
      'loop', 'true',
      'scale', 'noscale',
      'devicefont', 'false',
      'wmode', 'transparent',
      'allowScriptAccess','always',
      'menu', 'false'
    ); //end AC code
}

SonetPhotoBadge.prototype.render = function() {
  try {
    if (this.validateVars()) {
      this.genSwfTag();
    }
  } catch (ex) {
    if (this.isLocalDebug()) { throw ex; }
    this.renderError();
  }
}

SonetPhotoBadge.prototype.useShade = function() {
  return false;
}

