﻿
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" });
        }
        else {
            $("#home").css({ backgroundColor: "#000", color: "#FFF" });
        }        
    }
}


/***************************************************************************************
** Font size switcher 
****************************************************************************************/
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");
    }
}



/***************************************************************************************
** Layout switcher - with fix for IE. IE needs a 'jog' in width to apply the new layout
****************************************************************************************/
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);
}

/******************************************
** Set the date on the header
*******************************************/
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);
    }
}

/********** Slide Show **************/
function slideShow() {

    //Set the opacity of all images to 0
    $('#slides div').css({ opacity: 0.0 });

    //Get the first image and display it (set it to full opacity)
    $('#slides div:first').css({ opacity: 1.0 });  

    //Set the caption background to semi-transparent
    $('#caption').css({ opacity: 0.7 });  
    
    $('#caption').css({ width: $('#slides div').find('img').css('width') });

    //Get the caption of the first image from caption DIV attribute and display it
    $('#caption .content').html($('#slides div:first').find('.caption').html())
	.animate({ opacity: 0.7 }, 400);

    //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
    setInterval('gallery()', 5000);

}

function gallery() {

    //if no IMGs have the show class, grab the first image
    var current = ($('#slides div.show') ? $('#slides div.show') : $('#slides div:first'));

    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    //var next = ((current.next().length) ? ((current.next().hasClass('caption')) ? $('#gallery a:first') : current.next()) : $('#gallery a:first'));
   
    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    var next = ((current.next().length) ? current.next() : $('#slides div:first'));

    //Get next image caption
    var caption = next.find('.caption').html();

    //Set the fade in effect for the next image, show class has higher z-index
    next.css({ opacity: 0.0 }).addClass('show').animate({ opacity: 1.0 }, 1000);

    //Hide the current image
    current.animate({ opacity: 0.0 }, 1000).removeClass('show');

    //Set the opacity to 0 and height to 1px
    $('#gallery #caption').animate({ opacity: 0.0 }, { queue: false, duration: 0 }).animate({ height: '1px' }, { queue: true, duration: 300 });

    //Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
    $('#gallery #caption').animate({ opacity: 0.7 }, 100).animate({ height: '45px' }, 500);

    //Display the content
    $('#gallery .content').html(caption);


}

/******************************************************
** Start script - on document ready - calling any required functions above
*******************************************************/
$(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);

    slideShow();

//    SyntaxHighlighter.config.clipboardSwf = 'http://www.58bits.com/blog/scripts/clipboard.swf';
//    SyntaxHighlighter.all();
    
});

