var slider, snurrig, jobs, jobs_big;

var set_window_size = function () {
   var obj = {};
   if (typeof window.innerWidth !== undefined) {
       // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
       obj.width = window.innerWidth;
       obj.height = window.innerHeight;
   } else if (typeof document.documentElement !== undefined && typeof document.documentElement.clientWidth !== undefined && document.documentElement.clientWidth !== 0) {
       // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
       obj.width = document.documentElement.clientWidth;
       obj.height = document.documentElement.clientHeight;
   } else {
       // older versions of IE
       obj.width = document.getElementsByTagName('body')[0].clientWidth;
       obj.height = document.getElementsByTagName('body')[0].clientHeight;
   }
   $("#window-size").removeClass('red').removeClass('blue');
   if(obj.width <= 960 && obj.width > 640) {
		$("#window-size").addClass('blue');
	 }else if(obj.width <= 640){
		$("#window-size").addClass('red');
	 }
   $("#window-size span").html(obj.width);
};

var update_job_grid = function(width) {
	var jobs = $("#jobs")
	var uls = $("ul",jobs).size();
	var first_lis = $("ul:first li",jobs).size();
	var lis = $("li",jobs);
	
	var max_count = $("body").hasClass('home') ? 8 : 12;
	
	if(width > 768 && uls > 1 && first_lis < max_count) {
		insert_jobs(lis, max_count);
	} else if(width > 480 && width <= 768 && (first_lis > 6 || (first_lis < 6 && uls > 1))) {
		insert_jobs(lis, max_count);
	} else if(width <= 480 && (first_lis > 2 || (first_lis < 2 && uls > 1))) {
		insert_jobs(lis, max_count);
	}
};

var jobs_slider;
var front_slider;

var insert_jobs = function(jobs, max_count) {
		var width = $(window).width();
		$("#jobs ul").remove();
		
		var i = 0;
		if($("body").hasClass('home'))
		{
			if(width <= 768)
				max_count = 6;
			
			if(width <= 480)
				max_count = 2;
		} else {
			if(width <= 768)
				max_count = 9;
			
			if(width <= 480)
				max_count = 4;
		}
		
		$(jobs).each(function(index, value) {
			if(index % max_count === 0) {
				i=index;
				
				if($("#jobs-"+i).length == 0)
				{
					var url = $("<ul>").attr('id','jobs-'+i).css('width','25%');
					url.appendTo("#jobs");
				}
			}

			if(
				((max_count === 8 || max_count === 12) && (index+1) % 4 === 0) || 
				((max_count === 6 || max_count === 9) && (index+1) % 3 === 0) || 
				((max_count === 2 || max_count === 4) && (index+1) % 2 === 0)
			){
				$(this).addClass('last-column');
			} else {
				$(this).removeClass('last-column');
			}

			$(this).removeClass('hidden');
			if($(this).is(":visible")) {
				$(this).appendTo('#jobs-'+i);
			} else {
				$(this).show().appendTo('#jobs-'+i);
			}
		});
		
		reset_swipe();
};

var reset_swipe = function() {
  $("#front-snurrig").css('margin-left','0');
  $("#jobs").css('margin-left','0');
  
  if(front_slider === undefined)
  {
  	// Front page
  	front_slider = new Swipe(document.getElementById('front-snurrig-wrapper'),{
  		callback: function(event, index, elements){
  			$("#front-snurrig-nav li").removeClass('current');
  			$("#front-snurrig-nav li").eq(index+1).addClass('current');
  			if($('.swipe-icon').is(':visible')){
  			  $('.swipe-icon').css('right','-113px').css('display','none');
  			}
  		}
  	});
  	
  	$('#front-snurrig-nav .next').click(function() {
  		front_slider.next();
  		return false;
  	});
  	
  	$('#front-snurrig-nav .prev').click(function() {
  		front_slider.prev();
  		return false;
  	});
  	
  	$('#front-snurrig-nav .nav').click(function() {
  		front_slider.slide($(this).parent().index()-1, 300);
  		return false;
  	});
  	
  	// Jobs
  	jobs_slider = new Swipe(document.getElementById('jobs-wrapper'), {
  	  callback: function(event, index, elements){
        if($('.swipe-icon').is(':visible')){
          $('.swipe-icon').css('right','-113px').css('display','none');
        }
  	  }
  	}); 
  	$('#jobs-nav .next').click(function() {
  		jobs_slider.next();
  		return false;
  	});
  	
  	$('#jobs-nav .prev').click(function() {
  		jobs_slider.prev();
  		return false;
  	});
  } else {
  	jobs_slider.reset();
  }
};

