jQuery(document).ready(function(){
    
	//Twitter
	var div = jQuery(".tweet");
	div.tweet();
	div.find(".tweet_list").addClass("list");

	//Get total number
	slider_num_items = jQuery("div#slideshow-carousel li a").length;
	
	//Recent / Popular / Archive posts
	jQuery(".news_side li.box-link a").click(function(){
		var type = this.id;
		jQuery(".news_side .list").hide();
		jQuery(".news_side .list." + type).show();
		
		jQuery(".news_side .box-link a").removeClass("active");
		jQuery(this).addClass("active");
		
		return false;
	});
	
	
    //jCarousel Plugin
	jQuery("#carousel").jcarousel({
      vertical: true,
      scroll: 1,      
      wrap: 'last',
      initCallback: mycarousel_initCallback      
	});

  	//Front page Carousel - Initial Setup
    jQuery('div#slideshow-carousel a img').css({'opacity': '0.5'});
    jQuery('div#slideshow-carousel a img:first').css({'opacity': '1.0'});
    jQuery('div#slideshow-carousel li a:first').append('<span class="arrow"></span>');

    //Combine jCarousel with Image Display
    jQuery('div#slideshow-carousel li a').hover(
		function () {
			if (!jQuery(this).has('span').length) {
				clearInterval(slider_int);
				jQuery('div#slideshow-carousel li a img').stop(true, true).css({'opacity': '0.5'});
				jQuery(this).stop(true, true).children('img').css({'opacity': '1.0'});
			}
        },
        function () {
        		resetInt();
          		jQuery('div#slideshow-carousel li a img').stop(true, true).css({'opacity': '0.5'});
          		jQuery('div#slideshow-carousel li a').each(function () {

            	if (jQuery(this).has('span').length){
            		jQuery(this).children('img').css({'opacity': '1.0'});
            	}
			});
        }).click(function () {
        	resetInt();
        	
        	var rel = jQuery(this).attr('rel');
        	slider_current = rel.replace("p", "") * 1;
        	
			jQuery('span.arrow').remove();
    		jQuery(this).append('<span class="arrow"></span>');
        	jQuery('div#slideshow-main li').removeClass('active');
        	jQuery('div#slideshow-main li.' + rel).addClass('active');
        	return false;
	});
});

//Carousel Tweaking
function mycarousel_initCallback(carousel) {
	slider_carousel = carousel;
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
	
	//Set slider timeout
	resetInt();
}

//Move big image
var slider_num_items;
var slider_current = 1;
var slider_int;
var slider_carousel;

function moveBig(){
	if(++slider_current > slider_num_items){
		slider_current = 1;
	}
	var rel = "p" + slider_current;
	
	jQuery('span.arrow').remove();
	jQuery('div#slideshow-carousel li a[rel='+rel+']').append('<span class="arrow"></span>');
    jQuery('div#slideshow-main li').removeClass('active');
    jQuery('div#slideshow-main li.' + rel).addClass('active');

    jQuery('div#slideshow-carousel li a img').stop(true, true).css({'opacity': '0.5'});
	jQuery('div#slideshow-carousel li a[rel='+rel+']').stop(true, true).children('img').css({'opacity': '1.0'});
    
    slider_carousel.scroll(slider_current);
}

function resetInt(){
	clearInterval(slider_int);
	slider_int = setInterval(moveBig, 6000);
}
