//DNN Module: janet.Articles
//Version 01.00.05
//Creation Date: 2nd June 2009
//Author: Neil
//Copyright: Copyright 2006-2009 Janet Systems Ltd.
//URL: http://www.janetsystems.co.uk

var ie4=document.all&&!document.getElementById
var ns4=document.layers
var isIE = document.all ? true : false;
var rotateDelay=5000
var itemNum=0
var NumTagWords=2

function RadioButtonSelect(item, grdName) {
	var i, obj;
	for (i=0; i<document.all.length; i++) {
		obj = document.all(i);
		if (obj.type == "radio") {
		if (item.id.substr(0, grdName.length) == grdName)
			if (item.id == obj.id)
				obj.checked = true;
			else
				obj.checked = false;
		}
	}
}

function rotate(){
	if (itemNum>=rotateImages.length){
		itemNum=0;
	}
	if (document.getElementById){
		document.getElementById("janetClient_rotate").innerHTML="<span>" + rotateImages[itemNum]+ "</span>";
		itemNum++
		setTimeout("rotate()",rotateDelay)
	}
}
//news scroll
function janetArticles_Scroll(item)
{
	var it = document.getElementById(item);
	it.style.top=it.style.top.replace(/px|pt,*\)*/g,"")-1;
	if((parseInt(it.style.top) + parseInt(it.offsetHeight))<=0)
	{
		it.style.top=it.parentNode.offsetHeight;
	}
}
//gallery
function janetArticlesGalleryBack(item, itemNum) {
	var n = parseInt(document.getElementById(itemNum).innerHTML)
	n = n - 1;
	if (n < 0) {
		n = 3
	}
	document.getElementById(item).innerHTML="<span>" + janetArticlesGallery[n]+ "</span>";
	document.getElementById(itemNum).innerHTML=n;
}
function janetArticlesGalleryNext(item, itemNum) {
	var n = parseInt(document.getElementById(itemNum).innerHTML)
	n = n + 1;
	if (n > 3) {
		n = 0
	}
	document.getElementById(item).innerHTML="<span>" + janetArticlesGallery[n]+ "</span>";
	document.getElementById(itemNum).innerHTML=n;
}
function janetArticles_NextBack() {
	if (document.getElementById) {
		document.getElementById('Scroll').style.height=document.getElementById('Surround').offsetHeight +'px';
		var ReviewScroll = document.getElementById('ReviewScroll')
		var ReviewSurround = document.getElementById('ReviewSurround')
		if (ReviewScroll) {
			document.getElementById('Scroll').style.height=ReviewSurround.offsetHeight +'px';
			ReviewScroll.style.height=ReviewSurround.offsetHeight +'px';
		//alert(document.getElementById('ReviewSurround').offsetHeight +'px')
		//alert(document.getElementById('Surround').offsetHeight +'px')
			
		}
	}
}

function FillDatagrid(item,str) {
	document.getElementById(dtgDirectory).innerHTML = "";
}
//http://aspalliance.com/773
function FillTable(scity)
{
  //var auth = scity.getElementsByTagName('EventInfo'); 
  var tbl = document.getElementById(dtgListing);//.getElementsByTagName("tbody")[0];
  for(var i=0;i<tbl.context.childNodes(0).parentNode.childNodes.length;i++)
  {
   var row = document.createElement("TR"); 
   row.setAttribute("className","text");
   row.setAttribute("bgColor","#ECECEC");
  for(var j=0;j<auth.context.childNodes(0).childNodes.length;j++)
  {
   var cell = document.createElement("TD"); 
   cell.innerHTML = auth.context.childNodes(i).childNodes(j).text;
   row.appendChild(cell); 
  }
   tbl.appendChild(row)
  }
}
function setOpacity(id, opacity) {
//alert(id)
//alert(opacity)
  var obj = document.getElementById(id);
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari&lt;1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}
//handles fade effect of adding new article


function doInsertFlag() {

}

