/**
 * Applies the various style functions when called by the onload event...
 */
function StyleFixer() {
	AttachFlashHeader();
    NewWindowLinks();

	// news story images centring fix...
    var divs = document.getElementsByTagName('div');
    for (var x = 0; x < divs.length; x++) {
        if (divs[x].className.indexOf('storyImg') != -1) {
            // for news story images, the containing div must have a fixed width so that the margin: 0 auto; centring trick
            // works - but the width must be dynamic, so this is the bit that sets the width...
            var img = divs[x].getElementsByTagName('img')[0];
            if (img) {
                divs[x].style.width = img.offsetWidth + 'px';
            }
        }
    }

	// work out what page we're on...
	window.mediaLibraryPage = false;
	var h1s = document.getElementsByTagName('h1');
	for (x = 0; x < h1s.length; x++) {
		if ((h1s[x].className.indexOf('media') != -1) || (h1s[x].className.indexOf('photos') != -1)) {
			window.mediaLibraryPage = true;
		}
	}

	ColumnHeightFix();
	applyTextDirection();

	if (window.ie6 && window.direction == 'rtl') {
		window.attachEvent('onresize', applyTextDirection);
	}
}

/**
 * Fixes the height of 'farColumn' (the 'more photos' or 'more videos' column) to be the same
 * as the height of the other two columns on the page...
 */
function ColumnHeightFix() {
	// height fix to make the more photos side bar and the two main columns the same height...
	var twoCol = document.getElementById("twoCol");
	var morePhotosAllClick = document.getElementById("farColumn");
	
	// modifier is based on browser and text direction...
	var test = (window.direction == 'rtl') ? (window.ie) : (window.ie6) ;
	var modifier = (test) ? (21) : (19);
	
	// apply the modifier...
	if (twoCol && morePhotosAllClick) {
		if (twoCol.offsetHeight < (morePhotosAllClick.offsetHeight + modifier)) {
			//twoCol.style.height = (morePhotosAllClick.offsetHeight + modifier) + 'px';
			twoCol.style.paddingBottom = ((morePhotosAllClick.offsetHeight + modifier) - twoCol.offsetHeight) + 'px';
		} else if (twoCol.offsetHeight > (morePhotosAllClick.offsetHeight + modifier)) {
			//morePhotosAllClick.style.height = (twoCol.offsetHeight - modifier) + 'px';
			morePhotosAllClick.style.paddingBottom = (twoCol.offsetHeight - (morePhotosAllClick.offsetHeight + modifier)) + 'px';
		}
	}
}

/**
 * due to changes in the way plugin content is handled by some browsers, it is necessary to attach it using javascript
 * to avoid getting the 'click on the control to activate' alert...
 */
function AttachFlashHeader() {
    var flashHeader = document.getElementById('flashHeader');
    if (flashHeader && headerMovieSrc) {
        if (window.ie) {
            flashHeader.innerHTML = 
                "<object height=\"111\" width=\"788\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\">" +
                "\t<param value=\"" + headerMovieSrc + "\" name=\"movie\" />\n" +
                "\t<param value=\"high\" name=\"quality\" />\n" +
                "\t<param value=\"\" name=\"wmode\" />\n" +
                "\t<param value=\"false\" name=\"LOOP\" />\n" +
                "\t<embed quality=\"high\" height=\"111\" width=\"788\" src=\"" + headerMovieSrc + "\"></embed>\n" +
                "</object>";
        } else {
            var details = {quality: 'high', height: '111', width: '788', src: headerMovieSrc};
            var flashObject = document.createElement('object');
            flashObject.setAttribute('id', details.src);
            flashObject.setAttribute('height', details.height);
            flashObject.setAttribute('width', details.width);
            flashObject.setAttribute('codebase', 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000');
            var param = document.createElement('param');
            var params = [
                ['movie', details.src],
                ['quality', 'high'],
                ['wmode', ''],
                ['loop', 'false']
            ];
            for (var x = 0; x < params.length; x++) {
                param = document.createElement('param');
                param.setAttribute('name', params[x][0]);
                param.setAttribute('value', params[x][1]);
                flashObject.appendChild(param);
            }
            
            var embed = document.createElement('embed');
            embed.setAttribute('quality', 'high');
            embed.setAttribute('height', details.height);
            embed.setAttribute('width', details.width);
            embed.setAttribute('src', details.src);
            flashObject.appendChild(embed);
            flashHeader.appendChild(flashObject);
        }        
    }
}

/**
 * newWindowLinks - overrides the default link behaviour
 */
function NewWindowLinks() {
	var links = document.getElementsByTagName('a');
	var popupWidth = 850;
	var popupHeight = 650;
	var popupPosX = ((window.screen.width - popupWidth) / 2);
	var popupPosY = ((window.screen.height - popupHeight) / 2);
	var onNewWindowLinkClick = function() {
		var popup = window.open(this.href, null, 'width=' + popupWidth + ',height=' + popupHeight + ',left=' + popupPosX + ',top=' + popupPosY + ',resizable=yes,scrollbars=yes'); 
		return false;
	}
	for (var x = 0; x < links.length; x++) {
		if (links[x].className.indexOf('newWindowLink') != -1) {
			links[x].onclick = onNewWindowLinkClick;
		}
	}
}

/**
 * this is to fix a peculiar bug with the display of background images in h1 elements for ie6 when the text direction is
 * rtl (told you it was peculiar); we insert the image as an img tag instead...
 */
function ie6MediaHeadingFix() {
    // there should only be one h1 on the page, but just in case we'll need to check...
    var h1s = document.getElementsByTagName('h1');
    for (var x = 0; x < h1s.length; x++) {
        if (h1s[x].className.indexOf('ie6MediaHeading') != -1) {
            var ie6MediaHeading = h1s[x];
        }
    }
    if (ie6MediaHeading) {
        var img = document.createElement('img');
        img.src = '../img/iconMediaHeader.gif';
        img.width = 22;
        img.height = 22;
        img.style.marginRight = '-24px';
        ie6MediaHeading.insertBefore(img, ie6MediaHeading.firstChild);
    }
        
}

/**
 * sets the text direction for the page
 */
function applyTextDirection() {
	if (window.direction && !(window.ie6 && window.mediaLibraryPage && window.direction == 'ltr')) {
		var pageWrap = document.getElementById('pageWrap');
		pageWrap.dir = '';
		pageWrap.dir = window.direction;
	}
}

// browser independent way of attaching to the onload event of the window...
window.ie = false;
window.ie7 = false;
window.ie6 = false;
window.selectedAnchors = new Array();
if (window.ActiveXObject) {
    // ie...
    window.ie = true;
    window.attachEvent('onload', StyleFixer);
    // ie6 or earlier...
    if (!window.XMLHttpRequest) {
        window.attachEvent('onload', ie6MediaHeadingFix);
        window.ie6 = true;
    } else {
        window.ie7 = true;
    }
} else {
    if (document.addEventListener) {
        window.addEventListener('load', StyleFixer, true);
    } else {
        /*if (window.onload) {
            var existingFunction = window.onload;
            window.onload = function() {
                existingFunction();
                HeightFixer();
            }
        }*/
        window.onload = StyleFixer;
    }
}