/*
// Last edit: Chris Coray
// Reason: Added tooltips for .feature_table
*/

//used for anchor link smooth scrolling
function filterPath(string) {
	return string
	.replace(/^\//,'')
	.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
	.replace(/\/$/,'');
}

function getDir(){
	var url = document.location.href;
	url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
	url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
	var file = url.substring(url.lastIndexOf("/") + 1, url.length);
	return url.replace(file, "");
}

var dir = getDir();

function startPlay(url){
	url = url.replace(/http:\/\/(www)?(stage|test)?(\.)?(provo)?(\.)?novell\.com/, "");
//	_gaq.push(['_trackEvent','Video Plays', url, window.location.href]);
}

function stopPlay(url, percentage, seconds){
	url = url.replace(/http:\/\/(www)?(stage|test)?(\.)?(provo)?(\.)?novell\.com/, "");

	//percentage played
//	_gaq.push(['_trackEvent','Percentage Played', url, window.location.href, percentage]);

	//seconds played
//	_gaq.push(['_trackEvent','Seconds Played', url, window.location.href, seconds]);
}

$(document).ready(function(){
	//add active class to matched navigation elements
	var path = location.pathname;
	// var path2 = location.pathname.replace(/\/index\.html/,"");
	var path2 = location.pathname.replace(location.pathname.substring(location.pathname.lastIndexOf('/') + 1),""); //erase pagename for index

	var urlarray;
	if(path.charAt(path.length-1) == "/")
		urlarray = path.substring(0, path.length-1).split("/");
	else
		urlarray = path.split("/");

	path3 = path.replace(urlarray[urlarray.length-1] + "/","");
	path4 = path.replace(urlarray[urlarray.length-1],"");
	path5 = path4.replace(urlarray[urlarray.length-2] + "/","");
	path6 = (readCookie('novell_language') != 'en-us') ? location.pathname.substring(location.pathname.indexOf('/')+6,location.pathname.lastIndexOf('/')+1) : '';

	if(path.indexOf("products/howtobuy.html") > 0) {
		var defURL = 'http://shop.novell.com/';
		setRegion();
		if (n_country == null) {
			location.href = '/common/util/langselect.php?referer='+location.href;
		}
		if (n_region == 'emea') {
				$('a[href="http://shop.novell.com/"]').attr('href','http://shop.novell.com/store/novelleu/DisplayHomePage');
		}
	}

	var home = "/";
	$("a[href='" + [ path ] + "'], #products_nav a[href='" + [ path2 ] + "/'], #products_nav a[href='" + [ path2 ] + "'], a[href='" + [ path ] + "index.html']").parent().addClass("active"); //assign class
	if($("#products_nav li.active").length < 1){ //no subpage match, so try parent directory
		$("#products_nav a[href='" + [ path2 ] + "/'], #products_nav a[href='" + [ path2 ] + "']").parent().addClass("active"); //assign class
	}
	if($("#products_nav li.active").length < 1){ //no subpage match, so try parent directory again
		$("#products_nav a[href='" + [ path3 ] + "'], #products_nav a[href='" + [ path4 ] + "'], #products_nav a[href='" + [ path5 ] + "']").parent().addClass("active"); //assign class
	}
	if($("#products_nav li.active").length < 1){ //no subpage match, so try parent directory again
		if (path6 != '' || path6 != 'undefined') $("#products_nav a[href='" + [ path6 ] + "'], #products_nav a[href='" + [ path6 ] + "index.html']").parent().addClass("active"); //assign class
	}

	//activate all anchor links
	var locationPath = filterPath(location.pathname);

	var scrollElement = 'html, body';
	$('html, body').each(function () {
		var initScrollTop = $(this).attr('scrollTop');
		$(this).attr('scrollTop', initScrollTop + 1);
		if ($(this).attr('scrollTop') == initScrollTop + 1) {
			scrollElement = this.nodeName.toLowerCase();
			$(this).attr('scrollTop', initScrollTop);
			return false;
		}
	});

	$('a[href*=#]').each(function() {
		var thisPath = filterPath(this.pathname) || locationPath;
		if  (locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/, '')){
			if ($('a[name='+this.hash.replace(/#/, '')+']').length) {
				$(this).click(function(event) {
					var thishash = this.hash.replace(/#/, '');
					var targetOffset = $('a[name='+thishash+']').offset().top;
					var target = this.hash;
					event.preventDefault();
					$(scrollElement).animate({
						scrollTop: targetOffset
						}, 500,
						function() {
							location.hash = target;
							$(".active_hash").removeClass("active_hash");
							// $('a[name='+thishash+']').next().addClass("active_hash").html("<span>"+$('a[name='+thishash+']').next().text()+"</span>");
							// $('a[name='+thishash+']').next().html("<span>"+$('a[name='+thishash+']').next().text()+"</span>");

							var oldtitle = $('a[name='+thishash+']').next();
							var obj = $(oldtitle).clone(true);
							// obj.html("<span>"+$(oldtitle).text()+"</span>").insertAfter(oldtitle).hide().find("span").css({"background-image":"url(/common/img/hash_gradient.jpg)","background-repeat":"repeat-x","background-position":"left center","padding":"10px"});
							obj.html("<span>"+$(oldtitle).text()+"</span>").insertAfter(oldtitle).addClass("active_hash");
							$(oldtitle).hide();
							obj.fadeIn().css("opacity", "1");
							$(oldtitle).remove();
						}
					);
				});
			}
		}
	});

	//if they've come to a page w/ the anchor link set, select by default
	if(location.hash){
		var thishash = location.hash.replace(/#/, '');
		$('a[name='+thishash+']').next().addClass("active_hash").html("<span>"+$('a[name='+thishash+']').next().text()+"</span>");
	}



	//home page right column
	var togglecount = 0;
	$("#content_right_small").find(".toggle").each(function(){
		$(this).click(function(){
			if($(this).hasClass("toggle_on")){
				$(this).removeClass("toggle_on");
				$(this).next().hide();
				if($(this).next().next().attr("class") == "more_link") //hide more link as well
					$(this).next().next().hide();
			}
			else{
				$(this).addClass("toggle_on");
				$(this).next().show().next().show();
				if($(this).next().next().attr("class") == "more_link") //show more link as well
					$(this).next().next().show();
			}
		});

		if(togglecount > 0){
			$(this).next().hide();
			if($(this).next().next().attr("class") == "more_link") //hide more link as well
				$(this).next().next().hide();
		}
		else
			$(this).addClass("toggle_on");

		togglecount++;
	});

	//home page right column
	$("#content_right_small").find(".expander").each(function(){
			//only add links if we have more than two
			if($(this).children(".expand_block").length < 3){ //there are no more items to show
				$(this).find(".more_link").remove();
			}
			else{
				//hide extra items
				$(this).children(".expand_block:gt(1)").each(function(){
					$(this).addClass("hide");
				});

				$(this).next().find("a").each(function(){
					//initially hide minus link
					if($(this).hasClass("more_link_remove"))
						$(this).addClass("more_link_remove_off");

					if($(this).hasClass("more_link_add")){
						//set onclick handlers for expanding lists
						$(this).click(function(){
							$(this).parent().prev().find(".hide:first").each(function(){
								$(this).fadeIn().removeClass("hide").addClass("show");
							});

							//when clicked, show minus link
							$(this).parent().find("a.more_link_remove_off").removeClass("more_link_remove_off");


							if($(this).parent().prev().find(".hide:first").length < 1){ //there are no more items to show
								if($(this).parent().find(".more_link_add").addClass("more_link_add_off"));
							}

							return false;
						});
					}

					if($(this).hasClass("more_link_remove")){
						//set onclick handlers for removing lists
						$(this).click(function(){
							$(this).parent().prev().find(".show:last").each(function(){
								$(this).fadeOut().removeClass("show").addClass("hide");
							});

							//when clicked, show minus link
							$(this).parent().find("a.more_link_add_off").removeClass("more_link_add_off");

							if($(this).parent().prev().find(".show:last").length < 1){ //there are no more items to show
								if($(this).parent().find(".more_link_remove").addClass("more_link_remove_off"));
							}

							return false;
						});
					}
			});
		}
	});

	//feature list
	$(".features > a").removeAttr('href').css('cursor','text').click(function(e){
		e.preventDefault();
		return false;
	}).mouseover(function(){
		$(this).css('text-decoration','none !important');
	});
	$(".features > a:odd").addClass("odd").next().addClass("odd");
	$(".features > a:even").addClass("even").next().addClass("even");

	//feature comparison table
	$(".feature_table").each(function(){
		$(this).find("tbody tr:odd").addClass("odd");
	});
	$(".feature_table tbody h4").each(function(){
		if($(this).get(0) != $("table tbody h4:first").get(0))
			$(this).parent().parent().addClass("border");
	});
	$(".feature_table thead th:last").addClass("noborder");
	$(".feature_table tbody tr").each(function(){
		$(this).find("td:last").addClass("noborder");
	});
	$(".feature_table th:first").corner("3px left");
	$(".feature_table th:last").corner("3px right");

	$(document).ready(function(){
		$(".feature_table span.maybe").mouseover(function(){
			var getText = $(this).text();
			var getPosition = $(this).position();
			$("<div class='tooltip'>"+getText+"<\/div>").appendTo("#content_right_large");
			var toolH = $(".tooltip").height();
			var newTop = ((Math.round(getPosition.top))-toolH-35);
			var newLeft = (Math.round(getPosition.left)-55);
			$(".tooltip").css({"top": newTop+"px","left": newLeft+"px"});
			$(".tooltip").fadeIn("slow");
		}).mouseout(function() {
			$(".tooltip").fadeOut("slow");
			$(".tooltip").remove();
		});
	});

	//resource library icons
	$(".icons").each(function(){
		$(this).find("li:odd").addClass("odd").corner("5px");
	});

	//services/support callout
	$(".callout").corner("10px");

	//fix PNG for IE6
	// $(".icons li").ifixpng();

	$("#products_nav li, #content_right_small h4, #requestcall h3, #content_left_large h4, .content_nav li a").corner("5px top");
	$(".link_all").corner("5px bottom");
	$("#social, .pricing, .content_wrap, .features > a").corner("5px");
	$("#content_left_small li.active").corner("10px");

	$("#product_feature_meta p a, .buynow p a").hover(function(){
		$(this).parent().addClass("hover");
	},
	function(){
		$(this).parent().removeClass("hover");
	});

	if((n_lang != "en-us") && (n_lang != "ja-jp") && (n_lang != "zh-cn") && (n_lang != "zh-tw")){
		$("#products_nav li a").css("padding","5px 10px 13px 8px");
	}


	//SDL wasn't translating the submit button, so we're overriding now.
	if($(".contact #submit input").length){
		var submit_question = new Array();
		submit_question["en-us"] = "Submit Question";
		submit_question["de-de"] = "Frage stellen";
		submit_question["es-es"] = "Enviar pregunta";
		submit_question["fr-fr"] = "Poser une question";
		submit_question["it-it"] = "Inviate una domanda";
		submit_question["ja-jp"] = "&#36074;&#21839;&#12398;&#36865;&#20449;";
		submit_question["nl-nl"] = "Vraag indienen";
		submit_question["sv-se"] = "Skicka in fr&#229;ga";
		submit_question["zh-cn"] = "&#25552;&#20132;&#38382;&#39064;";

		var newDiv = document.createElement("div");
		newDiv.innerHTML = submit_question[n_lang];

		$(".contact #submit input").val(newDiv.innerHTML);
	}
});


$("#hdr a").each(function(){
	$(this).attr('href', function(i, h) {
		return h + (h.indexOf('?') != -1 ? "&ref=h" : "?ref=h");
	});
});
$("#product_feature a, #products_nav a").each(function(){
	$(this).attr('href', function(i, h) {
		return h + (h.indexOf('?') != -1 ? "&ref=b" : "?ref=b");
	});
});
$("#footer a").each(function(){
	$(this).attr('href', function(i, h) {
		return h + (h.indexOf('?') != -1 ? "&ref=d" : "?ref=d");
	});
});
$("#ftr a").each(function(){
	$(this).attr('href', function(i, h) {
		return h + (h.indexOf('?') != -1 ? "&ref=f" : "?ref=f");
	});
});