filtertime = false;
rotate_img = 0;
rotimer=false;
$(document).ready(function(){
	$('body').z();
	page_init();
});

function page_init(){
	if($('#filter_options').length) content_filter();
	var default_text = 'SEARCH';
	$('.search_field').focus(function(){
		if($(this).val() == default_text) $(this).val('');
	});
	$('.search_field').blur(function(){
		if($(this).val() == '' || $(this).val() == ' ') $(this).val(default_text);
	});
	
	$('a.lightbox').lightbox();
	
	google_map();
	ie6=$.browser.msie&&($.browser.version == "6.0");
	if($('#featured_product .copy img')) rotimer= setTimeout("image_rotator(1)", 5000);
	if($('.filter_span_colours').length) filter_block_colours();
}


function image_rotator(index){
  $('#featured_product .copy img:visible').fadeOut("slow", function(){
    $('#featured_product .copy img:eq('+index+')').fadeIn("slow", function(){
      var nxt = index+1;
      if(nxt >=  $('#featured_product .copy img').length) nxt = 0;
      clearTimeout(rotimer);
      rotimer = setTimeout("image_rotator("+nxt+")", 5000);
    });
  });
}

function google_map(){
  if(jQuery('#google_map').length && GBrowserIsCompatible() ){
    var g_map = new google.maps.Map2(document.getElementById('google_map'));
    g_map.enableScrollWheelZoom();
		g_map.setUIToDefault();
		var local_search = new google.search.LocalSearch();
		local_search.setSearchCompleteCallback(null, function(){		  
		  if(local_search.results[0]){
		    var resultLat = local_search.results[0].lat, resultLng = local_search.results[0].lng;
		    var latLng = new GLatLng(resultLat, resultLng);
        g_map.setCenter(latLng, 15);
        var gicon = new GIcon(G_DEFAULT_ICON);
	      var marker = new GMarker(latLng, {icon:gicon, title:jQuery('h1').text()});		      
	      GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml('<p>Clarke &amp; Strong Ltd<br />'+jQuery('#footer .street-address').html()+'<br />'+jQuery('#footer .locality').html()+'<br />'+jQuery('#footer .region').html()+'<br />'+jQuery('#footer .postal-code').html()+'<br />'+jQuery('#footer .tel').html()+'<br />'+jQuery('#footer .fax').html());
        });
        g_map.addOverlay(marker);
		  }
		});
		local_search.execute(jQuery('#footer .postal-code').html()+', UK');
  }
}

function filter_block_colours(){
  if($('.filter_span_colours').length){
    $('.filter_span_colours').each(function(){
      $(this).css('background', $(this).attr('rel'));
    });
  }
}

function content_filter(){
	$('.filter_input').click(function(){
	  var class_title = $(this).attr('title');
	  clearTimeout(filtertime);
	  filtertime = setTimeout("filter_timed('"+class_title+"')", 900);
		return true;
	});
}

function filter_timed(class_name){
  var form_vals = $('#filter_options').serialize(), 
      form_destination = $('#filter_options').attr('action') + '.ajax';      
	$('#product_page').fadeTo('fast', 0.5).find('h3.'+class_name).addClass('loading');
	
  $.ajax({
		type:"post",
		url:form_destination,
		data: form_vals,
		success: function(response){
			$('#product_page').replaceWith(response);
			page_init();
			clearTimeout(filtertime);
		}
	});
}


function carousel_picker(div_id, visible){
  $('#'+div_id).addClass('active_carousel').jCarouselLite({btnNext: ".next",btnPrev: ".prev", visible:visible, scroll:1, circular: false});    
}