// JavaScript Document

jQuery.noConflict ();

//var urlBase = 'http://localhost:8888/buraglia/wwwordpress/wp-content/themes/buraglia';
var urlBase = 'http://www.buraglia.com/wp-content/themes/buraglia';
var vel = 300;
var user_flickr = '72095307@N03';
var imagenCarruselTotal;
var imagenGaleriaTotal;
var imagenCarruselActual = 0;
var imagenGaleriaActual = 0;
var carruselInterval;
var claimInterval;
var claimActual = 0;
var claimTotal;

jQuery(document).ready (function() {
/*	WebFontConfig = {
		google: { 
			families: [ 'Muli' ] 
		},
		loading: function () {
		},
		fontactive: function () {
			empezar ();
		}
	};
	(function() {
   		var wf = document.createElement('script');
	    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
    	    '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
	    wf.type = 'text/javascript';
    	wf.async = 'true';
	    var s = document.getElementsByTagName('script')[0];
    	s.parentNode.insertBefore(wf, s);
	})();
	
});

function empezar () {*/
	jQuery ('#wrapper').fadeTo (vel, 1);
	
	cargarImagenesFlickr ('72157628513757245', jQuery ('#carrusel'), 960, 540, false, 'carrusel');
	
/********	HEADER	*******/
	jQuery ('.nav_elem')
		.last ()
			.addClass ('nav_elem_ultima')
		.end ()
		.each (function (i) {
			jQuery (this)
				.bind ('click', function () {
					moverPantalla (i);
				});
		});
			



/********	MARQUESINA	************/
	claimTotal = jQuery ('.marquesina_tex').length;
	
	chequearClaims ();
	
	claimInterval = setInterval ("moverClaim (true)", 5000);


			
/********	LOOP	************/
	jQuery ('.contenido_loop').each (function (j) {
		var altura = new Array();
		var cont = this;
		var count = -1;
		

		jQuery (this)
			.find ('.bloque').each (function (i) {
				var alt = parseInt (jQuery (this).height() + 10);
				
				if (i % 4 == 0) {
					jQuery (this)
						.addClass ('bloque_primero');
						
					count++;
					altura[count] = 0;
				};
				
				if (alt > altura[count]) {
					altura[count] = alt;
				}
			
				jQuery (this)
					.find ('strong')
						.first ()
							.addClass ('bloque_strong');
			});
			
		var count = -1;
		
		jQuery (this)
			.find ('.bloque').each (function (i) {
				if (i % 4 == 0) {
					count++;
				};
				
				if (j < jQuery ('.contenido_loop').size () - 1) {
					jQuery (this)
						.css ('height', altura[count]+'px');
				};
			});
			
		jQuery (this)
			.find ('.proyecto')
				.each (function (i) {
					if (i % 3 == 0) {
						jQuery (this)
							.addClass ('proyecto_primero');
					};
					
					jQuery (this)
						.hover (
							function () {
								jQuery (this)
									.find ('.proyecto_texto')
										.fadeTo (vel, 1);
							},
							function () {
								jQuery (this)
									.find ('.proyecto_texto')
										.fadeTo (vel, 0, function () { jQuery (this).hide(); });
							});
				});
	});



/********	CONTACTO	************/
	jQuery.ajax ({
		type: 'GET',
		url: urlBase+'/fichasContacto.php',
		dataType: 'html',
		success: function (html, textStatus) {
			jQuery ('.fichas_contacto')
				.html (html)
				.find ('.fichas_bloque')
					.each (function (i) {
						if (i % 3 == 0) {
							jQuery (this)
								.addClass ('fichas_bloque_primero');
						};
					});
		}
	});
	
	jQuery ('.titulo_desp')
		.bind ('click', function () {
			jQuery ('.contenido_desp')
				.slideToggle (vel);
		});

});

function moverPantalla (num) {
	var delay;
	
	var postOffset = jQuery ('.post_loop').eq(num).offset();
	var destino = postOffset.top - 90;
	
	jQuery ('html, body')
		.stop()
		.animate ({ scrollTop: destino });
};

