var ISPA = ISPA || {};

//lat lon of hq
//
ISPA.LAT_HQ = 40.28286;
ISPA.LON_HQ = -79.54874;

ISPA.initialize = function () {
	
	$("#help > a").click(function() {
		Zenbox.show();
		return false;		
	});
	$("#help").css({visibility:"visible"});
}

/**
 * only one map per any page
 */
ISPA.gmaps = {};
ISPA.gmaps.map = {};
ISPA.gmaps.mapElement = null;

ISPA.gmaps.initialize = function(mapEl, stateCode) {
	this.map = null;
	this.mapElement = mapEl;
	this.loadMap(stateCode);
};

ISPA.gmaps.loadMap = function(stateCode) {
	if (this.map) return true;
	var ll = new google.maps.LatLng(ISPA.LAT_HQ, ISPA.LON_HQ); //hq
	var o = {
		zoom: 5,
		center: ll,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		streetViewControl: false,
		panControl: false,
		//mapTypeControlOptions: { },
		zoomControlOptions: {
			position: google.maps.ControlPosition.RIGHT_TOP
		},
		//minZoom:
		minZoom: 4 
	};

	this.map = new google.maps.Map(document.getElementById(this.mapElement), o);
};

ISPA.gmaps.centerHq = function() {
	var a = this;
	var ll = new google.maps.LatLng(ISPA.LAT_HQ, ISPA.LON_HQ);
	this.map.panTo(ll);
}
	
ISPA.gmaps.centerState = function(state) {
	var a = this;
	if (state) {
		var ll = new google.maps.LatLng(state.center_lat, state.center_lon);
		this.map.panTo(ll);
	}
};


/**
 * Data from api to display/manipulate
 */
ISPA.data = {};
ISPA.data.states = {};

ISPA.data.loadStates = function(stateCode) {
	var a = this;
	$.ajax({
			url: "/ajax/states",
			//cache: true,
			type: "GET",
			dataType: "json",
			success: function(d, code) {
				if (d) {
					var sc = false;	
					for(i=0; i< d.length; i++) {
						a.states[i] = d[i];
						if (stateCode && (d[i].code == stateCode)) {
							sc = d[i];	
						}
					}
					//call center now that data is loaded
					if (sc) {
						ISPA.gmaps.centerState(sc);
					}
				}
				
			}
	});	
};

ISPA.data.getState = function(stCode) {
	var a = this;
	var st = null;
	var i = 0;
	for(var prop in a.states) {
		st = a.states[i];
		if (st.code == stCode.toUpperCase()) {
			return st;
		}
		i++;
	}
	return false;
};

/**
 *
 *
 */
function Tabs(el, clName, idxFirst, btnEl, titles) {
	var a = this;

	var o = {'disableScroll': true,
					 'buttonTitles' : titles,
					 'recenter' 	  : false								 
		};
	a.carousel = new Carousel(el, clName, idxFirst, btnEl, o);
}

function Carousel(el,clName, idxFirst, btnEl, opts) {
	var a = this;
	a.parentEl = el;
	a.panelClass = clName;   
	a.selectedIndex = idxFirst;  //index of what to show
	a.buttonEl = null;
	a.buttonTitles = null;
	a.disableScroll = false;
	a.size = 0;
	a.timer = null;
	a.recenter = true;

	if(opts) {
		if (opts.disableScroll !== undefined) {
			a.disableScroll = opts.disableScroll;
		}
		if (opts.buttonTitles !== undefined) {
			a.buttonTitles = opts.buttonTitles;
		}
		if (opts.recenter !== undefined) {
			a.recenter = opts.recenter;
		}
	}

	if (btnEl) {
		a.buttonEl = btnEl;
	}
	a.init();
}

Carousel.prototype.init = function() {
	var a = this;

	if (a.buttonEl) {
		a.initButtons();
		a.buttonEl.children().eq(a.selectedIndex).addClass('selected');
	}
	
	a.showPanel(a.selectedIndex);

	if (!a.disableScroll) {
		a.startScroll();
	}	
	return a;
}
Carousel.prototype.initButtons = function() {
	var a = this;
	
	var c = 0;
	$.each(a.parentEl.children('.'+a.panelClass), function() {
			var h = '<span></span>';
			if(a.buttonTitles && a.buttonTitles[c]) {
				h = '<span>' + a.buttonTitles[c] + '</span>';
			}
			a.buttonEl.append(h);
			c++;
	});
	a.size = c;

	a.buttonEl.delegate('span','click', function(e, i) {
		var t = $(this);
		clearTimeout(a.timer);
		a.showPanel(t.index());
		//a.startScroll();
	});
	
	//recenter
	if (a.recenter) {
		var ml = 480 - (((c*30)-10)/2);
		a.buttonEl.css({'margin-left': ml});
		a.buttonEl.addClass('clearfix').show();
	}

	return a;
}

Carousel.prototype.hideAll = function() {
	var a = this;
	a.parentEl.children('.'+a.panelClass).hide();
	return a;
}
Carousel.prototype.showPanel = function(idx) {
	var a = this;
	if (a.buttonEl) {
		a.buttonEl.children().removeClass('selected')
			.eq(idx).addClass('selected');
	}

	a.hideAll();
	a.parentEl.children('.'+a.panelClass).eq(idx).show();
	a.selectedIndex = idx;
	return a;
}

Carousel.prototype.startScroll = function() {
	var a = this;
	
	var f = function() {
		if (a.size == 1) return;
		
		var n = a.selectedIndex + 1;
		if(a.selectedIndex >= a.size-1) {
			n = 0;
		}
		a.showPanel(n);
	};

	a.timer = setInterval(f, 5000);
}


