﻿var markers = [];

$(function(){
	$("div.newsBox").hover(function(){clearInterval(headline_interval);},function(){headline_interval = setInterval(headline_rotate,10000);}).slice(1).hide();
	headline_interval = setInterval(headline_rotate,6000);
	$('#other-suppliers').hide();
});

function headline_rotate(){
	var curr = $("div.newsBox").index($("div.newsBox:visible")[0]);
	var next = (curr+1 == $("div.newsBox").size()) ? 0 : curr +1;
	$("div.newsBox:eq("+curr+")").fadeOut(1200, function(){$("div.newsBox:eq("+next+")").fadeIn();});
}

function createMarker(point, number, title) {
	var marker = new GMarker(point, G_DEFAULT_ICON);
	GEvent.addListener(marker, "click", function() {
		$('body')[0].canvas.setCenter(marker.getLatLng(), 16);
		$('.branches dd').hide();
		$('.branches dt').removeClass('selected');
		$('.branches dt:eq('+number+')').addClass('selected');
		$('.branches dd:eq('+number+')').show();
		$('dl.branches').scrollTo($('.branches dt:eq('+number+')'), 800);
		marker.openInfoWindowHtml(title);
	});
	markers.push(marker);
	return marker;
}

function initialize() {
	$('body')[0].canvas = new google.maps.Map2(document.getElementById("map"));
	$('body')[0].canvas.addControl(new GSmallMapControl());
	$('body')[0].canvas.enableDoubleClickZoom();
	$('body')[0].canvas.continuousZoomEnabled();
	$('body')[0].canvas.enableScrollWheelZoom();
	$('body')[0].canvas.setCenter(new google.maps.LatLng(ll,lt), mz);
	$('dl.branches a[rel=map]').each(function(i){
		var branchtitle = '<h3>' + $(this).parent().prev().html() + '</h3>';
		var branchtext =  $(this).parent().html();
		var branchtext = $(branchtext).remove('a[rel=map]').html();
		var maphtml = branchtitle+branchtext;
		var maphref = $(this).attr('href');
		var xspot = maphref.slice(maphref.indexOf('&ll='),maphref.indexOf('&spn=')).slice(4).split(',');
		var point = new GLatLng(xspot[0],xspot[1]);
		//console.log(point, branchtitle);
		$('body')[0].canvas.addOverlay(createMarker(point, i, maphtml));
	});
}

$(document).ready(function() {

	$('#map').each(function(){
		initialize();
	});
	
	$('#mapreset').click(function(){
		$('body')[0].canvas.setCenter(new google.maps.LatLng(ll,lt), mz);
	});

	$('#nav > li').hover(function(){
		$(this).addClass('sfhover');
	},function(){
		$(this).removeClass('sfhover');
	});

    $("a[rel='external']").attr("target","_blank");
    $("a[rel='map']").hide();
	
	$('dl.articles').each(function(){
		var list = this;
		$('dd', this).hide();
		$('dt', this).click(function(){
			$('dt', $(this).parent()).removeClass('selected');
			$('dd', $(this).parent()).hide();
			$(this).addClass('selected').next().show();
		});
	});
	
	$('dl.branches').each(function(){
		var list = this;
		$('dd', this).hide();
		$('dt', this).click(function(){
			var i = $('dl.branches dt').index(this);
			GEvent.trigger(markers[i], "click");
		});
	});
	
	$(".gems_detail").hide();
    
    $('.supplierPanel').hide();
    $('#supplier_menu a').click(function(){
        $('#supplier_menu a').removeClass('selected');
        $(this).addClass('selected');
        $('.supplierPanel').hide();
        $($(this).attr('href')).show();
    });

});
