var topics=new Array();
var authors=new Array();
var publications=new Array();
var other=new Array();
var spinner='<img src="/images/spinner_email.gif" />';
if (document.images)
{
  preload_image = new Image(18,15); 
  preload_image.src="/images/spinner_email.gif"; 
}
$(document).ready(function(){
	subscribeClick();
	unsubscribeClick();
	
	
	$('a.add_subscribe_other').click(function(){
		var topic = $(this).attr('rel');
		var title = $(this).attr('title');
		if(topic){
			if(jQuery.inArray(topic,other)==-1){
				other.push(topic);
				$('<tr><td class="subscription">' + title + '<input type="hidden" class="other_list" name="other_list[]" value="' + topic + '" /><input type="hidden" class="other_title" name="other_title[]" value="' + title + '" /></td><td class="remove"><a title="' + topic + '" href="javascript:void(\'0\');">Remove</a></td></tr>').appendTo('#pending_subscriptions table#pending_other');
				activateRemove(other,'#pending_other');
				$('#pending_subscriptions').show();
				$('#pending_other').show();
			}
		}
	});
	
	//add element to pending
	$('a#add_subscribe_topic').click(function(){
		//topic to add
		var topic = $('#subscribe_topic').val();
		var zelda = $("#zelda").val();
		$('#think_topic').html(spinner);
		$('#think_alerts').html('');
		
		//filter
		if(zelda==""){
			$.post("/subscriptions/tag_exists.php",{
				 zelda: zelda,
				 topic: topic
			   }, function(xml) {
				   
				
				if(xml){
						//alert(xml);
					if(jQuery.inArray(xml,topics)==-1){
						topics.push(xml);
						//alert(topics);
						$('<tr><td class="subscription">' + topic + '<input type="hidden" class="list" name="list[]" value="http://www.realclearsports.com/topic/rss/?id=' + xml + '" /></td><td class="remove"><a title="' + xml + '" href="javascript:void(\'0\');">Remove</a></td></tr>').appendTo('#pending_subscriptions table#pending_topics');
						activateRemove(topics,'#pending_topics');
						$('#subscribe_topic').val("");
						$('#subscribe_topic').focus();
						$('#pending_subscriptions').show();
						$('#pending_topics').show();
						$('#think_topic').html('');
					}else{
						//element is already in pending	
						$('#subscribe_topic').val("");
						$('#subscribe_topic').focus();
						$('#pending_subscriptions').show();
						$('#pending_topics').show();
						$('#think_topic').html('');
					}
				}else{
					//alert("We are sorry but we don't have any content on this topic.");
					$('#think_alerts').html('We are sorry but we don\'t have any content on this topic.');
					$('#think_topic').html('');
				}
			});
		}
	});
	
	//add element to pending
	$('a#add_subscribe_author').click(function(){
		//topic to add
		var topic = $('#subscribe_author').val();
		var zelda = $("#zelda").val();
		$('#think_author').html(spinner);
		$('#think_alerts').html('');
		//filter
		if(zelda==""){
			$.post("/subscriptions/author_exists.php",{
				 zelda: zelda,
				 topic: topic
			   }, function(xml) {
				if(xml){
					if(jQuery.inArray(xml,authors)==-1){
						authors.push(xml);
						//alert(topics);
						$('<tr><td class="subscription">' + topic + '<input type="hidden" class="list" name="list[]" value="http://www.realclearsports.com/authors/rss/?id=' + xml + '" /></td><td class="remove"><a title="' + xml + '" href="javascript:void(\'0\');">Remove</a></td></tr>').appendTo('#pending_subscriptions table#pending_authors');
						activateRemove(authors,'#pending_authors');
						$('#subscribe_author').val("");
						$('#subscribe_author').focus();
						$('#pending_subscriptions').show();
						$('#pending_authors').show();
						$('#think_author').html('');
					}else{
						//element is already in pending	
						$('#subscribe_author').val("");
						$('#subscribe_author').focus();
						$('#pending_subscriptions').show();
						$('#pending_authors').show();
						$('#think_author').html('');
					}
				}else{
					//alert("We are sorry but we don't have any content on this author.");	
					$('#think_alerts').html('We are sorry but we don\'t have any content on this topic.');
					$('#think_author').html('');
				}
			});
		}
	});
	
	//add element to pending
	$('a#add_subscribe_publication').click(function(){
		//topic to add
		var topic = $('#subscribe_publication').val();
		var zelda = $("#zelda").val();
		$('#think_publication').html(spinner);
		$('#think_alerts').html('');
		//filter
		if(zelda==""){
			$.post("/subscriptions/pub_exists.php",{
				 zelda: zelda,
				 topic: topic
			   }, function(xml) {
				if(xml){
					if(jQuery.inArray(xml,publications)==-1){
						publications.push(xml);
						//alert(topics);
						$('<tr><td class="subscription">' + topic + '<input type="hidden" class="list" name="list[]" value="http://www.realclearsports.com/publications/rss/?id=' + xml + '" /></td><td class="remove"><a title="' + xml + '" href="javascript:void(\'0\');">Remove</a></td></tr>').appendTo('#pending_subscriptions table#pending_publications');
						activateRemove(publications,'#pending_publications');
						$('#subscribe_publication').val("");
						$('#subscribe_publication').focus();
						$('#pending_subscriptions').show();
						$('#pending_publications').show();
						$('#think_publication').html('');
					}else{
						//element is already in pending	
						$('#subscribe_publication').val("");
						$('#subscribe_publication').focus();
						$('#pending_subscriptions').show();
						$('#pending_publications').show();
						$('#think_publication').html('');
					}
				}else{
					//alert("We are sorry but we don't have any content on this publication.");
					$('#think_alerts').html('We are sorry but we don\'t have any content on this topic.');
					$('#think_publication').html('');
				}
			});
		}
	});

}); 
/*
function addPending(xml,elements,element,pendingList,pendingInput){
	if(xml){
		if(jQuery.inArray(xml,elements)==-1){
			elements.push(xml);
			//alert(topics);
			$('<tr><td class="subscription">' + element + '<input type="hidden" class="list" name="list[]" value="http://www1.realclearpolitics.com/topic/rss/?id=' + xml + '" /></td><td class="remove"><a title="' + xml + '" href="javascript:void(\'0\');">Remove</a></td></tr>').appendTo('#pending_subscriptions table'+ pendingList +'');
			activateRemove();
			$(pendingInput).val("");
			$(pendingInput).focus();
			$('#pending_subscriptions').show();
		}else{
			//element is already in pending	
			$(pendingInput).val("");
			$(pendingInput).focus();
			$('#pending_subscriptions').show();
		}
	}else{
		alert("We are sorry but we don't have any content on this topic.");	
	}	
}
*/

