var displayStoryarcs = true;
var activeProjectId = "project_1";

$(document).ready(function() {
	if( $('#activeComic_caption').text().length > 0){
		$('#comic_caption').show();	
	}
	else{$('#comic_caption').hide();}
	
    	$('#loadingImage').hide()
	updateComicNav($('.older').length, $('.newer').length);	
   	
   	if (displayStoryarcs == true){
   		updateStoryarcNav($('.storyarcPanel').attr('id'));
   	}
	
    	initArchive();
    	hideShare();
	
	$('#backgroundPopup').click(function(){
		hideShare();
	});

	$('.about').click(function(){
		$('#slidingContainer').animate({
    			marginLeft: '0px'
  		}, 500, function() {
    				// Animation complete.
  			});
	});
	
	$('.logo').click(function(){
		$('#slidingContainer').animate({
    			marginLeft: '-940px'
  		}, 500, function() {
    				// Animation complete.
  			});
	});
	
	$('.archive').click(function(){
		$('html,body').animate({
    					scrollTop: 0
		}, 'fast');
		$('#slidingContainer').animate({
    			marginLeft: '-1880px'
  		}, 500, function() {

    				// Animation complete.
  			});
	});
	
	$('.share').click(function(){
		showShare();
	});
	
    	$('.storyarcNode').find('.title:first').hide()
    	
    	if (displayStoryarcs == true){
		$('.storyarcNode').hover(function() {
	       		$(this).find('.title:first').show()
	    	}, function() {
	       	 	jQuery(this).find('.title:first').hide();
	    	});
	}
	
	if( $.cookie('bookmark') == null ) { 
    		$('#bookmark').addClass('addBookmark');
	}
	else{
		$('#bookmark').addClass('removeBookmark');
	}
	
	$('#authorAmy').click(function () {
		$('#profileChris').hide();
		$('#profileAmy').toggle();
	});
	$('#authorChris').click(function () {
		$('#profileAmy').hide();
		$('#profileChris').toggle();
	});
	
	$('#directLink').click(function() { selectAllText(jQuery(this)) });

	
	$("#comicList").change( function(){
			var comic_id = $('#comicList').val();
			var url="php/comicDisplayHandler.php";
			
			$.get( url, 
				{action:'displayComic', project_id: activeProjectId, comic_id: comic_id},
				function(data){
					displayComic(data);
					$('#slidingContainer').animate({
    						marginLeft: '-940px'
  						}, 500, function() {
    						// Animation complete.
  					});
			});
			
	});	

	

});

function displayComic(data){
		var d = new Date();
		var cache = d.getTime();
		
		$('html,body').animate({
    			scrollTop: 0
		}, 'fast');


		$('#activeComic_url').hide();
		$('#loadingImage').show();

		$('#activeComic_id').attr('value', "comic_" + data.comic_id);
		$('#activeComic_title').text(data.comic_title);
		$('#activeComic_datePublished').attr('datetime', data.comic_date_published);
		$('#activeComic_datePublished').text(data.comic_date_formatted);
		$("#activeComic_url").attr("src","img/comics/" + encodeURIComponent(data.comic_url) +'?'+ cache );
		$("#activeStoryarcImage").css("background-image", "url(img/storyarcs/" + encodeURIComponent(data.storyarc_url) +'?'+ cache +')');
		$('#activeComic_caption').html(data.comic_caption);
		$('.discuss').attr("href", "http://cn-comics.com/forum/" + data.comic_forum_url);
			
		updateComicNav(data.comic_before, data.comic_after);
		updateSocialLinks(data.comic_id, data.comic_title);
		
		if (displayStoryarcs == true){
			updateStoryarcNav("storyarc_" + data.comic_storyarc_id);
		}
		
        	$("#activeComic_url").unbind();
        	$('#activeComic_url').imagesLoaded(function(){
		
			$('#loadingImage').hide();			
			$('#activeComic_url').show();

		});
		
		if(data.comic_caption==""){$('#comic_caption').hide();
		}else{$('#comic_caption').show();}
	}

