$(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 'education':
			$('#navbar_education').addClass('active');
			break;
			
		case 'search':
			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;
	}
	
	var preboldwidth = $('#navbar .active').width();
	$('#navbar .active').css({'font-weight' : 'bold', 'width' : preboldwidth+'px', 'padding' : '0 13px 0 10px'});
});

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) {
			var value = decode(c.substring(nameeq.length,c.length)); 
			
			// there was a problem where reading a non-existent cookie would return null, which would
			// be used as the string 'null', which in turn would get saved by later code to a cookie as the string 'null'
			// this filters out those values. After a time when people who habe had 'null' saved in their cookies get it
			// filtered out, this code should be removed.
			
			if(value != 'null ') {
				return value;
			}
			else {
				return '';
			}
		}
	}
	return '';
}

// search
$(document).ready(function() {
	$('#searchbutton').click(function() {
		$('#searchform').submit();
	});
	
	$('#searchbutton').hover(function() {
			$(this).attr('src', '/images/icon_search_hover.png');
		},
		function() {
			$(this).attr('src', '/images/icon_search.png');
		}
	);
	
	$('#searchform').submit(function() {
		if($('#search').val().length == 0 || $('#search').val() == 'Search') {
			return false;
		}
		return true;
	});
	
	$('#search').click(function() {
		if($('#search').val() == 'Search') {
			$('#search').val('');
		}
		$('#search').addClass('clicked');
	});
	
	// preload the search hover image
	var preload = new Image();
	preload.src = '/images/icon_search_hover.png';
});

// embeded videos
$(document).ready(function() {
	$('div.embeddedvideo').click(function() {
		var video_width = $(this).attr('videowidth');
		var video_height = $(this).attr('videoheight');
		var video_path = $(this).attr('videosrc');
		
		pageTracker._trackPageview(video_path);
		
		$('body').append(
			"<div class='videowindow'>"
			+"<div class='shading'></div>"
				+"<embed id='movie' name='movie' type='application/x-shockwave-flash' src='"+video_path+"' width='"+video_width+"' height='"+video_height+"' quality='high' quality='high' loop='false' wmode='window' menu='true' allowScriptAccess='always'></embed>"
			+"</div>");
		
		$('div.videowindow').css({
			'position': 'absolute',
			'left': '0px',
			'top': '0px',
			'width': '100%',
			'height': $(window).height(),
			'z-index': '10',
			'background-color' : '#000000',
			'opacity' : .7,
			'filter': 'alpha(opacity=70)',
			'cursor' : 'pointer'
		});
		
		$('embed').css({
			'position': 'absolute',
			'width': video_width,
			'height': video_height,
			'left': ($(window).width() - video_width) / 2,
			'top': ($(window).height() - video_height) / 2
		});
		
		var resizehandler = function() {
			// resizehandler is bound using the .bind method and a named function
			// so it can be safely unbound later without messing with any bindings
			// from other scripts
			$('div.videowindow').css({
				'height': $(window).height()
			});
			
			$('embed').css({
				'position': 'absolute',
				'width': video_width,
				'height': video_height,
				'left': ($(window).width() - video_width) / 2,
				'top': ($(window).height() - video_height) / 2
			});
		};
		
		$(window).bind('resize', resizehandler);
		
		$('div.videowindow, embed').click(function() {
			$(window).unbind('resize', resizehandler);
			$('div.videowindow').remove();
		});
	});
});