//http://www.java2s.com/Code/JavaScript/Language-Basics/Stringutilitywordcountreplaceandcapitalize.htm
function wordCount(tagWords,item,minWordLength) {
  //alert(item + "_designEditor");
  //alert(document.getElementById(item + "_designEditor"));
  var it;
  if (document.getElementById(item + "_designEditor")) {
	item = item + "_designEditor";
	it = document.getElementById(item);
 	str = prepStr(it.contentWindow.document.body.innerHTML);
  }
  else {
	item = item + "___Frame";
	it = document.getElementById(item);
	var iframe2 = it.contentWindow.document.getElementById('xEditingArea').firstChild;
	str = prepStr(iframe2.contentWindow.document.body.innerHTML);
  }
	var wordArray = new Array();
	var it2 = document.getElementById(tagWords);
  //
  //this is for ftb
	    //} else {
 		//		str = prepStr(it.contentWindow.document.body.innerText);
 	    //}
 	    //this is fck	    
 	    //contentWindow.document.body
 	    //alert(it.contentDocument.body.innerHTML);  
 	    //document.getElementById("NAT").style.border = "solid 1px #ff0000;";
 	    //document.getElementById("NAT").innerHTML = it.contentDocument.body.innerHTML;
			
//var iframe1 = document.getElementById(fieldId + '___Frame')

//alert(iframe2.contentWindow.document.body.innerHTML);
 	    
 	    
  var tempArray = str.split(' ').sort();
  var arSortWords = new Array(); 
  var count = 0;
  
  var BadWords = new Array();
  for (var i = 0; i < arBadWords.length; i++) {
		BadWords[arBadWords[i]] = 1;
  }
  
  

  // Iterate through all the words
  for (var i = 0; i < tempArray.length; i++) {

    // If an array element with the same name as
    // the current word exists, increment its value by 1
    if (wordArray[tempArray[i]]) {
      wordArray[tempArray[i]]++;
      }

    // Otherwise, assign the array element to the 
    /// name of the word, and give it a value of 1
    else {
      if ((tempArray[i].length >=minWordLength)) {
			if (BadWords[tempArray[i]]!=1) {
				wordArray[tempArray[i]] = 1; 
				arSortWords[count] = tempArray[i];
				count++;
			}
        }
		}
    }
    
    //get top listed words
    var arrStr = '';
    var msg="";
    var nn;
   // for (var i=0;i < 11;i++) {
	//	if (arSortWords[i] != undefined) {
	//		msg = msg + arSortWords[i] +  "(" + wordArray[arSortWords[i]] + ")\n";
	//	}
   // }
   // msg = msg + "--------------\n";
	for (var i=0;i < count - 1;i++) {
		for (var j=i;j < count;j++) {
			if (wordArray[arSortWords[i]] < wordArray[arSortWords[j]]) {
				nn = arSortWords[i];
				arSortWords[i] = arSortWords[j];
				arSortWords[j] = nn;
				//msg = msg + arSortWords[i] + "(" + wordArray[arSortWords[i]] + ") - " + arSortWords[j] + "(" + wordArray[arSortWords[j]] + ")\n";
			}
		}
	}
    //msg = msg + "--------------\n";
	
   //for (var i=0;i < 11;i++) {
	//	if (arSortWords[i] != undefined) {
	//		msg = msg + arSortWords[i] + "\n";
	//	}
   //}
	//alert(msg);
    
    
	//build the output string
    for (var i=0;i < NumTagWords;i++) {
		if (arSortWords[i] != undefined) {
			arrStr = arrStr + ", " + arSortWords[i];
		}
    }
      
      str = it2.innerHTML + arrStr;
		str = str.replace(/^, /,"");
		str = str.replace(/, $/,"");
		it2.innerHTML = str
    
  }
  
