/*
Last change:
2011-10-25 Johan Mattsson
	placeFlash() allowfullscreen
2010-10-20 Johan Mattsson
	Comments
2009-03-31 David Schulz
	Added height for embed-tag 
2009-02-11 David Schulz
	Updated detectFlash to support versions >= 10
2008-09-06 Ola Ljungars
	Added height variable
2008-10-29 Ola Ljungars
	Fixed bug if height was undefined
*/


//Internal javscript below

var requiredVersion = 5;

// System globals
var flash2Installed = false;
var flash3Installed = false;
var flash4Installed = false;
var flash5Installed = false;
var flash6Installed = false;
var maxVersion = 6;
var actualVersion = 0;
var hasRightVersion = false;

//detect os
var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false;

//detect browser
var appVer = navigator.appVersion.toLowerCase();
var iePos = appVer.indexOf('msie');
var isIE = (iePos != -1) ? true : false
var strUserAgent = navigator.userAgent.toLowerCase();

//if IE detect version
if (isIE) {
	is_minor = parseFloat(appVer.substring(iePos + 5, appVer.indexOf(';', iePos)))
	is_major = parseInt(is_minor);
}

jsVersion = 1.1;

if (isIE && isWin) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
	document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
	document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
	document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');
	document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');
	document.write('</SCRIPT\> \n');
}

//func_flash
/*
Tridion
	
This function embeds an flash if the user has flash or else it shows an image.
The function needs to be included on every page that uses flash (masterpages)
	
*/
function placeFlash(flashFile, alternateFile, objectClass, imageClass, alternateLink, altText, xmlFile, strHeight) {
	var height = strHeight;
	if (height != "" && typeof (height) != "undefined") {
		height = " height='" + height + "' ";
	}

	if (flashFile.indexOf("swf") != -1) {
		detectFlash(); //detect Flash version
	}

	//get values from xml
	var flashVars = "";


	if (hasRightVersion) {  // if we've detected an acceptable version
		var oeTags = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
          + 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=3,0,0,0" '
          + height
          + 'class="' + objectClass + '" >'
          + '<param name="movie" value="' + flashFile + '?xmlFile=' + xmlFile + '" />'
          + '<param name="bgcolor" value="white" />'
          + '<param name="scale" value="noborder" />'
          + '<param name="wmode" value="transparent">'
          + '<param name="allowFullscreen" value="true">'
          + '<embed src="' + flashFile + '?xmlFile=' + xmlFile + '" '
          + 'class="' + objectClass + '" '
          + 'bgcolor="white" '
          + 'scale="noborder" '
          + 'wmode="transparent" '
          + 'allowFullscreen="true" '
          + height
          + 'pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">'
          + '</embed>'
          + '</object>';
		document.write(oeTags);
	}
	else {
		if (alternateLink != "") {
			if (alternateFile == "")
				alternateFile = flashFile;
			var alternateContent = '<a href="' + alternateLink + '"><img class="' + imageClass + '" src="' + alternateFile + '" alt="' + altText + '" border="0" /></a>'
		}
		else {
			var alternateContent = '<img class="' + imageClass + '" src="' + alternateFile + '" alt="' + altText + '" border="0" />'
		}
		document.write(alternateContent);
	}
}

function detectFlash() {
	if (navigator.plugins) {
		if (navigator.plugins["Shockwave Flash 2.0"]
	        || navigator.plugins["Shockwave Flash"]) {

			var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;

			//var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));
			var flashVersion = parseInt(flashDescription.split(" ")[2].split(".")[0]);

			flash2Installed = flashVersion == 2;
			flash3Installed = flashVersion == 3;
			flash4Installed = flashVersion == 4;
			flash5Installed = flashVersion == 5;
			flash6Installed = flashVersion >= 6;
		}
	}

	for (var i = 2; i <= maxVersion; i++) {
		if (eval("flash" + i + "Installed") == true) actualVersion = i;
	}

	if (navigator.userAgent.indexOf("WebTV") != -1) actualVersion = 3;

	if (actualVersion >= requiredVersion) {
		hasRightVersion = true;
	}
}


// Comments
function toggleComment(objElement, strElementName) {
	if (objElement.className.indexOf("expanded") == -1) {
		if (objElement.className == "comment")
			objElement.className = "comment expanded";
		if (objElement.className == "comment indented")
			objElement.className = "comment indented expanded";
	}
	else {
		objElement.className = objElement.className.replace(" expanded", "");
	}
}

function expandAllComments() {
	var divMaster = document.getElementById("divComments");
	var aComments = divMaster.getElementsByTagName("div");
	for (i = 0; i < aComments.length; i++) {
		aComments[i].className = aComments[i].className.replace(" hidden", "");
		if (aComments[i].className == "comment") {
			if (aComments[i].id == "divCommentReadAll")
				aComments[i].className = "comment hidden";
			else {
				aComments[i].className = "comment expanded";
			}
		}
		if (aComments[i].className == "comment indented") {
			aComments[i].className = "comment indented expanded";
		}
	}
}

function replyToPost(strArticleID, strSubject, iInReplyTo) {
	if (iInReplyTo != "" && strSubject.indexOf("Re:") == -1)
		strSubject = "Re: " + strSubject;
	document.getElementById("commentIFrame").src = "/_inc/addComment.aspx?qArticleID=" + strArticleID + "&qSubject=" + strSubject + " &qInReplyTo=" + iInReplyTo;
	showCenter(window.center({ width: 460, height: 620 }));
	document.getElementById("commentIFrame").focus();
}

function editPost(strArticleID, iRowNomber) {
	document.getElementById("commentIFrame").src = "/_inc/addComment.aspx?qArticleID=" + strArticleID + "&qRowNumber=" + iRowNomber + "&editcomment=true";
	showCenter(window.center({ width: 460, height: 620 }));
	document.getElementById("commentIFrame").focus();
}

window.size = function() {
	var w = 0;
	var h = 0;

	//IE
	if (!window.innerWidth) {
		//strict mode
		if (!(document.documentElement.clientWidth == 0)) {
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return { width: w, height: h };
}

window.center = function() {
	var hWnd = (arguments[0] != null) ? arguments[0] : { width: 0, height: 0 };

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if (!window.pageYOffset) {
		//strict mode
		if (!(document.documentElement.scrollTop == 0)) {
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else {
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else {
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	_x = ((this.size().width - hWnd.width) / 2) + offsetX;
	_y = ((this.size().height - hWnd.height) / 2) + offsetY;

	return { x: _x, y: _y };
}

function showCenter(point) {
	var objDivCP = document.getElementById("divCommentsPost");
	objDivCP.style.display = "inline";

	var objDivFrm = document.getElementById("divCommentsPostFrm");
	objDivFrm.style.position = "absolute";
	objDivFrm.style.top = point.y + "px";
	objDivFrm.style.left = point.x + "px";
}
