// Page Load Event for all commonly invoked functions.
$(document).ready(function () {
    MoveItem();
    loadSideImagesCSS();
    outboundFilter();
	//by emas 1/11/2012
		addPrintOnPolicyManual();
	//eof emas

});

//emas 1/11/2012 add printer friendly button for HR Policy Manual

function addPrintOnPolicyManual(){
	var querystringcheck = 'PolicyId'; //id from sharepoint query string for policy manual
	var divid = '#maincolLSB'; // selector container for the print icon
	var dividContent = '#ctl00_PlaceHolderMain_DIVViewPolicy'; // selector container for the policy content
	var printicon ='/Style%20Library/Images/printer-friendly.png';
	
	if (window.location.href.toUpperCase().indexOf(querystringcheck.toUpperCase()) > -1) //policy manual page found
	{
		$(divid).append('<div id="pmprinticon"><a href="#"><img height="45" border="0" src="' + printicon + '"></a></div>');
		$('#pmprinticon').css('position','absolute');
		$('#pmprinticon').css('top','90px');
		$('#pmprinticon').css('right','2px');
		$('#pmprinticon a').click(function(e){
			var tmp = $(dividContent).html();
			var ctmp = $(dividContent + ' p:first').remove(); //removing 'Return to Policy...'
			ctmp = $(dividContent).html();
			$(dividContent).html(tmp); // restoring page with 'Return to Policy...'
			
			e.preventDefault();
			var newDate = new Date;
			 
				printwindow = window.open('',newDate.getTime(),'status=1,resizable=1,width=600,height=600,scrollbars=1');
				var jq = '<style>@media print{ #printbutton {display:none;}}</style><body><button style="padding:10px 30px;margin:20px;" onClick="javascript:window.print()" id="printbutton">Print</button>' + ctmp + '</body>';
				printwindow.document.write(jq);
				if (!$.browser.webkit) //for ie and firefox
					printwindow.location.reload();
		});
	}
}
//eof emas

// Use for collapsable panel, Childhood obesity
function collapsePans() {
    
    $(".toggle_container").hide();

    
    $("h3.trigger").toggle(function () {
            $(this).addClass("active");
        }, function () {
            $(this).removeClass("active");
        }
    );

    $("h3.trigger").click(function () {
        $(this).next(".toggle_container").slideToggle("slow,");
    });
    
    var qs = QueryString.get("sent");
    if (qs == "1") {
        $(".trigger a").eq(4).click();
    }
    else {
        $(".trigger a").eq(0).click();
    }
}



// rollover functions for the COmmunity Update Newsletter banner
function MoveItem() {
    $(".CUholder a.ltst")
    .mouseover(function () {
        $(this).parent().addClass("CUholder_ltst");
    })
    .mouseout(function () {
        $(this).parent().removeClass("CUholder_ltst");
    })
    ;
    $(".CUholder a.subs")
    .mouseover(function () {
        $(this).parent().addClass("CUholder_subs");
    })
    .mouseout(function () {
        $(this).parent().removeClass("CUholder_subs");
    })
    ;
}




// DO NOT USE -- may be some problems with IE8.
$.fn.equalHeights = function (px) {
    $(this).each(function () {
        var currentTallest = 0;
        $(this).children().each(function (i) {
            if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
        });
        // commented out below since we dont use the pxToEm method
        // need to pass (true) as 
        //if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
        // for ie6, set height since min-height isn't supported
        //if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({ 'height': currentTallest }); }
        $(this).children().css({ 'min-height': currentTallest });
    });
    return this;
};




function loadSideImagesCSS() {
var loc22 = window.location.toString().toLowerCase();
if (loc22.indexOf('govauthoring') == -1 && loc22.indexOf('tmossapp1') == -1 && loc22.indexOf('ciber-dev2008') == -1) {
$("head").append("<link type='text/css' rel='stylesheet' href='/style%20library/ProductionOnly.css' />");
}
}




