
/*-------------------------------------------
	jQuery ACCORDION PANELS */
	
var jQueryAccordion = function() {
	$("#accordionPanels").find("div").each(function() {
		$(this).css("display", "none");
		$(this).prev().addClass("inactive");							   
	});
	
	$("#accordionPanels div:first").prev().removeClass("inactive");
	$("#accordionPanels div:first").prev().addClass("active");
	$("#accordionPanels div:first").prev().attr("id", "active");
	$("#accordionPanels div:first").slideDown("slow");
	
	// Removes odd margin below div/img in the accordion.
	$("#accordionPanels div").find("img").each(function() {
		$(this).wrap("<div></div>");
		//$(this).remove();
	});

	$("#accordionPanels a").click(function(event) {
								  
		$("#accordionPanels").find("a").each(function() {
			$(this).removeAttr("id");
			$(this).removeClass("active");
		});
		
		var clicked = $(this);
		clicked.attr("id", "active");
		clicked.blur();
		
		$("#accordionPanels").find("div").each(function() {
			if ($(this).prev().attr("id") == clicked.attr("id")) {
				if (clicked.next().css("display") !== "block") {
					clicked.removeClass("inactive");
					clicked.addClass("active");
					clicked.next().slideDown("slow");
				} else {
					clicked.removeClass("active");
					clicked.addClass("inactive");
					clicked.next().slideUp("slow");
				}
			} else {
				$(this).prev().removeClass("active");
				$(this).prev().addClass("inactive");
				$(this).slideUp("slow");
			}
		});
		return false;
	});
}

/*-------------------------------------------
	jQuery TABBED PANELS */

var myPanel = null;
var jQueryTabbedPanels = function(target) {
	
	$("#servicesTabs a").click(function(event) {
		$("#servicesTabs a:focus").css("outline", "none");
		setTabbedPanel($(this).attr("href"));	
		return false;
	});
	
	if (target) {
		setTabbedPanel(target);		
	} else {
		setTabbedPanel("panel1");	
	}
}

var setTabbedPanel = function(target) {
	myPanel = $("#" + target);
	resetTabbedPanels();
}

var resetTabbedPanels = function() {

	// Remove all "Highlite" classes
	var count = 0;
	var num = myPanel.attr("id").substr(5,6);
	$("#servicesTabs ul").find("a").each(function() {
		count++;
		if (String(count) !== num) {
			$(this).removeAttr("class");
		}
		if (String(count) === num) {
			$(this).addClass("highlite");	 
		}
	});
	
	$("#servicesTabs").find("div").each(function() {
		if ($(this).attr("id").indexOf(num) < 0) {
			$(this).removeAttr("class");	
		} else {
			$(this).addClass("highlite");
			$("#panel1 div, ul").css("display", "block");
			//$("#panel1 div, a").css("display", "block");
			//$("#panel1 a").css("text-indent", 20);
		}
	});
}

/*-------------------------------------------
	TS MENUS - IE STATE CONTROLS */
	
var tsMenus = function() {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className += " sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}	
}


/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: || START 1: URL PARSING  ||:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/

function getFullURL() {
	var locURL = window.location.href;
	return locURL;	
}

function getURLParts() {
	var urlParts = getFullURL();
	var urlSplit = urlParts.split("//");
	var urlFinal = urlSplit[1];
	return urlFinal;
}

function getURLDirectory() {
	var urlParts = getURLParts();
	var urlSplit = urlParts.split("/");
	var urlFinal = urlSplit[(urlSplit.length - 2)];
	return urlFinal;
}

function getURLPage() {
	var urlParts = getURLParts();
	var urlSplit = urlParts.split("/");
	var urlFinal = urlSplit[(urlSplit.length - 1)];
	return urlFinal;
}

function getURLQueryString() {
	var fullURL = getFullURL();
	var qString = fullURL.split("?");
	return qString;
}