function prepStr(str) {
  str = str.toLowerCase();
  //str.replace(/<br>/gi," ");
  str.replace(/<br>/gi,"\n");
  str.replace("\n"," ");
  str = removeHTMLTags(str);
  str = str.replace(/['"-]/g, "");
  str = str.replace(/\W/g, " ");
  str = str.replace(/\s+/g, " ");
  if (str.indexOf("<")>0 || str.indexOf(">")>0) {
	str = ""
  }
  return str;
}
function removeHTMLTags(str){
 	 	str = str.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
 		return str.replace(/<\/?[^>]+(>|$)/g, " ");
}
//http://www.w3schools.com/jsref/jsref_sort.asp
function sortNumber(a,b)
{
	return a - b
}
function addSmallest(item) {
	//if wordArray[arWords[i]].sort(sortNumber)
}

function doSetCats(item) {
	var row = document.getElementById(item);
	//if row.
	//var row = document.getElementById(item).firstChild;
	
}
function CheckAllDataListCheckBoxes(aspCheckBoxID, checkVal) {
	re = new RegExp(':' + aspCheckBoxID + '$')
	for(i = 0; i < document.forms[0].elements.length; i++) {
		elm = document.forms[0].elements[i]
		if (elm.type == 'checkbox') {
			if (re.test(elm.name)) {
				elm.checked = checkVal
			}
		}
	}
}
// ################
//Category tree
function treeLine(item, image){
	var it = document.getElementById(item);
	if(it.style.display=='block') {
		it.style.display='none';
		image.className='treeCatPlus';
		
	}
	else {
		it.style.display='block';
		image.className='treeCatMinus';
	}
	return false;
}

function treeNodeChecked(item, checkState) {
		//alert(item.checked);
	
		//alert("treeNodeChecked");
		//node.parentNode
		var i=0;
		var j=0;
		var mDiv;
		var aDiv;
		var dltCategories;
		dltCategories = true;
		mDiv = item;
		//alert(mDiv.nodeName)		
		//alert(mDiv.parentNode)	
		if (item.checked) {	
			
			while (mDiv.parentNode != null && dltCategories) {	
			//alert(mDiv.parentNode.nodeName)	
			//alert(mDiv.parentNode.id.indexOf("dltCategories"))	
				if (mDiv.parentNode.nodeName=='TD') {	
					//alert(mDiv.parentNode.getElementsByTagName('INPUT')[0].checked)
					mDiv.parentNode.getElementsByTagName('INPUT')[0].checked = 'checked';
				}
				if (mDiv.id.indexOf("dltCategories") > 0 && mDiv.id.indexOf("cbxCategories") <0) {
					dltCategories=false;
				}
				mDiv = mDiv.parentNode;
			}
		}
	//for( i = 0; i < item.parentNode.childNodes.length; i++ ) {
		//alert(item.parentNode.childNodes.input.checkbox.length);
		//for( j = 0; j < node.childNodes.input.checkbox.length; j++ ) {
			//item.parentNode.childNodes.style.border='solid 1px black';
			//item.parentNode.childNodes.childNodes[i].input.checkbox.checked=checkState;
		//}
	//}
}


// ################
//Media Player
var ap_instances = new Array();

function ap_stopAll(playerID) {
	for(var i = 0;i<ap_instances.length;i++) {
		try {
			if(ap_instances[i] != playerID) document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 1);
			else document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 0);
		} catch( errorObject ) {
			// stop any errors
		}
	}
}

function ap_registerPlayers() {
	var objectID;
	var objectTags = document.getElementsByTagName("object");
	for(var i=0;i<objectTags.length;i++) {
		objectID = objectTags[i].id;
		if(objectID.indexOf("audioplayer") == 0) {
			ap_instances[i] = objectID.substring(11, objectID.length);
		}
	}
}

var ap_clearID = setInterval( ap_registerPlayers, 100 );

function HideMediaPlayer() {
	document.getElementById('MediaWrapper').style.display = 'none';
	document.getElementById('MediaPlayer').innerHTML = '';
}

