var h_timer, 
    active_div = '', 
    googlemap, 
    glat, 
    glng, 
    banners, 
    IE = (jQuery.browser.msie)? true:false, 
    ie6 = (IE && jQuery.browser.version.indexOf("6")==0)?true:false,
    ie7 = (IE && jQuery.browser.version.indexOf("7")==0)?true:false
    ;

jQuery(document).ready(function(){
  flash_offers();
  dealer_init();
});

function flash_offers(){
  if(jQuery('div.flash').length){
    jQuery(document).ready(function(){
      jQuery('div.flash').each(function(){    
        var swfid = this.id.replace("-offer-", "-object-");
        swfobject.registerObject(swfid, "9.0.115", "http://imwww.co.uk/flash/express-install.swf");
      });
    });
  }  
}

function dealer_init(){
  if(jQuery('#google_map').length && GBrowserIsCompatible()) init_gmap();
  
  if(typeof(headerbackgrounds) != "undefined" && headerbackgrounds.length && !ie6) rotate_backgrounds(jQuery('#rotate'), headerbackgrounds, 1);
  
  if(jQuery('#tabbed_list').length) jQuery('#tabbed_list #tabs').tabs();
  
  if(jQuery('form .car_picker .car img').length) click_image_selects_checkbox();
  
  if(jQuery('#car_grid').length) jQuery('#car_grid').expandinggrid({"grid_items":"grid_item", "expand_by":2.3, "columns":5, 'grid_space':15});
  
  if(jQuery('#colour_swatch').length) car_colours();
  
  if(jQuery('#gallery_list').length) jQuery('#gallery_list').simplescroller({item:'.gallery_image', visible:5,moveby:1, controlls:'#next_image, #previous_image', hideable:false, hidetrigger:'a.show_gallery', hideatstart:false});
  
  if(jQuery('#variation_tabs').length) jQuery("#variation_tabs").tabs();
  
  if(jQuery('.comparison_table').length && !ie6 && !ie7) jQuery('.comparison_table').table_highlight();

  if(jQuery('#gallery_list').length) jQuery('#gallery_list a[rel=image]').lightBox();
  
  if(typeof(vehicle_colours) != "undefined" && vehicle_colours.length && !ie6) colour_picker(); 
  
  if(jQuery('#car_listing').length) car_list();
}

function colour_picker(){
  console.log(vehicle_colours);
}
function car_list(){
  jQuery('#car_listing a.large_button').hide();
  jQuery('#car_listing .car').hover(
    function(){jQuery(this).find('a.large_button').show();},
    function(){jQuery(this).find('a.large_button').hide();}
  );
}

function car_colours(){
  jQuery('#colour_swatch a.rgb_block').each(function(){
    jQuery(this).css({'background':jQuery(this).attr('rel'), 'cursor':'default'});
  }).unbind("click").bind("click", function(){return false;});
}

function click_image_selects_checkbox(){
  jQuery('form .car_picker .car img').unbind("click").bind("click", function(){
    var checkboxfield = jQuery(this).parent().parent().find('input.checkbox_field');
    if(checkboxfield.attr('checked') == true) checkboxfield.attr('checked', '');
    else checkboxfield.attr('checked', 'checked');
  });
}
function init_gmap(){
  var gmap = new GMap2(document.getElementById("google_map")), 
      gpoint = new GLatLng(glat, glng),
      dealer_marker = new GMarker(gpoint)
      ;
  gmap.setCenter(gpoint, 13);
  gmap.addOverlay(dealer_marker);
}

function rotate_backgrounds(selector, backgrounds, pos){
  clearTimeout(h_timer);
  var timefunc = function(){
    selector.fadeTo("slow", 0.2, function(){
      if(pos >= backgrounds.length) pos=0; 
      jQuery(this).attr('src', backgrounds[pos]).load(function(){ //not sure if the load will work for ie..
        jQuery(this).css({"filter":''}).fadeTo("slow", 1);
      });
      rotate_backgrounds(selector, backgrounds, (pos+1));
    });
  };
  h_timer = setTimeout(timefunc, 8000);
}
