swfobject.registerObject("logoMC", "10.0.0");

if (location.hash != '') {
	new_page = location.hash.substr(1);
	location.hash = '';
	window.location = site_path+new_page;	
}

var actual_data = '';
var actual_title = '';
var actial_footer = 0;

$(document).ready(function(){
	$('.in_content').focus();

	
	ajaxAnchors();
	preLoadImages();
	
	actual_data = $('#target_content>div').html();
	actual_title = document.title;
	
	if ( $('#footer').is(':visible') )
		actial_footer = 1;
	
	$(window).hashchange( function(){
		new_page = location.hash.substr(1);
		
		if (new_page == '') {
			$('#target_content>div').fadeOut('fast', function(){
				
				$('#target_content>div').html(actual_data);
				
				document.title = actual_title;
				
				if (actial_footer == 1) {
					$('#footer').show();
				}
				
				$('#target_content>div').fadeIn('fast');
				
				preLoadImages();
				ajaxAnchors();	
			});
			
			return false;
		}
		
		$('#footer').fadeOut('fast');
		
		var count = $('#target_content>div').size();
		
		$('#target_content>div').fadeOut('fast', function(){
			count--;
			if ( count == 0 ) {
				$.get(site_path+new_page, function(data){
					$('#target_content>.d_content').html(data['content']);
					
					if (data['title']=='' || data['title']==null) {
						document.title = data['main_title'];
					} else {
						document.title = data['title'];	
					}
					checkPagination(data);
					preLoadImages();
					ajaxAnchors();		
					
					$('#target_content>.d_content').fadeIn('fast');
					if ( data['footer'] != '0' ) {
						$('#footer').fadeIn('fast');
					}
				}, 'json');
			}
		});
	});
	
	$('._ajax_open').live('click', function(event){
		event.preventDefault();
		var new_page = $(this).attr('href');
		new_page = new_page.replace(site_path, '');
		location.hash = new_page;
	});
	
});

function checkPagination (data) {
	$('.pagination_footer').remove();
	var pagination_max = Math.ceil( parseInt(data['pagination_max']) / parseInt(data['pagination_perpage']) );

	if ( pagination_max > 1 ) {
		var counter = parseInt(data['pagination']);
		var pagination = $('.pagination_footer');

			var a_target = 0;
			var add_class = '';
			$('.pagination_contener').html('').show();
			
			for(var i=0; i < pagination_max; i++) {
				a_target = ( i * parseInt(data['pagination_perpage']) );
				if (a_target == 0) {
					a_target = '';
				}

				if ( i ==  counter) {
					add_class = 'pagination_actual_footer';
				} else {
					add_class = ' ';
				}

				$('.pagination_contener').append('<a href="'+data['site']+'/'+a_target+'" class="pagination_footer _ajax_open '+add_class+'">'+(i+1)+'</a>');		
			}
	} else {
		$('.pagination_contener').hide();	
	}
}

function ajaxAnchors () {
	$('.menu_ajax').each(function(){
		$(this).attr('href', '#'+$(this).attr('alt'));	
	});
}

function preLoadImages () {
	$('.to_load_').each(function(){
		var this_ = this;
		$('<img />')
		.attr({
			'src': $(this_).attr('target'),
			'title': $(this_).attr('title'),
			'alt': $(this_).attr('alt')
		})
		.load(function(){
			$(this_).append( $(this) );
			$(this_).css({'background-image': 'url(null)', 'height': $(this).height(), 'margin-left': 'auto', 'margin-right': 'auto' });
		});
	});
	
	$('iframe').each(function(){
		$(this).fadeIn(3000);
	});
}
