// Javascript imported into literary pages

// On document ready
$(document).ready(function(){
    // Show side column
    /*
    $("#side-column div").show();
    var el = "#side-column-search, #side-column-search *";
   
    if (String($(el).text()).replace(/\s/g,"").length == 0) { $(el).hide();}
  
    el = ".head_big, .head_big *";
    if (String($(el).text()).replace(/\s/g,"").length == 0) { $(el).hide();}
    */
    // Set title to contents of first h1 in content-primary div
    var title = $("#content-primary h1:first").text();
    if (title.length == 0) { title = $("body #content h1:first").text();}
    if(!$.browser.msie) {$("head title:first").text(title);} //IE doesn't let Jquery change the head title.
    
    if (title == "") { title = $("body").attr("title");}
    document.title = title;
    
    // Open the second category (index #1 in zero-base array) 
    // alert($("#list0 h3:eq(1)").attr("class"));
	// use next lines for opening new lines ----
	// if ($("#list0 h3:eq(1)").attr("class").indexOf("selected") == -1 
	// && window.location.hash == "") { AccordionToggle( $("#list0 h3:eq(1)"),"none" );} 
	//    
  });

/* AJAX Variables and functions */

var loading_message = '<div class="shell-1">' +
                    '<p>' +
                        '<img src="/global/images/ajax-loader.gif" style="float: left; vertical-align: middle;"/>' +
                        '<span style="color: white;">...</span> <em>Loading ....</em>' +
                    '</p></div>';
	
// Import content function
function importContent(dest,typ,eurl) {

    $(dest).ajaxStart( function() {
		$(this).html(loading_message) ;
	});
	
	//alert(window.location.href + $("#list0 a").length);
    $("#list0 a").each(function() {
        var winloc = window.location.href;
        var myhref = $(this).attr("href");
        if((typeof(myhref) != "undefined" && myhref.indexOf(".") > -1 && winloc.indexOf(myhref) > -1) || (winloc == ("http://" + window.location.host + myhref))) {
            $("#list0 a").removeClass("selected"); // Remove any previously selected link
            $(this).addClass("selected");// Select the present matching link
            
            var navgroup = $(this).parents("ul").prev("h3"); // Find parent ul's previous h3 and its class
            var navclass = $(navgroup).attr("class");  // if it's not selected by accordion, then call the accordion for it
            if(typeof(navclass)!= "undefined" && navclass.indexOf("selected") == -1) {
                AccordionToggle($(navgroup), "none");
            }
        }
    });
    
    if (typ == "essay") {
        aurl = "http://" + window.location.host + "/global/php/essay_reader.php?url=" + eurl;
        $.ajax({
    		url: aurl,
    		success: function( data ) {
    		    if (dest.indexOf('shell-2') > -1) {
        		    $(dest).hide();
        			$(dest).html( data ) ; 
        			var strippedData = $(dest + " .shell-1").html();
        			$(dest).html (strippedData);
        			$(dest).show();
        		}
        		/*
        		$( dest + " a[href*=#essay]").each(function() {
                    var hrf = $(this).attr("href");
                    $(this).attr("href", "index.php?" + hrf);
                });*/
    		}
    	}) ;
    }
}
