﻿$(document).ready(function () {
	var inDesignModeProp = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode;
	var wikiInEditModeProp = document.forms[MSOWebPartPageFormName]._wikiPageMode;
	
	if ((inDesignModeProp && inDesignModeProp.value == '1') || (wikiInEditModeProp && wikiInEditModeProp.value == 'Edit')) {
		return;
	}
	
	$('a[rel="ISODocument"]').livequery(function () {
		var anchor = $(this);
		var qsParams = "libraryName=Documents&name=" + anchor.attr('href');		
		qsParams += "&cache=15";
		$.ajax({
            url: '/_vti_bin/WireStone/CaisoCms/documentservice.svc/format',
            dataType: 'html',
            data: qsParams,
            cache: false,
            success: function (events) {
            	if (events && events.length > 7) {
            		anchor.replaceWith(events);
            	}
            	else {
            		anchor.attr('rel', '');            	
            		anchor.attr('title', 'No document details found.');
            	}            	
            },
            error: function (e, xhr, settings, exception) {
    			alert("Error loading document url rewrite:\n" + xhr.statusText);
			}
	    });
	});
});
