var pageTitle;
var addthis_config = {
data_track_clickback:true,
ui_508_compliant: true,
ui_use_css : true
};

/* AddThis function. */
function addThisSharebox() { 
		   $("#toolbox").appendTo("#middle_content");
		   var tbx = document.getElementById("toolbox");
		   svcs = {email: 'Email', print: 'Print', facebook_like: '', tweet: '', google_plusone: '', expanded: 'More'};
		   for (var s in svcs) {
		   			//console.log(s + " , " + document.title + " , " + window.location);
		   			tbx.innerHTML += '<a class="addthis_button_'+s+'" addthis:title="Works for Me-PA '+document.title+'" addthis:url="'+window.location+'">'+svcs[s]+'</a>'; } 
		   addthis.toolbox("#toolbox");
}

//Load Hash URL Hack for IE
function loadHashURL() { 
var browserName=navigator.appName; 
 if (browserName=="Microsoft Internet Explorer") {
  	var currenturl = window.location.hash;
  	var currenturlhash = currenturl.substr(2);
  	if(currenturlhash != "") {	
  		$(".secondarylinks").find(currenturlhash).removeClass("mainhover");
		$(this).toggleClass("mainhover");
		$("#middle_content").load(currenturlhash);
		addThisSharebox(); }
 } 
 else
  {
  var currenturl = window.location.hash.substring(2);
	if(currenturl != "") {	
	currenturl.substring(0, currenturl.length-4);
	var currenthd = currenturl.substring(0, currenturl.length-4);
	$.ajax({
		url: currenturl,
        success: function(data) {
        	$("#"+currenthd).find("a").addClass("mainhover");
            $('#middle_content').html(data + "<br /><div id='toolbox' class='addthis_toolbox addthis_default_style'></div>");
            addThisSharebox();
        }
    });
} 

   }
}

function playAudio(fi) { 
	$(fi).jmp3({backcolor: "ffffff",forecolor: "000000",width: 90,showdownload: "false"});
}

