
/******************************************************* 
	author: John Farrell - john.farrell@eurorscg.com
    description: Contains all the Utility functions
    code libraries: JQuery / JavaScript
    date: 11/30/09
    version: 0.5
 *******************************************************/

var Logical_Tx = Logical_Tx || {};
Logical_Tx.Utility = function() {

	// Constants
	var _MainPg_CalloutHeight = $(".mainpage .mainCallouts_content").css("height");
	var _MainPg_MoreLinkTxt = $(".mainpage .moreLink").html();
	var IE = $.browser.msie ? parseInt($.browser.version.substr(0, 1)) : false;
	
	
	// global methods ////////////////////////////////////////////////////////
	
	/* OFF
	// for testing links across updated site pages, add "_1209" before the ".php" extension to send user to the associated updated page...
	function update_to_testing_href(obj_ref) {
		var this_href = $(obj_ref).attr("href");
		var new_href = this_href; // default to orig href
		
		// test that href does not already contain "_1209", is not an email address, is not a PDF file.
		if((this_href.indexOf("_1209") < 0) && (this_href.indexOf("@") < 0) && (this_href.indexOf(".pdf") < 0) ){ 
			if(this_href.indexOf(".php") > -1){
				// update new_href
				new_href = this_href.replace(".php", "_1209.php");
			} else if ( 
				// if href has no extension, such as "/my_url/", add "index_1209.php" after the trailing slash...
				function(this_href){
					var regex=/(^\/){1}[\w\W]*(\/$){1}/; // looking for pattern like "/some_url/"
					return regex.test(this_href); // true or false
				}
			){
				// a match - update new_href
				new_href = this_href.replace(this_href, this_href + "index_1209.php");
			}
	}
		// set the href to the value of new_href
		$(obj_ref).attr("href", new_href);
	}
	*/
	
	// end global methods ////////////////////////////////////////////////////////
	
	
    return {
		
		// basic functionality on home pg initiated on page load
        HomePg_Init: function(){
			
			/* OFF
			// update hrefs for testing...
			$("a").click(function(){
				// test if current page has a testing suffix
				var curPath = window.location.pathname;
				var curPage = curPath.substring(curPath.lastIndexOf('/') + 1);
				if(curPage.indexOf("_1209.php")){
					// add testing suffix to href of qualifying links
					update_to_testing_href($(this));
				}
			});
			*/
			
			// blur focus on any clicked links to remove annoying bounding box in FF...
            $("a").click(function() {
                if(!IE){
                    $(this).blur();
                }
            });
			
			//////////////////////////////////////////////////////
			
			
			// style the designated intro text of the first paragraph in the home page intro content
			function styleIntroTxt(){
				var str_findIntroTxt = "Logical Therapeutics, Inc.";
				var str_styleIntroTxt = "<span class=\"introText\">" + str_findIntroTxt + "</span>";
				var hmpgIntro_para1 = $(".mainpage #mainIntro p:first").html();
				var restyled_hmpgIntro_para1 = hmpgIntro_para1.replace(str_findIntroTxt, str_styleIntroTxt);
				// do the replace at the DOM level
				$(".mainpage #mainIntro p:first").html(restyled_hmpgIntro_para1);
			}
			
        	// expand/collapse main callout boxes on home page when "more" link is clicked
			$('.mainpage .moreLink').click(function(e){
				// if box is collapsed, expand it
				if($(this).hasClass("moreLink")){
					// stretch box to full size for content
					$(this).parent().siblings(".mainCallouts_content").css("height", "100%");
					// update link text to "close"
					$(this).text("close");
					// update link indicator bg img class
					$(this).removeClass("moreLink").addClass("moreLink_active");
				// if box is expanded, collapse it
				} else if ($(this).hasClass("moreLink_active")){
					//alert("height: " + _MainPg_CalloutHeight);
					// return box to the default size spec'd in original style code
					$(this).parent().siblings(".mainCallouts_content").css("height", _MainPg_CalloutHeight);
					// update link text to default text
					$(this).text(_MainPg_MoreLinkTxt);
					$(this).removeClass("moreLink_active").addClass("moreLink");
				}
				e.preventDefault(); // prevent default click-thru
				$(this).blur(); // remove link bounding-box
			});
			
			styleIntroTxt();
       	},
       	
		////////////////////////////////////////////////////////////////////////////////////////////
       	// basic functions specific sub-pages
       	SubPg_Init: function(){
			
			//check height of #middle_col. If < 450px, enlarge it to add white space below content.
			var midCol_height = document.getElementById("middle_col").offsetHeight;
			if(midCol_height < 450){
				$("#middle_col").css("height", "450px");
			}
       		
       		/* OFF
       		$("a").click(function(){
				// test if current page has a testing suffix
				var curPath = window.location.pathname;
				var curPage = curPath.substring(curPath.lastIndexOf('/') + 1);
				if(curPage.indexOf("_1209.php")){
					// add testing suffix to href of qualifying links
					update_to_testing_href($(this));
				}
			});
			*/
			
			//news article link highlights
			$("#news #middle_col ul.newslist li").mouseover(function(){
				// color li bg
				$(this).addClass("over");
				// color the child link
				$(this).find("a").addClass("over");
			}).mouseout(function(){		
				// uncolor li bg
				$(this).removeClass("over");
				// uncolor the child link
				$(this).find("a").removeClass("over");
			}).click(function(){
				var this_href = $(this).find("a").attr("href");
				if((!$(this).find("a").attr("target")) || ($(this).find("a").attr("target") == "_self") || ($(this).find("a").attr("target") == window.name)){
                    // open link in the currnt window, as a normal link
					var curPath = window.location.pathname;
					var curPage = curPath.substring(curPath.lastIndexOf('/') + 1);
					/*
					if(curPage.indexOf("_1209.php")){
						// add testing suffix to page name if current page has a testing suffix
						this_href = this_href.replace(".php", "_1209.php");
					}
					*/				
                    window.location.href = this_href; // send window location to link's href
                } else {
                    // open link in a new window, named for the link's target name...
                    window.open(hrefURL, $(this).find("a").attr("target"));
                }

			});
       		
       	}
    }
} ();

