/*jslint bitwise: true, eqeqeq: true, immed: true, newcap: true, nomen: false,
 onevar: false, plusplus: false, regexp: true, undef: true, white: true, indent: 2
 browser: true */

/*global jQuery: true Drupal: true window: true ThemeBuilder: true */

(function ($) {
  /**
   * Acquia object is created if it doesn't exist.
   */
  Drupal.behaviors.acquia = Drupal.behaviors.acquia || {};
  
  /**
  * Attach handler.
  */
  Drupal.behaviors.acquia = {
    equalCols: true,
    delay: NaN,
    attach: function (context, settings) {
      var addCommentTrigger = $('.comment-add a');
      var addCommentForm = $('#comment-new');
      var themebuilderIsOpen = Drupal.behaviors.acquia.checkThemeBuilderStatus();
      // Add only if the comment form exists on the page.
      if (addCommentForm.length > 0) {
        // Bind click event handlers to all .comment_add links
        addCommentTrigger.bind('click', {scrollTarget: addCommentForm}, Drupal.behaviors.acquia.scrollTo);
      }
      // Run functions that require a slight delay so that the screen can finish painting
      Drupal.behaviors.acquia.delay = setInterval(Drupal.behaviors.acquia.delayFunctions, 750);
      // Run the following only if the ThemeBuilder is closed
      if (!themebuilderIsOpen) {
        Drupal.behaviors.acquia.enhanceRotatingBanner();
      }
    }
  };
  
  
  /**
   * Run functions that require a slight delay so that the screen can finish painting
   */
  Drupal.behaviors.acquia.delayFunctions = function () {
    // Clear the timeout
    clearTimeout(Drupal.behaviors.acquia.delay);
    // Set all sidebars to the height of their parent container.
    Drupal.behaviors.acquia.equalizeColumns();
  };
  
  /**
   * Scrolls the page to the clicked element's location
   * @param {event} event
   *   The event.
   */
  Drupal.behaviors.acquia.scrollTo = function (event) {
    // Keep the page from jumping to the hash target
    event.preventDefault();
    var toolbar = {};
    toolbar.menuHeight = $('.toolbar-menu', '#toolbar').height() || 0;
    toolbar.drawerHeight = $('.toolbar-drawer', '#toolbar').height() || 0;
    var targetOffset = event.data.scrollTarget.offset().top - toolbar.menuHeight - toolbar.drawerHeight;
    $('html,body').animate({scrollTop: targetOffset}, 1000);
  };
  
  /**
   * Equalizes all .col elements in a stack to the height of the tallest .col
   */
  Drupal.behaviors.acquia.equalizeColumns = function () {
    if (Drupal.behaviors.acquia.equalCols) {
      var $stacks = $('.box');
      for (var i = 0; i < $stacks.length; i++) {
        // Get the current stack reference
        var _this = $stacks.eq(i);
        // Instantiate the boxHeight variable
        var boxHeight = 0;
        // Get the columns in the stack
        var cols = _this.find('.tb-height-balance');
        // We don't need to balance the columns if only one column is present
        if (cols.length > 1) {
          for (var j = 0; j < cols.size(); j++) {
            var col = cols.eq(j);
            // Only balance the height if the col is visible
            if (col.css('display') !== 'none' && col.css('visibility') !== 'hidden') {
              var colHeight = col.outerHeight(false);
              if (colHeight > boxHeight) {
                // If the height of the col is more than previous height, use it
                boxHeight = colHeight;
              }
            }
          }
        }
        // Only set the min-heights if boxHeight was assigned a real value
        if (boxHeight > 0) {
          cols.css('min-height', boxHeight);
        }
      }
    }
  };
  
  /**
   * Adds a click handler to the layout wrapper of the rotating banner.  Otherwise this element
   * would obsure the link wrapped around the image element
   */
  Drupal.behaviors.acquia.enhanceRotatingBanner = function () {
    var layouts = $('.layout-wrapper', '.rotating-banner');
    var len = layouts.length;
    if (len > 0) {
      while (len--) {
        Drupal.behaviors.acquia.pseudoLink(layouts.eq(len));
      }
    }
  };
  
  /**
   * Navigates to the designated element's data-link attribute value
   * 
   * @param {DomElement} element
   *   Element that the pseudo link will be applied to
   *
   * @attribute 
   *   data-link is an element attribute that uses the HTML5 data- pattern to store information in the DOM
   */
  Drupal.behaviors.acquia.pseudoLink = function (element) {
    var link = element.attr('data-link');
    if (link && link.length > 0) {
      //Bind a click event to the slide layout wrapper if it has a link, stored in data-link attribute
      element.click(function (event) {
        window.location = $(event.currentTarget).attr('data-link');
      });
    }
  };
  
  /**
   * Determines if the ThemeBuilder is open
   *
   * @return {Boolean}
   *   returns a boolean that represents the open state of the ThemeBuilder. True equals open.
   */
  Drupal.behaviors.acquia.checkThemeBuilderStatus = function () {   
    return $('body').hasClass('themebuilder') ? true : false;
  };
  
}(jQuery));
;
/*
 * Easy Retweet Button
 * http://ejohn.org/blog/retweet/
 *   by John Resig (ejohn.org)
 *
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

(function(){

window.RetweetJS = {
	// Your Bit.ly Username
	bitly_user: "retweetjs",

	// Your Bit.ly API Key
	// Found here: http://bit.ly/account
	bitly_key: "R_6287c92ecaf9efc6f39e4f33bdbf80b1",

	// The text to replace the links with
	link_text: (/windows/i.test( navigator.userAgent) ? "&#9658;" : "&#9851;") +
		"retweet",
		
	//Image source for retweet button
	retweet_button: "http://origin-static.ciscoeos.com/1271818001/zipupload/b2d/07f/52c0e69986deb6a64c7a9f8a53/images/twitter_share.png",

	// What # to show (Use "clicks" for # of clicks or "none" for nothing)
	count_type: "none",

	// Tweet Prefix text
	// "RT @jeresig " would result in: "RT @jeresig Link Title http://bit.ly/asdf"
	prefix: "",

	// Style information
	styling: "a.retweet { font: 12px Helvetica,Arial; color: #000; text-decoration: none; border: 0px; }" +
		"a.vert { display: block; text-align: center; font-size: 16px; float: left; margin: 4px; }" +
		"a.retweet strong.vert { display: block; margin-bottom: 4px; background: #F5F5F5; border: 1px solid #EEE; -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; padding: 3px; }" +
		"a.retweet span.vert { display: block; font-size: 12px; margin-left: 0px; }"
};

//////////////// No Need to Configure Below Here ////////////////

var loadCount = 1;

// Asynchronously load the Bit.ly JavaScript API
// If it hasn't been loaded already
if ( typeof BitlyClient === "undefined" ) {
	var head = document.getElementsByTagName("head")[0] ||
		document.documentElement;
	var script = document.createElement("script");
	script.src = "http://bit.ly/javascript-api.js?version=latest&login=" +
		RetweetJS.bitly_user + "&apiKey=" + RetweetJS.bitly_key;
	script.charSet = "utf-8";
	head.appendChild( script );

	var check = setInterval(function(){
		if ( typeof BitlyCB !== "undefined" ) {
			clearInterval( check );
			head.removeChild( script );
			loaded();
		}
	}, 10);

	loadCount = 0;
}

if ( document.addEventListener ) {
	document.addEventListener("DOMContentLoaded", loaded, false);

} else if ( window.attachEvent ) {
	window.attachEvent("onload", loaded);
}

function loaded(){
	// Need to wait for doc ready and js ready
	if ( ++loadCount < 2 ) {
		return;
	}

	var elems = [], urlElem = {}, hashURL = {};

	BitlyCB.shortenResponse = function(data) {
		for ( var url in data.results ) {
			var hash = data.results[url].userHash;
			hashURL[hash] = url;

			var elems = urlElem[ url ];

			for ( var i = 0; i < elems.length; i++ ) {
				elems[i].href += hash;
			}

			if ( RetweetJS.count_type === "clicks" ) {
				BitlyClient.stats(hash, 'BitlyCB.statsResponse');
			}
		}
	};

	BitlyCB.statsResponse = function(data) {
		var clicks = data.results.clicks, hash = data.results.userHash;
		var url = hashURL[ hash ], elems = urlElem[ url ];

		if ( clicks > 0 ) {
			for ( var i = 0; i < elems.length; i++ ) {
				var strong = document.createElement("strong");
				strong.appendChild( document.createTextNode( clicks + " " ) );
				elems[i].insertBefore(strong, elems[i].firstChild);

				if ( /(^|\s)vert(\s|$)/.test( elems[i].className ) ) {
					elems[i].firstChild.className = elems[i].lastChild.className = "vert";
				}
			}
		}

		hashURL[ hash ] = urlElem[ url ] = null;
	};

	if ( document.getElementsByClassName ) {
		elems = document.getElementsByClassName("retweet");
	} else {
		var tmp = document.getElementsByTagName("a");
		for ( var i = 0; i < tmp.length; i++ ) {
			if ( /(^|\s)retweet(\s|$)/.test( tmp[i].className ) ) {
				elems.push( tmp[i] );
			}
		}
	}

	if ( elems.length && RetweetJS.styling ) {
		var style = document.createElement("style");
		style.type = "text/css";

		try {
			style.appendChild( document.createTextNode( RetweetJS.styling ) );
		} catch (e) {
			if ( style.styleSheet ) {
				style.styleSheet.cssText = RetweetJS.styling;
			}
		}

		document.body.appendChild( style );
	}

	for ( var i = 0; i < elems.length; i++ ) {
		var elem = elems[i];

		if ( /(^|\s)self(\s|$)/.test( elem.className ) ) {
			elem.href = window.location;
			elem.title = document.title;
		}

		var origText = elem.title || elem.textContent || elem.innerText,
			href = elem.href;

		elem.innerHTML = '<img src="'+ RetweetJS.retweet_button +'"/>';
		elem.title = "";
		elem.href = "http://twitter.com/home?status=" +
			encodeURIComponent(RetweetJS.prefix + origText + " http://bit.ly/");

		if ( urlElem[ href ] ) {
			urlElem[ href ].push( elem );
		} else {
			urlElem[ href ] = [ elem ];
			BitlyClient.shorten(href, 'BitlyCB.shortenResponse');
		}
	}

}

})();
;

