
// File file contains some commonly used javascript. It's included in a file
// to improve performance by caching commonly used commands

//overlib defaults
var ol_bgclass='olbg';
var ol_fgclass='olfg';
var ol_textfontclass='oltext';
var ol_captionfontclass='olcaption';
var ol_width=250;

// Step size for side scroller
var side_scroller_step = 100;

// Use this to display/hide error messages
jQuery(document).ready(function() {

	/*
    jQuery(".message").fadeIn(2000);
    setTimeout("jQuery('.message').css('background-image', 'none'); jQuery('.message').fadeOut(500)", 2500);
	*/

    jQuery(".directory-name-short, .directory-name").hover(
	function() {
	    jQuery(this).css({
		'background-color': '#F5F5F5',
		'border': "solid 1px #CCC"
	    });
	},
	function() {
	    jQuery(this).css({
		"background": "transparent",
		"border": "solid 1px #FFF"
	    });
	});

  gabTabs();
});

// jQuery command for rounded boxes
// Originally inspired by http://15daysofjquery.com/wrap-it-up-pretty-corners/13/
jQuery(document).ready(function() {

	// Character countdown script for button_text in
	// com_contactbutton
	jQuery('#button_text').keyup( function() { chars_remaining('#button_text')
	});

	// Synchronize the fields in com_contactbutton
	// keep both facebook inputs in sync based on which is visible
	jQuery('#networks_cb_facebook_top').change(function() { sync_network_fields('networks_cb_facebook_top'); });
	jQuery('#networks_cb_myspace_top').change(function() { sync_network_fields('networks_cb_myspace_top'); });
	jQuery('#networks_cb_twitter_top').change(function() { sync_network_fields('networks_cb_twitter_top'); });
	jQuery('#networks_cb_digg_top').change(function() { sync_network_fields('networks_cb_digg_top'); });
	jQuery('#networks_cb_flickr_top').change(function() { sync_network_fields('networks_cb_flickr_top'); });
	jQuery('#networks_cb_linkedin_top').change(function() { sync_network_fields('networks_cb_linkedin_top'); });
	jQuery('#networks_cb_youtube_top').change(function() { sync_network_fields('networks_cb_youtube_top'); });
	jQuery('#networks_cb_classmates_top').change(function() { sync_network_fields('networks_cb_classmates_top'); });
	jQuery('#networks_cb_wordpress_top').change(function() { sync_network_fields('networks_cb_wordpress_top'); });
	jQuery('#networks_cb_tumblr_top').change(function() { sync_network_fields('networks_cb_tumblr_top'); });
	jQuery('#networks_cb_blogger_top').change(function() { sync_network_fields('networks_cb_blogger_top'); });
	jQuery('#networks_cb_livejournal_top').change(function() { sync_network_fields('networks_cb_livejournal_top'); });
	jQuery('#networks_cb_typepad_top').change(function() { sync_network_fields('networks_cb_typepad_top'); });
	jQuery('#networks_cb_vox_top').change(function() { sync_network_fields('networks_cb_vox_top'); });
	jQuery('#networks_cb_xanga_top').change(function() { sync_network_fields('networks_cb_xanga_top'); });
	jQuery('#networks_cb_live_top').change(function() { sync_network_fields('networks_cb_live_top'); });
	jQuery('#networks_cb_delicious_top').change(function() { sync_network_fields('networks_cb_delicious_top'); });
	jQuery('#networks_cb_technorati_top').change(function() { sync_network_fields('networks_cb_technorati_top'); });
	jQuery('#networks_cb_stumbleupon_top').change(function() { sync_network_fields('networks_cb_stumbleupon_top'); });
	jQuery('#networks_cb_sphinn_top').change(function() { sync_network_fields('networks_cb_sphinn_top'); });
	jQuery('#networks_cb_slashdot_top').change(function() { sync_network_fields('networks_cb_slashdot_top'); });
	jQuery('#networks_cb_reddit_top').change(function() { sync_network_fields('networks_cb_reddit_top'); });
	jQuery('#networks_cb_yahoobuzz_top').change(function() { sync_network_fields('networks_cb_yahoobuzz_top'); });
	jQuery('#networks_cb_mixx_top').change(function() { sync_network_fields('networks_cb_mixx_top'); });

	jQuery("input[name='button_type']").click(
		function(){
			chars_remaining('#button_text');
		});

	jQuery("#new-traces-down").click( function(){
		position = jQuery("#new-traces").position();
		if( position.top < jQuery("#new-traces").height() ) {
			jQuery("#new-traces").animate({"top": "-=" + side_scroller_step + "px" }, 'fast');
		}
	});

	jQuery("#new-traces-up").click( function(){
		position = jQuery("#new-traces").position();
		if(position.top < 0) {
			jQuery("#new-traces").animate({"top": "+=" + side_scroller_step + "px" }, 'fast');
		}
	});

	jQuery('a#all-networks-link').click( function() {
		jQuery('#top-networks').hide();
		jQuery('#all-networks').show();
		jQuery(this).parent().hide();
		jQuery('#top-networks-text').show();
	});

	jQuery('a#top-networks-link').click( function() {
		jQuery('#all-networks').hide();
		jQuery('#top-networks').show();
		jQuery(this).parent().hide();
		jQuery('#all-networks-text').show();
	});

});

