﻿
function setMenu() {

    var locationString = window.location.toString().toLowerCase();

    if (locationString) {
        if (locationString.indexOf('/blog') != -1) {
            $("#tech").css({ backgroundColor: "#000", color: "#FFF" });
        }
        else if (locationString.indexOf('/otherblog/aboutme') != -1) {
            $("#aboutme").css({ backgroundColor: "#000", color: "#FFF" });
        }
        else if (locationString.indexOf('/otherblog') != -1) {
            $("#other").css({ backgroundColor: "#000", color: "#FFF" });
        }
        else if (locationString.indexOf('/photos') != -1) {
            $("#photos").css({ backgroundColor: "#000", color: "#FFF" });
        }
    }
}

function setFont(size, setVisuals) {
    if (size) {
        $("body").css("font-size", size + '%');
        $.cookie("58bits_fontsize2", size, { expires: 1000, path: '/' });

        if (setVisuals) {
            setFontVisuals(size);
        }
    }
}

function setFontVisuals(size) {
    if (size == "75") {
        //alert('setting small');
        $("#fontsize_small").css("border-bottom", "solid 2px #F00");
        $("#fontsize_medium").css("border-bottom", "none");
        $("#fontsize_large").css("border-bottom", "none");
    }
    else if (size == "82") {
        //alert('setting medium');
        $("#fontsize_small").css("border-bottom", "none");
        $("#fontsize_medium").css("border-bottom", "solid 2px #F00");
        $("#fontsize_large").css("border-bottom", "none");
    }
    else if (size == "95") {
        //alert('setting large');
        $("#fontsize_small").css("border-bottom", "none");
        $("#fontsize_medium").css("border-bottom", "none");
        $("#fontsize_large").css("border-bottom", "solid 2px #F00");
    }
}

function setLayout(layout, setVisuals) {
    if (layout) {
        $("#main").attr("class", layout);

        $.cookie("58bits_layout2", layout, { expires: 1000, path: '/' });

        if (setVisuals) {
            setLayoutVisuals(layout)
        }
    }
}

function setLayoutVisuals(layout) {
    if (layout == "main_right") {
        $("#layout_right").css("padding-bottom", "0").css("border-bottom", "solid 2px #F00");
        $("#layout_left").css("padding-bottom", "2px").css("border-bottom", "none");
    }
    else {
        $("#layout_left").css("padding-bottom", "0").css("border-bottom", "solid 2px #F00");
        $("#layout_right").css("padding-bottom", "2px").css("border-bottom", "none");
    }
}


/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/

/**
* Create a cookie with the given name and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
*       used when the cookie was set.
*
* @param String name The name of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
*                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
*                             If set to null or omitted, the cookie will be a session cookie and will not be retained
*                             when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
*                        require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/*
Date Format 1.1
(c) 2007 Steven Levithan <stevenlevithan.com>
MIT license
With code by Scott Trenda (Z and o flags, and enhanced brevity)
*/

/*** dateFormat
Accepts a date, a mask, or a date and a mask.
Returns a formatted version of the given date.
The date defaults to the current date/time.
The mask defaults ``"ddd mmm d yyyy HH:MM:ss"``.
*/
var dateFormat = function() {
    var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function(value, length) {
		    value = String(value);
		    length = parseInt(length) || 2;
		    while (value.length < length)
		        value = "0" + value;
		    return value;
		};

    // Regexes and supporting functions are cached through closure
    return function(date, mask) {
        // Treat the first argument as a mask if it doesn't contain any numbers
        if (
			arguments.length == 1 &&
			(typeof date == "string" || date instanceof String) &&
			!/\d/.test(date)
		) {
            mask = date;
            date = undefined;
        }

        date = date ? new Date(date) : new Date();
        if (isNaN(date))
            throw "invalid date";

        var dF = dateFormat;
        mask = String(dF.masks[mask] || mask || dF.masks["default"]);

        var d = date.getDate(),
			D = date.getDay(),
			m = date.getMonth(),
			y = date.getFullYear(),
			H = date.getHours(),
			M = date.getMinutes(),
			s = date.getSeconds(),
			L = date.getMilliseconds(),
			o = date.getTimezoneOffset(),
			flags = {
			    d: d,
			    dd: pad(d),
			    ddd: dF.i18n.dayNames[D],
			    dddd: dF.i18n.dayNames[D + 7],
			    m: m + 1,
			    mm: pad(m + 1),
			    mmm: dF.i18n.monthNames[m],
			    mmmm: dF.i18n.monthNames[m + 12],
			    yy: String(y).slice(2),
			    yyyy: y,
			    h: H % 12 || 12,
			    hh: pad(H % 12 || 12),
			    H: H,
			    HH: pad(H),
			    M: M,
			    MM: pad(M),
			    s: s,
			    ss: pad(s),
			    l: pad(L, 3),
			    L: pad(L > 99 ? Math.round(L / 10) : L),
			    t: H < 12 ? "a" : "p",
			    tt: H < 12 ? "am" : "pm",
			    T: H < 12 ? "A" : "P",
			    TT: H < 12 ? "AM" : "PM",
			    Z: (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
			    o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4)
			};

        return mask.replace(token, function($0) {
            return ($0 in flags) ? flags[$0] : $0.slice(1, $0.length - 1);
        });
    };
} ();