function chequearClaims () {
	if (claimActual == 0) {
		jQuery ('.marquesina_boton_arriba')
			.removeClass ('marquesina_boton_activo')
			.removeClass ('marq_boton_hover')
			.addClass ('marquesina_boton_inactivo')
			.unbind ('mouseenter mouseleave click');
	}
	else {
		if (jQuery ('.marquesina_boton_arriba').hasClass ('marquesina_boton_inactivo')) {
			jQuery ('.marquesina_boton_arriba')
				.addClass ('marquesina_boton_activo')
				.removeClass ('marquesina_boton_inactivo')
				.hover (
					function () {
						jQuery (this)
							.addClass ('marq_boton_hover');
					},
					function () {
						jQuery (this)
							.removeClass ('marq_boton_hover');
					})
				.bind ('click', function () {
					moverClaim (false);
				});
		};
	};

	if (claimActual == claimTotal - 1) {
		jQuery ('.marquesina_boton_abajo')
			.removeClass ('marquesina_boton_activo')
			.removeClass ('marq_boton_hover')
			.addClass ('marquesina_boton_inactivo')
			.unbind ('mouseenter mouseleave click');
	}
	else {
		if (jQuery ('.marquesina_boton_abajo').hasClass ('marquesina_boton_inactivo')) {
			jQuery ('.marquesina_boton_abajo')
				.addClass ('marquesina_boton_activo')
				.removeClass ('marquesina_boton_inactivo')
				.removeClass ('marquesina_boton_hover')
				.hover (
					function () {
						jQuery (this)
							.addClass ('marq_boton_hover');
					},
					function () {
						jQuery (this)
							.removeClass ('marq_boton_hover');
					})
				.bind ('click', function () {
					moverClaim (true);
			});
		};
	};
};

function moverClaim (bol) {
	if (bol) {
		claimActual++;
		
		if (claimActual == claimTotal) {
			claimActual = 0;
		}
	}
	else {
		claimActual--;
	};
	
	var pos = 20 - claimActual * 80;
	
	jQuery ('.marquesina_texto_cont')
		.stop ()
			.animate ({
				top: pos
				}, vel);
			
	chequearClaims ();
};

function cargarImagenesFlickr (set, $capa, width, height, desc, tipo) {
	var urlJSON = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=5cf8cea99daa81e4d5da34212dd21395&photoset_id="+set+"&extras=description%2C+url_l%2C+url_sq&per_page=500&format=json&jsoncallback=?";
	
	jQuery.getJSON (urlJSON, function (data) {
		if (data.photoset.photo) {
			var color;
			
			if (tipo == "carrusel") {
				imagenCarruselTotal = data.photoset.photo.length;
			};
			
			var html = '<ul class="flickr">';
			jQuery.each (data.photoset.photo, function (i, n) {
				var item = data.photoset.photo[i];
				var ruta = item.url_l;
				var title = item.title;
//				ruta = ruta.replace ("_m.jpg", "_b.jpg");
				html += '<li class="flickr"><img src="'+ruta+'" title="'+item.title+'" width='+width+' height='+height+' />'
				if (desc) {
					html += '<div class="desc_flickr">'+title+'</div>';
				}
				html += '</li>';
			});
			
			html += '</ul>';
			$capa.append (html);
	
			$capa.find('img')
				.fadeTo (0, 0)
				.first()
					.fadeTo (vel, 1)
			
			if (desc) {
				$capa.find ('.desc_flickr')
					.first()
						.fadeTo (vel, 1);
			}
			
/*			if (imaTotal > 1 && tipo != "carrusel") {
				$capa.find ('.boton_derecha')
					.addClass (color)
					.addClass ('boton_activo')
					.removeClass ('boton_inactivo')
					.bind ('click', function () {
						avanzarImagen (false, $capa, tipo, imaTotal);
					});
			};*/
			
			if (tipo == "carrusel") {
				carruselInterval = setInterval ('avanzarImagen(false, jQuery("#carrusel"), "carrusel", imagenCarruselTotal, false)', 3000);
				
				var html = '';
				
				for (var i=0; i<imagenCarruselTotal; i++) {
					html += '<div class="carrusel_boton"></div>';
				};
				
				jQuery ('.carrusel_botones')
					.html (html)
	
				jQuery ('.carrusel_boton')
					.first ()
						.addClass ('carrusel_boton_activo')
					.end ()
					.each (function (i) {
						jQuery (this)
							.hover (
								function () {
									jQuery (this)
										.addClass ('carrusel_boton_hover');
								},
								function () {
									jQuery (this)
										.removeClass ('carrusel_boton_hover');
								})
							.bind ('click', function () {
								cambiarImagen (i, false, jQuery ('#carrusel'), "carrusel", imagenCarruselActual, imagenCarruselTotal, true);
							});
				});
			};
		}
	});
};