// Count the number of characters left in a field before maxlength
function chars_remaining( selector )
{
	var max = jQuery(selector).attr('maxlength');
	var x = jQuery(selector).val().length;
	var cnt = max - x;
	jQuery('#button_text_count').text(cnt + ' characters left out of ' + max);
}


// Copy field values to a hidden input
function sync_network_fields(fieldName)
{
	var x = fieldName.substr(0, (fieldName.length - 4));
	if (jQuery('#top-networks').is(':visible')) {
		jQuery('#' + x).val( jQuery('#' + fieldName).val() );
	}
}

// Uses jQuery to switch out tabs when viewing member profile
// linkid			the ID of the element you wish to select
// sibling_class 	the class shared between sibllings of the elements to toggle

/*
Example CSS structure - note that the "-link-" in the tab is crucial!
<li id="something-link-1" class="sibling-class"> ...
<li id="something-link-2" class="sibling-class"> ...
<li id="something-link-3" class="sibling-class selected"> ...

<div id="something-1"> ...
<div id="something-2"> ...

*/
function dnSwitchContentTabs( linkid, sibling_class )
{
	var arr = linkid.split("link-");
	var tabname = arr[0] + arr[1];

	jQuery('.' + sibling_class + ':visible').hide();
	jQuery('#' + tabname).show();

	jQuery('ul.tabs li').removeClass('selected');
	jQuery('#' + linkid).parent().addClass('selected');
	return false;
}

/**
*
*  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;
    }

};

/* +-------------------------------------------------+
 *
 * Animation for mock profile in GAB LVL 4
 *
/* +-------------------------------------------------*/
var scrollIndex = 1;
var doScroll = true;
var totalElements;
var autoScrollTimeout = false;

jQuery(document).ready( function() {
	totalElements = jQuery('#new-members-table td').length - 6;

	setTimeout('autoScroll()', 3000);
	jQuery('#new-members-scroller').hover(
		function() {
			doScroll = false;
		},
		function() {
			doScroll = true;
		}
	);

	initNameHover();
});

function initNameHover()
{
	jQuery('.member-scroller-link').hover(
		function() {
			jQuery(this).siblings('.member-scroller-name').show();
		},
		function() {
			jQuery(this).siblings('.member-scroller-name').hide();
		}
	);
}

function autoScroll()
{
	if( doScroll == true )
	{
		var box = jQuery('#new-members-table td:first');
		var position = jQuery("#new-members-table").position();
		var copy = box.clone();
		copy.children('a').css({width: "0px", margin: "0px"});
		copy.appendTo('#new-members-table tr');
		copy.children('a').animate({
			width: '100px',
			marginRight: '24px'
			}, 800, 'linear', function() {
				box.remove();
				initNameHover();
			});
	}
	autoScrollTimeout = setTimeout('autoScroll()', 3000);
}

function stopScroll()
{
    if (autoScrollTimeout) clearTimeout(autoScrollTimeout);
}

function gabTabs() {
  if ( jQuery( '#ghost-profile-tab-contact').length == 0 ) { return false; }
  var tabs=jQuery('.profile-right-column .tabs li a' );
  var content=jQuery( '.profile-right-column .tabbed-content div' );
  tabs.each( function(i) {
    tabs[i].onclick=function() {
      // change the selected tab
      jQuery( '.profile-right-column .tabs li' ).each( function( k) { jQuery( this ).removeClass('selected'); } );
      jQuery( this ).parents('li').addClass('selected');

      // find the contetns of stuff to show.
      var contents=jQuery( '.profile-right-column .tabbed-content .profile-item' );
      contents.each( function(j) {
        jQuery( contents[j]).removeClass('profile-item');
        jQuery( contents[j]).addClass('profile-item-hidden' );
        jQuery( contents[j]).removeClass('clearfix');
      });
      var myself=jQuery( this ).attr('id' );
      if ( myself == 'ghost-profile-tab-link-contact' ) {
        jQuery( '#ghost-profile-tab-contact').addClass('profile-item' );
        jQuery( '#ghost-profile-tab-contact').removeClass('profile-item-hidden' );
      }
      else if ( myself == 'ghost-profile-tab-link-social' ) {
        jQuery( '#ghost-profile-tab-social').addClass('profile-item' );
        jQuery( '#ghost-profile-tab-social').removeClass('profile-item-hidden' );
      }
      else if ( myself == 'ghost-profile-tab-link-photos' ) {
        jQuery( '#ghost-profile-tab-photos').addClass('profile-item' );
        jQuery( '#ghost-profile-tab-photos').removeClass('profile-item-hidden' );
      }
      else if ( myself == 'ghost-profile-tab-link-video' ) {
        jQuery( '#ghost-profile-tab-video').addClass('profile-item' );
        jQuery( '#ghost-profile-tab-video').removeClass('profile-item-hidden' );
      }
      else if ( myself == 'ghost-profile-tab-link-web' ) {
        jQuery( '#ghost-profile-tab-web').addClass('profile-item' );
        jQuery( '#ghost-profile-tab-web').removeClass('profile-item-hidden' );
      }
      return false;
    };
  });
}


