atv.home = {};
/**************************
	Check for legacy ids
***************************/

atv.home.legacycheck = function(){
	if(window.document.location.hash !== ""){
		var hash = window.document.location.hash.substr(1);
		var type = "";
		var prefix = hash.substr(0,hash.indexOf("+"));
		var id = hash.substr(prefix.length+1);
		var prefixMap = {"vi":"episode","pg":"show","ch":"channel","pd":"product"};

		$.getJSON(atv.options.gateway, {method:'CheckLegacy', returnformat:'json', type:prefixMap[prefix], id:id}, function(data){
			if(data.GO){
				location.href = data.URL;
			}
		});
	}
}(); // Executes function immediately

/********** Customize Home *********/
atv.init.customize = function(){
	if(atv.cufon){
		Cufon.replace('#header .user div.customize .dropdown div.columns a span.title');
	}

	if(atv.user.signedin){
		$("#header .user div.customize .dropdown div.columns a").click(function(){
			if($(this).hasClass("save")){
				atv.user.addPod($(this).attr("rel"));
				$(this).fadeTo(500,0.3,function(){
					$(this).removeClass("save").addClass("remove");
				});
				$.get(atv.options.gateway, {method:'getPod', returnformat:'plain', id:$(this).attr("rel")}, function(data){
					var numPods = 1000;
					var shortestColumn = $("#pods .column:last");
					$("#pods .column").each(function(){
						if($(this).find(".pod").length < numPods){
							numPods = $(this).find(".pod").length;
							shortestColumn = $(this);
						}
					});
					shortestColumn.append(data);
					atv.home.userpods.refresh();
				});
			} else {
				atv.user.removePod($(this).attr("rel"));
				$(this).fadeTo(500,1,function(){
					$(this).removeClass("remove").addClass("save");
				});
				$("#pod_"+$(this).attr("rel")).find("a.close").click();
			}
			return false;
		});

		var preSlideHeight = $("#header .user div.customize .dropdown").height();
		var dropdown = $("#header .user div.customize .dropdown");
		$("#header .user div.customize").click(function(){ return false; }).hoverIntent(function(){
			if(!dropdown.hasClass("open")){
				dropdown.css("height","0px").css("top","40px").stop().animate({height:preSlideHeight},300,function(){
					$(this).addClass("open");
				});
			}
		}, function(){
			if(dropdown.hasClass("open")){
				dropdown.stop().animate({height:0},300,function(){
					$(this).css("height",preSlideHeight).css("top","-1000px").removeClass("open");
				});
			}
		});
	} else {
		$("#header .user div.customize").click(function(){
			location.href = 'https://www.adobe.com/cfusion/entitlement/index.cfm?returnurl='+location.href+'&e=adobetv&loc='+atv.language;
		});
	}
	
};


/********** FMA *********/
atv.home.fma = {};
atv.home.fmatimer = {};
atv.home.settimer = function(){
	if(typeof(atv.home.fmatimer) == "number"){
		clearInterval(atv.home.fmatimer);
	}
	atv.home.fmatimer = window.setInterval(function() {
		atv.home.fma.next();
	}, 10000);
};
atv.home.fma.next = function(){
	var from = $("#fma .column.one .slides a.active");
	var toIndex = 0;
	if(parseInt(from.attr("pos"),10)+1 < $("#fma .column.one .slides a").length){
		toIndex = parseInt(from.attr("pos"),10)+1;
	}
	var to = $("#fma .column.one .slides a:eq("+toIndex+")");

	to.css("left","100%").show().animate({left: "0%"}, 250, "linear", function(){
		$(this).addClass("active");
	});
	from.animate({left:"-100%"}, 250, "linear", function(){
		$(this).hide().removeClass("active");
	});
};
atv.home.fma.back = function(){
	var from = $("#fma .column.one .slides a.active");
	var toIndex = parseInt(from.attr("pos"),10)-1;
	if(toIndex == -1){
		toIndex = $("#fma .column.one .slides a").length-1;
	}
	var to = $("#fma .column.one .slides a:eq("+toIndex+")");

	to.css("left","-100%").show().animate({left: "0%"}, 250, "linear", function(){
		$(this).addClass("active");
	});
	from.animate({left:"100%"}, 250, "linear", function(){
		$(this).hide().removeClass("active");
	});
};
atv.home.fma.watch = function(){
	window.location.href = $("#fma .column.one .slides a.active").attr("href");
};
atv.init.fma = function(){
	/******************
		FMA Sliders
	*******************/
	$("#fma .column.one .slides a")
		.each(function(i){
			$(this).attr("pos",i);
			if(i===0){
				$(this).show().addClass("active");
			} else {
				$(this).hide();
			}
		});

	$("#fma .column.one .nav a[href=#next]").click(function(){ atv.home.fma.next(); atv.home.settimer(); return false;});
	$("#fma .column.one .nav a[href=#back]").click(function(){ atv.home.fma.back(); atv.home.settimer(); return false; });
	$("#fma .column.one .nav a[href=#watch]").click(function(){ atv.home.fma.watch(); return false; });

	/* Auto Advance FMA */
	atv.home.settimer();	
};



atv.home.userpods = {};
atv.init.userpods = function(){
	/************************
		User Pods
	*************************/
	// Make sortable
	if(atv.user.signedin){
		$("#pods").addClass("loggedin").data("sortclick",false);
		$("#pods .column").sortable({
			connectWith: '#pods .column',
			items: '.pod:not(.ad)',
			placeholder: 'placeholder',
			update: function(){
				atv.home.userpods.update();
			},
			sort: function(){
				atv.home.userpods.update();
			},
			start: function(){
				$("#pods").data("sortclick",true);
			},
			stop: function(){
				setTimeout(function(){$("#pods").data("sortclick",false)},100);
			}
		}).disableSelection();

		$("#pods .pod .top").prepend('<a href="#" class="close" title="Remove Pod">Remove Pod</a>');
		$("#pods .pod a").click(function(){
			if($("#pods").data("sortclick")){
				return false;
			}
		});
		$("#pods .pod a.close").click(function(){
			atv.user.removePod($(this).parents(".pod").attr("id").replace("pod_",""));
			$(this).parents(".pod").fadeOut("medium",function(){
				$(this).remove();
			});
			return false;
		});
	}
};
atv.home.userpods.update = function(){
	// Save positions
};
atv.home.userpods.refresh = function(){
	$("#pods .column").sortable('refresh');
	$("#pods .pod a.close").unbind("click").click(function(){
		atv.user.removePod($(this).parents(".pod").attr("id").replace("pod_",""));
		$(this).parents(".pod").fadeOut("medium",function(){
			$(this).remove();
		});
		return false;
	});
};
