$(document).ready( function() {
	var url_split = window.location.href.split('/');
	
	switch(url_split[3]) {
		case 'applications':
			$('#navbar_applications').addClass('active');
			break;
			
		case 'company':
			$('#navbar_company').addClass('active');
			break;
			
		case 'products':
		case 'rmcquote':
			$('#navbar_products').addClass('active');
			break;
			
		case 'downloads':
		case 'dloads':
			$('#navbar_downloads').addClass('active');
			break;
			
		case 'support':
			$('#navbar_support').addClass('active');
			break;
			
		case 'main':
			if(url_split[4] == 'sitemap.php') {
				$('#navbar_sitemap').addClass('active');
			}
			else if(url_split[4] == 'contact.php') {
				$('#navbar_contact').addClass('active');
			}
			else {
				$('#navbar_company').addClass('active');
			}
			break;
		
		case '':
		case 'index.php':
			$('#navbar_home').addClass('active');
			break;
			
		default:
			$('#navbar_company').addClass('active');
			break;
	}
	
	// if border radius isn't supported, fix the element
	$('#navbar .active').each(function (element) {
		if(this.style['border-radius'] == undefined) {
			var width = $(this).outerWidth();
			var height = $(this).outerHeight();
			var colorstring = $(this).css('background-color');
			var color = '';
			
			if(colorstring.length == 6) {
				// this is the ideal case, where the color string is hopefully in hhhhhh notation (standard without # prefix)
				color = colorstring;
			}
			else if(colorstring.substring(0,4) == 'rgb(') {
				// some browsers return an odd rgb(n,n,n) value for colors instead of the html #hhhhhh notation
				// this needs to be converted to match, but without the leading #.
				var colorstring_split = colorstring.substring(5, colorstring.length-1).split(',');
				for(i=0;i<3;i++) {
					val = parseInt(colorstring_split[i]).toString(16);
					
					if(isNaN(val)) {
						val = '00';
					}
					
					if(val.length == 1) {
						val = "0"+val;
					}
					
					if(val.length > 2) {
						val = val.substring(0,2);
					}
					
					color += val; 
				}				
			}
			else if(colorstring.substring(0,1) == '#' && colorstring.length == 7) {
				color = colorstring.substring(1,7);
			}
			else {
				color = '000000';
			}
						
			$(this).css({'background-color': 'transparent'});
			
			// for IE 6 apply the png hack with the transparency filter hack, or the straight png hack on other browsers
			if($.browser.msie == true && $.browser.version == '6.0') {
				$(this).css({'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/images/dynamic/roundbox.php?width='+width+'&height='+height+'&color='+color+'", sizingMethod="scale")'});
			}
			else {
				$(this).css({'background-image': 'url(/images/dynamic/roundbox.php?width='+width+'&height='+height+'&color='+color+')'});
			}
		}
	});
});

function CreatePopup(src, features) {
		var theWindow = window.open(src.getAttribute('href'), '_blank', features);
		theWindow.focus();
		return theWindow;
	}
	
function ImageViewer(imageElement)
{
	var theTitle = imageElement.getElementsByTagName("img")[0].alt;
	var theImage = imageElement.href;
	
	window.location.href = "/includes/imageviewer.php?image=" +theImage + "&title=" + theTitle;
}

function WriteCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function ReadCookie(name) {
	var nameeq = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameeq) == 0) return decode(c.substring(nameeq.length,c.length));
	}
	return null;
}

$(document).ready(function() {
	var startstring = 'enter comments here'
	$("#commentbox_button").click(function() {
		$("#commentbox_button").hide();
		$("#commentbox").show(500);
		$("#comment_text").val(startstring);
		});
	
	$("#comment_submit").click(function() {
		$("#commentbox").hide(500);
		$("#commentbox_button").text("Thanks!");
		$("#commentbox_button").show();
		var ctext = escape($("#comment_text").val());
		var curl = escape(document.URL);
		$.post("/other/commentsmailer.php", { text: ctext, url: curl });
		});
	
	$("#comment_text").click(function() {
		var contents = $("#comment_text").val();
		if(contents == startstring)
			$("#comment_text").val("");
		});

	$("#container").click(function() {
		$("#commentbox").hide(500);
		$("#commentbox_button").text("Comments?");
		$("#commentbox_button").show();
		});
		
	$("#comment_close").click(function() {
		$("#commentbox").hide(500);
		$("#commentbox_button").text("Comments?");
		$("#commentbox_button").show();
		});
	});