﻿$(document).ready(function() {
    var navlead = $("#logo a").attr("href");
    $("table").attr("cellspacing", "0").attr("cellpadding", "0");
    $("table:not(.calendar):not(.noArrow) tr td:nth-child(2) a").prepend("<span class='sideContentArrow'>&#9658</span>");
    $("tbody tr:first-child").attr("class", "headerTR");
    $("tbody tr:nth-child(2)").attr("class", "detailsTR");
    $("table tr td:first-child").css("padding-left", "5px");

    $(".subnavigation div li a.parent").before("<span class='navArrow'>&#9658</span>");
    $(".subnavigation div li a.parent-open").before("<span class='navArrow'>&#9660</span>");

    $(".relatedlinks ul li a").before("<span class='relatedLinksArrow'>&#9658</span>");

    $(".relatedStoriesLink").before("<span class='sideContentArrow'>&#9658</span>");
    $(".newswrapper li, .eventlist li").prepend("<span class='sideContentArrow'>&#9658</span>");
    $(".Subhead").each(function(i) { $(this).parent("p").css("margin-bottom", "0px"); });
    $(".Subsubhead").each(function(i) {
        $(this).html($(this).children("strong").html());
        $(this).addClass("Subhead").removeClass("Subsubhead");
    });
    $(".freeform-content p:first").css("margin-top", "0px");
    $("#headerlinks li:not(:last-child)").append(" | ");
    $("#footerlinks li:not(:last-child)").append(" | ");

    $(".txtSearch").focus(function() {
        $(this).val("");
    }).blur(function() {
        if ($(this).val() == "")
            $(this).val("Search Harlan.com for...");
    }).keydown(function(e) {
        if (e.which == 13) // enter key
        {
            window.location.href = navlead + 'search/-' + escape($(this).val());
            return false;
        }
    });

    $(".txtEmailNews").focus(function() {
        $(this).val("");
    }).blur(function() {
        if ($(this).val() == "")
            $(this).val("Email address");
    }).keydown(function(e) {
        if (e.which == 13) // enter key
        {
            $("#news-update input:last").click();
            return false;
        }
    });

    $("ul.storieslist li > a > img:eq(0)").css("width", "72px").css("height", "91px").css("margin-top", "5px");
    $("#BottomContent ul.storieslist li > a > img").css("width", "71px").css("height", "95px").css("margin-top", "-4px");
    $("ul.storieslist li > a > img:eq(1)").css("width", "72px").css("height", "95px").css("margin-top", "5px");
    $("ul.storieslist li > a > img:eq(2)").css("width", "72px").css("height", "96px").css("margin-top", "6px");
    jQuery.each(jQuery.browser, function(i) {
        if ($.browser.msie && jQuery.browser.version.substr(0, 1) == "6") {
            $("ul.storieslist li:eq(0)").css("margin-top", "-3px");
            $("ul.storieslist li:eq(1)").css("margin-top", "-1px");
            $("ul.storieslist li:eq(2)").css("margin-top", "-3px");
        }
    });
    $("div.relatedlinks:eq(0) > ul > li > span").addClass("HomeRMSArrows");
    $("div.relatedlinks:eq(1) > ul > li > span").addClass("HomeCRSArrows");

    $(".SearchButton").hover(function() {
        this.src = this.src.replace("ButtonSearch.png", "ButtonSearchHover.png");
    }, function() {
        this.src = this.src.replace("ButtonSearchHover.png", "ButtonSearch.png");
    });

    $("#primary-nav ul li:has(ul)").hover(function() {
        $("ul", this).addClass("hover"); //.css("top", this.offsetHeight + "px");
    }, function() {
        $("ul", this).removeClass("hover");
    });

    $("#primary-nav li:has(li)").hover(function() {
        $("ul", this).addClass("over"); //.css("top", this.offsetHeight + "px");
    }, function() {
        $("ul", this).removeClass("over");
    });
    $("#primary-nav li").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });

    $(".subnavigation ul li ul li:has(ul)").find("span").css("cursor","pointer").click(function() {
        var $this = $(this);
        $this.next().next().slideToggle("slow");
        var html = $this.html();
        if (html.indexOf("\u25BA") >= 0) {
            $this.html(html.replace(/\u25BA/, "\u25BC"));
        } else {
            $this.html(html.replace(/\u25BC/, "\u25BA"));
        }
    });

    // table rollup
    // option #1 - roll up rest of table if first row is clicked
    /*$("#content table:not(.calendar) tr:first-child>td").click(function() {
    $("tr:gt(0)", $(this).parent().parent()).toggle();
    $(this).parent().toggleClass("headerTR-collapse");
    setEqualHeight();	// reset page height?
    return false;
    }).each(function (i) {
    var bgImgWidth = 9;
    var bgPaddingRight = 5;
    $(this).parent().css("background-position", (this.offsetWidth - (bgImgWidth + bgPaddingRight)) + "px 50%");
    if ($.browser.msie && parseFloat($.browser.version) < 7)	// IE6 craziness
    {
    $(this).append("<span class=\"ie6-arrow\">&nbsp;</span>");
    }
    });*/
    // option #2 - roll up entire table following anything with a .rollup class (this class will have to be added to the FCK styles and xml)
    /*$("#content .rollup").click(function() {
    $(this).next("table").toggle();
    });*/

    $("a[href='/flash/harlan.html']").click(function() {
        window.open(this.href, "hello", "resizable=yes,width=640,height=480");
        return false;
    });
    
    // image map template
    if ($('div.imagemap').length) {
		$('div.imagemap div.subtitle').appendTo('#content-title');
		$('div.freeform strong:first').addClass('noborder');
    }
    
    // global map template
    if ($('#globalmap').length) {
		var preload = new Array();
		var hovermaps = new Array();
		preload[0] = 'image.axd/0f32fe50148249ab9540cebbdbda7ba7.png'; // north america
		preload[1] = 'image.axd/c510c36d1c2b4622848f6bd78d952bee.png'; // asia
		preload[2] = 'image.axd/ec4b69e7cbf04b969c0d9f1111c6859d.png'; // europe
		preload[3] = 'image.axd/97089a4b9141458f957e8e14aa244786.png'; // middle east
		$(document.createElement("img")).load(function() {
			if (preload[0]) {
				this.src = preload.shift();
				hovermaps.push(this.src);
			}
		}).trigger("load");
    
		$('#globalmap div.subtitle').appendTo('#content-title');
		var globalmapsrc = $('#globalmap img').attr('src');
		$('#globalmap area').mouseover(function() {
			var index = $('#globalmap area').index(this);
			$('#globalmap img').attr('src', hovermaps[index]);
		}).mouseout(function() {
			$('#globalmap img').attr('src', globalmapsrc);
		});
    }

    // equal height for #primary-navigation and #content
    setColumnsEqualHeight(); // fire onload so all images get loaded
});

