// JavaScript Document

// This function parses the url to get the specified parameter.
function getUrlParameter(name, url) {
	var url = (url == null) ? window.location.href : url;
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url );
		return results[1];
}
// This function searches the url for the specified string (case sensitive).
function searchUrlPath(name, url) {
	var url = (url == null) ? window.location.href : url;
	if (url.search(name) > -1) {
		return true;
	} else {
		return false;
	}
}

$(document).ready(function() {
	
	/* Settings */
	hs.graphicsDir = '/resources/scripts/highslide/graphics/';
	hs.allowMultipleInstances = false;
	hs.dimmingDuration = 200;
	hs.expandDuration = 200;
	hs.restoreDuration = 200;
	hs.dimmingGeckoFix = true;
	hs.easing = 'easeInOutQuad';
	hs.fadeInOut = true;
	hs.Expander.prototype.onAfterExpand = function() { //Fixes wierd Safari bug where video is hidden until scrolling
		if (navigator.userAgent.toLowerCase().indexOf('safari')) {
			scrollBy(0,1);
			scrollBy(0,-1);
		}
    };
    hs.Expander.prototype.onAfterClose = function() {
    		BeginPulsatingBlocks();
    };
    	
    /* Window Resize Mod */
    	hs.addEventListener(window, 'resize', function() {
    		var i, exp;
    		hs.getPageSize();
    	 
    		for (i = 0; i < hs.expanders.length; i++) {
    			exp = hs.expanders[i];
    			if (exp) {
    				var x = exp.x,
    					y = exp.y;
    	 
    				// get new thumb positions
    				exp.tpos = hs.getPosition(exp.el);
    				x.calcThumb();
    				y.calcThumb();
    	 
    				// calculate new popup position
    			 	x.pos = x.tpos - x.cb + x.tb;
    				x.scroll = hs.page.scrollLeft;
    				x.clientSize = hs.page.width;
    				y.pos = y.tpos - y.cb + y.tb;
    				y.scroll = hs.page.scrollTop;
    				y.clientSize = hs.page.height;
    				exp.justify(x, true);
    				exp.justify(y, true);
    	 
    				// set new left and top to wrapper and outline
    				exp.moveTo(x.pos, y.pos);
    			}
    		}
    	});
    // End of Window Resize Mod
	
/* Screen Effects */
	
	function BeginPulsatingBlocks() {
		var
		    pulsatingBlocks = $('.pulsating-block'),
		    forever = 5 * 24 * 60 * 60 * 1000; // 5 days! (Which is forever in Internet time)
		if ($('.pulsating-block:animated').size() == 0) {
			pulsatingBlocks.filter('.attention1').each(function () {
				$(this).pulsate({color: 'rgb(255,255,255)'}, forever, this.className.split(' ')[0], 'Slower');
			});
			pulsatingBlocks.filter('.attention2').each(function () {
				$(this).css("color", "white");
				$(this).pulsate({left: '6px', color: 'rgb(0,0,0)'}, forever, 'Bounce', 'Slower');
			});
		}
	}
	BeginPulsatingBlocks();
	
/* Media Lightboxes */

	$('.image-frame a').click(function() {
		return hs.expand(this, {
			captionEval: 'this.a.title.replace("Zoom in on ", "")',
			align: 'center',
			wrapperClassName: 'wide-border'
		});
	});
	$('.video-link a').click(function() {
		$('.pulsating-block').stop().removeAttr('style');
		return hs.htmlExpand(this, {
			objectType: 'swf',
			src: '/resources/flash/jwplayer/jwplayer-5.1/player.swf',
			swfOptions: {
				version: '8',
				expressInstallSwfurl: '/resources/scripts/swfobject/expressInstall.swf',
				flashvars: {
					file: this.href,
					image: '/videos/watch-video.png',
					backcolor: '000000',
					frontcolor: 'ffffff',
					lightcolor: '999999',
					screencolor: '000000',
					bufferlength: '10',
					autostart: false
				},
				params: {
					allowscriptaccess: 'always',
					allowfullscreen: 'true',
					vmode: 'transparent'
				}
			},
			width: 600,
			height: 358,
			objectWidth: 600,
			objectHeight: 358, //video height + skin height
			maincontentText: '<div style="text-align:center; color: red; padding:25px">\n<p>You need to have <strong>Adobe Flash Player</strong> to view videos on this website. Please download it by clicking the button below.</p>\n<p>&nbsp;</p>\n<p><a href="http://www.adobe.com/go/getflashplayer">\n<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" target="_blank" alt="Get Adobe Flash player" />\n</a></p>\n</div>',
			wrapperClassName: 'no-frame floating-caption',
			dimmingOpacity: 0.86,
			align: 'center',
			transitions: ['fade'],
			allowSizeReduction: false,
			captionEval: 'this.a.title',
			preserveContent: false // always use this with flash, else the movie will be stopped on close
		});
	});
	
/* Textual Lightboxes */	
	
	$('div.page-container:not(.catalog-table-of-contents) #navigation2 .table-of-contents a, div.page-container:not(.catalog-table-of-contents) #navigation3 .table-of-contents a').click(function() {
		return hs.htmlExpand(this, {
			wrapperClassName: 'info-box absolute-header',
			align: 'center',
			objectType: 'ajax',
			transitions: ["fade"],
			width: 440,
			preserveContent: false,
			allowHeightReduction: false
		});
	});
	$('#navigation2 .help a, #navigation3 .help a').click(function() {
		$.cookie('hide-help', null);
		return hs.htmlExpand(this, {
			wrapperClassName: 'info-box absolute-header',
			align: 'center',
			objectType: 'ajax',
			transitions: ["fade"],
			preserveContent: false,
			allowHeightReduction: false
		});
	});
	
/* Help Box */	
	
	if ($.cookie('hide-help') != "true" && searchUrlPath("Page") == true) {
		hs.htmlExpand(null, {
			src: 'Element/Help',
			wrapperClassName: 'info-box absolute-header',
			align: 'center',
			objectType: 'ajax',
			transitions: ["fade"],
			width: 440
		});
	}
	
	$('a[title="Close (esc)"]').live("click", function() {
		if ($('#help-box').length) {
			$.cookie('hide-help', true);
		}
	});
	$('#help-box a.dont-show-session').live("click", function() {
		$.cookie('hide-help', true);
		return hs.close(this);
	});
	$('#help-box a.dont-show-permanent').live("click", function() {
		$.cookie('hide-help', true, { expires: 3650 });
		return hs.close(this);
	});
	$('#help-box a.just-close').live("click", function() {
		$.cookie('hide-help', null);
		return hs.close(this);
	});
	
});
