var markers;
var map;
var slideshowQ = false;
var slideshowI = 7000;

function initialize() {
	
  if ($('#map_canvas').attr('id')==undefined) return false;
	
  var latlng = new google.maps.LatLng(-25.439641, -49.280851);
  
  var myOptions = {
    zoom: 16,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: true,
    navigationControl: false,
    mapTypeControl: false,
    scaleControl: false
  };
  
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  
  markers = {
	batel: new google.maps.Marker({
	    position: new google.maps.LatLng(-25.439641, -49.280851), 
	    map: map, 
	    title:"Pizza Hut Batel"
	}),
	aguaVerde: new google.maps.Marker({
	    position: new google.maps.LatLng(-25.453476,-49.274535), 
	    map: map, 
	    title:"Pizza Hut Água Verde"
	}),
	cabral: new google.maps.Marker({
	    position: new google.maps.LatLng(-23.299019,-50.076826), 
	    map: map, 
	    title:"Pizza Hut Cabral"
	}),
	ecoville: new google.maps.Marker({
	    position:new google.maps.LatLng(-25.43641,-49.317498), 
	    map: map, 
	    title:"Pizza Hut Ecoville"
	}),
	barigui: new google.maps.Marker({
	    position: new google.maps.LatLng(-25.43534,-49.316463), 
	    map: map, 
	    title:"Pizza Hut Barigui"
	}),
	blumenau: new google.maps.Marker({
	    position: new google.maps.LatLng(-26.9206,-49.069904), 
	    map: map, 
	    title:"Pizza Hut Blumenau"
	}),
	beiramar: new google.maps.Marker({
	    position: new google.maps.LatLng(-27.584856,-48.544937), 
	    map: map, 
	    title:"Pizza Hut Beira Mar"
	}),
	iguatemi: new google.maps.Marker({
	    position: new google.maps.LatLng(-27.591346,-48.514467), 
	    map: map, 
	    title:"Pizza Hut Iguatemi"
	})
  };
}

function slideshowIniciar() {
	slideshowQ = true;
	setTimeout('slideshowProximo()',slideshowI);
}

function slideshowMudar(x, link) {
	
	slideshowQ = false;
	
	$('#slideshow #controles li.ativo').removeClass('ativo');
	$(link).parent().addClass('ativo');
	
	var el = $('#slideshow #imagens').children('.exibido');
	
	if ($($('#slideshow #imagens li')[x]).hasClass('exibido')) return;
	
	$($('#slideshow #imagens li')[x]).fadeIn(1500,
			function(){
				$(this).addClass('exibido');
			}
	);
	
	el.fadeOut(500,function(){
		el.removeClass('exibido');
	});
}

function slideshowProximo() {
	
	if (!slideshowQ) return;
	
	var linkAtual = $('#slideshow #controles').children('.ativo');
	
	var proximoLink = linkAtual.next();
	
	var el = $('#slideshow #imagens').children('.exibido');
	
	var proximo = el.next();
	
	if (!proximo.length) {
		 proximoLink = $('#slideshow #controles li').first();
		 proximo = $('#slideshow #imagens li').first();
	}
	
	linkAtual.removeClass('ativo');
	proximoLink.addClass('ativo');
	
	proximo.fadeIn(1500,
		function(){
			$(this).addClass('exibido');
		}
	);
	
	el.fadeOut(500,function(){
		el.removeClass('exibido');
	});
	
	setTimeout('slideshowProximo()',slideshowI);
}


/* jQuery onload dom */

