var geocoder;
var map;
var markersArray = [];
var infoW;
var duration = 500;
var tab = 0;
var cont = 0;
function makePassword()
{
	var field = document.getElementById('passwdtxt');
	field.style.display="none";
	var newf = document.getElementById('password');
	newf.style.display="";
	field.value = '';
	newf.focus();
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function prepareScroll() {
  if($(".sc_panel").length>1) {
	var id = 0;
	var i ;
	if($(".submenu a.active").length==1) {
	  i = $(".submenu a.active").attr("id").split("_");
	}
	else {
	  i = $(".sc_panel:first").attr("id").split("_");
	}
	id = i[1];
	if(!$("#content_"+id).prev().length) {
	  $("#sc_left").hide();
	}
	if(!$("#content_"+id).next().length) {
	  $("#sc_right").hide();
	}
	$(".scroll").scrollTo('#content_'+(id),0);
	tab = id;
  }
  else {
	$("#sc_left").hide();
	$("#sc_right").hide();
  }
  $("#sc_left").click(function(){prevTab();});
  $("#sc_right").click(function(){nextTab();});
}

function changeTab(id) {
  if($('#content_'+(id)).length) {
	$(".scroll").scrollTo('#content_'+(id),duration,{"onAfter":function(){
		$('.submenu a').removeClass("active");
		tab = id;
		$('#sub_' + id + '').addClass("active");
		if(!$('#content_'+tab).next().length) {
		  $("#sc_right").hide();
		}
		else {
		  $("#sc_right").show();
		}
		if(!$('#content_'+tab).prev().length) {
		  $("#sc_left").hide();
		}
		else {
		  $("#sc_left").show();
		}
		Cufon.refresh(".submenu a");
	}});
  }
}

function prepareContent() {
  if($('.innerContent').length) {
	var id = 0;
	var i ;
	if($(".subsubmenu a.active").length==1) {
	  i = $(".subsubmenu a.active").attr("id").split("_");
	}
	else {
	  i = $(".innerContent").attr("id").split("_");
	}
	id = i[1];
	cont = id;
	$('#subsub_' + id + '').addClass("active");
	Cufon.refresh(".subsubmenu a");
	$(".innerContent").hide();
	$('#subcont_'+cont).show();	
  }
}

function changeContent(id) {
  if($('#subcont_'+id).length) {
	$('.subsubmenu a').removeClass("active");
	$('#subcont_'+cont).hide();
	cont = id;
	$('#subcont_'+cont).show();
	$('#subsub_' + id + '').addClass("active");
	Cufon.refresh(".subsubmenu a");
	$('.scroll-pane').jScrollPane();
  }
}

function nextTab() {
  if($('#content_'+tab).next().length) {
	var i = $('#content_'+tab).next().attr("id").split("_");
	i = i[i.length-1];
	changeTab(i);
  }
}
function prevTab() {
  if($('#content_'+tab).prev().length) {
	var i = $('#content_'+tab).prev().attr("id").split("_");
	i = i[i.length-1];
	changeTab(i);
  }
}
function initialize() {
	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(56.9656, 24.1697 );
	var myOptions = {
		zoom: 14,
		mapTypeControl: false,
		navigationControl: true,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.SMALL
		},
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	//skolas
	codeAddress('Ūnijas iela 93, Rīga, LV-1084',1,adreses[0],false,56.9603903, 24.1848788);
	codeAddress('Induļa iela 4, Rīga, LV-1084',1,adreses[1],false,56.9627705, 24.1844153);
	codeAddress('Vaidavas iela 6, Rīga, LV-1084',1,adreses[2],false,56.9619775, 24.1759441);
	codeAddress('Purvciema iela 38, Rīga, LV-1035',1,adreses[3],false,56.9541152, 24.1813963);
	codeAddress('Nīcgales iela 22, Rīga, LV-1035',1,adreses[4],false,56.9543594, 24.1751832);
	//bernudarzi
	codeAddress('Ūnijas 83, Rīga, LV-1084',2,adreses[5],false,56.9627096, 24.1814586);
	codeAddress('Dzelzavas 17a, Rīga, LV-1084',2,adreses[6],false,56.9621242, 24.1718568);
	codeAddress('Purvciema 32, Rīga, LV-1035',2,adreses[7],false,56.9537749, 24.1796355);
	codeAddress('Vaidavas 11, Rīga, LV-1084',2,adreses[8],false,56.9629448, 24.1763044);
	//Rimi
	codeAddress('Stirnu iela 26, Rīga, LV-1082',3,adreses[9],false,56.959297, 24.1854623);
	//Maxima
	codeAddress('A.Deglava 67, Rīga, LV-1082',4,adreses[10],false,56.9489778, 24.1873145);
	//t/c Domina
	codeAddress('Ieriķu iela 3, Rīga, LV 1084',5,adreses[11],false,56.9656292, 24.1697417);
	//maja
	codeAddress('Ūnijas iela 82, Rīga, LV-1084',6,adreses[12],true,56.9601943, 24.1851171);
}

function codeAddress(address,type,title,center,plat,plong) {
  var image;
  var myLatlng =new google.maps.LatLng(plat,plong);
  if(type==1) {
	image = new google.maps.MarkerImage('/img/skola.png',
		new google.maps.Size(31,31),
		new google.maps.Point(0,0),
		new google.maps.Point(0,31));
  }
  else if(type==2) {
	image = new google.maps.MarkerImage('/img/bernudarzs.png',
		new google.maps.Size(30,30),
		new google.maps.Point(0,0),
		new google.maps.Point(0,30));
  }
  else if(type==3) {
	image = new google.maps.MarkerImage('/img/rimi.png',
		new google.maps.Size(58,21),
		new google.maps.Point(0,0),
		new google.maps.Point(0,21));
  }
  else if(type==4) {
	image = new google.maps.MarkerImage('/img/maxima.png',
		new google.maps.Size(57,12),
		new google.maps.Point(0,0),
		new google.maps.Point(0,12));
  }
  else if(type==5) {
	image = new google.maps.MarkerImage('/img/domina.png',
		new google.maps.Size(44,26),
		new google.maps.Point(0,0),
		new google.maps.Point(0,26));
  }
  else if(type==6) {
	image = new google.maps.MarkerImage('/img/majina.png',
		new google.maps.Size(31,31),
		new google.maps.Point(0,0),
		new google.maps.Point(0,31));
  }
	if(center) {
				map.setCenter(myLatlng);
			  }
				var marker = new google.maps.Marker({
					map: map,
					icon: image,
					title: title,
					position: myLatlng
				});
				 var html='<b>'+title+'</b>';

                 google.maps.event.addListener(marker, "click",
					function() {
					  if(infoW) {
						infoW.close();
					  }
					  infoW = new google.maps.InfoWindow();
					  infoW.setContent(html);
					  infoW.setPosition(myLatlng);
					  infoW.open(map,marker);
                     });
               
				markersArray.push(marker);
}

$(function(){  
  
	$("a[rel^='prettyPhoto']").prettyPhoto({
		showTitle: false,
		allowresize: true,
		theme: 'facebook',
		modal: true,
		changepicturecallback: function() {
		}
	});
	jQuery('.mycarousel').jcarousel({
		//wrap: 'circular'
  });
  
  prepareScroll();
  prepareContent();
  $('.scroll-pane').jScrollPane();  
});