function subscribeClick(){
	$("#subscribe").click(function() {
								  //alert("test");
		if($('#think_subscribe')){
			$('#think_subscribe').html(spinner);
		}
		validateForm();
	});	
}

function unsubscribeClick(){
	$("#unsubscribe").click(function() {
		//alert("test");
		sendUnsubscribe();
		//validateForm();
	});	
}

function activateRemove(myArray, container){
	$('#pending_subscriptions td.remove a').click(function(){	
		//alert($(this).attr("title"));
		//alert(jQuery.inArray($(this).attr("title"), myArray));
		myArray.splice(jQuery.inArray($(this).attr("title"), myArray), 1);
		if(myArray.length<=0){
			$(container).hide();
		}
		//alert(myArray);
		$(this).parent().parent().remove();
	});
}

function sendSubscribe() {
	var lists = new Array();
	var other_lists = new Array();
	var other_titles = new Array();

	$("#pending_subscriptions input.list").each(function(id) {
		var myVar=$(this).val();
		//alert(myVar);
		if(myVar!=""){
			lists.push(myVar);
		}
	});
	$("#pending_subscriptions input.other_list").each(function(id) {
		var myVar=$(this).val();
		//alert(myVar);
		if(myVar!=""){
			other_lists.push(myVar);
		}
	});
	$("#pending_subscriptions input.other_title").each(function(id) {
		var myVar=$(this).val();
		//alert(myVar);
		if(myVar!=""){
			other_titles.push(myVar);
		}
	});
	if(lists.length>0 || other_lists.length>0){
		$.post("/subscriptions/do_subscribe.php",{
			 zelda: $("#zelda").val(),
			 list_email: $("#list_email").val(),
			 'lists[]': lists,
			 'other_lists[]': other_lists,
			 'other_titles[]': other_titles
		   }, function(xml) {
			//alert(xml);
			//alert user of result
			//jQuery.facebox(xml);
			jQuery.facebox(xml);
			var pending='<p>Pending Subscriptions:</p><table id="pending_topics"><tr><th colspan="2">Topics:</th></tr></table><table id="pending_authors"><tr><th colspan="2">Authors:</th></tr></table><table id="pending_publications"><tr><th colspan="2">Publications:</th></tr></table><table cellpadding="0" cellspacing="0" border="0" id="pending_other"><tr><th colspan="2">Other Subscriptions:</th></tr></table><button type="button" id="subscribe" name="subscribe">Subscribe</button>';
			if($('#pending_subscriptions').attr('title')!='solid'){
				$('#pending_subscriptions').html(pending);
				$('#pending_subscriptions').hide();
				subscribeClick();
			}else{
				$('#think_subscribe').html('');	
				//$('#list_email').val('');
				$("#pending_subscriptions input.list").each(function(id) {
					if($(this).attr('type')=='checkbox'){
						$(this).attr('checked', false);
					}
				});
			}
			topics.length=0;
			authors.length=0;
			publications.length=0;
			other.length=0;
			
			//alert(other.length);
			//subscribeClick();
			//cleanForm();
		});
	}else{
		$('#think_subscribe').html('Please select a list to subscribe to.');
	}
}

function sendUnsubscribe() {
	lists = new Array();
    $("input:checked").each(function(id) {
    	myVar = $("input:checked").get(id);
        lists.push(myVar.value);
    }); 
	
	$.post("/subscriptions/do_unsubscribe.php",{
		 zelda: $("#zelda").val(),
		 list_email: $("#hidden_email").val(),
		 'lists[]': lists
	   }, function(xml) {
	 	//alert(xml);
		jQuery.facebox(xml);

		cleanForm();
	});
}

function cleanForm(){
	$('#lists table :input').each(function(){
		if(this.tagName!="BUTTON" && this.tagName!="button"){
			if(this.type=="checkbox"){
				this.checked=false;
			}else{
				this.value="";
			}
		}
	});
}

function validateForm(){
	var e = $('#list_email').val();	
	if(validEmail(e)){
		$('#think_email').html('');
		sendSubscribe();		
	}else{
		$('#think_email').html('Please enter a valid email address.');
		$('#think_subscribe').html('');
	}
}

function validEmail(email){      
	var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailReg.test(email); 
}

