site.embedVideoPlayer = function(){
	jQuery(function($){
		if(!site.globalVars.initialVideoSource)
			throw Error("need video srouce");
		
		swfobject.embedSWF(
			site.baseUrl() + "/swf/video-player.swf", "flash-vid-player", "580", "369", "9.0.115.0", 
			site.baseUrl() + "/swf/expressInstall.swf", 
			site.util.flattenObject(site.globalVars.initialVideoSource, {}), 
			{wmode:"transparent", allowscriptaccess:"always"}, 
			{}
		);
		$("#flash-vid-player div.vid-player-alt").show();
		
		pageTracker._trackPageview('/webisode/' + site.globalVars.initialVideoSource.title);
	});
};

site.showComments = function(){
	jQuery(function($){
		$('html,body').animate({scrollTop:613}, 1000);
	});
};

site.loadCommentsForVideo = function(videoSource){
	jQuery(function($){
		$.ajax({
			url:site.baseUrl() + "/comments/ajax-list/webisode/" + videoSource.id,
			dataType:"json",
			success:site.ajaxPageSuccessHandler
		});
		$("#comments div.loader").show();
	});
	
	jQuery(function($){
		$.ajax({
			url:site.baseUrl() + "/comments/ajax-featured-list/webisode/" + videoSource.id,
			dataType:"json",
			success:function(data, textStatus){
				if(data.featured == ""){
					$("#featured").html("");
				}else{
					$set = $("#featured");
					$set.removeAttr("id");
					$set.replaceWith(data.featured);
					
					console.log(site.resetFeaturedCommentIndex);
					site.resetFeaturedCommentIndex();
				}
			}
		});
		$("#comments div.loader").show();
	});
};

site.nextVideo = function(){
	jQuery(function($){
		var vars = site.globalVars;
		
		if(vars.selectedVideoIndex >= vars.videoList.length - 1) 
			vars.selectedVideoIndex = 0;
		else 
			vars.selectedVideoIndex++;
		
		var videoSource = vars.videoList[vars.selectedVideoIndex];
		
		var listItem = $("#webisode-" + videoSource.id + "-btn").parent().parent().parent();
		$("#webisode-nav ol li.selected").removeClass("selected");
		listItem.addClass("selected");
		
		pageTracker._trackPageview('/webisode/' + videoSource.title);

		$("#flash-vid-player").get(0).setVideoSource(videoSource, false);
		site.loadCommentsForVideo(videoSource);
	});
};

site.ajaxPageSuccessHandler = function(data, textStatus){
	jQuery(function($){
		var $set;
	
		$("#comments div.loader").hide();
		
		$set = $("#comments div.bottom-controls div.page-control");
		if($set.length == 0){
			$("#comments div.bottom-controls").append('<div class="page-control"></div>');
			$set = $("#comments div.bottom-controls div.page-control");
		}
		
		if(data.paginator != "") 
		$set.replaceWith(data.paginator);
		else $set.html(data.paginator);
		
		$set = $("#comments div.top-controls div.page-stats");
		$set.removeClass('page-stats');
		$set.replaceWith(data.pageStats);
		
		$set = $("#comment-list");
		$set.removeAttr("id");	
		$set.replaceWith(data.commentList);
		$("#comment-list div.comment-box div.wrap").hide().fadeIn();
	});
};