$(document).ready(function() {
  
  $('.swipe-icon').css('right','0');
  $("#front-snurrig article h1.entry-title, .single-post h1.entry-title, .page .page header h1").fitText(0.8,{maxFontSize:80});

	var win_width= $(window).width();
	update_job_grid(win_width);
	var win_height = $(window).height();
	$(window).resize(function(data) {
		win_width = $(window).width();
		update_job_grid(win_width);
	});

	

	$("#job-filters .category").click(function(){

		$(this).toggleClass('active','inactive');

		var kunder = [];
		$("#filters-9 .category.active").each(function(){
			kunder.push($(this).attr('class').replace('category ','').replace('inactive','').replace('active','').trim());
		});

		var uppdragstyp = [];
		$("#filters-47 .category.active").each(function(){
			uppdragstyp.push($(this).attr('class').replace('category ','').replace('inactive','').replace('active','').trim());
		});

		var nocsroll = [];
		$("#filters-28 .category.active").each(function(){
			nocsroll.push($(this).attr('class').replace('category ','').replace('inactive','').replace('active','').trim());
		});

		if(kunder.length > 0) {
			var selector = "#jobs-wrapper li:not(."+kunder.join(', .')+")";
			$(selector).addClass('hidden');
		}

		if(uppdragstyp.length > 0) {
			var selector = "#jobs-wrapper li:not(."+uppdragstyp.join(', .')+")";
			$(selector).addClass('hidden');
		}
		if(nocsroll.length > 0) {
			var selector = "#jobs-wrapper li:not(."+nocsroll.join(', .')+")";
			$(selector).addClass('hidden');
		}

		$('#jobs-wrapper li.hidden').appendTo('#jobs-inactive');

		var a = $('#jobs-wrapper li');

		if(kunder.length > 0) {
			a = $("#jobs-wrapper li."+kunder.join(', li.'));
		}
		if(uppdragstyp.length > 0) {
		 	a = $(a).filter("#jobs-wrapper li."+uppdragstyp.join(', li.'));
		}
		if(nocsroll.length > 0) {
			a = $(a).filter("#jobs-wrapper li."+nocsroll.join(', li.'));
		}

		insert_jobs(a, 12);

		return false;
	});
	
	$("#flip-filters").click(function(){
		$(this).toggleClass('inactive','active');
		$("#categories").slideToggle('fast');
		return false;
	});
	
	$(".divider").click(function(){
		window.open('http://www.youtube.com/watch?v=B9Hc204XVNA&feature=fvst','_newtab');
	});
	
	$(".no-next").hover(
		function(){
			$(this).prev().addClass('point-right');
		},
		function(){
			$(this).prev().removeClass('point-right');
		}
	);
	$(".no-prev").hover(
		function(){
			$(this).next().addClass('point-left');
		},
		function(){
			$(this).next().removeClass('point-left');
		}
	);
	$("#nav-toggle a").click(function(){
		$(this).toggleClass('active');
		$(".menu-main-menu-container").toggleClass('active');
		return false;
	});
	
	if(window.devicePixelRatio !== undefined && window.devicePixelRatio > 1) {
		$(".replace-x1").each(function(){
			$(this).attr('src', $(this).attr('src').replace('x1','x2'));
		});
	}
	
	reset_swipe();
	var dublin_original = 'http://nocweb.se/wp-content/uploads/2011/11/dublin.jpg';
	var dublin_crazy = 'http://nocweb.se/wp-content/uploads/2011/11/crazy_dublin.jpg';
	$("#post-2117 img[src='http://nocweb.se/wp-content/uploads/2011/11/dublin.jpg']").hover(
	  function(){
	    $(this).attr('src',dublin_crazy);
	  },
	  function(){
	    $(this).attr('src',dublin_original);
	  });
});


