$(init_page);
var categories=0;
function init_page() {
	$(window).resize(background_resize);
	background_resize();
	
  setup_blog_slider();
	
	$("#content_left").height($('#content').height() + 'px');
	$('#content_main a img.fancy').each(function() {
        $(this).parents('a:first').fancybox();
    });
}

function setup_blog_slider() {
  var w = 0;
  $("#latest_blog_posts >li").each(function() {
    w += parseInt($(this).outerWidth(true));
	categories++;
  });
  
  $("#latest_blog_posts").width(w + "px");
  
  $("#latest_blog_posts_wrapper .next.arrow").click(blog_slider_next);
  $("#latest_blog_posts_wrapper .prev.arrow").click(blog_slider_previous);
  
  $('a.zoombox').fancybox({
    'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
  });
	
  }
var blog_slider_busy = false;

function blog_slider_next() {
  var offset=301;
  
  if(blog_slider_busy || (parseInt($("#latest_blog_posts").width()) <= (parseInt($("#latest_blog_posts").css('left').replace('px', '').replace('PX', '').replace('-', '')) + 903)))
    return false;
    
  blog_slider_busy = true;    
  $("#latest_blog_posts").animate({
    left: '-='+offset+'px',
  },{
    duration: 1000,
    easing: 'swing',
    complete: function() {
      blog_slider_busy = false;
    }
  });
}  
function blog_slider_previous() {
  if(blog_slider_busy || (parseInt($("#latest_blog_posts").css('left').replace('px', '').replace('PX', '')) >= 0))
    return false;
  
  blog_slider_busy = true;  
  $("#latest_blog_posts").animate({
    left: '+=301px',
  },{
    duration: 1000,
    easing: 'swing',
    complete: function() {
      blog_slider_busy = false;
    }
  });
}

function background_resize(){ 
  var w_width = $(window).width();
  var w_height = $(window).height();
  
  $("#background").height(w_height + 'px');
  
	if (w_width > 1202 || w_width > 559.6){
		if (w_width > w_height / 0.47)
		{
			w = w_width;
			h = w_width * 0.47;
		} else
		{
			w = w_height / 0.47;
			h = w_height;
		}
	} else
	{
		w = 1202;
		h = 559.6;
	}	

 	$('#background img').width(w);
  $('#background img').height(h);
	$('#background img').css('margin-left', '-' + (w / 2) + 'px');
}
