$(document).ready(function(){
	function onAfter(){
		var description = this.alt;
		var explode = description.split('|');
		var newdescription = explode[0]+' <a href="'+explode[1]+'">More</a>';
		$('.sd-title').html(this.title);
		$('.sd-description').html(newdescription);
	}
	
	//Suckerfish hover function
	sfHover = function() {
		var sfEls = document.getElementById("navigation").getElementsByTagName("li");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
	
	//For IE 6 only 
	sfHover();
	
	$(".tabbed-box li").mouseover(function(){
		$(this).addClass("tb-hover");
	}).mouseout(function(){
		$(this).removeClass("tb-hover");
	})
	
	
	jQuery("#mycarousel").jcarousel();
	var firstcontent = $('#tab1').html();
	$('#tabactive').html(firstcontent);

	$('li.tb-item').live('click', function(event){
		event.preventDefault();
		var tab = parseInt($(this).index())+1;
		$('li.tb-item-selected').each(function(){
			$(this).removeClass('tb-item-selected').addClass('tb-item');
		})
		$(this).removeClass('tb-item').addClass('tb-item-selected');
		var newtab = '#tab'+tab;
		var newcontent = $(newtab).html();
		$('#tabactive').html(newcontent);
	});
	$('.slideshow-main').cycle({
		fx: 'fade',
		timeout: 5000,
		pager: '.slideshow-pager',
		after: onAfter
	});
	//comic thumbnail links when clicked
	$('.comic-thumbnail a').click(function(event){
		event.preventDefault();
		var linkindex = $(this).index();
		var image = $(this).attr('href');
		var html = '<img src="'+image+'" alt="cover" title="cover" />';
		$('.comic-cover-change img').fadeOut(function(){
			$('.comic-cover-change a.displayed').html('');
			$('.comic-cover-change a.displayed').removeClass('displayed').addClass('not-displayed');
			$('a.not-displayed').each(function(index){
				if(linkindex==index){
					$(this).removeClass('not-displayed').addClass('displayed');
					$(this).html(html);
				}
			})
		});
		$('.comic-thumbnail a.selected').removeClass('selected').addClass('not-selected');
		$(this).addClass('selected');
	});
	//condition when comic cover clicked, fancybox will be called
	$('a[rel=slide]').fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	//write comment and login pop out
	var loginhtml = $('#login').html();
	var forgothtml = $('#forgot-password').html();
	$('.cdc-comment .write-comment').fancybox();
	$('.account .login').fancybox({
		'onComplete' : function(){
			$('#login').hide(function(){
				$(this).html(loginhtml);
				$(this).fadeIn();
			});
		}
	});
	//clicking on forgot password link will change the login window content to forgot password
	$('.link-forgot-password').live('click', function(){
		$('#login').html(forgothtml);
	})
});
