/*
-------------------------------------------------------- 
The Fabler - Viewer specific js
Author: Sam Lu
Author URI: http://samlu.ca

-------------------------------------------------------- 
*/

// Functions

var Viewer = {
	init: function(){		
		// MORE FROM (ARTIST) - Located on right hand side of comic viewing page
		$('.more_from').attr('href', 'javascript:;');
		$('.more_from').click(function() {
			$('.more_from').toggleClass('more_from_open');
			$('.mf_list').slideToggle("fast");
		});
		
		// COMMENT FORM
		$('#comment-form-info').append('<input type="submit" value="" class="post_comment" id="commentSubmit" disabled="disabled" />');

		// SEND TO A FRIEND		
		$('.send_to_a_friend a').attr('href', 'javascript:;');
		$('.send_to_a_friend a').click(function() {
			$.modal('<div id="modalbox"><p id="spinner" style="display: none;">Sending Email... <img src="/images/spinner.gif"></p><form action="/comic/mail" method="POST" id="mail_form"><h3 id="send_to_a_friend">Send to a Friend</h3><label for="email">To:</label> <input type="text" name="to" id="to" value="" class="input-largetext" style="width: 275px; color: #000000;" /><label for="subj">Subject:</label> <input type="text" name="subj" id="subj" value="" class="input-largetext" style="width: 275px; color: #000000;" /><label for="msg" class="clearleft">Message:</label><textarea name="msg" id="msg" rows="4" cols="40" class="input-largetext clearleft" style="width:380px; color: #000000;">Hey! I thought you\'d like to check out this comic over at The Fabler!</textarea><br /><input type="button" title="Send" class="send_btn" /></div>',
					{
						onShow: function(){$("#comicviewer").css("visibility", "hidden");},
						onClose: function(dialog){$.modal.close(); $("#comicviewer").css("visibility", "visible");} 
					});
			$("#msg").focus(function(){this.select();});
			$("#simplemodal-container .send_btn").click(function(){submitMailForm();});
			$("#simplemodal-container").css("position", "absolute");
			
		});
		
		
	}
}

function submitMailForm(){
	$("#mail_form").css("display", "none");
	$("#spinner").css("display", "block");
	$("#modalbox .warnings, #modalbox .success").remove();
	
	$.ajax({
		"type": "POST",
		"url": "/comic/mail",
		"dataType": "xml",
		"data": "to=" + $("#to").val() + "&msg=" + $("#msg").val() + "&subj=" + $("#subj").val(),
		"success": function(data){handleMailResponse(data);},
		"error": function(){alert("Oops, something went wrong and we were unable to send your email.");}
	});
	
	$("#mail_form").css("display", "block");
	$("#spinner").css("display", "none");
	
}

function handleMailResponse(response){
	var resType = $(response).find("result").text();
	
	var errorList = "<ul>";
	var errorListEnd = "</ul>";
	var liStart = "<li>";
	var liEnd = "</li>";
	
	$("#mail_form").css("display", "block");
	$("#spinner").css("display", "none");
	
	var cntErr = 0;
	
	if(resType == "0"){
		if($(response).find("from").length > 0){
			errorList += liStart + "Please enter a valid From email address" + liEnd;
			cntErr++
		}
		
		if($(response).find("to").length > 0 && $(response).find("numRecips").length == 0){
			
			$(response).find("to").each(function(){
				errorList += liStart + $(this).text() + " is not a valid email address" + liEnd;
				cntErr++;
			});
			
		}
		
		if($(response).find("numRecips").length > 0){
			errorList += liStart + "Please enter between 1 and 10 \"to\" email addresses" + liEnd;
			cntErr++
		}
		
		
		if($(response).find("subject").length > 0){
			errorList += liStart + "Please enter a subject line" + liEnd;
			cntErr++;
		}
		
		if(cntErr > 0){			
			$("#simplemodal-container").css("height", 380 + 30 + (cntErr * 25) + "px");
		}
		
		$("#send_to_a_friend").before('<div class="warnings"><strong>The following errors occurred:</strong>' + errorList + '</div>');
		
	}else{
		$("#simplemodal-container").css("height", 380 + 40 + "px");
		$("#send_to_a_friend").before('<div class="success">Your email has been sent successfully</div>');
	}
}

$(document).ready(function(){
	Viewer.init();
});