function avanzarImagen (bol, $capa, tipo, imgTotal, marq) {
	var imgActual;
	
	if (tipo == "carrusel") {
		imgActual = imagenCarruselActual;
	}
	else if (tipo == "galeria") {
		imgActual = imagenGaleriaActual;
	}
	
	var num = imgActual + 1;
	
	num = (num == imgTotal) ? 0 : num;
	
	cambiarImagen (num, bol, $capa, tipo, imgActual, imgTotal, marq);	
}

function retrocederImagen (bol, $capa, tipo, imgTotal, marq) {
	var imgActual;
	
	if (tipo == "carrusel") {
		imgActual = imagenCarruselActual;
	}
	else if (tipo == "galeria") {
		imgActual = imagenGaleriaActual;
	}
	
	var num = imgActual - 1;
	
	num = (num == -1) ? imgTotal-1 : num;
	
	cambiarImagen (num, bol, $capa, tipo, imgActual, imgTotal, marq);	
}

function cambiarImagen (num, bol, $capa, tipo, imgActual, imgTotal, marq) {
	if (marq) {
		if (tipo == "carrusel") {
			clearInterval (carruselInterval);
		};
	};
/*	var color;
	
	if (tipo == "tablon") {
		color = 'rosa';
	}
	else {
		color = 'verde';
	};*/
	
/*	if (num == 0) {
		$capa.find ('.boton_izquierda')
			.unbind ('mouseenter mouseleave click')
			.removeClass (color)
			.removeClass ('boton_activo')
			.removeClass ('boton_hover')
			.addClass ('boton_inactivo');
	}
	else {
		if ($capa.find ('.boton_izquierda').hasClass ('boton_inactivo')) {
			$capa.find ('.boton_izquierda')
				.removeClass ('boton_inactivo')
				.addClass ('boton_activo')
				.addClass (color)
				.hover (function () {
//					jQuery (this).addClass ('boton_hover');
				}, function () {
//					jQuery (this).removeClass ('boton_hover');
				})
				.bind ('click', function () {
					retrocederImagen (false, $capa, tipo, imgTotal);
				});
		}
	}*/
	
/*	if (num == imgTotal - 1) {
		$capa.find ('.boton_derecha')
			.unbind ('mouseenter mouseleave click')
			.removeClass (color)
			.removeClass ('boton_activo')
			.removeClass ('boton_hover')
			.addClass ('boton_inactivo');
	}
	else {
		if ($capa.find ('.boton_derecha').hasClass ('boton_inactivo')) {
			$capa.find ('.boton_derecha')
				.removeClass ('boton_inactivo')
				.addClass ('boton_activo')
				.addClass (color)
				.hover (function () {
//					jQuery (this).addClass ('boton_hover');
				}, function () {
//					jQuery (this).removeClass ('boton_hover');
				})
				.bind ('click', function () {
					avanzarImagen (false, $capa, tipo, imgTotal);
				});
		}
	}*/
	
	$capa.find ('img')
		.eq (imgActual)
			.stop().fadeTo (vel*2, 0, function () { jQuery (this).css ('display', 'none'); })
		.end()
		.eq (num)
			.stop().fadeTo (vel*2, 1)
		.end();
		
	if (bol) {
		$capa.find ('.desc_flickr')
			.eq (imgActual)
				.stop().fadeTo (vel*2, 0, function () { jQuery (this).css ('display', 'none'); })
			.end()
			.eq (num)
				.stop().fadeTo (vel*2, 1)
			.end();
	};
		
/*	if (tipo == "tablon") {
		$capa.find ('.text_flickr')
			.eq (imgActual)
				.stop().fadeTo (vel*2, 0, function () { jQuery (this).css ('display', 'none'); })
			.end()
			.eq (num)
				.stop().fadeTo (vel*2, 1)
			.end();
		
	}*/
	
	var imgAnt = imgActual;
	imgActual = num;
	
	if (tipo == "carrusel") {
		imagenCarruselActual = imgActual;
		
		jQuery ('.carrusel_boton')
			.eq (imgActual)
				.addClass ('carrusel_boton_activo')
			.end ()
			.eq (imgAnt)
				.removeClass ('carrusel_boton_activo');
	}
/*	else if (tipo == "galeria") {
		imagenGaleriaActual = imgActual;
	}*/
	
	if (marq) {
		if (tipo == "carrusel") {
			carruselInterval = setInterval ('avanzarImagen(false, jQuery("#carrusel"), "carrusel", imagenCarruselTotal, false)', 3000);
		};
	};
};
