// JavaScript Document

var defaultLang = "Ru";
var langLabels = new Array("btnHomeLang","btnAboutLang","btnServicesLang","btnPricesLang","btnNewsLang","btnLocationsLang","btnDeliveryLang","btnContactLang","titleHome","homeText01","homeText02","homeText03","homeText04","homeText05","homeText06","homeText07","homeText08","homeText09","homeText10","homeText11","homeText12","homeText13","copyright","btnHomeFooterLang","btnAboutFooterLang","btnServicesFooterLang","btnPricesFooterLang","btnNewsFooterLang","btnLocationsFooterLang","btnDeliveryFooterLang","btnContactFooterLang","titleAbout","aboutText01","aboutText02","deliveryTitle","deliveryText01","deliveryText02","deliveryText03","deliveryText04","contactTitle","contactText01","contactText02","contactText03","contactText04","contactText05","btnSendEmail");

Event.observe(window, 'load', function() {

	// adding listeners 
	addListeners();
	
	var hash = document.location.hash.replace("#","");
	if(hash=="home") $('scroller').style.height = '1700px';	 			
	//if(hash=="about") $('scroller').style.height = '515px';				
	if(hash=="about") $('scroller').style.height = '900px';				
	if(hash=="delivery") $('scroller').style.height = '515px';				
	if(hash=="contact") $('scroller').style.height = '560px';				
	if(hash=="services") $('scroller').style.height = '950px';				
	if(hash=="news") $('scroller').style.height = '950px';				
	if(hash=="prices") $('scroller').style.height = '1060px';				
	if(hash=="locations") {
      //      $('scroller').style.height = '920px';
        }
        
	/** ajax callers */
	invokeAjax('ajax/contact.php','contact');
	invokeAjax('ajax/about.php','about');
	invokeAjax('ajax/delivery.php','delivery');
	invokeAjax('ajax/services.php?lang=' + defaultLang,'services');	
	invokeAjax('ajax/news.php?lang=' + defaultLang,'news');
	invokeAjax('ajax/prices.php?lang=' + defaultLang,'prices','initializeAccordion()');
	invokeAjax('ajax/locations.php','locations','initializeLocations()');

	setWidth(); //max width of the page is 1600px 
	initializeImages();	
	
	initializeLanguage();
		
}); 

var times = 0;
var images = new Array();

function initializeImages(){

	images = new Array();
	
	var image = new Image()
	image. src = "images/prices/img_prices.jpg";
	images[0]= image;
	
	image = new Image()
	image. src = "images/services/img_serv.jpg";
	images[1]= image;
	
	image = new Image()
	image. src = "images/about/img_about.jpg";
	images[2]= image;
	
	image = new Image()
	image. src = "images/contact/img_cont.jpg";
	images[3]= image;
	
	image = new Image()
	image. src = "images/home/img_home.jpg";
	images[4]= image;
	
	image = new Image()
	image. src = "images/locations/img_locat.jpg";
	images[5]= image;
	
	image = new Image()
	image. src = "images/news/img_news.jpg";
	images[6]= image;
	
	loadImages();
}
var times = 0;
function loadImages(){
	
	times++;
	var bool = true;
	
	//console.info('trying ... ' + times);	
	for(i=0; i< images.length; i++) { 		
		if(images[i].complete==false) bool = false;				
		//console.info(images[i].src + ' -- ' + images[i].complete );
	}

	if(bool || times > 100) showContent();
	else loadImages();	

}

var showContentInterval;
var pageIsReady = false;
function showContent(){ showContentInterval = setInterval('checkIfPageIsReady()',500);}
function checkIfPageIsReady(){
	
	if(pageIsReady){
		$('loading').style.display = 'none';	
		$('main').style.display = '';
		clearInterval(showContentInterval);
	}
	
}

function setWidth(){
	
	var width = document.documentElement.offsetWidth;
	/* alert(width); */
	if(width > 1600) width = 1600;
	
	$('scroller').style.width = width + 'px';
	$('home').style.width = width + 'px';
	$('about').style.width = width + 'px';
	$('services').style.width = width + 'px';
	$('prices').style.width = width + 'px';
	$('news').style.width = width + 'px';
	$('delivery').style.width = width + 'px';
	$('contact').style.width = width + 'px';
	$('locations').style.width = width + 'px';
	
}

/** add listeners to each div element inside the main div */
function addListeners(){
		
	var elements = $('menu').childElements();	
	elements.each(
		function(element){
			if(element.id!='top' && element.id!='bottom'){
				Event.observe(element,'mouseover', function(){
                                    //if(element.className != 'black-background-selected')
                                        element.className = 'black-background';
                                });
				Event.observe(element,'mouseout', function(){
                                    //if(element.className == 'black-background')
                                        element.className = 'c_' + element.id;
                                });
			}				
		}
	);	

}

function invokeAjax(url, target,callback){
	
	new Ajax.Request(url, {
		method: 'post',
		onLoading: function() {},
	
		onSuccess: function(transport) {
			 $(target).innerHTML = transport.responseText;
			 
			 if(callback!=undefined) setTimeout(callback,100);			 
		}
	});	
	
}


