// JavaScript Document
// RollOvers

jQuery.noConflict(); 

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function toggle(opt) {
	if (document.getElementById(opt).style.display == "block") {
		document.getElementById(opt).style.display = "none";
	} else {
		document.getElementById(opt).style.display = "block";
	}
}

function actionEmail(opt) {
	jQuery('.email-label-from').css("color","#000");
	jQuery('.email-label-to').css("color","#000");
	jQuery('.email-label-subject').css("color","#000");
	var eurl = jQuery('#lightwindow_contents input.email-item-eurl').val();
	var from = jQuery('#lightwindow_contents input.email-item-from').val();
	var to = jQuery('#lightwindow_contents input.email-item-to').val();
	var subject = jQuery('#lightwindow_contents input.email-item-subject').val();
	var send = 1;
	if (from == "") {
		jQuery('.email-label-from').css("color","#f00");
		send = 0;
	}
	if (to == "") {
		jQuery('.email-label-to').css("color","#f00");
		send = 0;
	}
	if (subject == "") {
		jQuery('.email-label-subject').css("color","#f00");
		send = 0;
	}
	//alert(from + " : " + to + " : " + subject + " : " + send)
	if (send) {
		jQuery('.email-thanks').css("display","block");
		jQuery('.email-input').css("display","none");
		return true;
	} else {
		jQuery('.email-error').css("display","block");
	}
	return false;
}

function emailAgain() {
		jQuery('.email-label-from').css("color","#000");
		jQuery('.email-label-to').css("color","#000");
		jQuery('.email-label-subject').css("color","#000");
		jQuery('.email-thanks').css("display","none");
		jQuery('.email-error').css("display","none");
		jQuery('.email-input').css("display","block");
		return false;
}

jQuery(document).ready(function() {

//<!-- AddThis Button BEGIN -->
//<script type="text/javascript">var addthis_pub="benjamindauer";</script>
//<script //type="text/javascript" src="http://s7.addthis.com/js/200/addthis_widget.js"></script>
//<!-- AddThis Button END -->

	jQuery("#action-icons").ready(function() { 
		AddThis = '<script type="text/javascript">var addthis_pub="aiawebteam"; var addthis_brand="AIA.org";var addthis_header_color="#ffffff"; var addthis_header_background="#000000"; var addthis_options="email, favorites, print, live, linkedin, google, facebook, twitter, more";</script>';
		AddThis = AddThis + '<a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s7.addthis.com/static/btn/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a>';
		AddThis = AddThis + '<script //type="text/javascript" src="http://s7.addthis.com/js/200/addthis_widget.js"></script>';
		jQuery("#action-icons").html(AddThis);
	});

	// Print action
	jQuery("#print_page").click(function() { 
		window.print()
	});

	// Setup email div
	jQuery("a#email_page").ready(function() {
		// http://info.aia.org/aia/emailto.cfm?emailFrom=tom@idobi.com&emailTo=tom@idobi.com&subject=subject&eurl=http%3A%2F%2Fwww.aia.org&title=title
		var emailCode = '<div class="action-email-form">';
		emailCode = emailCode + '<h3>Email A Friend</h3>';
		emailCode = emailCode + '<div class="email-error">Please fill in all required fields</div>';
		emailCode = emailCode + '<form class="email-input" action="http://info.aia.org/aia/emailto.cfm" target="action-response" name="emailForm" method="get" onsubmit="return actionEmail(this);return false;">';
		emailCode = emailCode + '<input class="email-item-eurl" type="hidden" name="eurl" value="' + location.href + '" />';
		emailCode = emailCode + '<label class="email-label-from" for="emailFrom">From</label>';
		emailCode = emailCode + '<input class="email-item-input email-item-from" type="text" name="emailFrom" value="" /><br />';
		emailCode = emailCode + '<label class="email-label-to" for="emailTo">To</label>';
		emailCode = emailCode + '<input class="email-item-input email-item-to" type="text" name="emailTo" value="" /><br />';
		emailCode = emailCode + '<label class="email-label-subject" for="subject">Subject</label>';
		emailCode = emailCode + '<input class="email-item-input email-item-subject" type="text" name="title" value="" /><br />';
		emailCode = emailCode + '<input class="email-item-input email-item-subject" type="hidden" name="briefMessage" value="" />';
		emailCode = emailCode + '<input class="action-email-submit" type="submit" value="Send" />';
		emailCode = emailCode + '</form>'
		emailCode = emailCode + '<div class="email-thanks"><p>Your email has been sent.</p><p><a onclick="return emailAgain();" href="#">Send another email</a></p></div>';
		emailCode = emailCode + '</div>'
		jQuery("#action-icons").after('<iframe id="action-response" name="action-response">');
		jQuery("#action-icons").after('<div id="action-email">');
		jQuery("#action-email").html(emailCode);
//		jQuery("a#email_page").attr("href", "#action-email");
	});

/*
	jQuery("#lightwindow_overlay").click(function() {
		myLightWindow.createWindow('email_page');
		alert('hello');
	});
*/
	// Email action
	jQuery("a#email_page").click(function() {

		jQuery('.email-label-from').css("color","#000");
		jQuery('.email-label-to').css("color","#000");
		jQuery('.email-label-subject').css("color","#000");
		jQuery('.email-thanks').css("display","none");
		jQuery('.email-error').css("display","none");
		jQuery('.email-input').css("display","block");
/*
		myLightWindow.activateWindow({
			href: "#action-email", 
			title: 'Email a friend',
			height: '200'
		});
*/
	});

	// Search for video links and assign class and id for lightwindow
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		if (links[i].innerHTML == "<u>Link</u>") {
			links[i].setAttribute("class","popup_video");
			links[i].setAttribute("id","popup_video_"+i);
		}
	}

	// Open video (class=popup_video) in lightwindow
	jQuery("a.popup_video").click(function() {
		var temp = jQuery(this).attr("href");
		myLightWindow.activateWindow({
			href: temp, 
			title: 'Video'
		});
		return false;
	});
	
	// Share Page
	jQuery("#share-page").ready(function() {
		// facebook, linked in, youtube and flickr
		// 
		// Facebook
		// <script>function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}</script><a href="http://www.facebook.com/share.php?u=<url>" onclick="return fbs_click()" target="_blank"><img src="http://b.static.ak.fbcdn.net/images/share/facebook_share_icon.gif?8:26981" alt="" /></a>
		//
		// Linkedin
		// http://www.linkedin.com/shareArticle?mini=true&url={articleUrl}&title={articleTitle}&summary={articleSummary}&source={articleSource}
		
		var facebook = '<script>function fbs_click() {u=location.href;t=document.title;window.open(\'http://www.facebook.com/sharer.php?u=\'+encodeURIComponent(u)+\'&t=\'+encodeURIComponent(t),\'sharer\',\'toolbar=0,status=0,width=626,height=436\');return false;}</script><a class="action-facebook" href="http://www.facebook.com/share.php?u='+location.href+'" onclick="return fbs_click()" target="_blank"><img src="http://b.static.ak.fbcdn.net/images/share/facebook_share_icon.gif?8:26981" alt="" /></a>';
		jQuery("#share-page").html(jQuery("#share-page").html()+facebook);
		var linkedin = '<a class="action-linkedin" href="http://www.linkedin.com/shareArticle?mini=true&url='+location.href+'&title='+document.title+'">LinkedIn</a>';
		jQuery("#share-page").html(jQuery("#share-page").html()+linkedin);
		

		//$Launchpad.ShowButton({wid: "49d5adcf831ce265", userId: "49d5ad3f288c4b0f", source: "right-content"});

	});


});