﻿/* aipla javascript, jquery required */

$(document).ready(function() {

		SetFooterSiteMap();
		
		BuildTabs();
		
		BuildSearchControls();
		
		StyleLoginControl();
		
		BindTextSizeControls();

});

function StyleLoginControl() {
	$('table[id$=_Login]').parent().addClass('login-container');
//	$('#memberContent .fieldInput input');
}

function BuildSearchControls() {
	$('#search').prepend('<div id="advSearch">');
	$('#search #advSearch').html($('a[id$=_AdvSearchLink]').first());	
}

function SetFooterSiteMap() {

	var navHtml = "";
	$('#navigation-bar ul.root ul.static li.static').each(function() {
		navHtml += '<div class="site-map-subsite">' + $(this).html() + '</div>';
	});
	
	$('#footer-site-map').html(navHtml+'<div class="clear"></div>');

}
function BuildTabs() {
	var tabsHTML = "";
	var tabContainersHTML = "";
	$('#tabs').each(function() {
		$('#content-left-zone .s4-wpcell-plain').each(function(index) {
			$(this).hide();
			var title = $(this).find('.ms-WPTitle').text();
			var id = index;
			var body = $(this).find('.ms-WPBody').html();
			tabsHTML  += '<li class="tab" id="tab_'+id+'"><div>'+							
							'<div class="tab-right"></div>'+			
							'<div class="tab-center">'+title+'</div>'+
							'<div class="tab-left"></div>'+
							'<div class="clear"></div>'+							
						'</div></li>';
			tabContainersHTML += ''+
				'<div class="tab-container" style="display:none;" id="tab-container_'+id+'">'+
					body+
				'</div>';
		});
	});
	
	$('#tabs .tab-tabs').html('<ul>'+tabsHTML+'</ul><div class="clear"></div>');
	$('#tabs .tab-content-middle-center').html(tabContainersHTML);
	
	// show first tab
	$('#tabs .tab-container:first').show();
		$('#tabs .tab:first').addClass('active');
		
	// add click to show tab
	$('#tabs .tab').click(function() {
		$('#tabs .tab-container').hide();
		$('#tabs .tab').removeClass('active');
		
		var id = $(this).attr('id').replace('tab_','');
		$(this).addClass('active');
		$('#tab-container_'+id).show();
	});

	
}

function BindTextSizeControls(){
	var theSize = readCookie('fontsize');
	if(theSize != null) {
		fontSize(theSize);
	}
	else {
		fontSize('s');
	}
	$('.font-size-1').click(function(e){
		e.preventDefault();
		fontSize('s');
	});
	$('.font-size-2').click(function(e){
		e.preventDefault();
		fontSize('m');
	});
	$('.font-size-3').click(function(e){
		e.preventDefault();
		fontSize('l');
	});
}
function fontSize(theSize){
	switch(theSize) {
		case 'm':
			createCookie('fontsize', theSize, 365);
			$('#content').css('font-size', '1.15em');
			$('.font-size').css('font-weight', 'normal');
			$('.font-size').css('text-decoration', 'none');
			$('.font-size-2').css('font-weight', 'bold');
			$('.font-size-2').css('text-decoration', 'underline');
			break;
		case 'l':
			createCookie('fontsize', theSize, 365);
			$('#content').css('font-size', '1.3em');
			$('.font-size').css('font-weight', 'normal');
			$('.font-size').css('text-decoration', 'none');
			$('.font-size-3').css('font-weight', 'bold');
			$('.font-size-3').css('text-decoration', 'underline');
			break;
		default:
			createCookie('fontsize', theSize, 365);
			$('#content').css('font-size', '1em');
			$('.font-size').css('font-weight', 'normal');
			$('.font-size').css('text-decoration', 'none');
			$('.font-size-1').css('font-weight', 'bold');
			$('.font-size-1').css('text-decoration', 'underline');
	}
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}




// email a friend link

function emailFriend() {
	var email = "mailto:?subject=I thought you would be interested in this AIPLA Content:  " + document.title;
	email += "&body=I thought you might be interested in this content on the American Intellectual Property Law Association's website: " + document.title;
	email += ". You can view it here:  " + location.href; 
	location.href = email;
}