function PlayMedia(blnAudio,strPlayer,strFileName,strTitle) {
	strTitle = strTitle.substring(0, 15);
	//strFileName = escape(strFileName);
	//centre and show media player div
	document.getElementById('MediaWrapper').style.position = 'absolute';
	document.getElementById('MediaWrapper').style.display = 'block';
	document.getElementById('MediaWrapper').style.width = "258px";
	document.getElementById('MediaWrapper').style.height = '262px';
	document.getElementById('MediaWrapper').style.margin = '40% 200px';
	
	var strHref = window.location.href;
	var aHref = strHref.split("/");
	var spacer = ''
	spacer += '/DesktopModules/janet.Articles/images/spacer.gif';
	//insert content
	var strInnerHTML
	strInnerHTML = ''// <a href="#" onClick="HideMediaPlayer(); return false;"><div id="MediaClose">&nbsp;<div></a>';
	if (blnAudio=='True') {
		document.getElementById('MediaWrapper').style.height = '62px';
		document.getElementById('MediaWrapper').style.width = "290px";
		var table;
		table = '<div id="MediaTitle">';
		table += '<TABLE cellSpacing="0" cellPadding="0" border="0" width="27" height="22"><TR>';
		table += '<TD width="27" height="22" class="janetarticles_video_1C"><a href="#" onClick="HideMediaPlayer(); return false;"><img src="' + spacer + '" width="27" height="22" border="0"></a></TD>';
		//table += '<td width="293" height="22" nowrap>&nbsp;' + strTitle + '&nbsp;</td>';
		//table += '<td width="100%" height="28"><img src="' + spacer + '" height="38" id="mediaSpacer"></td>';
		table += '</TR></table></div>';
		table += '<div id="InnerHTML"></div>';
		strInnerHTML += ' <object type="application/x-shockwave-flash" data="' + strPlayer + '" id="audioplayer1" height="24" width="290">'
		strInnerHTML += ' <param name="movie" value="' + strPlayer + '">'
		strInnerHTML += ' <param name="FlashVars" value="playerID=1&soundFile=' + strFileName + '">'
		strInnerHTML += ' <param name="quality" value="high">'
		strInnerHTML += ' <param name="menu" value="false">'
		strInnerHTML += ' <param name="wmode" value="transparent">'
		document.getElementById('MediaPlayer').innerHTML = table;
		document.getElementById('InnerHTML').innerHTML = strInnerHTML;

		document.getElementById('MediaTitle').style.width = "27px";
}
	else {
		//document.getElementById('MediaWrapper').style.paddingTop = '0px';
	//	strInnerHTML += ' <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="232" height="200" id="player" align="middle">';
	//	strInnerHTML += ' <param name="movie" value="' + strPlayer + '?file=' + strFileName + '" />';
	//	strInnerHTML += ' <param name="menu" value="false" />';
	//	strInnerHTML += ' <param name="quality" value="high" />';
	//	strInnerHTML += ' <param name="bgcolor" value="#ffffff" />';
	//	strInnerHTML += ' <embed src="' + strPlayer + '?file=' + strFileName + '"';
	//	strInnerHTML += ' quality=high bgcolor=#ffffff width=232 height=200 ';
	//	strInnerHTML += ' type=application/x-shockwave-flash pluginspage=http://www.macromedia.com/go/getflashplayer />';
	//	strInnerHTML += ' </embed>';
	//	strInnerHTML += ' </object>';
		
	//strInnerHTML = '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/_If_4UwgepQ&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/_If_4UwgepQ&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>'
		
		
		
	var table;
	table = '<div id="MediaTitle">';
	table += '<TABLE cellSpacing="0" cellPadding="0" border="0" width="232" height="22"><TR>';
	table += '<TD width="27" height="22" class="janetarticles_video_1C"><a href="#" onClick="HideMediaPlayer(); return false;"><img src="' + spacer + '" width="27" height="22" border="0"></a></TD>';
	table += '<td width="205" height="22" nowrap>&nbsp;' + strTitle + '&nbsp;</td>';
	//table += '<td width="100%" height="28"><img src="' + spacer + '" height="38" id="mediaSpacer"></td>';
	table += '</TR></table></div>';
	table += '<div id="InnerHTML"></div>';

	document.getElementById('MediaPlayer').innerHTML = table;
	
	var s1 = new SWFObject(strPlayer,"FlashPlayer","232","200","7");
	s1.addParam("allowfullscreen","true");
	s1.addVariable("file",strFileName);
	s1.addVariable("width","232");
	s1.addVariable("height","200");
	//s1.addVariable("wmode","transparent");
	s1.write("InnerHTML");
		}

	var winW = 630, winH = 460;
	var MediaPlayerW;
	var MediaPlayerH;
	var Offset = 0;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight ;
			Offset = window.pageYOffset;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
			Offset = document.body.scrollTop;
			//document.getElementById('InnerHTML').style.marginLeft= "-36px";
			//document.getElementById('InnerHTML').style.marginRight= "-52px";
			//document.getElementById('InnerHTML').style.paddingTop= "2px";
			//document.getElementById('InnerHTML').style.paddingBottom= "2px";
			//document.getElementById('FlashPlayer').style.display= "block";
			//document.getElementById('InnerHTML').style.margin= "0";
			//document.getElementById('InnerHTML').style.padding= "0";
		}
	}
	MediaPlayerW = document.getElementById('MediaPlayer').offsetWidth;
	MediaPlayerH = document.getElementById('MediaPlayer').offsetHeight;
	document.getElementById('MediaWrapper').style.marginLeft = parseInt((winW - MediaPlayerW)/3) + 'px';
	document.getElementById('MediaWrapper').style.marginTop = parseInt((winH - MediaPlayerH)/3) + Offset + 'px';

	return false;
}