jQuery(document).ready(function() {
	acomodar_divs();
	//botones_inicio();
	FB.init({
		appId      : '187264734654184',
		status     : true,
		xfbml      : true,
		version    : 'v2.7' // or v2.6, v2.5, v2.4, v2.3
	});


});

jQuery(window).resize(function() {
	acomodar_divs();
});

var isTouch = false;

	jQuery(window).on("load",function() {
		//var chromeH = window.innerHeight - window.outerHeight;
		//alert(window.innerHeight + "-" + window.outerHeight + "-"+chromeH+"-"+jQuery(window).height());
		acomodar_divs();
		
		if ("ontouchstart" in document.documentElement){
			isTouch = true;
			jQuery('#divBG').css("overflow", "scroll");
			
			ww = jQuery(window).width();
			if(ww <= 658 ){
				jQuery('#divBG').css("height", "82%");
			}else{
				jQuery('#divBG').css("height", "100vh");
			}
			jQuery('#divBGAux').css("overflow", "scroll");
			jQuery("#divBG").scroll(function(e) {
				
				var scrolled = jQuery("#divBG").scrollLeft();

				width = jQuery('#divBGCapa').width();
				var porcentajeMov = (scrolled*100)/width;
				var opacidad = 1 - (porcentajeMov*1.5*(.65))/100;
				if(opacidad < 0.35){
					opacidad = 0.35;
				}
				jQuery('.divBGFondo').css('opacity', opacidad);

				if(porcentajeMov > 0 ){
					jQuery('footer .txtAldea').css('opacity', 0);
					jQuery('footer ul.icons').css('opacity', 1);
				}else{
					jQuery('footer .txtAldea').css('opacity', 1);
					jQuery('footer ul.icons').css('opacity', 0);
				}
				
			});
			jQuery('#touchhand').show();
			setTimeout(function () {
				jQuery('#touchhand').css("background-position-x", "25%");
				setTimeout(function () {
					jQuery('#touchhand').hide();
				}, 1500);
			}, 500);
		}else{
			isTouch = false;
			jQuery(window).scroll(function(e) {
				doScroll();
			});
			jQuery('#divBG').css("overflow", "hidden");
			jQuery('#divBG').css("height", "100vh");
			jQuery('#divBGAux').css("overflow", "hidden");

		}
		

	});

	jQuery('.iconsbtn').on("click",function(event) {
		//var chromeH = window.innerHeight - window.outerHeight;
		//alert(window.innerHeight + "-" + window.outerHeight + "-"+chromeH+"-"+jQuery(window).height());
		//acomodar_divs();
		var id=event.delegateTarget.parentElement.id;
		if(jQuery('#'+id).height() <= 48){
			jQuery('.contenidoPantalla').height("48px");
			jQuery('#'+id).height("calc(100% - 72px");
		}else{
			jQuery('.contenidoPantalla').height("48px");
		}

		var widthPantallaAux = jQuery(window).width();
		if(widthPantallaAux <= 658){
			widthPantallaAux = widthPantallaAux-9*widthPantallaAux/100;
			if(widthPantallaAux > 494){
				widthPantallaAux = 494;
			}
		}else{
			widthPantallaAux = 494;
		}

		var scrollNum = 1;
		var itemId = 1;
		var videos = false;
		switch (id) {
			case 'pantallaPlaylist':
				scrollNum = 1;
				itemId = 1;
				break;
			case 'pantallaNoticias':
				scrollNum = 2;
				itemId = 2;
				break;
			case 'pantallaVideos':
				scrollNum = 3;
				itemId = 3;
				videos = true;
				break;
			case 'pantallaMerchandaising':
				scrollNum = 4;
				itemId = 5;
			default:
				break;
		}

		if(isTouch){
			jQuery('#divBG').scrollLeft(widthPantallaAux*scrollNum);
		}else{
			

			var anchoCapa = jQuery('.divBGCapa').width();
			height = jQuery(window).height();

			var aux = widthPantallaAux*scrollNum;
			var porcentajeMov = aux*100/(anchoCapa-widthPantallaAux);
			var scrollTop = porcentajeMov*height/100;

			window.scroll(0, scrollTop);
		}

		idElement = id+"Item_"+itemId;
		var altoIframe = Math.trunc(jQuery('#main').height()-96-24);
		var elemento = jQuery('#'+idElement);
		if(videos){
			if(widthPantallaAux > 658){
				altoIframe = 580;
			}
			callWebServiceData(idElement,elemento,altoIframe/2);
			idElement = id+"Item_"+(itemId+1);
			callWebServiceData(idElement,elemento,altoIframe/2);
		}else{
			callWebServiceData(idElement,elemento,altoIframe);
		}

		//jQuery('#divAuxScroll').scrollTop(x);
		//jQuery('body').scrollTop(100);


		//.scrollTop();
		//.scrollLeft( value );

    });
    

    

	function acomodar_divs() {
		//height = jQuery(window).height();
		height = jQuery("#divBGAux").height();
		
		if (!is_home) {
			height = height - 74;
		}else{
			widthAux = jQuery(window).width();
			if(widthAux<=480){
				jQuery('.info_caption').css("width", "48px");
				
			}else{
				jQuery('.info_caption').css("width", "auto");
			}
		}
		jQuery('#foto_pajaros.alto_ventana').css("height", height);
		jQuery('.alto_ventana').css("min-height", jQuery(window).height());
		fondo_imagenes();

		var widthPantallaAux = jQuery(window).width();
		if(widthPantallaAux > 494){
			widthPantallaAux = 494;
		}
		jQuery('#divAuxScroll').css("height", jQuery('#main').width()-widthPantallaAux);
		//jQuery('.divBGFondoEstrellas').css("width", jQuery('#main').width()-widthPantallaAux);
		//console.log(widthPantallaAux + "-" + jQuery('#main').width() + "-"+(jQuery('#main').width()-widthPantallaAux));
		//alert(height);

		
		ww = jQuery(window).width();
		if(ww <= 658 ){
			jQuery('#divBG').css("height", "82%");
		}else{
			jQuery('#divBG').css("height", "100vh");
		}
		
		
	}


	function fondo_imagenes() {
		//height = jQuery(window).height();
		width = jQuery(window).width();
		if(width <= 658){
			height = jQuery("#divBGCapa").height();
			jQuery('.iframePantalla').height(height-120);
		}else{
			height = jQuery("#divBGAux").height()-48;

			wh = jQuery("#divBGCapa").height()-181;
			if(wh < 580){
				jQuery('.iframePantalla').height(wh);
			}else{
				jQuery('.iframePantalla').height(580);
			}
		}
		
		
		var div_img = jQuery('#divBG');

		jQuery('.espacioInicial').height(height);
		
		
		
		if(width <= 658){
			jQuery('.contenedorPantalla').width(width-(9*width/100));
		}else{
			jQuery('.contenedorPantalla').width(658);
		}
		jQuery('.contenedorPantalla').height(height);


		var div_imgLogo = jQuery('.espacioInicial');
		var image_url_logo = div_imgLogo.css('background-image'),
					imageLogo;

		image_url_logo = image_url_logo.match(/^url\("?(.+?)"?\)$/);

		if (image_url_logo[1]) {
			image_url_logo = image_url_logo[1];
			imageLogo = new Image();

			jQuery(imageLogo).on('load',function() {
				if(width>494){
					width = 494;
				}
				if (width > height) {
					a = width / height;
					b = imageLogo.width / imageLogo.height;
					if (a > b) {
						div_imgLogo.css({'background-size': 'auto 65%'});
					} else {
						div_imgLogo.css({'background-size': '65% auto'});
					}
				} else {
					div_imgLogo.css({'background-size': '65% auto'});
				}
				//alert(image.width + 'x' + image.height);
			});

			imageLogo.src = image_url_logo;

		}
		
	}
	var lastScroll = 0;

	function doScroll(){
		height = jQuery(window).height();
		var st = jQuery(this).scrollTop();
		
		//Determines up-or-down scrolling
		$ml = st / (height / 2);
		if ($ml > 1) {
			$ml = 1;
		}
		//$ml=jQuery('#content-wrraper').css("left");
		//$ml=parseInt($ml.slice(0,-2));

		if (is_home) {
			jQuery('.site-header').css("opacity", $ml);
		}
		$ml = lastScroll = st;

		moverDivCapa(st);

		if(lastScroll>(height-jQuery('.contenedorBotonInicio').height()-72)){
			var margin = parseInt(jQuery('.container').css('margin-right'))+15;
			//var margin = marginTxt.substring(0,marginTxt.length-2)+15;
			if(margin<48){
				//jQuery('.contenedorBotonInicio').css('width',jQuery('.container').css('padding-right'));
				jQuery('.contenedorBotonInicio').css('width',0);
				jQuery('.btnMenu').css('right',0);
			}
		}else{
			jQuery('.contenedorBotonInicio').css('width',48);
			jQuery('.btnMenu').css('right','-48px');
		}

	}
	

	function abrirMenu(){
		var menu = jQuery('.contenedorBotonInicio').width();
		if(menu<48){
			jQuery('.contenedorBotonInicio').css('width',48);
		}else{
			jQuery('.contenedorBotonInicio').css('width',0);
		}
	}

	function moverDivCapa(scrollTop){
		var anchoCapa = jQuery('.divBGCapa').width();
		var width = jQuery(window).width();
		var widthAux = width;
		if(widthAux>494){
			widthAux = 494;
		}
		height = jQuery(window).height();
		heightPage = jQuery('#page').height();
		var porcentajeMov = (scrollTop*100)/height;
		if(porcentajeMov>100){
			porcentajeMov = 100;
		}
		if(porcentajeMov<0){
			porcentajeMov = 0;
		}
		//console.log(height+"-"+heightPage+"-"+porcentajeMov);
		var pixMov = (porcentajeMov*(anchoCapa-width))/100;
		//console.log(porcentajeMov+"-"+pixMov+"-"+anchoCapa);
		jQuery('.divBGCapa').css('left','-'+pixMov+'px');
		var pixMovFondo = (porcentajeMov*(anchoCapa-(anchoCapa-widthAux/1.5)))/100;
		jQuery('.divBGFondo').css('left','-'+pixMovFondo+'px');
		jQuery('.divBGFondoEstrellas').css('left','-'+pixMovFondo+'px');

		var opacidad = 1 - (porcentajeMov*(.65))/100;
		if(opacidad < 0.35){
			opacidad = 0.35;
		}
		jQuery('.divBGFondo').css('opacity', opacidad);

		if(porcentajeMov > 0 ){
			jQuery('footer .txtAldea').css('opacity', 0);
			jQuery('footer ul.icons').css('opacity', 1);
		}else{
			jQuery('footer .txtAldea').css('opacity', 1);
			jQuery('footer ul.icons').css('opacity', 0);
		}
	}	

	

	function callWebServiceData(id,elemento,altoIframe){
		if(elemento.length>0){
			var item = listaContenido[id];
			if(elemento.hasClass( "cargando" )){
				jQuery.ajax({
					url: urlSitio+'/wp-content/plugins/u99_redessociales/u99_redessociales_content.php',
					cache: false,
					dataType: 'json',
					data: { 
						ll: u99_ll,
						ca: u99_ca,
						sitioID: item['sitio_id'],
						postID: item['post_id'],
						tipo: item['tipo'],
						fuente: item['fuente'],
						altoIframe: altoIframe
					},
					type: 'POST',
					success:  function (response) {
						if(response.codigorespuesta === 'S'){
							elemento.append(response.respuestas);
							elemento.removeClass('cargando');
							switch (item['fuente']) {
								case 'facebook':
									FB.init({
										appId      : '187264734654184',
										status     : true,
										xfbml      : true,
										version    : 'v2.7' // or v2.6, v2.5, v2.4, v2.3
									});
									break;
								case 'instagram':
									window.instgrm.Embeds.process();
								case 'twitter':
									twttr.widgets.load();
									break;
								default:
									break;
							}
							
						}
						//console.log(response);
					},
					fail: function(response){
						//volverAcargar();
					},
					error: function(xhr, textStatus, errorThrown){
						//volverAcargar();
					}
				});
			}else{
				if(item['fuente']==='facebook'){
					FB.init({
						appId      : '187264734654184',
						status     : true,
						xfbml      : true,
						version    : 'v2.7' // or v2.6, v2.5, v2.4, v2.3
					});
				}
			}
		}
	}

	function mostrarRemera(linkRemera){
		var width = jQuery(window).width();
		var height = jQuery(window).height();
		if(width > height){
			width = "auto";
			height = "calc(100% - 142px)";
			txtImg = "height: 100%;";
		}else{
			height = "auto";
			width = "100%";
			txtImg = "width: 100%;";
		}
		jQuery('#mostrarRemera').html(''
				+ '<div class="mostrarRemera">'
				+ '<div style="height:'+height+'; width:'+width+'"><img style="'+txtImg+'" src="'+linkRemera+'" >'
				+ '<div class="remerabtns"><a href="mailto:bgarcia@bgarcia.com.uy?subject=Comprar Remera&body=Me gusatría comprar la remera: '+linkRemera+'"><span class="icon circle fa-shopping-cart fa-2x"></span></a></div>'
                + '</div></div>'
				);
		jQuery('#mostrarRemera').show();
	}

	function cerrarMostrarRemera(){
		jQuery('#mostrarRemera').hide();
	}