jQuery(document).ready( function($) {
  
  $(".prev_next_ajax a").click( function() {
	var link = $(this);
	var link_href = link.attr("href");
	
	if(link_href.indexOf('non_embassy_press_releases') != -1){	
		var link_href_id = link_href.split('/non_embassy_press_releases/')[1];
		location.hash = 'non_embassy_press_releases/' + link_href_id;
	}else if(link_href.indexOf('embassy_press_releases') != -1){
		var link_href_id = link_href.split('/embassy_press_releases/')[1];
		location.hash = 'embassy_press_releases/' + link_href_id;
	}else if(link_href.indexOf('press_items') != -1){
		var link_href_id = link_href.split('/press_items/')[1];
		location.hash = 'press_items/' + link_href_id;
	}else if (link_href.indexOf('photo_gallery') != -1){
		var link_href_id = link_href.split('/photo_gallery/')[1];
		location.hash = 'photo_gallery/' + link_href_id;
	}
	
	
	$("#dynamic_content").html($('<p class="loading">Please wait. Loading data..</p>'));

	var json_content = true;
	
    if (!json_content){
		$.post(link.attr("href"), {
			rukijax: 1
		  }, function(data) {
			$("#dynamic_content").hide();
			$("#dynamic_content").html($(data));
			$("#dynamic_content").fadeIn('fast');
			initLightbox();
		  }
		)
	}else{
		$.post(link.attr("href"), {
			rukijax: 1
		  }, function(data) {
			$("#dynamic_content").hide();
			$("#dynamic_content").html(unescape(data.mid));
			$("#dynamic_content").fadeIn('fast');
			//$("#post_links").html(unescape(data.links));
			initLightbox();
		  }
		)
	};
	
    return false;
  });

	//checks the current url and redirects to the url without hash
	var site_url = location.toString();
	if(site_url.indexOf('#')!= -1){
		var post_id = site_url.split('#')[1];
		var new_url = 'http://' + location.host + '/' + post_id;
		window.location.replace(new_url);
	}

});


//next and previous links
function navigate(a){
	var link = jQuery(a);
	var link_href = link.attr("href");
	//var link_href_id = link_href.split('?')[1];
	//location.hash = link_href_id;

	if(link_href.indexOf('non_embassy_press_releases') != -1){	
		var link_href_id = link_href.split('/embassy_press_releases/')[1];
		location.hash = 'non_embassy_press_releases/' + link_href_id;
	}else if(link_href.indexOf('embassy_press_releases') != -1){	
		var link_href_id = link_href.split('/embassy_press_releases/')[1];
		location.hash = 'embassy_press_releases/' + link_href_id;
	}else if(link_href.indexOf('press_items') != -1){	
		var link_href_id = link_href.split('/press_items/')[1];
		location.hash = 'press_items/' + link_href_id;
	}else if (link_href.indexOf('photo_gallery') != -1){
		var link_href_id = link_href.split('/photo_gallery/')[1];
		location.hash = 'photo_gallery/' + link_href_id;
	}
	
	
	jQuery("#dynamic_content").html(jQuery('<p class="loading">Please wait. Loading data..</p>'));
	
	jQuery.post(link.attr("href"), {
			rukijax: 1
		  }, function(data) {
				jQuery("#dynamic_content").hide();
				jQuery("#dynamic_content").html(unescape(data.mid));
				jQuery("#dynamic_content").fadeIn('fast');
				//jQuery("#post_links").html(unescape(data.links));
				initLightbox();
		  }
		);
	
	return false;

};
 