function outboundFilter() {

    // get the current domain
    var loc = window.location.toString().toLowerCase();
    
    // we only want to invoke this function if
    // in alachuacounty.us domain
    if (loc.indexOf('alachuacounty.us') != 0) {
        
        // these domains are excluded from the filter
        var domains = [
                "alachuacounty.us",
                "alachua.fl.us",
                "alachuaclerk.org",
				"govauthoring",
				"production_internet",
				"alachua.granicus",
				"agency.governmentjobs",
				"test_internet",
				"integration_internet"
            ];


        $("a").each(function () {
            var href = $(this).attr("href");
			if (href){ //added emas if href is not undefined
				var doneWithThisLink = false;

				// go ahead and add target=blank to 'exclude' links because we 
				// know that they are external anyway. ('exclude' is only put on external links)
				if ($(this).hasClass("exclude")) {
					$(this).attr("target", "_blank");
					return true; // same as 'continue' in jqery .each loop
				}


				// if this link starts with "http" 
				// then we consider if for the outbound link filter.
				if (href.indexOf("http") == 0) {
					var found = false;
					for (var i in domains) {
						if ($(this).attr("href").indexOf(domains[i]) != -1) {
							found = true;
							break;
						}
					}
					if (!found) {
						$(this).attr("href", "/outbound/?text=" + $(this).attr("title") + "&link=" + $(this).attr("href"));
						//$(this).append("--out!");
					}
					return true; // same as 'continue' in jqery .each loop
				}


				// check out the link to see if its a document link

				// if the last segement of the URL has a "." but NOT a ".aspx"
				// then its a document link, like "/EPD/Library/land/file.doc"

				var ar = href.split("/");
				var lastseg = ar[ar.length - 1];
				if (lastseg.indexOf(".") != -1 && lastseg.indexOf("aspx") == -1) {
					$(this).attr("target", "_blank");

					// add the image icon if there isn't an IMG tag inside this A tag...
					if ($(this).find("img").size() == 0) {
						$(this).prepend("<img src='" + docLinkImage(lastseg) + "' class='ms-asset-icon' border='0' alt='' />");
					}
					
					// add the Analytics code to track downloaded media files (PDF, DOC, etc)
					// var pageTracker = _gaq._getTracker("UA-16484336-1");
					$(this).click(function(){
						// pageTracker._trackPageview(getStrippedPath($(this).attr("href")));
						_gaq.push(['_trackPageview', 'getStrippedPath($(this).attr("href"))']);
					});
					
					return true; // same as 'continue' in jqery .each loop
				}

				if (href.indexOf("mailto:") != -1) {
					if ($(this).find("img").size() == 0) {
						$(this).prepend("<img src='" + docLinkImage("@") + "' class='ms-asset-icon' border='0' alt='' />");
					}
				}
			
			} // eof emas

        });
    }

}


function getStrippedPath(href) {
    var domains = new Array();
    domains[0] = "http://www.alachuacounty.us";
    domains[1] = "http://alachuacounty.us";
    // rip out the domain, if its there.
    href = href.replace(domains[0], "").replace(domains[1], "");
    
    // if href was a relative link ("Docs/Foo.pdf") then add a front "/" to make it match
    // the other links. Root relative links and absolute links with both have a starting "/"
    // at this point in the function.
    if (href.charAt(0) != "/")
        href = "/" + href;
        
    return "/Downloads" + href;
}


function docLinkImage(segment) {
    // http://govauthoring/_layouts/IMAGES/ictif.gif - tif
    // http://govauthoring/_layouts/IMAGES/icgen.gif - generic
    // http://govauthoring/_layouts/IMAGES/icpdf.gif - pdf
    // http://govauthoring/_layouts/IMAGES/icjpg.gif - jpg
    // http://govauthoring/_layouts/IMAGES/icdoc.gif - doc
    // http://govauthoring/_layouts/IMAGES/ichtm.gif - html
    // http://govauthoring/_layouts/IMAGES/icppt.gif - PPT
    // http://govauthoring/_layouts/IMAGES/icmail.gif - @

    var seg = "/_layouts/IMAGES/";

    if (segment.indexOf(".doc") > 0)
        seg += "icdoc";

    else if (segment.indexOf(".tif") > 0)
        seg += "ictif";

    else if (segment.indexOf(".pdf") > 0)
        seg += "icpdf";

    else if (segment.indexOf(".jpg") > 0 || segment.indexOf(".jpeg") > 0)
        seg += "icjpg";

    else if (segment.indexOf(".htm") > 0)
        seg += "ichtm";

    else if (segment.indexOf(".ppt") > 0)
        seg += "icppt";

    else if (segment.indexOf("@") > 0)
        seg += "icmail";

    else
        seg += "icgen";

    return seg + ".gif";


}

