var core = {};

core.utilities = {};
core.player = {};
core.commentBox = {};
core.shareStoryForm  = {};

core.utilities.formValuesToJSON = function(formId){
	var data = {};
	$.each($("#" + formId).serializeArray(), function(i, field){
		 if(data[field.name]){
		 	if(data[field.name].constructor != Array){
		 		data[field.name] = [data[field.name]];
		 	}
		 	data[field.name].push(field.value);
		 } else {
		 	data[field.name] = field.value;
		 }
    });
  	return data;
};


core.commentBox.toggle = function(){
	if($("#comment-box-wrapper").css("display") == "none"){
		$("#comment-box-wrapper").show("fast");
		$("#comment-box form p:last").addClass("comment-box-state-position");
		core.commentBox.refresh();
	} else {
		
		$("#comment-box-wrapper").css("display", "none");
	}
}

core.commentBox.showThankYou = function(isEsp){
	var thankYouHtml = '<h4>Thank You</h4><a href="#" onclick="core.commentBox.toggle();" id="close-comments">close</a><p class="center">Unfortunately, we cannot reply personally to feedback.</p>';
	if(isEsp){
		thankYouHtml = '<h4>Gracias</h4><a href="#" onclick="core.commentBox.toggle();" id="close-comments">cerrar</a><p class="center">Desafortunadamente, no podemos responder personalmente a su sugerencia.</p>';
	}
	$("#comment-box").html(thankYouHtml);
	core.commentBox.refresh();
	setTimeout('$("#comment-box-wrapper").fadeOut("slow")',3000)
};


core.commentBox.showUserErrors = function(name, errors){
	$("*[@name='"+ name +"']").after((function(){
		return (errors.length > 1)
			? ' <span class="error">' + errors[1] + '</span>'
			: '';
	})());

};

core.commentBox.submitFeedback = function(){
	
	$("#comment-box span.error").remove();
	$("#comment-submit").attr("disabled", "disabled");
	$("#comment-box #ajax-loader").show();
	var json = core.utilities.formValuesToJSON("feedback-form");
	var langToken = window.location.href.split("lang=")
	var lang = (langToken.length > 1) ? langToken[1] : "eng"
	$.post("/parents/feedback/process?lang=" + lang  + "&nocache=" + (new Date()).getTime(), json, null, "script");
	$("#comment-submit").removeAttr("disabled");
	$("#comment-box #ajax-loader").hide();
	return false;
};

core.commentBox.refresh = function(){
	
	$("#comment-box span.error").remove();
	$("#comment-box #id_feedback").val("");
	
	sIFR.refresh.commentBoxHeader();
	
};


core.player.hashRegExp = /.*#(.[^-]*)-?(\d+)?/;

core.player.init = function(clipNav, clipToken){
	
	if(core.player.isPresent){
		//$("#player a[@href^='#']").click(core.player.process);
		//$("#clip-nav li").click(core.player.process);
		$(".clips li").click(core.player.process);
		$(".player-content:not(:first)").hide();
		if(document.location.hash){
			var re = core.player.hashRegExp.exec(document.location.hash);
			core.player.showActiveClipNav(re[1], re[2] || "x", true);
		} else {
			core.player.showActiveClipNav(clipNav, clipToken, true);
		}
	}
	
}

core.player.play = function(clipToken){
	
	document.getElementById("videoPlayer").selectClip(clipToken);
	
}


core.player.showClip = function(aHref, li, clipToken){
	
	
	var re = core.player.hashRegExp.exec(aHref);
	clipNav = re[1];
	clipToken = re[2];
		
	
	core.player.showClipCaptionAndListItem(li, clipToken);
	core.player.showActiveClipNav(clipNav = re[1], clipNav = re[2]);
	core.player.play(clipToken);
	core.player.omnitureTrack(aHref);
}


core.player.showClipCaptionAndListItem = function(liOrAHref, clipToken){
	$("ul.clips li").removeClass("active");
	
	(typeof liOrAHref == "object")
		? liOrAHref.addClass("active")
		: $("a[@href='" + liOrAHref + "']").parent().addClass("active");
	
	$(".player-content").hide();
	$("#player-left-panel #player-content-" + clipToken).show();
}



core.player.showActiveClipNav = function(clipNav, clipToken, init){
	$("ul#clip-nav li").removeClass('active');
	$("li#clip-nav-" + clipNav).addClass('active');
	$("ul.clips").hide();
	$("ul#"+ clipNav +"-clips").show();
	
	if(init){
		setTimeout(function(){core.player.play(clipToken)}, 500);
		core.player.showClipCaptionAndListItem("#" + clipNav+ "-" + clipToken, clipToken);
	} else{
		//document.location = document.location.pathname + "#" + clipNav + "-0";
	}
}

 var nextclip = function(clip){
	 var clipNav = "#veterans";
	 var li = null;
	 var aHref = null;
	 
	 if(clip >=5){
		 clipNav = "#parents";
	 }
	 
	 var aHref = clipNav+"-"+clip;
	 var li = $("a[@href='" + aHref + "']").parent();
	 
	 core.player.showClip(aHref, li, clip);
	 
 }
 
core.player.process = function(clipNav, clipToken){
	
	
	var a, li;
	clipToken = clipToken || 0;
	
	if(this.tagName == "LI"){
		li = $(this);
		a = $("a:first", this);
		var href = a.attr("href");
		if(href.indexOf("#") == 0){
			//document.location = document.location.pathname + href;
		}
	} else {
		a = $(this);
		li = a.parent();
	}
	
	(typeof clipNav == "object")
		? core.player.showClip(a.attr("href"), li, clipToken)
		: core.player.showActiveClipNav(clipNav, clipToken);
		

	
}


core.shareStoryForm.init = function(){
	if(core.shareStoryForm.isPresent)
	{
		$("input:not(:first)", $("a#show_more_uploads").parent()).hide();
	}

}

core.player.omnitureTrack = function(href){
	
		var theUrl = "in-their-own-words"+href;
		
		s.linkTrackVars="eVar6,prop6,events";
		s.linkTrackEvents="event4";
		
		s.events = "event2";
		s.prop6 = theUrl;
		
		s.tl(this,'c','link_'+theUrl);
		
	
}

core.shareStoryForm.showMoreUploads = function(){
	
	var input = $("input:hidden:first", $("a#show_more_uploads").parent())
	if(input){
		input.addClass("file_upload").show();
		if(input.attr("name") ==  "file_3")
		{
			$("a#show_more_uploads").hide();
		}
	}

}

$(function(){
	
	
	core.player.init("parents", 5);
	core.shareStoryForm.init();
	

});