$(document).ready(function(){
loadHashURL();


//$("a[target=_blank]").attr('target','_blank');

$(".secondarylinks").delegate("a", "click", function(event) {
		event.preventDefault();
 		var ae = "!" + $(this).attr("href");
 		var pageTitle;
 		var mp3html = "";
 		$("#jwaudio").remove();
        window.location.hash = ae;
        $(".secondarylinks").find("a").removeClass("mainhover");
		$(this).toggleClass("mainhover");
		var u = $(this).parent().attr('id');
		var audiofile= '<div id="jwaudio">Audio: Play this section <div id="player"></div>';
		var mp3html = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
		mp3html += 'width="100" height="30" ';
		mp3html += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">';
		mp3html += '<param name="movie" value="audio/singlemp3player.swf?';
		mp3html += 'file=audio/media/'+u+'.mp3&backColor=ffffff&frontColor=000000&showDownload=false" />';
		mp3html += '<param name="wmode" value="transparent" />';
		mp3html += '<embed wmode="transparent" width="100" height="30" ';
		mp3html += 'src="audio/singlemp3player.swf?'
		mp3html += 'file=audio/media/'+u+'.mp3&backColor=ffffff&frontColor=000000&showDownload=false"';
		mp3html += 'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		mp3html += '</object>';
        //return false;
        $.ajax({
        url: $(this).attr("href"),
        success: function(data) {
           $('#middle_content').before(audiofile);
           $("#player").html(mp3html);
           $('#middle_content').html(data + "<br /><div id='toolbox' class='addthis_toolbox addthis_default_style'></div>");
           pageTitle = $('#middle_content').find("h2").html();
           document.title = pageTitle; 
		   addThisSharebox();
		  /*$("a[target=_blank]").leaveNotice({
			siteName: 'Works for me - PA',
			exitMessage:"<h3>You are exiting the {SITENAME}.</h3><br />Thank you for visiting our site.",
			preLinkMessage:"<hr /><p>You will now access:<br /><strong> {URL} </strong></p><hr />",
			timeOut: 5000
	}); */
           $("body").scrollTo(".mainhover");
           $('#middle_content p').highlightRegex(/works for me/ig); 
           //$("div:contains('Works for me')").css("font-weight", "bold");
        }
    });
     //console.log(u);
});


 $('#skip-to-content').focus(function(){
            $(this).addClass('skip-to-content-link-focus');
        });
    $('#skip-to-content').blur(function(){
            $(this).removeClass('skip-to-content-link-focus');
});


$("a[href*='.pdf']").click(function(){
	$(this).attr({"target":"_self"});
	return false;
});

/*------Load the Newsletter and the links for download and view-----*/


$("#newslettermonth li").click(function() {
		$("#newslettermonth li").removeClass('monthselect'); 
		$(this).addClass('monthselect');
		$('#newsletter1').remove();
		if(document.location.pathname=="/new/newsletter/youthnewsletter.php") { 
		var $getMY = "/profile/youthnewsletters/viewlist/"+$(this).attr("id");
		var $getPDF = "downloads/youth/"+$(this).attr("id");
		var $getPrint = "/profile/youthnewsletters/viewpdf/"+$(this).attr("id");
		}
		else {
		var $getMY = "/profile/employernewsletters/viewlist/"+$(this).attr("id");
		var $getPDF = "downloads/"+$(this).attr("id");
		var $getPrint = "/profile/employernewsletters/viewpdf/"+$(this).attr("id");
		}
		var $month = $(this).text();
		$.ajax({
			url: $getMY,
			success: function(data){ 
				$('#main').append("<div id='newsletter1'><h2>"+$month+"</h2><br /><a href='"+$getPDF+".pdf' alt='PDF' style='padding-left:10px;padding-right:10px;'>PDF</a><span style='border-right:3px solid black;'></span><a href='"+$getPrint+"' alt='Print' style='padding-left:10px;'>Print</a><div class='slider'>");
				$(".slider").html(data);
				$('#newsletter1').mobilyslider({
					content: '.slider',
					children: 'div',
    				transition: 'fade',
    				pauseOnHover: false,
    				autoplaySpeed: 3000,
    				bullets: true,
    				arrows: true,
    				arrowsHide: false
				});

			}
		});
});

		
/* Font Resize Script*/
	 	//min font size
	var min=9; 
	//max font size
	var max=26;	
	//grab the default font size
	var reset = $('#middle_content').css('fontSize'); 
	//font resize these elements
	var elm = $('#middle_content, #bottom_content, #main_highlights');  
	//set the default font size and remove px from the value
	var size = str_replace(reset, 'px', ''); 
	//Increase font size
	$('a.fontSizePlus').click(function() {
		//if the font size is lower or equal than the max value
		if (size<=max) {
			//increase the size
			size++;	
			//set the font size
			elm.css({'fontSize' : size});
		}
		//cancel a click event
		return false;	
	});
	$('a.fontSizeMinus').click(function() {
		//if the font size is greater or equal than min value
		if (size>=min) {
			//decrease the size
			size--;	
			//set the font size
			elm.css({'fontSize' : size});
		}
		//cancel a click event
		return false;	
	});
	//Reset the font size
	$('a.fontReset').click(function () {
		//set the default font size	
		 elm.css({'fontSize' : reset});		
	});
	function initAddThis() 
     {
          addthis.init()
     }
     // After the DOM has loaded...
     initAddThis();
});

function str_replace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

/*
 * jQuery Highlight Regex Plugin v0.1.1
 *
 * Based on highlight v3 by Johann Burkard
 * http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html
 *
 * (c) 2009-10 Jacob Rothstein
 * MIT license
 */(function(a){var b=function(c){if(!!c&&!!c.childNodes){var d=a.makeArray(c.childNodes),e=null;a.each(d,function(a,d){d.nodeType===3?d.nodeValue===""?c.removeChild(d):e!==null?(e.nodeValue+=d.nodeValue,c.removeChild(d)):e=d:(e=null,d.childNodes&&b(d))})}};a.fn.highlightRegex=function(c,d){typeof d=="undefined"&&(d={}),d.className=d.className||"highlight",d.tagType=d.tagType||"span",typeof c=="undefined"||c.source===""?a(this).find(d.tagType+"."+d.className).each(function(){a(this).replaceWith(a(this).text()),b(a(this).parent().get(0))}):a(this).each(function(){var e=a(this).get(0);b(e),a.each(a.makeArray(e.childNodes),function(e,f){var g,h,i,j,k,l;b(f);if(f.nodeType==3)while(f.data&&(j=f.data.search(c))>=0){k=f.data.slice(j).match(c)[0];if(k.length>0)g=document.createElement(d.tagType),g.className=d.className,l=f.parentNode,h=f.splitText(j),f=h.splitText(k.length),i=h.cloneNode(!0),g.appendChild(i),l.replaceChild(g,h);else break}else a(f).highlightRegex(c,d)})});return a(this)}})(jQuery)
 

