/* 
-----------------------------------------------------------------
	Film Archive
	feature-project.js
	
	Functions for the Feature Project section
-----------------------------------------------------------------
*/

	function enhanceIndexedFeatures()
	{					
		var main = document.getElementById('main');
		main.className = 'enhanced'; // hook for applying block colour hovers
		
		matchRowHeights(main);
	}
	
	total_indexed_features = 0;
	
	function matchRowHeights(main)
	{
	/*	Indexed features are in columns.
		If the 2 columns are not the same height,
		then increase the height of the prop image (min-height fix) holding the link open, 
		so that the 2 columns appear to be the same height.
		This is preferable to setting the container height, as this could cause cropped text
		in gecko if the text is resized after page load.
	*/

		mainDivs = main.getElementsByTagName('div');
		
		var r=0; // row
		var c=0; // col
		
		for (var i=0; i<mainDivs.length; i++)
		{
			if (mainDivs[i].className.indexOf('row') != -1)
			{			
				// col 1				
							
				window['feature_' + r] = new Object;				
				window['feature_' + r].c1_el = mainDivs[i].getElementsByTagName('div')[0];
				window['feature_' + r].c1_linkEl = window['feature_' + r].c1_el.getElementsByTagName('a')[0];	
				window['feature_' + r].c1_linkEl.id = ('fp_' + c);			
				window['feature_' + r].c1_propEl = window['feature_' + r].c1_el.getElementsByTagName('img')[0];
				window['feature_' + r].c1_thumbEl = window['feature_' + r].c1_el.getElementsByTagName('a')[1];	

				addEvent(window['feature_' + r].c1_linkEl, 'mouseover', interactWithFeature, false);
				addEvent(window['feature_' + r].c1_linkEl, 'mouseout', interactWithFeature, false);				

				addEvent(window['feature_' + r].c1_thumbEl, 'mouseover', interactWithFeature, false);					
				
				c++;					
				
				// col 2 (if column 2 exists)
						
				if (mainDivs[i].getElementsByTagName('div')[3])
				{						
					window['feature_' + r].c2_el = mainDivs[i].getElementsByTagName('div')[3]; // note this is now 3 since the liner was added
					window['feature_' + r].c2_linkEl = window['feature_' + r].c2_el.getElementsByTagName('a')[0];		
					window['feature_' + r].c2_linkEl.id = ('fp_' + c);					
					window['feature_' + r].c2_propEl = window['feature_' + r].c2_el.getElementsByTagName('img')[0];
					window['feature_' + r].c2_thumbEl = window['feature_' + r].c2_el.getElementsByTagName('a')[1];
	
					addEvent(window['feature_' + r].c2_linkEl, 'mouseover', interactWithFeature, false);
					addEvent(window['feature_' + r].c2_linkEl, 'mouseout', interactWithFeature, false);
	
					addEvent(window['feature_' + r].c2_thumbEl, 'mouseover', interactWithFeature, false);					
					
					col_1_height = getDimension(window['feature_' + r].c1_linkEl, 'height');	
					col_2_height = getDimension(window['feature_' + r].c2_linkEl, 'height');
	
					col_1_prop_height = getDimension(window['feature_' + r].c1_propEl, 'height');					
					col_2_prop_height = getDimension(window['feature_' + r].c2_propEl, 'height');				
					
					var indexed_feature_a_vertPadding = 14;
					
					if (col_1_height > col_2_height)
					{
						window['feature_' + r].c2_propEl.style.height = ((col_1_height - indexed_feature_a_vertPadding) + 'px');
					}
					else if (col_2_height > col_1_height)
					{
						window['feature_' + r].c1_propEl.style.height = ((col_2_height- indexed_feature_a_vertPadding) + 'px');
					}
					
					c++;						
				}							
				
				// row 
				
				r++;				
			}
		}
		total_indexed_features = r;
	}
	
	function counterStickiness(selectedId)
	{
		for (var m=0; m<total_indexed_features; m++)
		{
			// col 1
			
			var c1_linkEl = window['feature_' + m].c1_linkEl;		
			
			if (c1_linkEl.id != selectedId)
			{
				var c1_linkElClass = c1_linkEl.className;			
				c1_linkEl.className = c1_linkElClass.replace(/hover/, 'blur');			
			}
			
			// if col 2
			
			if (window['feature_' + m].c2_el)
			{
				var c2_linkEl = window['feature_' + m].c2_linkEl;					
				
				if (c2_linkEl.id != selectedId)
				{
					var c2_linkElClass = c2_linkEl.className;			
					c2_linkEl.className = c2_linkElClass.replace(/hover/, 'blur');	
				}	
			}
			
		}
	}

	function getDimension(el, dimension)
	{
		if (dimension == 'width')
		{
			return el.offsetWidth;
		}
		else if (dimension == 'height')
		{
			return el.offsetHeight;
		}				
	}	
	
