//===============================================
// EXTERNAL LINKS
//===============================================
function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

/*
JSTarget function by Roger Johansson, www.456bereastreet.com
*/
var JSTarget = {
	init: function(att,val,warning) {
		if (document.getElementById && document.createElement && document.appendChild) {
			var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'class' : att;
			var strVal = ((typeof val == 'undefined') || (val == null)) ? 'non-html' : val;
			var strWarning = ((typeof warning == 'undefined') || (warning == null)) ? ' (opens in a new window)' : warning;
			var oWarning;
			var arrLinks = document.getElementsByTagName('a');
			var oLink;
			var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
			for (var i = 0; i < arrLinks.length; i++) {
				oLink = arrLinks[i];
				if ((strAtt == 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(strAtt)))) {
					oWarning = document.createElement("em");
					oWarning.appendChild(document.createTextNode(strWarning));
					oLink.appendChild(oWarning);
					oLink.onclick = JSTarget.openWin;
				}
			}
			oWarning = null;
		}
	},
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {
		    var oWin = window.open(this.getAttribute('href'), '_blank');
			if (oWin) {
				if (oWin.focus) oWin.focus();
				return false;
			}
			oWin = null;
			return true;
		}
	}
};

if(document.getElementById && document.createTextNode)
{
  addEvent(window, 'load', function(){JSTarget.init("class","external","");});
  addEvent(window, 'load', function(){JSTarget.init("rel","external","");});
  addEvent(window, 'load', function(){JSTarget.init("class","pdf","");});
}



// jQuery Functions
// ================
$(document).ready(function(){

	// add classes to subnav list items
	// --------------------------------
	if ($("#ip-subnav")) {
	
		// misc vars for loops below
		var myCellCount;
		var moo;
		var baa;
		var x;
	
		myCellCount = $("#ip-subnav li").size();
		x = 1;
	
		for(i=0;i<=myCellCount;i++) {
			moo = "#main #main-right #main-right-content-right #ip-subnav ul li:eq(" + i + ")";
			baa = "subnav-style-" + x;
	
			$(moo).addClass(baa);
	
			if (x == 5) {
				x = 1;
			} else {
				x = x + 1;
			}
		}
	}


	// capitalize the first letter of each word for the top level store links
	if ($('#block-menu-menu-store-nav ul.menu')) {

		$('#block-menu-menu-store-nav > div > ul > li').each(function() {
			var currentText = $(this).children('a').text();
			var textBits = currentText.split(" ");
			var fixedBits = "";
			var bitLength;
			var firstLetter;
			var balWord;
			var i;

			for(i=0; i<textBits.length; i++) {
				bitLength = textBits[i].length;
				firstLetter = textBits[i].substr(0,1);
				balWord = textBits[i].substr(1);
				fixedBits = fixedBits + "<span class=\"ucase\">" + firstLetter + "</span>" + balWord + " ";
			}

			$(this).children('a').html(fixedBits);

		});

	}


	// change the target of the email signup form
	if ($('#node-17')) {
		$('#node-17 input.submit').click(function() {
			changeFormTarget('#node-17 form');  // change the form action and target the upload iframe
			resetFormTarget('#node-17 form'); // wait a second and reset the form action back to default and remove the iframe targeting
		});
	}


	// misc helper functions
	function changeFormTarget(whichone) {
		$(whichone).attr("target","upload_target");
	}

	function resetFormTarget(whichone) {
		setTimeout(function(){
			$(whichone).removeAttr("target");
		}, 1000);
	}



});



