/*--- fade gallery ---*/
function fadeGallery(){
	var stay_time = 5000; //in ms
	var change_speed = 600; //in ms
	$('div.fade-gallery').each(function(){
		var _hold = $(this);
		var _f = true;
		var _t;
		var _list = _hold.find('li');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		if(_list.length > 1){
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time);
			}
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.eq(_a).removeClass('active').animate({opacity: 0},{ queue:false, duration: change_speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1},{ queue:false, duration: change_speed});
				_a = _ind;
			}
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time+change_speed);
			}
		}
	});
}
/*--- gallery function ---*/
function initGallery(){
	var fade_speed = 600; //in ms
	var slide_speed = 600; //in ms
	var _hold = $('#gallery');
	if(_hold.length){
		var img_hold = _hold.find('div.gallery > div.img-hold');
		var list_hold = _hold.find('div.swicher > div > ul');
		var _btn = list_hold.find('a');
		
		var btn_prev_img = _hold.find('div.gallery .link-prev a');
		var btn_next_img = _hold.find('div.gallery .link-next a');
		var btn_prev = _hold.find('div.swicher a.last');
		var btn_next = _hold.find('div.swicher a.next');
		
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		var hold_w = list_hold.parent().width();
		var list_w = list_hold.children().length*list_hold.children().outerWidth();
		var _step = hold_w;
		var _m = 0;
		var fade_f = true;
		
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.get(_a)._img = $('<img src="'+_btn.eq(_a).attr('href')+'"/>');
		_btn.get(_a)._f = true;
		_btn.get(_a)._img.addClass('active');
		img_hold.append(_btn.get(_a)._img);
		list_hold.css('left', -_m);
		
		btn_prev_img.click(function(){
			if(_a > 0) changeEl(_a - 1);
			else changeEl(_btn.length - 1);
			return false;
		});
		btn_next_img.click(function(){
			if(_a < _btn.length - 1) changeEl(_a + 1);
			else changeEl(0);
			return false;
		});
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		
		btn_next.click(function(){
			moveList(true);
			return false;
		});
		btn_prev.click(function(){
			moveList(false);
			return false;
		});
		
		function changeEl(_ind){
			if(fade_f && _ind != _a){
				fade_f = false;
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_btn.get(_a)._img.removeClass('active').animate({opacity: 0}, {queue:false, duration: fade_speed});
				if(_btn.get(_ind)._f){
					_btn.get(_ind)._img.addClass('active').animate({opacity: 1}, {queue:false, duration: fade_speed});
					_a = _ind;
					fade_f = true;
				}
				else{
					_btn.get(_ind)._img = $('<img />');
					_btn.get(_ind)._img.load(function(){
						_btn.get(_ind)._img.addClass('active').animate({opacity: 1}, {queue:false, duration: fade_speed});
						_a = _ind;
						fade_f = true;
					});
					_btn.get(_ind)._f = true;
					_btn.get(_ind)._img.css('opacity', 0);
					img_hold.append(_btn.get(_ind)._img);
					_btn.get(_ind)._img.attr('src', _btn.eq(_ind).attr('href'));
				}
			}
		}
		function moveList(_f){
			if(_f){
				if(list_w - _m > hold_w) _m += _step;
				else _m = 0;
			}
			else{
				if(_m > 0) _m -= _step;
				else _m = Math.round((list_w-hold_w)/_step)*_step;
			}
			list_hold.animate({left:-_m}, {queue:false, duration:slide_speed});
		}
	}
}
/*-------- MENU OVERLAY --------*/
function loadOverlay(){
	$('li#nav-call').hover (
		function() { $('#nav-overlay').css('display', 'block'); },
		function() { $('#nav-overlay').css('display', 'none'); }
	);
	$('div#nav-overlay').hover (
		function() { $(this).css('display', 'block'); },
		function() { $(this).css('display', 'none'); }
	);
}

function clearBox(input) {
	if(input.value=input.defaultValue) {
		input.value = '';	
	}
}

function resetBox(input) {
	if(!input.value) {
		input.value = input.defaultValue;	
	}
}

function validateSignup(myForm){

	error = 0;
	
	var myEmail = myForm.email.value;
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myEmail))){
		error = 1;
	}
	
	if(error == 1){
		alert('Please enter a valid email address and try again.');
		return false;
	}
	else {
		return true;
	}
}

function Validate() {

	/* Items that require a text input */
	var myVars = new Array("frm_name",
												 "frm_email",
												 "frm_enquiry"
													);
	
	for(i=0;i<myVars.length;i++){
		var id = myVars[i];
		if(document.getElementById(id).value == ""){
			document.getElementById(id).style.borderColor = "#f86060";
			document.getElementById(id).style.backgroundColor = "#ffeeee";
			
			var errors = 'yes';
		}
		else {
			document.getElementById(id).style.borderColor = "#cccccc";
			document.getElementById(id).style.backgroundColor = "#ffffff";
		}
	}
	
	var email = document.getElementById("frm_email").value;
	if(email != ''){
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){
			document.getElementById("frm_email").style.borderColor = "#f86060";
			document.getElementById("frm_email").style.backgroundColor = "#ffeeee";
	
			var errors = 'yes';
		}
		else {
			document.getElementById("frm_email").style.borderColor = "#cccccc";
			document.getElementById("frm_email").style.backgroundColor = "#ffffff";
		}
	}

	if(errors == 'yes'){
		alert("Your registration failed.\n\n Please correct the errors highlighted in red.\n");
		/* self.scrollTo(0, 0); */
		return false;
	}
	else {
		return true;
	}
}

var ie6 = false;
$(document).ready(function(){
	fadeGallery();
	initGallery();
	if(!ie6) {
		loadOverlay();
	}
});

function loadMap() {
  if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		var point = new GLatLng(53.37196808613869, -1.479184627532959);
		
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(point, 15);
	
		function createMarker(point, html) {
			var marker = new GMarker(point);
			GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
			});
			return marker;
		}
	
		map.addOverlay(createMarker(point, '<strong>Karl Benz Interiors</strong><br />01 Napier Street<br />Sheffield<br />S11 8HA'));

  }
}