// Some common format strings
dateFormat.masks = {
    "default": "ddd mmm d yyyy HH:MM:ss",
    shortDate: "m/d/yy",
    mediumDate: "mmm d, yyyy",
    longDate: "mmmm d, yyyy",
    fullDate: "dddd, mmmm d, yyyy",
    shortTime: "h:MM TT",
    mediumTime: "h:MM:ss TT",
    longTime: "h:MM:ss TT Z",
    isoDate: "yyyy-mm-dd",
    isoTime: "HH:MM:ss",
    isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
    isoFullDateTime: "yyyy-mm-dd'T'HH:MM:ss.lo"
};

// Internationalization strings
dateFormat.i18n = {
    dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
    monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function(mask) {
    return dateFormat(this, mask);
}

function setDate() {
    var now = new Date();

    $("#date").html(dateFormat(now, "dddd, mmmm d yyyy - h:MM:ss TT"));
}

//Called from the HTML of the page - after the MAIN div element but before any other elements or divs - so that 
//the user doesn't see the layout 'flash' from the default.
function setStyles() {
    var font = $.cookie('58bits_fontsize2')
    if (font) {
        setFont(font, false);
    }

    var layout = $.cookie('58bits_layout2')
    if (layout) {
        setLayout(layout, false);
    }
}



var captionTimer;

function showCaptionTimer(element) {
    clearTimeout(captionTimer)
    captionTimer = setTimeout(function() { showCaption(element); }, 400);
}

function hideCaptionTimer(element) {
    clearTimeout(captionTimer);
    hideCaption(element);
}


function showCaption(element) {
    if ($("#gallery_togglecaptions").html() == "Show all Captions") {
        if (element) {
            var filename = $(element).attr("id");
            if (filename) {                
                var selector = "#" + filename + " div.gallery_thumbnail_metadata";
                
                $(selector).show().animate({
                    top: "170px"
                }, 500);
            }
        }
    }
}

var animationTimer;

function hideCaption(element) {
    if ($("#gallery_togglecaptions").html() == "Show all Captions") {
        if(element) {
            var filename = $(element).attr("id");
            if (filename) {               
                var selector = "#" + filename + " div.gallery_thumbnail_metadata";
                animationTimer = setTimeout(function() { $(selector).hide(); }, 500);
                $(selector).animate({
                    top: "270px"
                }, 500);
            }            
        }
    }  
}

function forceShowCaption(metaDataElement)
{
    var filename = $(metaDataElement).parent().attr("id")
    if (filename) {
        var selector = "#" + filename + " div.gallery_thumbnail_metadata";
        $(selector).css("top","270px");
    }
    
}

function setCaptions()    
{
      //var captions = $.cookie('58bits_showcaptions')
      //if (captions && captions == "hide")     
      
      if ($("#gallery_togglecaptions").html() == "Show all Captions")
      {
        $("#gallery_togglecaptions").html("Hide all Captions");
        //$("div.gallery_thumbnail_metadata").slideDown(250);
        $("div.gallery_thumbnail_metadata").show().animate({
            top: "170px"
        }, 500);
  
        $.cookie("58bits_showcaptions", "show", { expires: 1000, path: '/'});       
      }
      else
      {
        $("#gallery_togglecaptions").html("Show all Captions");
        //$("div.gallery_thumbnail_metadata").slideUp(150);
        animationTimer = setTimeout(function() { $("div.gallery_thumbnail_metadata").hide(); }, 500);
        $("div.gallery_thumbnail_metadata").animate({
            top: "270px"
        }, 500);        
        
        $.cookie("58bits_showcaptions", "hide", { expires: 1000, path: '/'});
      }     
}


$(document).ready(function() {

    var font = $.cookie('58bits_fontsize2')
    if (font) {
        setFontVisuals(font);
    }

    var layout = $.cookie('58bits_layout2')
    if (layout) {
        setLayoutVisuals(layout);
    }

    setMenu();

    setDate();

    setInterval("setDate()", 5000);

    if ($("div.gallery_thumbnail_image").get(0)) {
        $("div.gallery_hidecaptions").show();

        var captions = $.cookie('58bits_showcaptions')
        if (captions && captions == "show") {
            $("div.gallery_thumbnail_metadata").css("top", "170px").show();
            $("#gallery_togglecaptions").html("Hide all Captions");
        }
        else {
            $("div.gallery_thumbnail_metadata").css("top", "270px").hide();
            $("#gallery_togglecaptions").html("Show all Captions");
        }

        $("div.gallery_thumbnail").hover(
            function() {
                showCaptionTimer(this);
            },
            function() {
                hideCaptionTimer(this);
            }
        );
    }
});