/*
 * ENABLE JS-ONLY STYLES
 */
$('html').addClass('js-enabled');

/*
 * DETERMINE WHETHER HOME PAGE FLASH PLAYS FULLY
 */
var sr_visited = false;
function setVisited() {
	//check cookie
	var _hasVisited = function() {
		var _ret = false;
		if(document.cookie != "") {
			var cookies = document.cookie.split("; ");
			for(var i=0; i<cookies.length; i++) {
				if(cookies[i].split('=')[0] === 'sr_visited') {
					_ret = true;
				}
			}
		}
		return _ret;		
	}
	if(_hasVisited()) {
		sr_visited = true;
		return;
	} else {
		var _expireDate = new Date();
		_expireDate.setHours(_expireDate.getHours()+1);
		var _str = "sr_visited=true;expires=" + _expireDate;
		document.cookie = _str;
	}
}
setVisited();
		
/*
 * ONLOAD EVENTS
 */
$(document).ready(function() {
	/* GLOBAL ENHANCEMENTS
	*/
	//open new window
	$('a.external-link').click(function() {
		//do tracking call
		try {
			pageTracker._trackPageview(this.href);
		} catch(e) {
		}		
		window.open(this.href);
		return false;
	});
	//attach tracking to .pdf links
	$('a[href$=".pdf"]').click(function() {
		//do tracking call (for initial related video click)
		try {
			pageTracker._trackPageview(this.href);
		} catch(e) {
		}		
	}); 

	//suppress nav link to current page
	$('.sidebar li.current a').css('cursor','default').click(function() {
		return false;
	});
	//suppress nav links on home page
	$('#home .pgmenu .current a').css('cursor','default').click(function() {
		return false;
	});
	
	//enable dynamic highlights under nav items
	$('#header .pgmenu').pgmenu({easing:'easeOutCubic',delay:1000});
	//enable downloads and report hovers (for IE 6)
	if ($.browser.msie && parseInt($.browser.version) === 6) {
		$('.tools dl').hover(
			function() {
				$(this).addClass('hover_');
				$(this).siblings().find('dd a').blur();
			},
			function() {
				$(this).removeClass('hover_');
			}
		);	
	}
	//extend related video link to icon
	$('div.related.video h3').css('cursor','pointer').bind('click', function() {
		$(this).next().find('a').click();
	});
	//extend related downlaod link to icons
	$('div.related.downloads h3').css('cursor','pointer').click(function() {
		var _link = $(this).next().find('a').attr('href');
		document.location = _link;
	});		
	//hide tab bodies
	$('.tab .body').addClass('hide_');	
	//enable tab toggles
	$('.accordion h3').toggle(
		function () {
			$(this).addClass('selected').next().removeClass('hide_');
		},
		function () {
			$(this).removeClass('selected').next().addClass('hide_');
		}	
	);
	/* HOME PAGE ENHANCEMENTS
	*/
	//extend link to headers
	if($('body#home').length != 0) {
		$('div.feature h2').css('cursor','pointer').click(function() {
			var _link = $(this).siblings('a').attr('href');
			document.location = _link;
		});
	}	
	/* VIDEO
	*/	
	//enable video overlay
	$('a.video-link').click(function(e) {
		if($('#overlay').length !== 0) {
			return false;
		}
		//create overlay container
		$('body').append('<div id="overlay" />');
		var _href = this.href;
		$('#overlay').load(_href + ' #videocontent', function(data){
				//modify close button
				$('#button-close').parent().click(function() {
					$('#overlay').remove();
					return false;
				});
				//modify links
				$('#videocontent .links a').click(function() {
					var __href = this.href;
					var $par = $(this).parent();
					if(!$par.hasClass('current')) {
						//load video
						$('#movie').load($(this).attr('href') + ' #movie object', function(data) {
							//change current link state
							$('#videocontent .links li').removeClass('current');
							$par.addClass('current');
						});
						//do tracking call (for video links in overlay)
						try {
							pageTracker._trackPageview(__href);
						} catch(e) {
						}
					}
					return false;
				});
				//do tracking call (for initial related video click)
				try {
					pageTracker._trackPageview(_href);
				} catch(e) {
				}
		}).vCenter();
		e.preventDefault();
	});
});

 /*
	Executes proprietary command to force IE to cache all background images 
		which prevents the browser from fetching the same image repeatedly when using CSS hover background repositioning
	(http://support.microsoft.com/?scid=kb;en-us;823727&spid=2073&sid=global)
*/
if($.browser.msie && parseInt($.browser.version) === 6) {
	try {
		document.execCommand("BackgroundImageCache",false,true);
	} catch(e) {
		// just in case
	}
}

/*
 * positions element in center of viewport
 */
(function($){
  $.fn.vCenter = function(options) {
    var pos = {
      sTop : function() {
        return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop;
      },
      wHeight : function() { 
        return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
      }
    };
    return this.each(function(index) {
      if (index == 0) {
        var $this = $(this);
        var elHeight = $this.height();
		    var elTop = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
        $this.css({
          position: 'absolute',
          //marginTop: '0',
          top: elTop
        });
      }
    });
  };
})(jQuery);
