﻿function ElemByID(name){
	if (document.layers) 
		return document.layers[name];
	else if (document.all)
		return document.all[name];
	else if (document.getElementById)
		return document.getElementById(name);
}
$(document).ready(function(){
var cityregion;
cityregion = new Array();
$.getJSON('inc/file.php',onAjaxSuccess);
function onAjaxSuccess (data){
		$.each(data.cities, function(i,item){
			//  add city
			cityregion[item.city]=item.dealer.cregion;
			var dealerinfo = '<strong>'+item.cityname+'</strong><br />'+item.dealer.company+'<br />';
			if(item.dealer.exclusive != 0) dealerinfo += '<span class="exclusive">Эксклюзивный региональный дилер</span><br />'
			dealerinfo += item.dealer.adress+'<br />'+item.dealer.contact+'<br />';
			if(item.dealer.email) dealerinfo += 'E-mail: <a href="mailto:'+item.dealer.email+'">'+item.dealer.email+'</a>';
			if(item.dealer.additional) dealerinfo += '<br />'+item.dealer.additional;
			$('#cities').append('<li id="'+item.city+'"></li>');
			$('#popups').append('<li id="'+item.city+'-info" class="info">'+dealerinfo+'</li>');
			// set city position
			$('#'+item.city).css({
				top:item.position[1]+'px',
				left:item.position[0]+'px'});
			$('#'+item.city+'-info').css({
				top:item.position[1]+7+'px',
				left:item.position[0]+7+'px'});
		});

		$.each(data.regions, function(i,item){
			//  add region
			$('#regions').append('<li id="'+item.region+'"></li>');
			// set region position
			$('#'+item.region).css({
				width:item.dimensions[0]+'px',
				height:item.dimensions[1]+'px',
				top:item.position[1]+'px',
				left:item.position[0]+'px',
				backgroundPosition:'-'+item.bgoffset[0]+'px -'+item.bgoffset[1]+'px'});
		});

$('#cities > li').each(function (){
	// options
	var hideDelay = 100;
	var hideDelayTimer = null;

	// tracker
	var shown = false;
	var currentcity = this.id;
	var trigger = $('#'+currentcity);
	var popup = $('#'+currentcity+'-info');

	// set the mouseover and mouseout on both element
	$([trigger.get(0), popup.get(0)]).mouseover(function (){
	// stops the hide event if we move from the trigger to the popup element
	if (hideDelayTimer) clearTimeout(hideDelayTimer);

	// don't trigger the animation again if we're being shown, or already visible
	if (shown){
		return;
	}
	else{
		//highlite region
		$.each(cityregion[currentcity], function(i,item){
		document.getElementById(item).style.display='block';
		});
		// brings the popup back in to view
		document.getElementById(currentcity+'-info').style.display='block';
		// once the animation is complete, set the tracker variables
		shown = true;
	}
	}).mouseout(function (){
		// reset the timer if we get fired again - avoids double animations
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		// store the timer so that it can be cleared in the mouseover if required
		hideDelayTimer = setTimeout(function (){
			hideDelayTimer = null;
			// hide the popup entirely after the effect (opacity alone doesn't do the job)
			document.getElementById(currentcity+'-info').style.display='none';
			// hide region highlition
			$.each(cityregion[currentcity], function(i,item){
			document.getElementById(item).style.display='none';
			});
			shown = false;
		}, hideDelay);
	});
});
};
});
