﻿var cssSelector = 0;

function changeFontSize(direction) {
    cssSelector = getCssSelectorFromCookie();

    if (direction == "up") {
        cssSelector++;
    } else if (direction == "down") {
        cssSelector--;
    } else {
        cssSelector = 0;
    }

    if (cssSelector > 2) {
        cssSelector = 2;
    }


    if (cssSelector < 0) {
        cssSelector = 0;
    }
	
	setCookie("cssSelector", cssSelector);
	loadCss(cssSelector);
}


function getCssSelectorFromCookie() {
	var rtval = parseInt(getCookie("cssSelector"));
	if (isNaN(rtval)) {
		return 0;
	}

	return rtval;
}

function loadCss(selectorNumber) {
    var num = parseInt(selectorNumber);
    
    switch (num) {
        case -2:
            $("#cssFile").attr("href", cssPath + "xs.css");
            break;
        case -1:
            $("#cssFile").attr("href", cssPath + "s.css");
            break;
        case 0:
            $("#cssFile").attr("href", cssPath + "m.css");
            break;
        case 1:
            $("#cssFile").attr("href", cssPath + "l.css");
            break;
        case 2:
            $("#cssFile").attr("href", cssPath + "xl.css");
            break;
    }
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
    ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function bookmarkThis(){
    var title = document.title;
    
    if (document.all){
        window.external.AddFavorite(location.href,title) 
    } 
    else if (window.sidebar) { 
        window.sidebar.addPanel(title, location.href, "") 
    } 
    else { 
        alert("Sorry! Your browser doesn't support this function.") 
    }
}


function sendThis() {
    var currentPage;
    var mailSubject, mailBody;
    
    currentPage = location.href;
    
    mailSubject = "I want you to check this site out";
    mailBody = "Hey, I just found this site.  I think you will like it. " + currentPage;    
    
    window.location = "mailto:?Subject=" + mailSubject + "&Body=" + mailBody;
}


function resizeFont(multiplier) {
  if (document.body.style.fontSize == "") {
    document.body.style.fontSize = "12px";
  }
  document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier * 1) + "px";
}

function resetFontSize(multiplier) {
  document.body.style.fontSize = "12px";
}


function writeNewWindow(content) {
    var newWindow;
    newWindow = window.open("", "_blank", "width=800,height=600,scrollbars=1,menubar=1,resizable=1,toolbar=1");

    newWindow.document.writeln(content);
    newWindow.document.close();
}

function pdfExport(exportType) {
    var htmlHead = "<head>" + $("head").html() + "</head>";

    var newForm = '<form id="pdfForm" action="http://www.hincksdellcrest.org/pdfexport.aspx" method="post">';
    newForm = newForm + '<input type="hidden" id="htmlContent" name="htmlContent" />';
    newForm = newForm + '</form>';
    $myBody = $("body").append(newForm);

    var contentRight = $(".contentRight").html();


    if (exportType == "full") {
        // show all hidden elements
        contentRight = contentRight.replace(/display: none/gi, "");
        
        //writeNewWindow(contentRight);
    }

    // Remove all occurances of string "Back to top" using regex with global property
    contentRight = contentRight.replace(/Back to top/gi, "");

    
    // Hiding subchapter selector
    contentRight = contentRight.replace(/id=\"subchapterSelector\"/gi, 'id="subchapterSelector" style="display: none;"');
    // IE Fix
    contentRight = contentRight.replace(/id=subchapterSelector/gi, 'id=subchapterSelector style="display : none;"');
    
    // Hiding the content filter
    contentRight = contentRight.replace(/id=\"div_chapterFilter\"/gi, 'id="div_chapterFilter" style="display: none;"');
    // IE Fix
    contentRight = contentRight.replace(/id=div_chapterFilter/gi, 'id=div_chapterFilter style="display : none;"');

    //writeNewWindow(contentRight);            

    $("#htmlContent").val(htmlHead + "<body>" + contentRight + "</body>");
    $("#pdfForm").submit();
    $("#pdfForm").remove();
    
}