// Stop flash from interfering with nav

function fix_flash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for (i = 0; i < embeds.length; i++) {
        embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if (embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin', new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
                new_embed.setAttribute('wmode', 'transparent');
            embed.parentNode.replaceChild(new_embed, embed);
        }
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for (i = 0; i < objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if (object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for (j = 0; j < children.length; j++) {
                try {
                    if (children[j] != null) {
                        var theName = children[j].getAttribute('name');
                        if (theName != null && theName.match(/flashvars/i)) {
                            new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
                        }
                    }
                }
                catch (err) {
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin', new_object);
            object.parentNode.removeChild(object);
        }
    }
}


// Navigation control

$(document).ready(function() {
	
	// Set links here
	
	var pageItem = new Array();
	pageItem[0] = "About";
	pageItem[1] = "Overview";
	pageItem[2] = "Contact";
	//pageItem[3] = "Partners";
	
	var pageItemLink = new Array();
	pageItemLink[0] = "about.html";
	pageItemLink[1] = "overview.html";
	pageItemLink[2] = "contact.html";
	//pageItemLink[3] = "/new/partners.html";
	
	var pageSubItem = new Array();
	pageSubItem[0] = ["What Sets Us Apart", "Mission Statement", "Executive Management"];
	pageSubItem[1] = ["Services", "Process and Technology", "Performance and Reporting"];
	pageSubItem[2] = [];
	//pageSubItem[3] = ["HealthCare Recovery Group", "Spartan Recoveries", "AMSoft"];
	
	var pageSubItemLink = new Array();
	pageSubItemLink[0] = ["whatSetsUsApart.html", "missionStatement.html", "executiveManagement.html"];
	pageSubItemLink[1] = ["services.html", "processAndTechnology.html", "performanceAndReporting.html"];
	pageSubItemLink[2] = [];
	//pageSubItemLink[3] = ["#", "http://www.spartanrecoveries.com", "#"];
	
	// Top nav UI
	
	var path = window.location.pathname;
	
	$('#header').append('<div id="bannerOverflow"></div>');
	$('#bannerOverflow').append('<div id="banner"><a href="/"><div class="bannerHit"></div></a></div>');
	$('#banner').append('<div class="nav"></div>');
	$('#banner .nav').append('<ul></ul>');
	
	for (var i = 0; i < pageItem.length; i ++) {
		if (path == pageItemLink[i]) {
			$('.nav ul:first-child').append('<li class="selected"><a href="' + pageItemLink[i] + '">' + pageItem[i] + '</a></li>');
		} else {
			$('.nav ul:first-child').append('<li><a href="' + pageItemLink[i] + '">' + pageItem[i] + '</a></li>');
		}
		if (!pageSubItem[i].length < 1) {
			$('#banner .nav ul li:last-child').append('<ul></ul>');
			for (var j = 0; j < pageSubItem[i].length; j ++) {
				$('#banner .nav ul li:last-child ul').append('<li><a href="' + pageSubItemLink[i][j] + '">' + pageSubItem[i][j] + '</a></li>');
			}
		}
	}
	
	$('#banner .nav ul li:last-child ul').addClass('subMenuRight');
	
	$('#header').append('<div id="bannerShadow"></div>');
	
	fix_flash();
	
	$('.nav ul li').hoverIntent(function() {
		clearTimeout($(this).data('timeout'));
		$(this).children('ul').slideDown('fast');
	},function() {
		$(this).children('ul').slideUp('fast');
	});
	
	// Path nav UI
	
	(function($) {
		var cache = [];
		$.preLoadImages = function() {
			var args_len = arguments.length;
			for (var i = args_len; i --;) {
				var cacheImage = document.createElement('img');
				cacheImage.src = arguments[i];
				cache.push(cacheImage);
			}
		}
	})(jQuery);
	
	jQuery.preLoadImages("images/btnHome-hl.png", "images/btnHomeRecursive-hl.png", "images/btnPath-hl.png", "images/btnPathRecursive-hl.png", "images/btnPathLong-hl.png");
	
	$('#path .home').hover(function() {
		$(this).addClass('home-hl');
	}, function() {
		$(this).removeClass('home-hl');
	});
	
	$('#path .homeRec').hover(function() {
		$(this).addClass('homeRec-hl');
	}, function() {
		$(this).removeClass('homeRec-hl');
	});
	
	$('#path .btn').hover(function() {
		$(this).addClass('btn-hl');
	}, function() {
		$(this).removeClass('btn-hl');
	});
	
	$('#path .btnRec').hover(function() {
		$(this).addClass('btnRec-hl');
	}, function() {
		$(this).removeClass('btnRec-hl');
	});
	
	$('#path .btnLong').hover(function() {
		$(this).addClass('btnLong-hl');
	}, function() {
		$(this).removeClass('btnLong-hl');
	});
	
	$('#path div').click(function() {
		if ($(this).hasClass('homeRec') || $(this).hasClass('home'))  {
			window.location = "/";
		} else {
			var pageTitle = $(this).html();
			var pageLocation = "#";
			for (i = 0; i < pageItem.length; i ++) {
				if (pageTitle == pageItem[i]) {
					pageLocation = pageItemLink[i];
					break;
				} 
				for (j = 0; j < pageSubItem[i].length; j ++) {
					if (pageTitle == pageSubItem[i][j]) {
						pageLocation = pageSubItemLink[i][j];
						break;
					}
				}
			}
			window.location = pageLocation;
		}
	});
	
	// Bottom nav UI
	
	$('#footer').append('<div id="footNav"></div>');
	$('#footNav').append('<ul></ul>');
	
	for (i = 0; i < pageItem.length; i ++) {
		$('#footNav ul:first-child').append('<li class="footli' + i + '"><a href="' + pageItemLink[i] + '">' + pageItem[i] + '</a></li>');
		$('.footli' + i).append('<ul class="footul' + i + '"></ul>');
		if (pageItem[i] == "Contact") {
			$('.footul' + i).append(
				'<li>4 Armstrong Road, 4th floor</li>' +
				'<li>Shelton, CT 06484</li>' +
				'<li>Phone: (866) 684-7512</li>' +
				'<li>Fax: (203) 312-1799</li>'
			);
		} else {
			for (var j = 0; j < pageSubItem[i].length; j ++) {
				$('.footul' + i).append('<li><a href="' + pageSubItemLink[i][j] + '">' + pageSubItem[i][j] + '</a></li>');
			}
		}
	}
	
	if ($(window).height() < ($('#footer').offset().top + $('#footer').height() + 25)) {
		$('#footer').append('<div id="footnote">HealthCare Subrogation Group © 2001 - 2012</div>');
	} else {
		$('#footer').append('<div style="position:fixed;bottom:0px;" id="footnote">HealthCare Subrogation Group © 2001 - 2012</div>');
	}
	
});