function initializeAccordion(){
	var bottomAccordion = new accordion('prices-block');
}

var marker44;
var markers = new Array();
var map = null;
function initializeLocations(){
	
	if (GBrowserIsCompatible()) 
	{	
		/* main point info */
		var mainCoordX = $('mainCoordX').innerHTML;
		var mainCoordY = $('mainCoordY').innerHTML;
		var mainName = $('mainName').innerHTML;
		var mainAddress = $('mainAddress').innerHTML;
		var mainImage = $('mainImage').innerHTML;
		
		map = new GMap2(document.getElementById("map"));
		
		var geocoder = new GClientGeocoder();

		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		map.setCenter(new GLatLng(mainCoordX,mainCoordY), 15);		
				
		/* main point */
		var point = new GLatLng(mainCoordX,mainCoordY);
		marker44 = createMarker(point,mainName,mainAddress,mainImage);
                map.addOverlay(marker44);
		
		/* start selectin main location */
		findMainLocation(mainName,mainAddress,mainImage);
		
		/* secondary points */ 		
		var elements = $('not-main-elements').childElements();
		elements.each(
			function(element){
				
				var id = element.id;	
				var coordX = $('coordX-' + id).innerHTML;
				var coordY = $('coordY-' + id).innerHTML;
				var name = $('name-' + id).innerHTML;
				var address = $('address-' + id).innerHTML;
				var image = $('image-' + id).innerHTML;
				
				point = new GLatLng(coordX,coordY);
				markers[id] = createMarker(point,name,address,image);			
				map.addOverlay(markers[id]);
			}
		);
								
	}
	
}

/* gMapzzzzzz? */
function createMarker(point, name, address, thumb) 
{
	var marker = new GMarker(point);
  	GEvent.addListener(marker, "mouseover", function() {
		marker.openInfoWindowHtml(getPopUpHtml(name, address, thumb));
	});
    return marker;
}

function getPopUpHtml(name, address, thumb)
{
	return "<div style='width:190px;' ><div class='popTitle'>" + name + "</div><div class='popImage'><img src='" + thumb + "' alt='img' width='181' height='81'/></div><div class='popDetails'>" + address + "</div></div>";
}

 function findLocation(markerID, name, address,  thumb) {    	
	fixMap();
	markers[markerID].openInfoWindowHtml(getPopUpHtml(name, address, thumb));
	map.setCenter(markers[markerID].getLatLng());
}
function findMainLocation(  name, address,  thumb ){
	marker44.openInfoWindowHtml(getPopUpHtml(name, address, thumb));
	map.setCenter(marker44.getLatLng());
}


/* defines an global image that will check every half second if the info is loaded, when it's loaded it will kill the interval*/ 
var initializeLanguageInterval;
function initializeLanguage(){ initializeLanguageInterval = setInterval('startChecking()',500);}
function startChecking(){
	
	if(	$('about').innerHTML!="" && 
		$('services').innerHTML!="" && 
		$('prices').innerHTML!="" && 
		$('news').innerHTML!="" && 
		$('locations').innerHTML!="" && 
		$('delivery').innerHTML!="" && 
		$('contact').innerHTML!=""){
		/* initialize Lang */ 
		clearInterval(initializeLanguageInterval);
		langLabels.each(
			function(langLabel){
				if(defaultLang=="En") hideRussian(langLabel);
				else hideEnglish(langLabel);
			}
		);
		
		
		pageIsReady = true;
		
	}
	
}


/* language switcher*/ 
function switchLang(lang){
	
	langLabels.each(
		function(langLabel){
			if(lang=="En") {
				hideRussian(langLabel);
				showEnglish(langLabel);
			}
			else {
				hideEnglish(langLabel);
				showRussian(langLabel);							
			}
		}
	);
	
	invokeAjax('ajax/services.php?lang=' + lang,'services');		
	invokeAjax('ajax/prices.php?lang=' + lang,'prices','initializeAccordion()');
	invokeAjax('ajax/news.php?lang=' + lang,'news');
	invokeAjax('ajax/locations.php?lang=' + lang,'locations','initializeLocations()');
}

/* Hide Elements */ 
function hideRussian(id){ hide(id + 'Ru'); }
function hideEnglish(id){ hide(id + 'En'); }
function hide(completeID){ $(completeID).style.display = 'none'; }

/* Show Elements */ 
function showRussian(id){ show(id + 'Ru');}
function showEnglish(id){ show(id + 'En');}
function show(completeID){ $(completeID).style.display = ''; }

/** contact form */
function sendEmail(lang){
	
	new Ajax.Request('ajax/processSendEmail.php?lang='+lang, {
		method: 'post',
		parameters: Form.serialize('thisform'),
		onLoading: function() {},
	
		onSuccess: function(transport) {
			 var arr = transport.responseText.split('-');	
			 alert(arr[1]);
			 if(arr[0]=='ok') window.location = '/#home';
		}
	});	
	
	
}

function fixMap(){
    initializeLocations();
}