function getPageURL(url) {
	var fn = url.match(/\/([a-z0-9_-]+\.\w+)/i);
    return (fn == null) ? "" : fn[1];
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: || END 1: URL PARSING  ||:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/


/* -------------------------- BEGIN: PHOTO GALLERY -------------------------- */

var galleryTotal = 0;
var galleryList = [];
var galleryCurr = -1;
var gallerySrc = "/lib/images/photo-gallery/LargeImages/"; //  Match with web.config "Fldr_PhotoGalleryLG"

$(".photoGrid").ready(function() {

    $(".photoGrid").find("img").each(function() {

        var imgPathArray = $(this).attr("src").split("/");
        var imgName = "";
        if (imgPathArray.length > 0) {
            imgName = imgPathArray[imgPathArray.length - 1];

            var tmpArray = imgName.split(".");
            imgName = tmpArray[0] + "." + tmpArray[1];
        }

        var imgCap = $(this).attr("alt");
        var imgNum = galleryTotal;

        galleryList[galleryTotal] = [imgNum, imgName, imgCap];

        $(this).click(function() {
            setPhotoModal(imgNum, imgName, imgCap);
        });

        galleryTotal++;
    });
});

function setPhotoModal (imgNum, imgName, imgCap) {
	var tmp = "<div id=\"photoModalHolder\">";
		tmp += "<div id=\"photoModalPhotoHolder\"><img src=\"" + gallerySrc + galleryList[imgNum][1] + "\" /></div>";
		tmp += "<div id=\"photoModalHTML\" class=\"caption\">" + imgCap + "</div>";
		tmp += "<div id=\"photoModalLinks\">";
		tmp += "<a href=\"javascript:void(0)\" onclick=\"$.modal.close(); return false;\" style=\"float:left;\">Close</a>";
		tmp += "<a href=\"javascript:void(0)\" onclick=\"updatePhotoModal('prev'); return false;\">Previous</a>";
		tmp += " | ";
		tmp += "<a href=\"javascript:void(0)\" onclick=\"updatePhotoModal('next'); return false;\">Next</a>";
		tmp += "</div></div>";
		$.modal(tmp, { position: ["50%", "50%"] });
		galleryCurr = imgNum;
		loadPhotoModal(imgNum);
}

function loadPhotoModal(newNum) {
	
	var oldWidth = $("#simplemodal-container").width();
	var img = jQuery("<img>");
	$(img).attr("src", gallerySrc + galleryList[newNum][1]);
	
	$(img).load(function() {
		
		$("#photoModalPhotoHolder").html(img); 
		
		var newWidth = Math.max($(img).outerWidth(true), 300);
		var newHeight = $(img).height() + $(".links").height() + $("#photoModalHTML").height() + 30;
		var newMarginLeft = parseInt(newWidth/2) * -1;
		var newMarginTop = parseInt(newHeight/2) * -1;
		
		$("#simplemodal-container").animate({
			width: newWidth,
			height: newHeight,
			marginLeft: newMarginLeft,
			marginTop: newMarginTop
			}, 600, function() {
				$("#photoModalPhotoHolder").width(newWidth);
				$("#photoModalHTML").width(newWidth);
				$("#photoModalLinks").width(newWidth);	
				$("#photoModalHolder").animate({ opacity: 50 }, 600);
		});
	}).attr("src", gallerySrc + galleryList[newNum][1]);			
}

var updatePhotoModal = function(dir) {

    var newNum = null;
    // Next
    if (dir == 'next') {
        if (galleryCurr == (galleryTotal - 1)) {
            galleryCurr = 0;
            newNum = galleryCurr;
        } else {
            galleryCurr++;
            newNum = galleryCurr;
        }
    }

    // Prev
    if (dir == 'prev') {
        if (galleryCurr == 0) {
            // Go to begining
            galleryCurr = (galleryTotal - 1);
            newNum = galleryCurr;
        } else {
            galleryCurr--;
            newNum = galleryCurr;
        }
    }

    if (newNum !== null) {
        loadPhotoModal(newNum);
        $("#photoModalHTML").text(galleryList[newNum][2]);
    }
}
/* -------------------------- END: PHOTO GALLERY -------------------------- */