// HUY NGUYEN
// Common javascript used throughout the website

  $j = jQuery.noConflict();

  // PRODUCT PAGES
  function change_image(element, placeholder, folder) {
    var image     = $j(element).attr('src');
    var filename  = image.match(/[\/|\\]([^\\\/]+)$/);
    var new_image = 'layout_images/products/' + folder + '/' + filename[1];

    $j(placeholder).attr('src', new_image);

    return false;
  }

  function switch_src_alt(element) {
    alert(element);
  }

  function view_vehicle(cPath, osCsid) {
    if (cPath != '') {
      if (osCsid != '') location.href = 'store.php?cPath=' + cPath + '&osCsid=' + osCsid;
      else              location.href = 'store.php?cPath=' + cPath;
    }
  }

  // Product Info
  function switch_product_image(element) {
    var src = $j(element).attr('src');
    src = src.split('&', 1);
    $j('#image_p').empty();
    $j('#image_p').append('<img src="' + src + '&w=230&h=200' + '" alt="" title="" />');
    
    // $j('#image_placeholder').attr('src', src + '&w=230&h=200');
  }

  $j(function() {

    $j("#top_navigation a").hover(function() {
      $j(this).addClass('menu_button_hover');
    }, function() {
      $j(this).removeClass('menu_button_hover');
    });

    $j(".links").hover(function() {
      $j(this).removeClass('links');
      $j(this).addClass('links_hover');
    }, function() {
      $j(this).removeClass('links_hover');
      $j(this).addClass('links');
    });


    // Switch src and alt (livequery way)
    $j('.switch_src_alt') 
      .livequery(function(){ 
      // use the helper function hover to bind a mouseover and mouseout event 
        $j(this) 
          .hover(function() { 
            var alt = $j(this).attr('alt');
            var src = $j(this).attr('src');

            $j(this).attr('alt', src);
            $j(this).attr('src', alt);
          }, function() { 
            alt = $j(this).attr('alt');
            src = $j(this).attr('src');

            $j(this).attr('alt', src);
            $j(this).attr('src', alt);
          }); 
        }, function() { 
          // unbind the mouseover and mouseout events 
          $j(this) 
              .unbind('mouseover') 
              .unbind('mouseout'); 
        }); 


    /*
    $j(".switch_src_alt").hover(function() {
      var alt = $j(this).attr('alt');
      var src = $j(this).attr('src');

      $j(this).attr('alt', src);
      $j(this).attr('src', alt);
    }, function() {
      alt = $j(this).attr('alt');
      src = $j(this).attr('src');

      $j(this).attr('alt', src);
      $j(this).attr('src', alt);
    });
    */
  });


  /* TRIM FUNCTIONS: start (magic method / overloading) */
  String.prototype.trim = function()  { return this.replace(/^\s+|\s+$/g,""); }
  String.prototype.ltrim = function() { return this.replace(/^\s+/,""); }
  String.prototype.rtrim = function() { return this.replace(/\s+$/,""); }
  /* TRIM FUNCTIONS: end */


  /* Clear default form field when user focuses on input */
  function clearField(field) {
    if (field.defaultValue == field.value) field.value = ""
  }

  /* Fill in form with default value if nothing was entered */
  function fillField(field) {
    var entered_value = field.value;
    if (entered_value.trim() == '') field.value = field.defaultValue;
  }

  // convert to array key:value (FOR USE WITH STRING COMPARISONS)
  function oc(a) {
    var o = {};
    for(var i=0;i<a.length;i++) {
      o[a[i]]='';
    }
    return o;
  }

