function VehicleFixer_Engine() {
  this.majorVersion = "0";
  this.minorVersion = "0.1";
  this.isLoaded = false;
  this.instances = new Array();

  // Browser check
  var ua = navigator.userAgent;
  this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
  this.isMSIE5 = this.isMSIE && (ua.indexOf('MSIE 5') != -1);
  this.isMSIE5_0 = this.isMSIE && (ua.indexOf('MSIE 5.0') != -1);
  this.isGecko = ua.indexOf('Gecko') != -1;
  this.isSafari = ua.indexOf('Safari') != -1;
  this.isOpera = ua.indexOf('Opera') != -1;
  this.isMac = ua.indexOf('Mac') != -1;
  this.isNS7 = ua.indexOf('Netscape/7') != -1;
  this.isNS71 = ua.indexOf('Netscape/7.1') != -1;

  // Fake MSIE on Opera and if Opera fakes IE, Gecko or Safari cancel those
  if (this.isOpera) {
    this.isMSIE = true;
    this.isGecko = false;
    this.isSafari =  false;
  }

  // Widget id instance counter
  this.idCounter = 0;

}


VehicleFixer_Engine.prototype = {

  init : function(settings) {
    if (!this.configs) this.configs = [];
    if (!settings) settings = [];
    this.settings = settings;
    
    // Check if valid browser has execcommand support
    if (typeof(document.execCommand) == 'undefined')
      return;

    // Get script base path
    if (!vfxr.baseURL) {
      var elements = document.getElementsByTagName('script');

      for (var i=0; i<elements.length; i++) {
        if (elements[i].src && (elements[i].src.indexOf("vehiclefixer.js") != -1)) {
          var src = elements[i].src;
          src = src.substring(0, src.lastIndexOf('/'));
          vfxr.baseURL = src;
          break;
        }
      }
    }

    // Get document base path
    this.documentBasePath = document.location.href;
    if (this.documentBasePath.indexOf('?') != -1)
      this.documentBasePath = this.documentBasePath.substring(0, this.documentBasePath.indexOf('?'));
    this.documentURL = this.documentBasePath;
    this.documentBasePath = this.documentBasePath.substring(0, this.documentBasePath.lastIndexOf('/'));

    // If not HTTP absolute
    if (vfxr.baseURL.indexOf('://') == -1 && vfxr.baseURL.charAt(0) != '/') {
      // If site absolute
      vfxr.baseURL = this.documentBasePath + "/" + vfxr.baseURL;
    }

    // If not super absolute make it so
    var baseHREF = this.documentURL;
    var h = document.location.href;
    var p = h.indexOf('://');
    if (p > 0 && document.location.protocol != "file:") {
      p = h.indexOf('/', p + 3);
      h = h.substring(0, p);

      if (baseHREF.indexOf('://') == -1)
        baseHREF = h + baseHREF;
    }

    // Trim away query part
    if (baseHREF.indexOf('?') != -1)
      baseHREF = baseHREF.substring(0, baseHREF.indexOf('?'));

    this.settings['base_href'] = baseHREF.substring(0, baseHREF.lastIndexOf('/')) + "/";

    // Save away this config
    settings['index'] = this.configs.length;
    this.configs[this.configs.length] = settings;
  },

  addWidget : function(q) {
    var id = "vehicle_fixer_widget_" + vfxr.idCounter++;
    var inst = new VehicleFixer_Widget(vfxr.settings);

    if (typeof(doc) == "undefined")
      doc = document;

    if (typeof(win) == "undefined")
      win = window;
    this.instances[id] = inst;
    inst.id = id;
    inst.insert(doc);
    inst.fetch(q);
    this.selectedInstance = inst;
  },
  
  execCommand : function(command, user_interface, value) {
    // Default input
    user_interface = user_interface ? user_interface : false;
    value = value ? value : null;

    if (vfxr.selectedInstance)
      vfxr.selectedInstance.switchSettings();

    if (this.selectedInstance) {
      this.selectedInstance.execCommand(command, user_interface, value);
    } 
  },

  trim : function(s) {
    return s.replace(/^\s*|\s*$/g, "");
  }
}

function VehicleFixer_Widget(settings) {
  this.isVehicleFixer_Widget = true;
  this.settings = settings;
}