$(function(){
	
	/*Footer multicolumns list*/

	var lis = $('#footer ul li');
	
	var liH = lis.first().height();
	var liW = lis.first().width();

	var nRow = Math.floor($('#footer ul').height()/liH);
	
	lis.each(function(x, li) {
		
		var margin =	''+(((!(x%nRow))&&(x>0))?(-nRow*liH):(0))+'px 0 0 '+
						(Math.floor(x/nRow)*liW)+'px';
		
		$(li).css('margin',margin);
		
	});
	
	/*Fim Footer multicolumns list*/
	
	/* carrega o mapa da home */
	initialize();
	
	/* adiciona os eventos onclick nos links do mapa e cria os marcadores */
	if (map!=undefined) {
		$('#localizacao ul li a').each(function(x, el) {
		
			//alert(this.id);
			$(el).click(function() {
				//alert(this.id);
				$('#localizacao ul li a.ativo').removeClass('ativo');
				$(this).addClass('ativo');
				map.setOptions({
					center: markers[this.id].position,
					zoom: 16
				});
			});
		});
	
		map.setCenter(markers[$('#localizacao a.ativo').attr('id')].position);
	}
	/* fim do mapa */
	
	/* slideshow */
	$('#controles a').each(function(x, el){
		$(el).click(function(){
			slideshowMudar(x, el);
		});
	});
	slideshowIniciar();
	/* fim slideshow */
	
	$('#fotos #proximo').click(function(){
		
		var lis = $('#fotos ul li');
		
		var margem = lis.first().css('marginTop');
		
		margem = parseInt(margem.substr(0,margem.length-2));
		
		if (margem != (-310*(lis.length-1)))
		
			$('#fotos ul li').first().animate({'marginTop':'-=310px'});
		
		else
			
			$('#fotos ul li').first().animate({'marginTop':'0px'});
		
		return false;
	});
	$('#fotos #anterior').click(function(){

		var lis = $('#fotos ul li');
		
		var margem = lis.first().css('marginTop');
		
		margem = parseInt(margem.substr(0,margem.length-2));
		
		if (margem != 0)
		
			$('#fotos ul li').first().animate({'marginTop':'+=310px'});
		
		else
			
			$('#fotos ul li').first().animate({'marginTop':(-310*(lis.length-1))+'px'});
		
		return false;
	});
	
	$('#cidades select').change(function(){
		
		var cidades = ['curitiba', 'florianopolis', 'blumenau'];
		
		var cidade = $(this).val();
		var cidadePos = -1;
		var cidadeAtual;
		$.each(cidades, function(index, value) { 
			
			if (window.location.pathname.search(value) > 0){
				cidadePos = window.location.pathname.search(value);
				cidadeAtual = value;
			}
			
		});

		var requestUrl = window.location.pathname.slice(0, cidadePos);
		if (requestUrl.indexOf('/') == 0) requestUrl = requestUrl.slice(1); 
		
		$.post('http://'+window.location.hostname+requestUrl+'/ajax',{cidade:$(this).val()}, function(d) {
			var novaUrl = "";
			if (cidadePos > 0){
				novaUrl = window.location.pathname.slice(cidadePos+cidadeAtual.length);
			}
			
		 
			window.location = 'http://'+window.location.hostname+requestUrl+'/'+cidade+novaUrl;
			setTimeout('window.location.reload(true)', 2000);
			//alert(window.location);

		});
		
	});
	
	$('a[href="#"]').qtip({
	      content: {
	         text: 'Em breve!' // Use each elements title attribute
	      },
	      style: 'red', // Give it some style
	      position: {
	          corner: {
	             target: 'topMiddle',
	             tooltip: 'bottomMiddle'
	          }
	      }
	});
	
	$('li.delivery a[href="#"]').qtip({
	      content: {
	         text: 'Em breve!' // Use each elements title attribute
	      },
	      style: 'green', // Give it some style
	      position: {
	          corner: {
	             target: 'topMiddle',
	             tooltip: 'bottomMiddle'
	          }
	      }
	});
	
	$('#delivery').qtip({
	      content: {
	         text: 'Em breve!' // Use each elements title attribute
	      },
	      style: 'green', // Give it some style
	      position: {
	          corner: {
	             target: 'topMiddle',
	             tooltip: 'bottomMiddle'
	          }
	      }
	});
	
	$('a[href="#"]').click(function() {return false;});
	   
});


