if(window.location.hash == '' && window.location.href.indexOf('sok') == -1 && window.location.href.indexOf('?_escaped_fragment_=lang=') == -1 ) {
	window.location.hash = '!lang=sv';
}	

if (window.location.href.indexOf('?_escaped_fragment_=lang=') == -1 ) {
	$.post("/session.php", { "lang": getLang()}, function(data) {
		if (data == 1) {
			location.reload();
		}
	});
}

function getLang() {
	href = window.location.href;
	if (href.indexOf('?_escaped_fragment_=lang=') != -1) {
		return href.substring(href.length - 2);
	}
	hash = window.location.hash;
	lang = hash.split('&')[0];
	return lang.substring(lang.length - 2);
}


$(document).ready(function() {
	
	var start = 0;

	 $('a[rel*=external]').click(function() {
          window.open($(this).attr('href'));
          return false;
     });
	
	(function($) {$.fn.escape = function() {
	    return escape(this.val());};})(jQuery);
	
	$('.comments').livequery('click', function() {
		$comment_holder = $(this).parent().siblings('.comment_holder');
		if ($comment_holder.attr('style') == 'display: none;' && $comment_holder.html() == '') {
			$id = $(this).attr('rel');
			$.get('/comments.php?id=' + $id,function(data){
				$comment_holder.html(data);
				$comment_holder.slideDown();
			},'html'); 
			
		} else if ($comment_holder.attr('style') == 'display: none;') {
			$comment_holder.slideDown();
		} else {
			$comment_holder.slideUp();
		}
	});

	$('.close').click(function() {
		$('.new_comment').fadeOut();
	});
	
	$('.blog_map').click(function() {
		window.location.href = '/aventyr/aconcagua2011/karta#!lang='+getLang();
	});
	
	$('.new_comment_link').click(function() {
		$new_comment = $('.new_comment');
		$id_field = $new_comment.children('form').children('fieldset').children('input#nyhet_id');
		$id = $(this).attr('rel');
		$id_field.val($id);
		
		$new_comment.center();
		$new_comment.fadeIn();
	});
	
	jQuery.fn.center = function () {
	    this.css("position","absolute");
	    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	    return this;
	}
	
	$('.more').click(function(event) {
		event.preventDefault() ;
		start += 5;
		hash = window.location.hash;
		
		window.location.hash = getHashUrl(5);
		
		getBlogPosts(start);
		$('#more').attr('href', getHashUrl(10));
		return false;
	});
	
	if (hash.indexOf('end') != -1 && $(".blog_posts").length != 0){
		end = parseInt(hash.substring(hash.lastIndexOf('=')+1));
		start = end - 5;
		for(i=1 ; i <= start/5 ; i++) {
			getBlogPosts(i*5);
		}
	}
	function getHashUrl(offset){
		if (hash.indexOf('end') == -1){
			return hash + '&end='+ (start + offset);
		} else {
			return hash.substring(0, hash.indexOf('&end=') + 5) + (start + offset);
		}
	}
	
	function getBlogPosts(nr){
		more_url = $(".more_url").text();
		url = more_url + nr;
		
		$.get(url,function(data){
			$(".blog_posts").append(data);
			$('a.lightbox').lightBox();
			$('a').each(function(index) {
				var href = $(this).attr('href');
				if(href !== undefined && !$(this).hasClass('noHash') && href.indexOf('#!lang=') == -1 && href.indexOf('http') == -1){
					$(this).attr('href', href + '#!lang=' + getLang());
				}
			});
		},'html'); 
	}
	$('#more').attr('href', getHashUrl(10));

	var validator = $("#new_comment_form").validate({ 
		submitHandler: function(form) {
			var str = $('#new_comment_form').serialize();
			$.ajax( {
				type : "POST",
				url : "/new_comment.php",
				data : str,
				success : function(msg) {
					$msg = $('.msg'); 
					if(msg == 1) {
						$msg.removeClass('errorMsg');
						$msg.text("Din kommentar är lagrad och inväntar godkänning!");
					} else {
						$msg.addClass('errorMsg');
						$msg.text("Något gick fel! Testa att ladda sidan på nytt och testa igen!");
					}
					$msg.fadeIn();
					$msg.fadeOut(7000);
				}
			});
			$new_comment.fadeOut();
			$('.input').val('');
			
			return false;
		}
	});

	
	$('#defaultCountdown').countdown({
		until: new Date(2011, 12 - 1, 15, 19, 10, 0), 
		format: 'YDHMS',
		layout: '<div id="t7_timer">'+
					'<div id="t7_vals">'+
						'<div id="t7_y" class="t7_numbs">{ynn}</div>'+
						'<div id="t7_d" class="t7_numbs">{dnnn}</div>'+
						'<div id="t7_h" class="t7_numbs">{hnn}</div>'+
						'<div id="t7_m" class="t7_numbs">{mnn}</div>'+
						'<div id="t7_s" class="t7_numbs">{snn}</div>'+
					'</div>'+
					'<div id="t7_labels">'+
						'<div id="t7_yl" class="t7_labs">years</div>'+
						'<div id="t7_dl" class="t7_labs">days</div>'+
						'<div id="t7_hl" class="t7_labs">hours</div>'+
						'<div id="t7_ml" class="t7_labs">mins</div>'+
						'<div id="t7_sl" class="t7_labs">secs</div>'+
					'</div>'+
					'<div id="t7_timer_over"></div>'+
				'</div>'
	});
	
	
	$('.swe').click(function() {
		$.post("/session.php", { "lang": "sv"}, function(data) {
			if (data == 1) {
				location.reload();
			}
		});
	});
	
	$('.eng').click(function() {
		$.post("/session.php", { "lang": "en"}, function(data) {
			if (data == 1) {
				location.reload();				
			}
		});
	});
	
	$('.profilePics').innerfade({
		speed: 'slow',
		timeout: 6000,
		type: 'random_start',
		containerheight: '220px'
	});
	
	$('a.lightbox').lightBox();
	
	$('a').each(function(index) {
		var href = $(this).attr('href');
		if(href !== undefined && !$(this).hasClass('noHash') && href.indexOf('#!lang=') == -1 && href.indexOf('http') == -1){
			$(this).attr('href', href + '#!lang=' + getLang());
		}
	});
	
});