jQuery(function($){
	var EMAIL_DEFAULT = "Enter email address here.";
	
	var userFoundInspiring = {};
	var userFoundHealing = {};
	var sortOption;
	
	function rateComment($link, type){
		switch(type){
			case "inspired":
				if(userFoundInspiring[$link.attr("id")] != null) return false;
				else userFoundInspiring[$link.attr("id")] = true;
				
				break;
			case "healed":
				if(userFoundHealing[$link.attr("id")] != null) return false;
				else userFoundHealing[$link.attr("id")] = true;
				
				break;
			default:
				return false;
		}
		
		var $count = $link.parent().parent().find('div.count p');
		var count = Number($.trim($count.html()));
		
		$count.html(count + 1);
		$count.hide().fadeIn('fast');
		
		// find comment id
		var url = $link.attr('href');
		url = url.substr(url.indexOf('comment/') + 8);
		var id = url.indexOf('/') != -1 ? url.substr(0, url.indexOf('/')) : url;
		
		$.ajax({
			url:site.baseUrl() + "/comments/ajax-comment/comment/" + id + "/count/" + type
		});
	}
		
	$("form.comment-form div.input.submit input").live('click', function(event){
		$("#comment-form div.loader").show();
		
		$.ajax({
			url:site.baseUrl() + "/comments/ajax-form/webisode/" + site.globalVars.webisodeId,
			dataType:"json",
			type:"POST",
			data:$("form.comment-form").serialize(),
			success:function(data, textStatus){
				$("#comment-form").replaceWith(data.commentForm);
				
				if(data.formSuccess)
					$($("#comments ul.sort a")[0]).trigger('click');
				else{
					$("#comment-name").val(site.util.stripslashes($("#comment-name").val()));
					$("#comment-city").val(site.util.stripslashes($("#comment-city").val()));
					$("#comment-comment").val(site.util.stripslashes($("#comment-comment").val()));
				}
				
				$("#comment-form div.loader").hide();
			}
		});
		
		return false;
	});
	
	$("form.email-signup-form div.input.submit input").live('click', function(event){
		$("#email-signup div.loader").show();
		
		$.ajax({
			url:site.baseUrl() + "/signup/ajax-form/",
			dataType:"json",
			type:"POST",
			data:$("form.email-signup-form").serialize(),
			success:function(data, textStatus){
				$("#email-signup").replaceWith(data.emailSignup);
				
				$("#email-signup div.loader").hide();
			}
		});
		
		return false;
	});
	
	$("#comments ul.sort a").click(function(event){
		$("#comments ul.sort li").removeClass("selected");
		$(this).parent().addClass("selected");
		
		// find sort option
		var url = $(this).attr('href');
		url = url.substr(url.indexOf('sort') + 5);
		sortOption = url.indexOf('/') != -1 ? url.substr(0, url.indexOf('/')) : url;
		
		$.ajax({
			url:site.baseUrl() + "/comments/ajax-list/webisode/" + site.globalVars.webisodeId + "/sort/" + sortOption,
			dataType:"json",
			success:site.ajaxPageSuccessHandler
		});
		
		$("#comments div.loader").show();
		
		return false;
	});

	$("#comments div.bottom-controls div.page-control a").live('click', function(event){
		var arr = $(this).attr('href').split('/');
		var pageNum = arr[arr.length - 1];
		
		var url = site.baseUrl() + "/comments/ajax-list/webisode/" + site.globalVars.webisodeId;
		if(sortOption) url += "/sort/" + sortOption;
		url += "/page/" + pageNum;
		
		$.ajax({
			url:url,
			dataType:"json",
			success:site.ajaxPageSuccessHandler
		});
		
		$("#comments div.loader").show();
				
		return false;
	});
	
	$("#comments div.comment-box div.rate-controls li.inspired div.link a").live('click', function(event){
		rateComment($(this), "inspired");
		return false;
	});
	
	$("#comments div.comment-box div.rate-controls li.healed div.link a").live('click', function(event){
		rateComment($(this), "healed");
		return false;
	});
	
	// *** Webisode Navigation *** //
	
	function loadWebisodeFromNavButton($navBtn){
		var videoSource = $navBtn.data("videoSource");
		var listItem = $navBtn.parent().parent().parent();
		
		if(!videoSource) return false;
		
		$("#webisode-nav ol li.selected").removeClass("selected");
		listItem.addClass("selected");
		
		site.globalVars.webisodeId = $navBtn.data("webisodeId");
		
		$("#flash-vid-player").get(0).setVideoSource(videoSource, false);
		site.loadCommentsForVideo(videoSource);
		pageTracker._trackPageview('/webisode/' + videoSource.title);
		
		return false;
	}
	
	$("#webisode-nav li").bind('click', null, function(event){
		loadWebisodeFromNavButton($(this).find("div.info h5 a"));
	});
	
	$("#webisode-nav li h5 a").bind('click', null, function(event){
		loadWebisodeFromNavButton($(this));
		return false;
	});
	
	// *** EMAIL SIGNUP *** //
	
	$("#email-signup-input").val(EMAIL_DEFAULT);
	
	$("#email-signup-input").focus(function(event){
		if($(this).val() == EMAIL_DEFAULT){
			$(this).removeClass("default").val("");
		}
	});
	
	$("#email-signup-input").blur(function(event){
		if($(this).val() == "")
			$(this).addClass("default").val(EMAIL_DEFAULT);
	});
	
	$("#email-signup-submit").click(function(event){
		if($("#email-signup-input").val() == EMAIL_DEFAULT)
			return false;
	});
	
	// *** FEATURED COMMENTS CARASOUL *** //
	
	var autoInterval;
	var selectedIndex;
	var selectedComment;
	var $featuredList = $('#featured div.list div.comment');
	
	function resetFeaturedCommentIndex(){
		selectedIndex = null;
		console.log(setFeaturedCommentIndex);
		setFeaturedCommentIndex(0);
	}
	
	function setFeaturedCommentIndex(index){
		var $featuredList = $('#featured div.list div.comment');
		
		if(index == selectedIndex) 
			return;
		else if(selectedIndex < 0) 
			index = 0;
		else if(selectedIndex > $featuredList.length - 1) 
			index = $featuredList.length - 1;
		
		if(selectedComment){
			selectedComment.hide();
		}
				
		var $comment = $($featuredList[index]);
		$comment.fadeIn();
		
		selectedIndex = index;
		selectedComment = $comment;
	}
	
	site.resetFeaturedCommentIndex = resetFeaturedCommentIndex;
	
	function prevFeaturedComment(){
		var $featuredList = $('#featured div.list div.comment');
		if(selectedIndex <= 0) 
			setFeaturedCommentIndex($featuredList.length - 1);
		else 
			setFeaturedCommentIndex(selectedIndex - 1);
	}

	function nextFeaturedComment(){
		var $featuredList = $('#featured div.list div.comment');
		if(selectedIndex >= $featuredList.length - 1) 
			setFeaturedCommentIndex(0);
		else
			setFeaturedCommentIndex(selectedIndex + 1);
	}
	
	$("#featured a.prev-btn").live("click", function(event){
		if(autoInterval) clearInterval(autoInterval);
		prevFeaturedComment();
		return false;
	});
	
	$("#featured a.next-btn").live("click", function(event){
		if(autoInterval) clearInterval(autoInterval);
		nextFeaturedComment();
		return false;
	});
	
	$("#atffc").remove();
	
	$featuredList.hide();
	setFeaturedCommentIndex(0);
	autoInterval = setInterval(nextFeaturedComment, 15000);
});
