var frameWidth = 0;
var delaytime = 0;
var moverWidth = 0;
var i = 0;
var branches = [
	{
		name : " - Sheen",
		longitude : 51.464908, 
		latitude : -0.263768
	},
	{
		name : " - Battersea",
		longitude : 51.470728, 
		latitude : -0.170867
	},
	{
		name : " - Clapahm",
		longitude : 51.459754, 
		latitude : -0.171318
	}
];
$(function(){
	delaytime = 1500;
	frameWidth = parseFloat($("div#frame").css("width"), 10);
	moverWidth = frameWidth * 7;

	$("div#mover").css("width", moverWidth);
	$("div#home_block").css("left", 0 * frameWidth);
	$("div#tyres_block").css("left", 1 * frameWidth);
	$("div#alloys_block").css("left", 2 * frameWidth);
	$("div#alignment_block").css("left", 3 * frameWidth);
	$("div#mobile_block").css("left", 4 * frameWidth);
	$("div#aboutus_block").css("left", 5 * frameWidth);
	$("div#contact_block").css("left", 6 * frameWidth);

	$("a#email_form").click(function(){
		$("div#mover").animate({"left":frameWidth * -6},delaytime);
		});
	$("a#mobile_read_more").click(function(){
		$("div#mover").animate({"left":frameWidth * -4},delaytime);
		});

	$("ul.nav_bar li a, ul#footer_nav li a, #email_form, #mobile_read_more").each(function(){
		$(this).attr("href", "javascript:void(0)");
	});
	for(var i = 1; i <= 7; i++)
	{
		navClick(i);
		footerClick(i);
	}
	for(var i = 1; i <= 7; i++)
	{
	}
	$("img#right_arrow").click(function(){
		var currentLeft = parseFloat($("div#mover").css("left"), 10);
		if((-1 * currentLeft) < (moverWidth - frameWidth))
		{
			$("div#mover").animate({"left":currentLeft - frameWidth}, delaytime);			
		}
	});
	$("img#left_arrow").click(function(){
		var currentLeft = parseFloat($("div#mover").css("left"), 10);
		if(currentLeft < 0)
		{
			$("div#mover").animate({"left":currentLeft + frameWidth}, delaytime);			
		}
	});
	$("#tel_all .jCarouselLite").jCarouselLite({
		auto: -4900,
		speed: 5000,
		visible : 2
	});
	$(window).scroll(function(){
		var bodyTop = 0;
		if(typeof window.pageYOffset != 'undefined'){
			bodyTop = window.pageYOffset;
		}
		else if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat'){
			bodyTop = document.documentElement.scrollTop;
		}
		else if(typeof document.body != 'underfined'){
			bodyTop = document.body.scrollTop;
		}
		$("#map_bg").css("top", bodyTop);
		$("#map_content").css("top",bodyTop);
	});
	for(var k = 0; k < branches.length; k++)
	{
		googleMap(k);
	}	
	$("#out").click(function(){
			$("#map_content").css("display", "none");
			$("#map_bg").css("display","none");
		});
	$('#message').css('display', 'none');
	$('#email_submit').click(function(){
	//	$('#message').css('display', 'inline');
		//check email
		$emailAddress = $("#email input#email_address").val();
		//alert($emailAddress);
		if(test_email($emailAddress))
		{
			$('#message').html('(Sending ...)');
			$('#message').fadeIn();
			$.post('email.php', $('#email').serializeArray(), function(data){
				if(data.status == 'OK')
				{
					$('#message').html('(Your message has been sent!)');
				}else if(data.status == 'Fail'){
					$('#message').html('(Send Fail!)');
				}else
				{
					$('#message').html('(unknow)');
				}
				$('#message').fadeIn();
				setTimeout('out()', 1000);
			}, 'json');
		}
		else{
			$("#email input#email_address").css("color" , "red");
			$("#email input#email_address").val('Incorrent email format');
			$("#email input#email_address").fadeIn();
		}
		});

})
function googleMap(i){
	var j = i + 1;
	$("#map_"+j).click(function(){
			$("#map_bg").css("display", "block");
			$("#map_content").css("display", "block");
			$("#map_name span").html("Branch "+branches[i].name);
			initialize(branches[i].longitude, branches[i].latitude, i);
		});
}
function navClick(i){
	$("ul.nav_bar li:nth-child("+i+")").click(function(){
		$("div#mover").animate({"left":frameWidth * (1 - i)},delaytime);
	});
}
function footerClick(i){
	$("ul#footer_nav li:nth-child("+i+")").click(function(){
		$("div#mover").animate({"left":frameWidth * (1 - i)},delaytime);
	});
}
function test_email(strEmail) {
  var myReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
  if(myReg.test(strEmail)) return true;
  return false;
 };

var out = function(){
	$('#message').fadeOut();
};
function initialize(x, y, i){
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("google_map"));
        map.setCenter(new GLatLng(x, y), 14);
        map.addControl(new GSmallMapControl());

        var baseIcon = new GIcon();
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);

        function createMarker(point, index, submary) {
          // Create a lettered icon for this point using our icon class
          var letter = String.fromCharCode("A".charCodeAt(0) + index);
          var letteredIcon = new GIcon(baseIcon);
          letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

          markerOptions = { icon:letteredIcon };
          var marker = new GMarker(point, markerOptions);

          GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml("<b>" + submary + "</b>");
          });
          return marker;
        }
	var latlng = new GLatLng(x, y);
	  map.addOverlay(createMarker(latlng, i, branches[i].name));
      }
    }

