$(document).ready(function(){
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))){
		$('#main').css('overflow','auto')
    }
	//-----ARRASTRE HORIZONTAL
	dragPrimary=function(){
		var limitex=$(window).width()-$('#primary').width()-200;
		$('#primary').draggable({ disabled: false })
		$('#primary').draggable({axis: 'x'});
		$('#primary').draggable({containment: [limitex,0,200,0]});
		/*$('#primary').draggable({start: $('a[class="enlace-work"]').unbind('click')});
		$('#primary').draggable({stop: $('a[class="enlace-work"]').bind('click')});*/
	}
	resize=function(){
		if($('#primary').width()>$(window).width()-200){
			dragPrimary();
		} else {
			$('#primary').draggable({ disabled: true })
		}
		if(!(navigator.userAgent.match(/iPhone/i)) || !(navigator.userAgent.match(/iPod/i)) || !(navigator.userAgent.match(/iPad/i))){
			$('.post-wrapper').height(1000)
		}
		$('.post-wrapper').height($(window).height())
	}
	resize();
	$.ajaxSetup({cache:false});
	$('a[class="enlace-work"]').click(function(){
		var post_id = $(this).attr('rel');
		var contenido = $('#post_ext-'+post_id);
		var previo = $('#post-'+post_id);
		var otros_contenidos = $('.contenido-post');
		var otros_previos = $('div[id*="post-"]');
		otros_previos.parent().animate({width:200}, 300 ,'swing');
		otros_previos.fadeTo(500, 1);
		otros_contenidos.hide();
		previo.fadeTo(500, 0);
		contenido.load('?page_id=8',{id:post_id},function() {
			contenido.parent().animate({width:700}, 250 ,'swing');
			contenido.show('slide', { direction: 'left' }, 300,function(){resize()});
			var limitey=contenido.height()*(-1)+$(window).height();
			$('.col_izq').draggable({axis: 'y'});
			$('.col_izq').draggable({containment: [0,limitey,0,0]});
			$('.col_izq').mousemove(function(){
				dragPrimary();
			});
			//-----carga suave de imagenes
			$('.attachment-galeria').css('display','none');
			$('.attachment-galeria').load(function () {
				$(this).fadeIn(300);
			});
			/*Shadowbox.clearCache();
			Shadowbox.init({ skipSetup: true }); Shadowbox.setup();*/
			$('#cerrar').click(function(){
				var otros_contenidos = $('.contenido-post');
				var otros_previos = $('div[id*="post-"]');
				contenido.html("")
				otros_previos.animate({width:200}, 200 ,'swing');
				otros_previos.fadeTo(500, 1);
				otros_contenidos.hide();
				contenido.parent().animate({width:200}, 500 ,'swing',function(){resize()});
				return false;
			});
		});
		return false;
	});
	$('a[class="enlace-work"] img').animate({opacity:0}, 0);
	$('a[class="enlace-work"] img').hover(
		function(){
			//$('a[class="enlace-work"] img').stop().animate({opacity:0}, 200)
			$(this).animate({opacity:1}, 200);
		},
		function(){
			//$('a[class="enlace-work"] img').stop().animate({opacity:1}, 200);
			$(this).animate({opacity:0}, 200);
		}
	);
	$(window).resize(function() {
		resize()
	});
	
	//---------------TEXTOS HEADER-------------------*/
	textoActual=0
	$.getJSON('/textos.json', function(data) {
		items=[]
		$.each(data.textos, function(i){
			items.push(data.textos[i].texto)
		});
		cambiarTexto();
    });
	cambiarTexto = function(){
			$('#header_1').fadeOut(200,function(){
				$('#header_1').html('<p>'+items[textoActual++]+'</p>');
				$(this).fadeIn(500);
				if (textoActual==items.length){
					textoActual=0;
				}
			}
		);
	}
	$('#header_1').click(function(event){
       cambiarTexto()
    });
	//--------BOTON MANIFIESTO Y WORK WORK
	$('#site-title').click(function(event){
       $('#primary').animate({left:0},1000)
    });
	$('#work').click(function(event){
       $('#primary').animate({left:0},1000)
    });
});
