/*
 * End-user JS class for rendering a ImageShack Syndicate widget.
*/
var ImageShackSyndicateWidget = function(options) {
    this.baseURL = 'http://imageshack.us/syndicate/widget.php?';
    this.widgetURL = null;
    
    // Set the defaults if they aren't defined
    if (options.theme.shell.color === undefined) options.theme.shell.color = '#111';
    if (options.theme.shell.backgroundColor === undefined) options.theme.shell.backgroundColor = '#ccc';
    if (options.theme.shell.backgroundImage === undefined) options.theme.shell.backgroundImage = '';
    if (options.theme.shell.text === undefined) options.theme.shell.text = 'Select photos and videos to upload.';
    if (options.theme.shell.buttonColor === undefined) options.theme.shell.buttonColor = '#99ddff';
    if (options.width === undefined) options.width = 300;
    if (options.height === undefined) options.height = 110;
    
    // Set default width and height
    if (options.width < 300) options.width = 300;
    if (options.width > 900) options.width = 900;
    if (options.height < 110) options.height = 110;
    if (options.height > 300) options.height = 300;
    
    this.URLOptions = new Object();
    this.URLOptions['shellColor'] = encodeURIComponent(options.theme.shell.color);
    this.URLOptions['shellBackgroundColor'] = encodeURIComponent(options.theme.shell.backgroundColor);
    this.URLOptions['shellBackgroundImage'] = encodeURIComponent(options.theme.shell.backgroundImage);
    this.URLOptions['shellText'] = encodeURIComponent(options.theme.shell.text);
    this.URLOptions['width'] = options.width;
    this.URLOptions['height'] = options.height;
    this.URLOptions['buttonColor'] = encodeURIComponent(options.theme.shell.buttonColor);
    
    // Insert and "run" the widget
    this.render = function() {
        this.buildWidgetURL();
        document.write(
            '<iframe src="' + this.widgetURL + '" width="' + options.width + '" height="' + options.height + '" allowtransparency="true" marginwidth="5" marginheight="5" frameborder="0" scrolling="no"></iframe>'
        );
    };
    
    // Dynamically build the widget url from this.URLOptions
    this.buildWidgetURL = function() {
        this.widgetURL = this.baseURL;
        var i = 0;
        for (key in this.URLOptions) {
            if (i == 0) {
                this.widgetURL += key + '=' + this.URLOptions[key];
            } else {
                this.widgetURL += '&' + key + '=' + this.URLOptions[key];
            }
            i++;
        }
    };
};

var showPath = function() {
  document.getElementById('fakeinput').value = document.getElementById('realinput').value;
}

var is_image = function(filename) {
  // If the file is video, change the upload url.
  var image_extensions = "JPG JPEG BMP GIF PNG TIF TIFF ICO";
  var curr_file_extension = filename.substr(filename.length-4, filename.length);
  curr_file_extension = curr_file_extension.replace(".", "").toUpperCase();

  return ( (image_extensions.indexOf(curr_file_extension) != -1)  ? true : false )
}

var upload = function() {
  var file = document.getElementById('realinput');
  var postform = document.getElementById('postform');
  var max = document.getElementById('max');
  if( file.value === undefined || file.value == null || file.value == "" ) {
    alert("Please select a file to upload.");
    return false;
  }
  if( is_image(file.value) ) {
    max.value = "5120000";
    postform.action = "http://post.imageshack.us";
  }
  else {
    max.value = "512000000";
    postform.action = "http://imageshack.us/videoupload.php";
  }
  postform.submit();
}