$(window).load(function() {
	// make sure the height hasn't been affected by any other content
	setColumnsEqualHeight();
});

function setColumnsEqualHeight()
{
	var $toMeasure = $("#content-left-shadow, #content-middle, #content-right-shadow, #secondary-nav");
	var $toResize = $("#content-bottom-container, #content-left-shadow, #content-middle, #content-right-shadow");
	setEqualHeight($toMeasure, $toResize);
}

function setEqualHeight($toMeasureAndResize) {
	setEqualHeight($toMeasureAndResize, $toMeasureAndResize);
}

function setEqualHeight($toMeasure, $toResize) {
	if ($toMeasure.length > 0 && $toResize.length > 0) {
		// set back to auto for dynamic content
		$toResize.css("height", "auto");
		
		var heights = [];
		for (var i = 0; i < $toMeasure.length; i++) {
			heights[i] = $toMeasure[i].offsetHeight;
		}
		var maxHeight = getMax(heights);

		// check max of min-height's + padding for objects to set (if possible)
		var minheights = [maxHeight];
		try {
			for (var i = minheights.length; i < $toResize.length; i++) {
				minheights[i] = getOffsetMin($($toResize[i]));
			}
		} catch(e) {}
		maxHeight = getMax(minheights);
		
		$toResize.each(function (i) {
			var $this = $(this);
			$this.css("height", (maxHeight - getVPaddingOffset($this)) + "px");
		});
	}
}

function getMinHeight($object) {
	return parseInt($object.css("min-height"), 10);
}

function getVPaddingOffset($object) {
	var top = parseInt($object.css("padding-top"), 10);
	var bottom = parseInt($object.css("padding-bottom"), 10);
	return (top + bottom);
}

function getOffsetMin($object) {
	return (getMinHeight($object) + getVPaddingOffset($object));
}

function getMax(nums) {
	if (nums.length > 1) {
		var newMax = nums[0];
		for (var i = 0; i < nums.length; i++) {
			newMax = Math.max(newMax, nums[i]);
		}
		return newMax;
	} else if (nums.length == 1) {
		return nums[0];
	}
	return 0;	// error
}
