﻿/* Created by Corey Dutson */

var $j = jQuery.noConflict();
var fontCookie = "fontSize";
var cookieVal = GetCookie(fontCookie);

function setCookie(sName, sValue) {

    var date = new Date(2050, 11, 31);
    //date.setTime(date.getTime()+(1*24*60*60*1000));
    document.cookie = sName + "=" + escape(sValue) + ";expires=" + date.toGMTString() + ";path=/";
    /*	
    //delete cookie!
    document.cookie = sName + "=" + escape(sValue) +";expires=Fri, 21 Dec 1976 04:31:24 GMT;path=/";
    document.cookie = sName + "=" + escape(sValue) +";expires=Fri, 21 Dec 1976 04:31:24 GMT;";
    */
}

function GetCookie(sName) {
    // cookies are separated by semicolons
    var aCookie = document.cookie.split("; ");

    for (var i = 0; i < aCookie.length; i++) {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return unescape(aCrumb[1]);
    }

    // a cookie with the requested name does not exist
    return null;
}

// Prints the page

// TODO: get original size first, store it, resize to small
// call the print, and then resize to former

function printPage() {
    $j("body").css({ fontSize: "50%" });

    window.print();

    resizeAfterPrint();
}
function resizeAfterPrint() {
    // RESIZE TO FORMER STATE
    var result = GetCookie(fontCookie)

    if (result == "large")
        $j("body").css({ fontSize: "82%" });
    else if (result == "small")
        $j("body").css({ fontSize: "69%" });
    else
        $j("body").css({ fontSize: "78%" });

}

// Used with the emailer
function emailPage() {
    mailLine = "mailto:?subject=Viewfinder - " + document.title + "&body=" + location.href;
    location.href = mailLine;
}

// Used for the text resizer
function SetFontSize(caller, dimension) {
    setCookie(fontCookie, dimension);
    if (dimension == "large")
        $j("body").css({ fontSize: "82%" });
    else if (dimension == "small")
        $j("body").css({ fontSize: "69%" });
    else
        $j("body").css({ fontSize: "78%" });


    ResetButtons(caller);
}

function ResetButtons(caller) {

    $j('#fontSizeLarge img:first').attr({ "src": $j('#fontSizeLarge img:first').attr("rollOffImg") });
    $j('#fontSizeSmall img:first').attr({ "src": $j('#fontSizeSmall img:first').attr("rollOffImg") });
    $j('#fontSizeNormal img:first').attr({ "src": $j('#fontSizeNormal img:first').attr("rollOffImg") });

    $j('img:first', caller).attr({ "src": $j('img:first', caller).attr("rollOverImg") });
}

// for printing purposes
var isPrinting = false;

function setPrintState() {
    isPrinting = true;
    $j("body").css({ fontSize: "50%" });
}
function setScreenState() {
    isPrinting = false;
    resizeAfterPrint();
}

$j(document).ready(function() {

    /********************
    RESIZER SECTION 
    *********************/

    // set handlers
    document.getElementsByTagName('body')[0].onbeforeprint = setPrintState;
    document.getElementsByTagName('body')[0].onafterprint = setScreenState;

    if (!isPrinting) {

        // Set fontsize initially
        if (cookieVal + "" != "" && cookieVal != null && cookieVal != "small") {
            if (cookieVal == "large")
                SetFontSize($j('#fontSizeLarge'), "large");
            else if (cookieVal == "small")
                SetFontSize($j('#fontSizeSmall'), "small");
            else
                SetFontSize($j('#fontSizeNormal'), "normal");
        }
        else {

            SetFontSize($j('#fontSizeSmall'), "small");
        }
    }
    else {
        //	  $j("body").css({ fontSize:"50%" });
    }



    // onclick
    $j("#fontSizeLarge").click(function() {
        SetFontSize($j(this), "large");
    });

    $j("#fontSizeNormal").click(function() {
        SetFontSize($j(this), "normal");
    });

    $j("#fontSizeSmall").click(function() {
        SetFontSize($j(this), "small");
    });


    // Hover
    $j("#fontSizeLarge").hover(function() {
        $j('img:first', this).attr({ "src": $j('img:first', this).attr("rollOverImg") });
    }, function() {
        if (GetCookie(fontCookie) != "large")
            $j('img:first', this).attr({ "src": $j('img:first', this).attr("rollOffImg") });
    });

    $j("#fontSizeNormal").hover(function() {
        $j('img:first', this).attr({ "src": $j('img:first', this).attr("rollOverImg") });
    }, function() {
        if (GetCookie(fontCookie) + "" != "" && GetCookie(fontCookie) != "normal")
            $j('img:first', this).attr({ "src": $j('img:first', this).attr("rollOffImg") });
    });

    $j("#fontSizeSmall").hover(function() {
        $j('img:first', this).attr({ "src": $j('img:first', this).attr("rollOverImg") });
    }, function() {
        if (GetCookie(fontCookie) != "small")
            $j('img:first', this).attr({ "src": $j('img:first', this).attr("rollOffImg") });
    });

    /********************
    END RESIZER SECTION 
    *********************/

});
