jQuery.fn.log = function (msg) {
	console.log("%s: %o", msg, this);
	return this;
};

init_common = function(scope)
{
	// hide all next elements to start
	$('.toggle_next',scope).next().hide();
	$('.toggle_next.show',scope).next().show();
	// function to show hide the next element
	$('.toggle_next',scope).click(function(event){
		event.preventDefault();
		$(this).toggleClass('selected');
		$(this).next().slideToggle("slow");
	});
	$('.launch',scope).click(function(event){
		$(this).toggleClass('selected');
	});
	$('.toggle_hidden',scope).click(function(event){
		event.preventDefault();
		$('.toggle_next').next().toggle();
		$('.toggle_next.standalone').next().hide();
	});

	$('.datepick',scope).datepicker({
		dateFormat: 'yy-mm-dd',
	});

	$('.sortable',scope).sortable({
		distance: 15
	});

	$("ul.sortable li").disableSelection();

	$('.btn').button().click(function(){
		$(this).toggleClass('selected');
	});

	$("nav.primary ul.subnav", scope).hide();
	$("nav.primary ul.nav span", scope).hover(function() { 
		$(this).next("ul.nav").slideDown('fast').show();
		$(this).prev("a").addClass('selected');
		$(this).parent().hover(function() {}, function(){  
			$(this).find("ul.nav").slideUp('fast'); 
			$(this).find("a").removeClass('selected');
		});  
	});

	$('.hotswap', scope).click(function(event){
		event.preventDefault();
		var link = $(this).attr('href') + '.ajax';
		var dest = $(this);
		var parent = $(this).parent();
		dest.html("<img src='/content/icons/action_refresh.gif'>");
		$.get(link, function(data){ 
			dest.replaceWith(data); 
			init_hotswap(parent);
		});
	});

	$('.close').button().click(function(){
		target = $('#launch_dropzone');
		if(target) $(target).slideUp('slow');
	});

	$('.launch',scope).click(function(event){
		event.preventDefault();
		link = $(this).attr('href');
		dest = $('#launch_dropzone');
		dest.hide();

		dest.data('active', true);
		dest.data('undo', dest.html() );
		closeBtn = "<a class='btn right close'>Close</a>";
		$.get(link, function(data){
			dest.html( closeBtn + data);
			init_common(dest); 
			$.scrollTo( 0, 300 );
			dest.slideDown(); 
		});
	});

	$('#notification').ajaxStart(function(){
		$(this).hide();
		$(this).log('AJAX Starting process');
		$(this).html("<p>LOADING CONTENT PLEASE WAIT...</p>");
	});

	$('#notification').ajaxError(function(e){
		$(this).html("<p>AN AJAX ERROR OCCURED, PLEASE TRY AGAIN</p>");
		$(this).log('AJAX Error occured: '+e);
	});

	$('#notification').ajaxSuccess(function(){
		$(this).html("<p>&nbsp;</p>");
	});

	$('#notification').ajaxStop(function(){
		// $(this).stop().slideUp('slow');
		$(this).log('AJAX Finished process');
	});

	// ajax prev and next buttons
	$('.prev, .next').click(function(e){

		e.preventDefault();

		targ = '#' + $(this).attr('rel');
		parent = $(this).parent();
		n_max = parent.attr('rel');

		n = parent.data('n');
		if(n == undefined) n = 1;
	
		url = $(this).attr('href').replace(/(\d+)$/i, "");

		if($(this).hasClass('next')) n = n + 1;
		if($(this).hasClass('prev')) n = n - 1;
	
		if(n > n_max) n = 1;
		if(n <= 0) n = n_max;
	
		url = url + n +' '+targ;
		//alert(url);

		$.scrollTo( parent, 700 );
		$(targ).load(url, function(){
		        $('#page_counter').html('PAGE '+n+' of '+n_max);
		});
	
		parent.data('n',n);
	});

}

$(document).ready(function(){
	init_common($(this));
});