/* 
	Event Functions
*/

	function addEvent(obj, evType, fn, useCapture)
	{
		// http://www.scottandrew.com/weblog/articles/cbs-events
		
		if (obj.addEventListener)
		{
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} 
		else if (obj.attachEvent)
		{
			// IE ~ almost the same as mouseover and mouseout except that they don't react to event bubbling
			if (evType == 'mouseover')
			{
				evType = 'mouseenter';
			}
			else if (evType == 'mouseout')
			{
				evType = 'mouseleave';
			}			
			
			var r = obj.attachEvent('on' + evType, fn);
			return r;
		} 
	}	
	
	function interactWithFeature(e)
	{	
		//	this is reqd because with css alone, hovering over the img kills the link focus
		
		if (!e) 
		{
			var e = window.event; // http://www.quirksmode.org/js/events_properties.html
		}
		
		//
	
		if (e.target) 
		{
			targ = e.target;
		}
		else if (e.srcElement) 
		{
			targ = e.srcElement;
		}
		if (targ.nodeType == 3) 
		{	
			// defeat Safari bug
			targ = targ.parentNode;
		}
		
		// IE
		
		if (whichbrowser.isIE5up)
		{
			_this = targ;		
		}
		else
		{
			_this = this;	
		}

		//	Identify related/required elements
		
		var featureLink = _this.parentNode.getElementsByTagName('a')[0];
		var featureLinkHref = featureLink.href;			
		
		//	HOVER
	
		if ((e.type == 'mouseover') || (e.type == 'mouseenter'))
		{	
			var featureLinkClass = featureLink.className;			
					
			if (featureLinkClass.indexOf('blur') != -1)
			{			
				// If className already contains 'blur' (ie if it has been hovered over already), just change this part of the className 							
				featureLink.className = featureLinkClass.replace(/blur/, 'hover');			
			}	
			else if (featureLinkClass.indexOf('hover') == -1)
			{				
			//	else add hover class, if it doesn't exist already (check reqd because mouseover also triggered when mousing off second img link)
				featureLink.className += ' hover';				
			}		
			
		}
		
		//	BLUR		
		
		else if ((e.type == 'mouseout') || (e.type == 'mouseleave'))
		{	
			var featureLinkClass = featureLink.className;			
		
			featureLink.className = featureLinkClass.replace(/hover/, 'blur');		
			
			counterStickiness(featureLink.id);
		}	
	}	
	
/* 
	Browser Detect
*/

//	VARIABLES

	var whichbrowser;
	
//	FUNCTIONS	

	function detectbrowser() 
	{
		// Browser Detect  v2.1.6
		// documentation: http://www.dithered.com/javascript/browser_detect/index.html
		// license: http://creativecommons.org/licenses/by/1.0/
		// code by Chris Nott (chris[at]dithered[dot]com)	
		
		var ua = navigator.userAgent.toLowerCase(); 
		
		// browser engine name
		this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);   
		
		// browser name
		this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
		this.isSafari      = (ua.indexOf('safari') != - 1); // Intro #owners DIV
		this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
		this.isOpera       = (ua.indexOf('opera') != -1); // Crew Profiles Info DIV
		this.isIcab        = (ua.indexOf('icab') != -1); 
		this.isAol         = (ua.indexOf('aol') != -1); 
		this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
		this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
		this.isFirefox    = (ua.indexOf('firefox/') != -1);
		this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
		
		// spoofing and compatible browsers
		this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
		this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);	
	
		// rendering engine versions
		this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
		this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
		this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );
		
		// browser version
		this.versionMinor = parseFloat(navigator.appVersion); 	
	
	   // correct version number
		if (this.isGecko && !this.isMozilla) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
		}
		else if (this.isMozilla) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
		}
		else if (this.isIE && this.versionMinor >= 4) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
		}
		else if (this.isKonqueror) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
		}
		else if (this.isSafari) {
		  this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
		}
		else if (this.isOmniweb) {
		  this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
		}
		else if (this.isOpera) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
		}
		else if (this.isIcab) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
		}
		
		this.versionMajor = parseInt(this.versionMinor); 
		
		// dom support
		this.isDOM1 = (document.getElementById);
		this.isDOM2Event = (document.addEventListener && document.removeEventListener);
		
		// css compatibility mode
		this.mode = document.compatMode ? document.compatMode : 'BackCompat';
		
		// platform
		this.isWin    = (ua.indexOf('win') != -1);
		this.isWin32  = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
		this.isMac    = (ua.indexOf('mac') != -1);
		this.isUnix   = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
		this.isLinux  = (ua.indexOf('linux') != -1); 
		
		// detect ie version
		this.isIE5x = (this.isIE && this.versionMajor == 5);
		this.isIE55 = (this.isIE && this.versionMinor == 5.5);
		this.isIE5up = (this.isIE && this.versionMajor >= 5);
		this.isIE6x = (this.isIE && this.versionMajor == 6);
		this.isIE6up = (this.isIE && this.versionMajor >= 6);
		this.isIE7x = (this.isIE && this.versionMajor == 7);	
		this.isIE7up = (this.isIE && this.versionMajor >= 7);		
		
		// detect ns version
		this.isNS6x = (this.isNS && this.versionMajor == 6);
	
		// detect opera version
		this.isOpera6 = ((this.isOpera) && (this.versionMajor <= 6));	
		
		// opera
		this.isAnyOpera = ((this.isOpera) && (this.versionMajor >= 6));	
		this.isOpera7orBelow = ((this.isOpera) && (this.versionMajor <= 7));	
		
		this.isNetscape7orBelow = ((this.isNS) && (this.versionMajor <= 7));			
		
		
		if (((this.isMac) && (this.isIE5x)) || (this.isNS6x) || (this.isOpera7orBelow) || (this.isNetscape7orBelow))
		{		
			this.isHiFi = false;		
		}		
		else
		{
			this.isHiFi = true;			
		}
	}

	function init()
	{
		if (whichbrowser.isHiFi)
		{
			enhanceIndexedFeatures();
		}
	}

	whichbrowser = new detectbrowser();	
	window.onload = init;
