/* set up the mailto link */

String.prototype.rot13 = function(){
  return this.replace(/[a-zA-Z]/g, function(c){
    return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
  });
};


/* works with the slide gallery */

function galleryButtons() { 
  if( 0 == galleryData.position ) { 
    jQuery('#gallery_prev').removeClass('gallery-move-active').addClass('gallery-move-inactive');
    jQuery('#gallery_next').removeClass('gallery-move-inactive').addClass('gallery-move-active');
  } else if ( galleryData.count == galleryData.position ) { 
    jQuery('#gallery_next').removeClass('gallery-move-active').addClass('gallery-move-inactive');
    jQuery('#gallery_prev').removeClass('gallery-move-inactive').addClass('gallery-move-active');
  } else { 
    jQuery('#gallery_next').removeClass('gallery-move-inactive').addClass('gallery-move-active');
    jQuery('#gallery_prev').removeClass('gallery-move-inactive').addClass('gallery-move-active');
  }
}


function indicatorColor(moving) {
  if(moving) jQuery('#indicator').removeClass().addClass('moving');
  else if(0 == galleryData.position) jQuery('#indicator').removeClass().addClass('begin');
  else if(galleryData.count == galleryData.position) jQuery('#indicator').removeClass().addClass('end');
  else jQuery('#indicator').removeClass().addClass('moving');
}

function indicatorMove() {
  indicatorColor(1);
  if( 0 == galleryData.position ) var indicatorPosition = '0px';
  else {
    var indicatorPositionPx = Math.round( 
          (galleryData.position / galleryData.count) 
          * 
          ( galleryData.buttonWidth - galleryData.indicatorWidth ) 
        );
    var indicatorPosition = indicatorPositionPx + 'px' ;
  }
  jQuery('#indicator').clearQueue();
  jQuery('#indicator').animate({ left: indicatorPosition }, galleryData.slideSpeed , indicatorColor);
}

function galleryLeft() { 
  if(galleryData.position < galleryData.count ) galleryData.position += 1; 
  else galleryData.position = 0;
  galleryMove();
  galleryButtons(); 
}

function galleryRight() {
  if(galleryData.position > 0) galleryData.position -= 1;
  else galleryData.position = galleryData.count;
  galleryMove();
  galleryButtons(); 
}

function changeText() {
if(galleryData.texts[galleryData.position])
  jQuery('#postcontent').html(Base64.decode(galleryData.texts[galleryData.position]));
}

function galleryMove() {
    jQuery(galleryData.slider).clearQueue();
    var i = galleryData.position;
    var moveTo = -1 * galleryData.positions[galleryData.position];
    jQuery(galleryData.slider).animate({ left: moveTo }, galleryData.slideSpeed ); 
    indicatorMove();
    galleryButtons(); 
    changeText();
}


function galleryInit() {
  jQuery(galleryData.container).css("overflow", "hidden");
  jQuery(galleryData.container).css("height", galleryData.galleryHeight);
  var html = '<div id="gallerycontrol" style="display: none;"><div id="indicator">' 
    + '</div><a id="gallery_prev" href="#">Previous</a><a id="gallery_next" href="#">Next</a></div>';
  jQuery(galleryData.container).after(html);

  jQuery("#gallery_prev").click( function(event) { event.preventDefault(); galleryRight(); });
  jQuery("#gallery_next").click( function(event) { event.preventDefault(); galleryLeft(); });

  // galleryData.indicatorStart = galleryData.buttonWidth - galleryData.indicatorWidth;
  // galleryData.indicatorStartCss = galleryData.indicatorStart + 'px';
  galleryData.indicatorWidthCss = galleryData.indicatorWidth + 'px';

  jQuery("#indicator").css({"width": galleryData.indicatorWidthCss, "left": 0 } );
  galleryButtons();
  
  jQuery(window).load(function () { 
    jQuery("#gallerycontrol").slideDown(800, function() { 
      galleryButtons(); 
      indicatorColor();

//        jQuery("#indicator").animate({ right: galleryData.indicatorStartCss }, 800, function(){ galleryButtons(); indicatorColor(); }); 
      } );
  });

}




/* end works with the slide gallery */


/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

/*
var keyStr = "ABCDEFGHIJKLMNOP" +
						"QRSTUVWXYZabcdef" +
						"ghijklmnopqrstuv" +
						"wxyz0123456789+/" +
						"=";

function encode64(input) {

	var output = "";
	var chr1, chr2, chr3 = "";
	var enc1, enc2, enc3, enc4 = "";
	var i = 0;

	do {
		 chr1 = input.charCodeAt(i++);
		 chr2 = input.charCodeAt(i++);
		 chr3 = input.charCodeAt(i++);

		 enc1 = chr1 >> 2;
		 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		 enc4 = chr3 & 63;

		 if (isNaN(chr2)) {
				enc3 = enc4 = 64;
		 } else if (isNaN(chr3)) {
				enc4 = 64;
		 }

		 output = output +
				keyStr.charAt(enc1) +
				keyStr.charAt(enc2) +
				keyStr.charAt(enc3) +
				keyStr.charAt(enc4);
		 chr1 = chr2 = chr3 = "";
		 enc1 = enc2 = enc3 = enc4 = "";
	} while (i < input.length);

	return output;
}
*/


/* maps */

function addMap(mapGoesHere, lat, long, mapZoom) {

	var myMap;
    if(!mapZoom) mapZoom = 15;
	var myOptions = {
		zoom: mapZoom,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}

	myMap = new google.maps.Map(document.getElementById(mapGoesHere), myOptions);

	var NSlocation = new google.maps.LatLng(lat, long);
	var NScenter   = new google.maps.LatLng(lat, long);
	
	myMap.setCenter(NScenter);
 
    var marker = new google.maps.Marker({
        map: myMap, 
        position: NSlocation
	});

    
}

/* the main menu show-hide */

function openSesame(object) {
	jQuery(object).css('opacity',0);
	jQuery(object).css('display','inline');
	jQuery(object).fadeTo(500, 1);
}

function closeSesame(object) {
	jQuery(object).fadeTo(500, 0, function() {
	jQuery(object).css('opacity',0);
	jQuery(object).css('display','none');
	});
}


/* home page fader */

var currentHomePageFade = 1;

function homePageFader(max) {
    /* bunch 'o stuff hardcoded for expediancy */
    
    var outimg = '#oli' + currentHomePageFade.toString();
    var outtxt = '#olt' + currentHomePageFade.toString();
    
    currentHomePageFade += 1;
    if( currentHomePageFade > homePageFadeMax) currentHomePageFade = 1;

    var inimg = '#oli' + currentHomePageFade.toString();
    var intxt = '#olt' + currentHomePageFade.toString();
    
    /* times staggered */
    jQuery(outimg).fadeOut(1500);
    jQuery(outtxt).fadeOut(1000);
    jQuery(inimg).fadeIn(1600);
    jQuery(intxt).fadeIn(1000);
    
    setTimeout( "homePageFader()", 6000); 
    
}

