var h_timer, active_div = '', googlemap, glat, glng, banners;
jQuery(window).load(function(){
	dealer_init();
});

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

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");
      });
    });
  } 
  if(jQuery('div.headerflash').length){
    jQuery(document).ready(function(){
      jQuery('div.headerflash').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(){
	on_hover();
	banner_height();
	photo_gallery();
	car_gallery();
	car_tabs();
	if($('#gallery_list').length) $('#gallery_list a[rel=image]').lightBox();
	if($('form').length) form_extras();
	nav_height();
	color_swatch();
	if(jQuery('.comparison_table').length) jQuery('.comparison_table').table_highlight();
	if(jQuery('#car_grid').length) jQuery('#car_grid').expandinggrid({"grid_items":"grid_item", "expand_by":2.1, "columns":5, 'grid_space':15});
	
}

/** twitter **/
function load_twitter(){
	$('#twitter').fadeTo('fast', 0.5, function(){
		$.ajax({
			type:'POST',
			url:'/_latest_tweet.ajaxed',
			timeout: 1000,
			success:function(response){
				if(response.length) $('#twitter').replaceWith(response);
        setTimeout("load_twitter();", 10000);
			},
			error:function(){
				$('#twitter').fadeTo('fast', 1);
				setTimeout("load_twitter();", 10000);
			}
		});
	});	
}


function color_swatch(){
  $('a.rgb_block').each(function(){
    $(this).css('background-color', $(this).attr('rel'));
  });
  $('a.rgb_block').click(function(){return false;});
  if(typeof(banner_file) != "undefined") $('#banner').css('background-image', "url('"+banner_file+"')");
}


/** NAV HEIGHT **/
function nav_height(){
	if($('#tertiary_navigation').length){
		var nv_height = $('#tertiary_navigation').height();
		if($('#list_content').length && $('#list_content').height() > nv_height) $('#tertiary_navigation').css({height:$('#list_content').height()+'px'});
		else if($('#page_content').length && $('#page_content').height() > nv_height) $('#tertiary_navigation').css({height:$('#page_content').height()+'px'});			
	}
	
}
/** FORM EXTRAS **/

function form_extras(){
	//car clicks
	$('form .car input[type=checkbox]').each(function(){
		if($(this).attr('checked')) $(this).parent().parent().addClass('solid').fadeTo(100, 1);
	});
	$('form .car').click(function(){
		var that = this;
		$(this).find('input[type=checkbox]').each(function(){
			if($(this).attr('checked')) {
				$(this).attr('checked', '');
				$(that).removeClass('solid');
			}else{
				$(this).attr('checked', 'checked');
				$(that).addClass('solid');
			}
		});
	});
	
	var details_height = $('#form_details').outerHeight(), car_list_height = $('#available_cars').outerHeight();
	if(details_height > car_list_height) $('#available_cars').css({height:(details_height-40)+'px'});
	else if(car_list_height > details_height) $('#form_details').css({height:(car_list_height-40)+'px'});
}

/*** TABS ***/
function car_tabs(){
	if($('#variation_tabs').length){				
		$("#variation_tabs").tabs();
	}
	
}
function car_tab_height(){}
/*** GALLERY ***/
function photo_gallery(){
	if($('#gallery_list').length){
		$('#gallery_list').simplescroller({item:'.gallery_image', visible:5,moveby:1, controlls:'#next_image, #previous_image', hideable:true, hidetrigger:'a.show_gallery', hideatstart:true});
		
		var g_visible = true;
		$('a.show_gallery').effect('pulsate', {times:10}, 2200);
		
		$('a.show_gallery').click(function(){
			if(g_visible){
				$(this).html('Hide Photographs').addClass('visible');
				g_visible = false;
			}else{
				$(this).html('View Photographs').removeClass('visible');
				g_visible = true;
			}
			return false;
		});	
	}else{
		$('a.show_gallery').hide();
	}
	var this_h = this_sh = bheight = 0;
	if($('#view_gallery').length) this_h = $('#view_gallery').outerHeight();
	if($('#colour_swatch').length) this_sh = $('#colour_swatch').outerHeight();
	if($('#banner').length) bheight = $('#banner').outerHeight()
	var mtop = (bheight - this_h), cstop = (bheight - this_sh);
	
	if($('#view_gallery, #colour_swatch').length){
		if($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0") ){
			$('#view_gallery').css({'margin-top':(mtop)+'px'});
			$('#colour_swatch').css({'margin-top':(cstop - (this_h/2)-2)+'px'});
			banner_height(30);
		}else{
			$('#view_gallery').css({'margin-top':mtop+'px'});
			$('#colour_swatch').css({'margin-top':cstop+'px'});
			banner_height(30);
		}
	}
	
}

function car_gallery(){
	if($('#car_list').length){
	  if($('#banner').hasClass('banner-')) $('#car_list').simplescroller({item:'.car', visible:5,moveby:1, controlls:'#next, #previous', start_at:6});
		else $('#car_list').simplescroller({item:'.car', visible:5,moveby:1, controlls:'#next, #previous'});
	}
}

/*** BANNER HEIGHT  - to please all those designers .. ***/
function banner_height(negitive_modify){
  var copy_h = extra_h = 0;
  var min_height=260;
	if($('.banner_copy').length || $('.banner_content').length){
		if($('.banner_copy').length) copy_h = $('.banner_copy').outerHeight();
		if($('.banner_content').length) extra_h = $('.banner_content').outerHeight();
		if(extra_h > copy_h){
			if(negitive_modify) extra_h -= negitive_modify;
			$('.banner_copy').css('height', extra_h+'px');
		}
		if(!negitive_modify && min_height > extra_h && min_height > copy_h){
			$('.banner_copy').css('height', min_height+'px');
		}
	}
}

/*** CAR FADE ***/
function on_hover(){
	$('div.car, div.gallery_image').hover(
		function(){
			active_div = this.id;
			h_timer = setTimeout("div_fade();", 300);
		},
		function(){
			clear_car_effects();
		}
	);	
}

function div_fade(){
	$('div.car, div.gallery_image').each(function(){
		if(this.id != active_div && !$(this).hasClass('solid')) $(this).fadeTo(300, '0.5');
		else if(this.id == active_div) $(this).fadeTo(300, '1');
	});
	clearTimeout(h_timer);
}

function clear_car_effects(){
	$('div.car,div.gallery_image').stop();
	$('div.car,div.gallery_image').each(function(){
		if(!$(this).hasClass('solid')) $(this).fadeTo(300, '0.8');
	});
	clearTimeout(h_timer);
}

