/*
	Initializaion
*/
jQuery(function($) {
	$('.sortable').each(function() {
		$(this).sortable({});
		$(this).parent('form').bind('submit', { sortableContainer: this }, function(event) {
			$(this).find('[@name=serializedList]').val($(event.data.sortableContainer).sortable("serialize"));
		}); 
	});
	
	$('a.lightBox').lightBox({
		overlayBgColor: '#FFF',
		overlayOpacity: 0.8,
		imageLoading: '/graphics/images/lightbox-loading.gif',
		imageBtnClose: '/graphics/images/lightbox-close.gif',
		imageBtnPrev: '/graphics/images/lightbox-prev.gif',
		imageBtnNext: '/graphics/images/lightbox-next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Bild',
		txtOf: 'av'
	});
	
	$('a.submissionSwitch').each(function() {
		$(this).bind('click', function() {
			var imageName = $(this).attr('rel');
			$('#masterImage').attr('href', 'http://www.xtraworks.se/image/'+imageName+'/');
			$('#masterImage img').attr('src', 'http://www.xtraworks.se/image/'+imageName+'/assignment/');
			return false;
		});
	});
	
	$('p.responseTrigger').click(function() {
		formElement = $(this).parent().parent().find('form');
		if ($(formElement).css('display') == "none") {
			/*$(formElement).css({
				display: "block"
			});*/
			$(formElement).slideDown('fast');
		} else {
			/*$(formElement).css({
				display: "none"
			});*/
			$(formElement).slideUp('fast');
		}
	});
	
	$("#searchHeader form input.text").click(function() {
		if ($(this).val() == "Sök") {
			$(this).val('');
		}
	});
	
	$(".tipToggle").click(function() {
		temporary = $("#tipFriend").css('display');
		if (temporary == "none") {
			$("#tipFriend").slideDown('fast');
		} else {
			$("#tipFriend").slideUp('fast');
		}
	});
	
	$("#workgroupForum li.header").find(".source").click(function() {
		postId = $(this).parent().parent().attr('id');
		postId = postId.replace("thread_", "");
		//threadUrl = $(this).find("a").get(0);
		threadUrl = $(this).attr('href');
		var dataDiv = $(this).parent().parent().find('div.posts');
		if (dataDiv.length > 0) {
			var externalDiv = $(this).find('div.external');
			if ($(dataDiv).css('display') == "none") {
				$(dataDiv).css("display", "block");
				if (externalDiv.length > 0) {
				$(externalDiv).html("Laddar");
					$.get(threadUrl+"?module=Arbetsgrupp&thread="+postId, function(data) {
						$(externalDiv).html(data);
						$(externalDiv).slideDown();
					})
				}
			} else {
				$(dataDiv).slideUp();
			}
		}
		
		return false;
	});
});