(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

jQuery.preLoadImages("images/obdReader_large.jpg",
"images/proseries_large.jpg", "images/advancedScanner_large.jpg", "images/canOBD_large.jpg", "images/canProseries_large.jpg", "images/vwaudi_large.jpg", "images/vwAudiCan_large.jpg", "images/superVWaudi_large.jpg", "images/327com_large.jpg", "images/elm327usb_large.jpg", "images/bluetooth_large.jpg", "images/vagCommander_large.jpg", "images/bmwService_large.jpg", "images/bmwService2_large.jpg", "images/10in1_large.jpg", "images/superVWaudi_large.jpg", "images/tg3_large.jpg");

$(document).ready(function() {
	
	//When you click on a link with class of poplight and the href starts with a # 
$('a.poplight').click(function() {
    var popID = $(this).attr('rel'); //Get Popup Name
    //var popURL = $(this).attr('href'); //Get Popup href to define size

    //Pull Query & Variables from href URL
    //var query= popURL.split('?');
    //var dim= query[1].split('&');
    //var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().prepend('<a href="#" class="close"><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
    
    $('#popup_img').attr('src',$('img',$(this).closest('div')).attr('src').replace(/_thumb/i, "_large"));
    $('#popup_name h3').text($('.productName',$(this).closest('div')).text());

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
});

//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
    $('#fade , .popup_block').fadeOut(function() {
        $('#fade, a.close').remove();  //fade them both out
    });
    return false;
});

});
