$(function(){

	var touch = {},
		endTime = 0,
		thisEl,
		likedAnime = new Array();
	
	$("#load-wrap").on("click", "#sort-latest", function(){
		$("#sort-full").removeClass("sort-current");
		$(this).addClass("sort-current");
		$("li.complete").hide();
	});
	$("#load-wrap").on("click", "#sort-full", function(){
		$("#sort-latest").removeClass("sort-current");
		$(this).addClass("sort-current");
		$("li.complete").show();
	});
	$("#load-wrap").on("click", "#top-splash", function(){
		$("#anime-desc").toggleClass('fadeOut');
	});
	
	/* Props to David Kaneda & Thomas Fuchs */
	/* localStorage.clear(); */
	if(localStorage.getItem($("#post-id").val()+'_like') == 'true'){
		$("#like-button").find('img').attr('src','http://animebullet.com/wp-content/themes/animebullet/img/like.png');
	}
	$("#load-wrap").on("touchstart", ".as", function(e){
		touch.startTime = (new Date).getTime();
		touch.target = e.originalEvent.touches[0].target;
		touch.x1 = e.originalEvent.touches[0].pageX;
		touch.y1 = e.originalEvent.touches[0].pageY;	
		$(this).addClass('clicked');
	}).on("touchmove",".as", function(e){
		/* Not needed */
		touch.x2 = e.originalEvent.touches[0].pageX;
		touch.y2 = e.originalEvent.touches[0].pageY;
	}).on("touchend",".as", function(e){
		endTime = (new Date).getTime();
		/* User behavior */
		if((e.originalEvent.changedTouches[0].pageX-touch.x1) == 0 && (e.originalEvent.changedTouches[0].pageY-touch.y1) == 0 && ((new Date).getTime() - touch.startTime) > 120 && localStorage.getItem($("#post-id").val()+'_like') !='true'){
			thisEl = $(this);
			thisEl.removeClass('clicked').addClass('fadeOut').parent().addClass('like-loading');
			$.ajax({
				url: 'http://animebullet.com/yui-like.php',
				type: 'POST',
				data: { id: $("#post-id").val(), ajax: 'true' },
				success: function(r){
					$("#likes-count").text(parseInt($("#likes-count").text())+1);
					thisEl.find('img').attr('src','http://animebullet.com/wp-content/themes/animebullet/img/like.png');
					thisEl.removeClass('fadeOut').parent().removeClass('like-loading');
					localStorage.setItem($("#post-id").val()+'_like', 'true');
					likedAnime.push($("#post-id").val());
					$("#like-button").addClass('disabled');
				},
				error: function(){
					alert('An error occured!');
				}
			});
			touch = {};
		} else {
			$(this).toggleClass('clicked');
			touch = {};
		}
	});
	$("#not-webapp").show();
	function updateFilter(){
		var filter = $("#filter");
		filter.val('');
		$(".selected").each(function(){
			filter.val(filter.val()+$(this).attr('title')+',');
		});
		$("#search-filter").attr('href','http://animebullet.com/genres/'+filter.val().substring(0, filter.val().length-1)+'/');
	}
	$("#load-wrap").on("click",".genre-filter a", function(e){
		$(this).toggleClass('selected');
		updateFilter();
	});
	
	if(window.navigator.standalone){
	$("#load-wrap").on("click", "a", function(){
		$("#back-btn").show();
	});
	$("#back-btn, #logo").click(function(){
		$("#back-btn").hide();
	});
	$("#first-b").remove(); /* Never working with this again */
		$("body").on("click","a", function(event){
			event.preventDefault();
			var href = $(this).attr("href"),
					title = $(this).attr("title");
			if(href.indexOf(location.hostname) > -1){
				if($(this).hasClass('video-ajax')){
					window.location = href;
				} else {
					$("#load-wrap").addClass('fadeOut').load(href+' #load-content', function(r,t,x){
						$(document).scrollTop(0);
						window.history.pushState({
							"pageTitle": "",
							"html": ""
						},"", href);
						if($.inArray($("#post-id").val(), likedAnime) > -1 || localStorage.getItem($("#post-id").val()+'_like') == 'true'){
							$("#like-button").addClass('disabled').find('img').attr('src','http://animebullet.com/wp-content/themes/animebullet/img/like.png');
						}
						$("#load-wrap").removeClass('fadeOut');
					});
				}
			}
		});
	}
		/*
		if(window.history.length <= 1){
			$("#first-b").hide();
			var track = 0;
		} else {
			var track = window.history.length-1;
		}
		window.history.replaceState({
			"pageTitle": $(document.documentElement).find('title').text(),
			"html": document.documentElement.innerHTML
		},"", document.URL);
		$("body").on("click","a", function(event){
			event.preventDefault();
			var href = $(this).attr("href"),
					title = $(this).attr("title");
			
			if(href.indexOf(location.hostname) > -1){
				if($(this).hasClass('video-ajax')){
					window.location = href;
				} else if($(this).attr("id") == "first-b"){
					window.history.go(-1);
				} else {
					$("#load-wrap").load(href+' #load-content', function(r,t,x){
						track++;
						document.title =  title;
						window.history.pushState({
							"pageTitle": title,
							"html": r
						},"", href);
						$("#first-b").show();
					});
				}
			}
		});
		window.onpopstate = function(e){
			if(e.state){
				var all = e.state.html;
				$("#load-wrap").html($("#load-content", all));
				document.title =  e.state.pageTitle;
				track--;
				if(track == 0){
					$("#first-b").hide();
				}
			}
		} */
});