VehicleFixer_Widget.prototype = { 
  init : function(settings) {
    if (!settings) settings = [];
    this.settings = settings;    
    this.id = '';
    this.current = 0;
    this.videos = [];
  },

  left : function() {
    this.current = this.current > 0 ? this.current - 1 : 0;
    this.update();
  },
  
  right : function() {
    this.current = this.current < this.videos.length - 4 ? this.current + 1 : this.videos.length - 4;
    this.update();
  },

  fetch : function(q) {
    // Go get the ids
    vfxr.selectedInstance = this;
    this.lastQuery = q;
    Effect.Appear("vehiclefixer_widget_indicator_" + this.id);
    try {
      new Ajax.Request('http://vehiclefixer.com/search?q=' + encodeURIComponent(q), {
        method: 'get', 
        crossSite: true,
        onSuccess: function(transport) {
          eval(transport.responseText);
          inst = vfxr.selectedInstance;
          inst.videos = videos;
          inst.titles = titles;
          inst.links = links;
          inst.current = 0;          
          rss = $('vehiclefixer_widget_rss_' + inst.id);
          rss.innerHTML = (!inst.lastQuery || inst.lastQuery == '') ? "Find your video" : "Found " + inst.videos.length + " videos for " + inst.lastQuery;
          Effect.Appear(rss);
          if (!vfxr.isMSIE) Effect.Pulsate(rss);
          Element.show(rss);
          inst.update();
        },  
        onComplete: function() {
          inst = vfxr.selectedInstance;
          Element.hide("vehiclefixer_widget_indicator_" + inst.id);
        }
      });
    } catch(e) {
      Element.hide("vehiclefixer_widget_indicator_" + inst.id);
    }
  },
  
  insert : function(doc) {
    doc.write(
    '<div class="vehiclefixer_widget" id="' + this.id + '">'+  
              '<div class="vehiclefixer_indicator" style="display:none;" id="vehiclefixer_widget_indicator_' + this.id + '"><img src="http://vehiclefixer.com/images/indicator4.gif" border="0" /></div>' + 
    '<div class="vehiclefixer_button_left" id="vehiclefixer_widget_button_left_' + this.id + '"><a href="javascript:void(0);" onclick="vfxr.instances[\'' + this.id + '\'].left();"><img src="http://vehiclefixer.com/images/left.png" border="0" style="display:none;"/></a></div>'+  
    '<div id="vehiclefixer_widget_thumbs_' + this.id + '">'+
    '<div class="vehiclefixer_thumb"><a href="javascript:void(0);"><img width="144" height="108" style="display:none;" /></a><div></div></div>'+  
    '<div class="vehiclefixer_thumb"><a href="javascript:void(0);"><img width="144" height="108" style="display:none;" /></a><div></div></div>'+  
    '<div class="vehiclefixer_thumb"><a href="javascript:void(0);"><img width="144" height="108" style="display:none;" /></a><div></div></div>'+  
    '<div class="vehiclefixer_thumb"><a href="javascript:void(0);"><img width="144" height="108" style="display:none;" /></a><div></div></div>'+  
    '</div>'+
    '<div class="vehiclefixer_button_right" id="vehiclefixer_widget_button_right_' + this.id + '"><a href="javascript:void(0);" onclick="vfxr.instances[\'' + this.id + '\'].right();"><img src="http://vehiclefixer.com/images/right.png" border="0" style="display:none;"/></a></div>'+  
    '<div id="vehiclefixer_widget_rss_' + this.id + '" class="vehiclefixer_rss">Find your video</div></div>');  
//    '<div class="vehiclefixer_embed">Embed this widget in your blog or auction</div>'+  
  },
  
  update : function() {
    var container = this.div();    
    var images = $('vehiclefixer_widget_thumbs_' + this.id).getElementsByTagName('IMG');
    var index = this.current;
    var pos = 0;
    if (images.length != 4) return;    
    while (pos < 4) {
      if (index + pos < this.videos.length) {
        images[pos].src = "http://frame.revver.com/frame/144x108/" + this.videos[index+pos] + ".jpg";
        par = images[pos].parentNode;
        par.href= "http://vehiclefixer.com/videos/" + this.links[index+pos];
        par.target= "_blank";
        sib = par.nextSibling;
        sib.innerHTML = '<a href="http://vehiclefixer.com/videos/' + this.links[index+pos] + '" target="_blank">' + this.titles[index+pos] + '</a>';
        Element.show(images[pos]);
      } else {
        if (!vfxr.isMSIE) Element.hide(images[pos]);
        images[pos].src = null;
        par = images[pos].parentNode;
        par.href= 'javascript:void(0)';
        sib = par.nextSibling;
        sib.innerHTML = '';
      }     
      pos++;
    }
    try {
    
      var button_left = $("vehiclefixer_widget_button_left_" + this.id);
      var button_right = $("vehiclefixer_widget_button_right_" + this.id);
      button_left = button_left.firstChild.firstChild
      button_right = button_right.firstChild.firstChild

      if (this.current == 0) {
        button_left.src = null;
        Element.hide(button_left);
      } else {
        button_left.src = "http://vehiclefixer.com/images/left.png";
        Effect.Appear(button_left);
      }

      if (this.current + 4 >= this.videos.length) {
        button_right.src = null;
        Element.hide(button_right);
      } else {
        button_right.src = "http://vehiclefixer.com/images/right.png";
        Effect.Appear(button_right);
      }

    } catch(ex) {
      alert(ex);
    }
  },
  
  div : function() {
    return $(this.id);
  }
}

// Global instances
var VehicleFixer = VehicleFixer_Engine; // Compatiblity with gzip compressors
var vfxr = new VehicleFixer_Engine();