function disableNavigation(){
   $('.storyarcNode').unbind();
   $('.oldest, .older, .newest, .newer').unbind();
}

	
	function updateComicNav(before, after){
		
		var url="php/comicDisplayHandler.php";
		var activeId = $('#activeComic_id').attr('value');
		
		$('.oldest').unbind();
		$('.oldest').click(function(){
                        disableNavigation();
			$.get(	url, 
				{action: 'displayOldestComic', project_id: activeProjectId},
				function(data){
					displayComic(data);
				}
			)
		});
		
		if (before == 1){
			$('.inactiveOlder').addClass('older').removeClass('inactiveOlder');
		
			$('.older').unbind();
			$('.older').click(function(){
                                disableNavigation();
				$.get(	url, 
					{action: 'displayOlderComic', project_id: activeProjectId, comic_id: activeId},
					function(data){
						displayComic(data);
					}
				)
			});	
		} else {
			$('.older').unbind();
			$('.older').addClass('inactiveOlder').removeClass('older');
		}
		
		
		$('.newest').unbind()
		$('.newest').click(function(){
                        disableNavigation();
			$.get(	url, 
				{action: 'displayNewestComic', project_id: activeProjectId},
				function(data){
					displayComic(data);
				}
			)
		});
		
		if (after == 1){
			$('.inactiveNewer').addClass('newer').removeClass('inactiveNewer');
		
			$('.newer').unbind();
			$('.newer').click(function(){
                                disableNavigation();
				$.get(	url, 
					{action: 'displayNewerComic', project_id: activeProjectId, comic_id: activeId},
					function(data){
						displayComic(data);
					}
				)
			});	
		} else {
			$('.newer').unbind();
			$('.newer').addClass('inactiveNewer').removeClass('newer');
		}
		
	
	}
	
	function updateStoryarcNav(storyarc_id){
		var url="php/comicDisplayHandler.php";
		
		$('.storyarcNode').unbind();
		
		if (storyarc_id != "storyarc_1"){
			$("#activeStoryarcImage").show();
			$.get( url,
			{action: 'displayStoryarcNav', project_id: activeProjectId, storyarc_id: storyarc_id},
			function(data){
				$('.storyarcPanel').html(data);
				$('.storyarcNode').find('.title:first').hide();
				
				$('.storyarcNode').unbind();
				$('.storyarcNode').click(function(){
						var url="php/comicDisplayHandler.php";
						var comic_id = $(this).attr('id');
	
						disableNavigation();
	
						$.get( url, 
						{action:'displayComic', project_id: activeProjectId, comic_id: comic_id},
						function(data){
							displayComic(data);
						});	
				});
				
				
				$('.storyarcNode').hover(function() {
				   $(this).find('.title:first').show()
				}, function() {
					jQuery(this).find('.title:first').hide();
				});
				
			});
		}
		else{ $('.storyarcPanel').empty(); $("#activeStoryarcImage").hide()}
	}
	
	function updateSocialLinks(comic_id, comic_title){
		$('.share ul').hide();
		$('.share .arrow').hide();
		$('.share').click(function(){
			$('.share ul').show();
			$('.share .arrow').show();
		});
		$('.socialLink.emailIcon').attr('href', "mailto:?Subject=" + encodeURI("[citation_needed] - " + comic_title));
		$('.socialLink.facebookIcon').attr('href', "http://www.facebook.com/sharer/sharer.php?u=" + encodeURI("http://cn-comics.com/index.php?comic_id=comic_" + comic_id) + "&t=" + encodeURI("[citation_needed] - " + comic_title));
		$('.socialLink.twitterIcon').attr('href', "https://twitter.com/share?text=" + encodeURI("Check out " + comic_title +" at ") + "&url=" + encodeURI("http://cn-comics.com/index.php?comic_id=comic_" + comic_id) + "&via=" + encodeURI("cncomics"));
		$('.socialLink.diggIcon').attr('href', "http://digg.com/submit?url=" + encodeURI("http://cn-comics.com/index.php?comic_id=comic_" + comic_id) + "&title="+ encodeURI("[citation_needed] -" + comic_title));
		$('.socialLink.stumbleuponIcon').attr('href', "http://www.stumbleupon.com/submit?url=" + encodeURI("http://cn-comics.com/index.php?comic_id=comic_" + comic_id) + "&title="+ encodeURI("[citation_needed] -"+ comic_title));
		$('.socialLink.redditIcon').attr('href', "http://reddit.com/submit?url=" + encodeURI("http://cn-comics.com/index.php?comic_id=comic_"+comic_id) + "&title=" + encodeURI("[citation_needed] - " + comic_title));			
		$('#directLink').val("http://cn-comics.com/index.php?comic_id=comic_" + comic_id);
	}
	
	
	function hideShare(){
		$("#backgroundPopup").hide();  
		$(".share ul").hide(); 
		$(".share .arrow").hide(); 
	}
	
	function showShare(){
		$("#backgroundPopup").show()
		$(".share ul, .share .arrow").show(); 
		$('#directLink').select
	}		
	
	function initArchive(){
                $('#archive li').unbind();
		$('#archive li').click(function(){
			
			var url="php/comicDisplayHandler.php"
			var storyarc_id = $(this).attr('id');
			
			$.get(	url, 
				{action: 'displayStoryarcFirst', project_id: activeProjectId, storyarc_id: storyarc_id},
				function(data){
					$('#slidingContainer').animate({
    						marginLeft: '-940px'
  					}, 500, function() {
    						// Animation complete.
  					});
					displayComic(data);
				});
		});
	}
	
	function selectAllText(textbox) {
    		textbox.focus();
    		textbox.select();
	}

