$(function() {
	resize($('#background'));
	$(window).resize(function() {
		resize($('#background'));
    });
	var link = $('#left-nav li a[href='+ document.location +']');
	var carousel_items = $('#carousel li');
	$('#manifest').cycle({ timeout: 7000 });
    $('#quote').cycle({ timeout: 9000 });
    if(link.length > 0) {
    	link.css('color','#99CC00');
    	Cufon.refresh();
    }
	$('#functions a').click(function() {
		var docu_height = $(document).height(); var target = $(this).attr('class');
		if(target == 'all') { $('.group').show(); $('#functions a').removeClass('selected');$(this).addClass('selected'); Cufon.refresh(); } 
		else { $('.group').hide(); $('#' + target +'').show();	$('#functions a').removeClass('selected'); $(this).addClass('selected'); Cufon.refresh(); }
		$('#outer-wrapper').css('height',docu_height + 'px');
	});			
	carousel_items.each(function(i) { $('#paginator-inner table tr').append('<td><a href="#" class="btn">' + (i+1) + '</a></td>'); });
	$('#left-nav ul li').each(function() { if($(this).hasClass('current', 'hasSubItems')) {	$(this).children('ul').show();}	});  	    	
	$('.video-link, .photo').prettyPhoto({ theme:'dark_rounded',showTitle:false,opacity: 0.50});		$.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photos.search&per_page=8&extras=url_o&user_id=42517374@N03&api_key=aec63af325865a932890d7a3b02921c2&format=json&jsoncallback=?', function(data) {
					$.each(data.photos.photo, function(i,item){
            			var url = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret;
            			var s = '_s.jpg'; var m = '_m.jpg'; var b = '_b.jpg'; var url_o = "";
            			
            			if(item.url_o==undefined) { url_o = url + b; } else { url_o = item.url_o;}
        				$('<a rel="photo-gallery[1]" href="'+ url_o +'"><img src="'+ url + s +'" height="63" width="63" /></a>').appendTo('#bottom-images-wrapper');
         	 		});
         	 		$('<a href="http://www.flickr.com/photos/vallankumous/">Katso kaikki kuvat &raquo;</a>').appendTo('#bottom-images-wrapper');
         	 		$('#bottom-images-wrapper a:nth-child(4n) img').addClass('last'); $('a[rel^="photo-gallery"]').prettyPhoto({theme:'dark_rounded',showTitle:false,outerLink: true,opacity: 0.50});
     	 		}); 		
	showCarousel();
	setImageGallery();
});

function showCarousel() {
	if($('#viewer').length > 0 ) {
		/* SLIDESHOW-ANIMAATION ASETUKSET */
		$('#carousel').jcarousel({
			scroll:1,
			auto:5,
			animation: 1100,
			initCallback:carousel_initCallback,
			itemVisibleInCallback:addCurrent,
			itemVisibleOutCallback:removeCurrent,
			buttonNextHTML:null,
			buttonPrevHTML:null,
			wrap: 'both'
		});
	}
}

function carousel_initCallback(carousel) {
    $('#paginator-inner a.btn').bind('click', function() { carousel.scroll($.jcarousel.intval($(this).text())); carousel.startAuto(0); return false; });
    $('#carousel-next').bind('click', function() { carousel.next(); carousel.startAuto(0); return false; });
    $('#carousel-prev').bind('click', function() { carousel.prev(); carousel.startAuto(0); return false; });
}
function addCurrent(carousel) {	$('#paginator-inner table tr td:nth-child('+ carousel.first +')').addClass('current'); }
function removeCurrent(carousel) { $('#paginator-inner table tr td:nth-child('+ carousel.prevFirst +')').removeClass('current'); }
function setImageGallery() {
	if($('#image-gallery').length > 0 ) {
		$('span#nav span').each(function() {
			$(this).children('div').appendTo('#image-gallery');
			$('span.description').show();
		});
		var $gallery = $('#image-gallery').cycle({timeout: 0,speed: 600 });
		$gallery.children().each(function(i) {$('#nav span a.thumb:eq('+ (i)+')').click(function() { $gallery.cycle(i+1); return false; }); });
	}
}
function resize(elem) {
	var startwidth = 1280;  
    var startheight = 720;
    var ratio = startheight/startwidth;
    var browserwidth = $(window).width();
    var browserheight = $(window).height();
    if ((browserheight/browserwidth) > ratio) {
        elem.height(browserheight);
        elem.width(browserheight / ratio);
        elem.children().height(browserheight);
        elem.children().width(browserheight / ratio);
    } else {
        elem.width(browserwidth);
        elem.height(browserwidth * ratio);
        elem.children().width(browserwidth);
        elem.children().height(browserwidth * ratio);
    }
    elem.children().css('left', (browserwidth - $(this).width())/2);
    elem.children().css('top', (browserheight - $(this).height())/2);
}