$(document).ready( function() {

	$('.dialog').bind('click', function() {

		var a = $(this);
		var dialog = $(a.attr('href'));
		var title = a.attr('title');

		dialog.dialog({resizable:false,
			modal: true,
			title: title
		});
		dialog.css({'display':'block'})
		return false;
	});

	//Utils.display_picture('.display_picture', '#big_picture')



	Utils.load_content('.tag_content_loader', function(invoker, reciever){

		var container = reciever.parent()
		container.css('height', container.height() + 'px');

		invoker.siblings().css({
			'font-weight': 'normal',
			'text-decoration': 'none'
		});
		invoker.css({
			'font-weight': 'bold',
			'text-decoration': 'underline'
		});
	});

	Utils.ContentLoader('.content_loader');

 	$("a.fancybox").fancybox({
 		'hideOnContentClick': false
 	});

 	$("a.photobox").fancybox({
 		overlayOpacity: 0.7,
 		hideOnContentClick: false
 	});


	Utils.toggler('.toggler');
	
	new Utils.AjaxForm('form.ajax');
	
	


});



/**********************************************/






Utils = {};

Utils.toggler = function(selector) {

	var toggler = $(selector);

	$.each(toggler, function() {
		var href = $(this).attr('href');
		var reciever = $('#' + href.split('#')[1]);

		$(this).bind('click', function() {
			reciever.toggle('slow');
			return false;
		});
	});



}


Utils.display_picture = function(selector, target) {

	var invokers = $(selector);
	var target = $(target);

	$.each(invokers, function() {
		var elem = $(this);
		elem.bind('click', function() {
			target.html('<img src="'+ elem.attr('href') +'"/>');
			return false;
		});
	});

}

/**
*	Loads content via ajax in an area.
*	The element specified by selector must be an a-tag.
*
*	href="/url/to/view/#<id>"
*
*	Sends an ajax-request and puts the result in $(#<id>)
*/
Utils.load_content = function(selector, callback) {

	var invokers = $(selector);
	
	// add functionality to each elem
	$.each(invokers, function() {
		
		var invoker = $(this);

		// load an insert content on click
		invoker.bind('click', function() {
			var href = invoker.attr('href');

			var url = href.split('#')[0];
			var reciever = $('#' + href.split('#')[1]);

			$.ajax({type: 'POST',
		    		url: url,
		    		dataType: 'html',
		    		success: function(response) {
						if (callback != null) callback(invoker, reciever);
						reciever.css({'display':'none'});
						reciever.html(response);
						reciever.fadeIn(2000);

		    		}
			});
			return false;

		});

	});
}




Utils.makeSortable = function(selector, url) {

	var container = $(selector);

	container.sortable({
		opacity: 0.7,
		//handle: '.dragzone',
        scroll: true,
		forceHelperSize: true,
        stop: function() {

			var data = [];

			$.each(container.children(), function() {
				data.push($(this).attr("id"));
			});


        	$.ajax({
        		type: 'POST',
        		url: url,
        		data: {'elems': $.toJSON(data)},
        		success: function(msg) {

        		}
        	});
        }

	});
}




/**
 * Loads content and places it in the dom.
 * usage: <a class="selector hide" href="....url...#reciever"></a>
 * Loads the href and places result in #reciecever. 
 */
Utils.ContentLoader = function(selector) {
	
	var invokers = $(selector);
	
	// add functionality to each elem
	$.each(invokers, function() {
	
		var invoker = $(this);
		var href = invoker.attr('href');
		var url = href.split('#')[0];
		var reciever = $('#' + href.split('#')[1]);

		$.ajax({type: 'GET',
	    		url: url,
	    		dataType: 'html',
	    		success: function(response) {
					reciever.html(response);

	    		}
		});
		return false;
	});
}





/**
 * Add ajax func to all forms specified by selector and inserts the result in the element
 * specified by the selector name="#id"
 */
Utils.ajax_form = function (selector) {
	
	new Utils.AjaxForm(selector, {
		callback: function(response, form) {
			var target = form.attr('name');
			$(target).html(response);
			
		}		
	});
	
}


Utils.AjaxForm = function(selector, config) {

	var action = null;
	var form = null;
	var options = null;


	var submit = function() {
		$.ajax({type: 'POST',
       		url: action,
       		dataType: 'html',
       		data: form.serializeArray(),
       		success: function(response) {
			
				if (options != null && options.callback != null) {
					options.callback(response, form);
					
				}
			}	
       	});
	}


	// constructor
	function __init__() {

		form = $(selector);
		action = form.attr('action');
		options = config;
		
		form.bind('submit', function(){
			submit();
			
			return false;
		});
	}

	__init__();

}



var confirm = function(elem) {
	var elem = $(elem);
	elem.empty();
	elem.append('Saved');
	elem.hide("puff", {}, 1000);

}












