// -------------------------------------------------------------------
// Advanced RSS Ticker (Ajax invocation) core file
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// -------------------------------------------------------------------

//Relative URL syntax:
var lastrssbridgeurl="lastrss/bridge.php"

//Absolute URL syntax. Uncomment below line if you wish to use an absolute reference:
//var lastrssbridgeurl="http://"+window.location.hostname+"/lastrss/bridge.php"

////////////No need to edit beyond here//////////////

function createAjaxObj() {
	var httprequest=false
	if (window.XMLHttpRequest) { // if Mozilla, Safari etc
		httprequest=new XMLHttpRequest()
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml')
	} else if (window.ActiveXObject) { // if IE
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}
	return httprequest
}

// -------------------------------------------------------------------
// Main RSS Ticker Object function
// rssticker_ajax(RSS_id, cachetime, divId, divClass, delay, optionallogicswitch)
// -------------------------------------------------------------------

function rssticker_ajax(RSS_id, cachetime, divId, divClass, delay, logicswitch) {
	this.RSS_id=RSS_id //Array key indicating which RSS feed to display
	this.cachetime=cachetime //Time to cache feed, in minutes. 0=no cache.
	this.tickerid=divId //ID of ticker div to display information
	this.delay=delay //Delay between msg change, in miliseconds.
	this.logicswitch=(typeof logicswitch!="undefined")? logicswitch : ""
	this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
	this.pointer=0
	this.opacitysetting=0.2 //Opacity value when reset. Internal use.
	this.title=[], this.newsId=[], this.filePath=[], this.link=[], this.description=[], this.pubdate=[], this.linkedWork=[], this.linkedClients=[] //Arrays to hold each component of an RSS item
	this.ajaxobj=createAjaxObj()
	document.write('<div id="'+divId+'" class="'+divClass+'" >Loading News...</div>')
	if (window.getComputedStyle) //detect if moz-opacity is defined in external CSS for specified class
		this.mozopacityisdefined=(window.getComputedStyle(document.getElementById(this.tickerid), "").getPropertyValue("-moz-opacity")==1)? 0 : 1
	//if (window.getComputedStyle)
		//this.khtmlopacityisdefined=(window.getComputedStyle(document.getElementById(this.tickerid), "").getPropertyValue("-khtml-opacity")==1)? 0 : 1
	//if (window.getComputedStyle)
		//this.css3opacityisdefined=(window.getComputedStyle(document.getElementById(this.tickerid), "").getPropertyValue("opacity")==1)? 0 : 1
	this.getAjaxcontent()
}

// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to "bridge.php" with the supplied parameters
// -------------------------------------------------------------------

rssticker_ajax.prototype.getAjaxcontent=function(){
	if (this.ajaxobj){
		var instanceOfTicker=this
		var parameters="id="+encodeURIComponent(this.RSS_id)+"&cachetime="+this.cachetime+"&bustcache="+new Date().getTime()
		this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
		this.ajaxobj.open('GET', lastrssbridgeurl+"?"+parameters, true)
		this.ajaxobj.send(null)
	}
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

rssticker_ajax.prototype.initialize=function(){ 
	if (this.ajaxobj.readyState == 4){ //if request of file completed
		if (this.ajaxobj.status==200){ //if request was successful
			var xmldata=this.ajaxobj.responseXML
			if(xmldata.getElementsByTagName("item").length==0){ //if no <item> elements found in returned content
				document.getElementById(this.tickerid).innerHTML="<b>Error</b> fetching remote RSS feed!<br />"+this.ajaxobj.responseText
				return
			}
			var instanceOfTicker=this
			this.feeditems=xmldata.getElementsByTagName("item")
			//Cycle through RSS XML object and store each peice of an item inside a corresponding array
			for (var i=0; i<this.feeditems.length; i++){
				this.newsId[i]=this.feeditems[i].getElementsByTagName("newsId")[0].firstChild.nodeValue
				this.title[i]=this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue
				this.filePath[i]=this.feeditems[i].getElementsByTagName("filePath")[0].firstChild.nodeValue
				this.link[i]=this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue
				this.description[i]=this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue
				this.pubdate[i]=this.feeditems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue
				this.linkedWork[i]=this.feeditems[i].getElementsByTagName("linkedWork")[0].firstChild.nodeValue
				this.linkedClients[i]=this.feeditems[i].getElementsByTagName("linkedClients")[0].firstChild.nodeValue				
			}
			document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
			document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0} 
			this.rotatemsg()   
		}
	}
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through RSS messages and displays them
// -------------------------------------------------------------------

rssticker_ajax.prototype.rotatemsg=function(){
	var instanceOfTicker=this
	if (this.mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
		setTimeout(function(){instanceOfTicker.rotatemsg()}, 100)
	else{ //else, construct item, show and rotate it!
		var tickerDiv=document.getElementById(this.tickerid)
		if (tickerDiv.id == "homeFeatured2") {
			if (this.filePath[this.pointer] != "no image") {
				var image='<h1 class="newsHeadline featured2Headline">'+this.title[this.pointer]+'</h1><div id="homeFeatured2_imgContainer"><img src="images/news/'+this.filePath[this.pointer]+'" alt="'+this.title[this.pointer]+'"/></div>'
			} else {
				var image='<h1 class="newsHeadline featured2Headline">'+this.title[this.pointer]+'</h1>'
			}
			var linktitle='<div style="float: left; height:104px;">'
			var description='<span class="featured2text white">'+this.description[this.pointer]+' <a href="'+this.link[this.pointer]+'" title="'+this.title[this.pointer]+'" class="white">more</a></span></div>' 
			var links=''
			if (this.linkedClients[this.pointer] == "Yes" || this.linkedWork[this.pointer] == "Yes") {
				links+='<div class="spacer"></div><div class="homeFeat_linksContainer"><p class="homeRelatedWhite bold">View Related : '
				if (this.linkedClients[this.pointer] == "Yes") {
					links+='<a href="clients.php?newsId='+this.newsId[this.pointer]+'" title="Related Clients" class="homeMoreLink">Client</a> '
				}
				if (this.linkedWork[this.pointer] == "Yes") {
					links+='<a href="work.php?newsId='+this.newsId[this.pointer]+'" title="Related Work" class="homeMoreLink">Work</a>'
				}
				links+='</p></div>'
			}
			links+='<div class="homeFeat2_nextContainer"><a onclick="goToNextFeatured2('+this.newsId[this.pointer]+');" title="Next Article"><img src="images/nextArrowWhite.gif" alt="Next" /></a></div>'
		} else {
			var image='<div id="homeFeatured1_imgContainer"><img src="images/news/'+this.filePath[this.pointer]+'" alt="'+this.title[this.pointer]+'"/></div>'
			var linktitle='<div style="height:225px;"><h1 class="newsHeadline">'+this.title[this.pointer]+'</h1>'
			var description='<p class="homepage">'+this.description[this.pointer]+' <a href="'+this.link[this.pointer]+'" title="'+this.title[this.pointer]+'" class="homeMoreLink">more</a></p></div>'
			var links=''
			if (this.linkedClients[this.pointer] == "Yes" || this.linkedWork[this.pointer] == "Yes") {
				links+='<div class="homeFeat_linksContainer"><p class="homeRelated bold">View Related : '
				if (this.linkedClients[this.pointer] == "Yes") {
					links+='<a href="clients.php?newsId='+this.newsId[this.pointer]+'" title="Related Clients" class="homeMoreLink">Client</a> '
				}
				if (this.linkedWork[this.pointer] == "Yes") {
					links+='<a href="work.php?newsId='+this.newsId[this.pointer]+'" title="Related Work" class="homeMoreLink">Work</a>'
				}
				links+='</p></div>'
			}			
			links+='<div class="homeFeat1_nextContainer"><a onclick="goToNextFeatured1('+this.newsId[this.pointer]+');" title="Next Article"><img src="images/nextArrow.gif" alt="Next" /></a></div>'
		}
		var feeddate='<p>'+this.pubdate[this.pointer]+'</p>'
		// if (this.logicswitch.indexOf("description")==-1) description=""
		if (this.logicswitch.indexOf("date")==-1) feeddate=""
		var tickercontent=image+linktitle+feeddate+description+links //STRING FOR FEED CONTENTS 
		this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
		tickerDiv.innerHTML=tickercontent
		
		//com_stewartspeak_replacement(); // Call function to replace font
		
		this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) //FADE EFFECT- PLAY IT
		this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0
		setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) //update container every second
	}
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

rssticker_ajax.prototype.fadetransition=function(fadetype, timerid){
	var tickerDiv=document.getElementById(this.tickerid)
	if (fadetype=="reset")
		this.opacitysetting=0.2
	if (tickerDiv.filters && tickerDiv.filters[0]){
		if (typeof tickerDiv.filters[0].opacity=="number") //IE6+
			tickerDiv.filters[0].opacity=this.opacitysetting*100
		else //IE 5.5
			tickerDiv.style.filter="alpha(opacity="+this.opacitysetting*100+")"
	} else if (typeof tickerDiv.style.MozOpacity!="undefined" && this.mozopacityisdefined){
tickerDiv.style.MozOpacity=this.opacitysetting
	}
	//else if (typeof tickerDiv.style.KhtmlOpacity!="undefined" && this.khtmlopacityisdefined){
	//tickerDiv.style.KhtmlOpacity=this.opacitysetting
	//}
	//else if (typeof tickerDiv.style.opacity!="undefined" && this.css3opacityisdefined){
	//tickerDiv.style.opacity=this.opacitysetting
	//}
	if (fadetype=="up")
		this.opacitysetting+=0.2
	if (fadetype=="up" && this.opacitysetting>=1)
		clearInterval(this[timerid])	
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

// Show Next featured Article at position 1
function goToNextFeatured1(currentArticle) {
	xmlHttp=createAjaxObj()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			xmlDoc=xmlHttp.responseXML;	
			var container='<div id="homeFeatured1">'
			var image='<div id="homeFeatured1_imgContainer"><img src="images/news/'+xmlDoc.getElementsByTagName("filePath")[0].childNodes[0].nodeValue+'" alt="'+xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue+'"/></div>'
			var linktitle='<div style="height:225px;"><h1 class="newsHeadline">'+xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue+'</h1>'
			var description='<p class="homepage">'+xmlDoc.getElementsByTagName("description")[0].childNodes[0].nodeValue+' <a href="'+xmlDoc.getElementsByTagName("link")[0].childNodes[0].nodeValue+'" title="'+xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue+'" class="homeMoreLink">more</a></p></div>'
			var links=''
			if (xmlDoc.getElementsByTagName("linkedClients")[0].childNodes[0].nodeValue == "Yes" || xmlDoc.getElementsByTagName("linkedWork")[0].childNodes[0].nodeValue == "Yes") {
				links+='<div class="homeFeat_linksContainer"><p class="homeRelated bold">View Related : '
				if (xmlDoc.getElementsByTagName("linkedClients")[0].childNodes[0].nodeValue == "Yes") {
					links+='<a href="clients.php?newsId='+xmlDoc.getElementsByTagName("newsId")[0].childNodes[0].nodeValue+'" title="Related Clients" class="homeMoreLink">Client</a> '
				}
				if (xmlDoc.getElementsByTagName("linkedWork")[0].childNodes[0].nodeValue == "Yes") {
					links+='<a href="work.php?newsId='+xmlDoc.getElementsByTagName("newsId")[0].childNodes[0].nodeValue+'" title="Related Work" class="homeMoreLink">Work</a>'
				}
				links+='</p></div>'
			}			
			links+='<div class="homeFeat1_nextContainer"><a onclick="goToNextFeatured1('+xmlDoc.getElementsByTagName("newsId")[0].childNodes[0].nodeValue+');" title="Next Article"><img src="images/nextArrow.png" alt="Next" /></a></div></div>'	
			document.getElementById("homeFeatured1_Container").innerHTML=container+image+linktitle+description+links
		}
	}
	var url = "goToNext1.php?id=" + currentArticle;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

// Show Next featured Article at position 2
function goToNextFeatured2(currentArticle2) {
	xmlHttp=createAjaxObj()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			xmlDoc=xmlHttp.responseXML;	
			var container='<div id="homeFeatured2">'
			var image='<h1 class="newsHeadline featured2Headline">'+xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue+'</h1><div id="homeFeatured2_imgContainer"><img src="images/news/'+xmlDoc.getElementsByTagName("filePath")[0].childNodes[0].nodeValue+'" alt="'+xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue+'"/></div>'
			var linktitle='<div style="float: left">'
			var description='<p class="featured2text white">'+xmlDoc.getElementsByTagName("description")[0].childNodes[0].nodeValue+' <a href="'+xmlDoc.getElementsByTagName("link")[0].childNodes[0].nodeValue+'" title="'+xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue+'" class="white">more</a></p></div>' 			
			var links=''
			if (xmlDoc.getElementsByTagName("linkedClients")[0].childNodes[0].nodeValue == "Yes" || xmlDoc.getElementsByTagName("linkedWork")[0].childNodes[0].nodeValue == "Yes") {
				links+='<div class="homeFeat_linksContainer"><p class="homeRelatedWhite bold">View Related : '
				if (xmlDoc.getElementsByTagName("linkedClients")[0].childNodes[0].nodeValue == "Yes") {
					links+='<a href="clients.php?newsId='+xmlDoc.getElementsByTagName("newsId")[0].childNodes[0].nodeValue+'" title="Related Clients" class="homeMoreLink">Client</a> '
				}
				if (xmlDoc.getElementsByTagName("linkedWork")[0].childNodes[0].nodeValue == "Yes") {
					links+='<a href="work.php?newsId='+xmlDoc.getElementsByTagName("newsId")[0].childNodes[0].nodeValue+'" title="Related Work" class="homeMoreLink">Work</a>'
				}
				links+='</p></div>'
			}
			links+='<div class="homeFeat2_nextContainer"><a onclick="goToNextFeatured2('+xmlDoc.getElementsByTagName("newsId")[0].childNodes[0].nodeValue+');" title="Next Article"><img src="images/nextArrowWhite.png" alt="Next" /></a></div>'
			document.getElementById("homeFeatured2_Container").innerHTML=container+image+linktitle+description+links
		}
	}
	var url = "goToNext2.php?id=" + currentArticle2;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

// Show piece of work that has been clicked on
function showWork(workId) { 
	xmlHttp=createAjaxObj()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			xmlDoc=xmlHttp.responseXML;	
			// Replace Main piece of work with new one
			var newMainWork=""
			if (xmlDoc.getElementsByTagName("type")[0].childNodes[0].nodeValue == "Image") {
				newMainWork='<img src="work/images/'+xmlDoc.getElementsByTagName("filePath")[0].childNodes[0].nodeValue+'" alt="'+xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue+'" /><h2 class="mainWorkTitle">'+xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue+'</h2>'
				if (xmlDoc.getElementsByTagName("text")[0].childNodes[0]) {
					newMainWork+='<p class="mainWorkText">'+xmlDoc.getElementsByTagName("text")[0].childNodes[0].nodeValue+'</p>'	
				}				
			} else {
				newMainWork='<object type="application/x-shockwave-flash" data="player.swf?cPath='+xmlDoc.getElementsByTagName("filePath")[0].childNodes[0].nodeValue+'" width="332" height="250"><param name="movie" value="player.swf?cPath='+xmlDoc.getElementsByTagName("filePath")[0].childNodes[0].nodeValue+'" /></object><h2 class="mainWorkTitle">'+xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue+'</h2>'
				if (xmlDoc.getElementsByTagName("text")[0].childNodes[0]) {
					newMainWork+='<p class="mainWorkText">'+xmlDoc.getElementsByTagName("text")[0].childNodes[0].nodeValue+'</p>'	
				}
			}	
			document.getElementById("mainWork").innerHTML=newMainWork
		}
    }	
	var url = "getWork.php?workId=" + workId;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function moreWork(dbPosition) {
	xmlHttp=createAjaxObj()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			xmlDoc=xmlHttp.responseXML;	
			
			if (xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue <= 2) { var topPadding = "40px"; } else { var topPadding = "100px"; }
			
			// show previous arrow and start div for rest of work 
			var workHTML = '<div id="prevArrow" style="padding-top:' + topPadding + '">'
			
			if (xmlDoc.getElementsByTagName("previous")[0].childNodes[0].nodeValue < 0) {
				workHTML += '<img src="images/arrow_prev_off.gif" title="Previous" />'
			} else {
				workHTML += '<a onclick="moreWork(' + xmlDoc.getElementsByTagName("previous")[0].childNodes[0].nodeValue + ')"><img src="images/arrow_prev.gif" title="Previous" /></a>'
			}
			
			workHTML += '</div><div id="restOfWorkContainer">';
			
			if (xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue > 0) {
				for (var iNode = 0; iNode < xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue; iNode++) {
					var position = iNode + 1
					workHTML += '<div id="otherWork'+position+'" class="otherWork" style="text-align:center;"><a onclick="showWork('+xmlDoc.getElementsByTagName("workId")[iNode].childNodes[0].nodeValue+')" title="'+xmlDoc.getElementsByTagName("title")[iNode].childNodes[0].nodeValue+'">'
					
					if (xmlDoc.getElementsByTagName("type")[iNode].childNodes[0].nodeValue == "Image") {
						workHTML += '<img src="work/images/'+xmlDoc.getElementsByTagName("filePath")[iNode].childNodes[0].nodeValue+'" alt="'+xmlDoc.getElementsByTagName("title")[iNode].childNodes[0].nodeValue+'" width="'+xmlDoc.getElementsByTagName("width")[iNode].childNodes[0].nodeValue+'" height="'+xmlDoc.getElementsByTagName("height")[iNode].childNodes[0].nodeValue+'" />'
					} else {
						workHTML += '<div style="padding-top:9px;"><object type="application/x-shockwave-flash" data="player_small.swf?cPath='+xmlDoc.getElementsByTagName("filePath")[iNode].childNodes[0].nodeValue+'&workId='+xmlDoc.getElementsByTagName("workId")[iNode].childNodes[0].nodeValue+'" width="140" height="86"><param name="movie" value="player_small.swf?cPath='+xmlDoc.getElementsByTagName("filePath")[iNode].childNodes[0].nodeValue+'&workId='+xmlDoc.getElementsByTagName("workId")[iNode].childNodes[0].nodeValue+'" /></object></div>'
					} 	
					
					workHTML += '</a></div>'					
				}
			}
			
			// show next arrow and end div for rest of work ?>
			workHTML += '</div><div id="nextArrow" style="padding-top:' + topPadding + '">'
							
			if (xmlDoc.getElementsByTagName("next")[0].childNodes[0].nodeValue > totalWork) { 
				workHTML += '<img src="images/arrow_next_off.gif" title="Next" />'
			} else {			
				workHTML += '<a onclick="moreWork(' + xmlDoc.getElementsByTagName("next")[0].childNodes[0].nodeValue + ')"><img src="images/arrow_next.gif" title="Next" /></a>'
			}
			workHTML += '</div>'

			document.getElementById("restOfWorkSection").innerHTML= workHTML;
		}		
    }
	var url = "getMoreWork.php?dbPosition=" + dbPosition + "&clientId=" + currentClient + "&type=" + type;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

// Show showreel that has been clicked on
function showShowreel(showreelId) { 
	xmlHttp=createAjaxObj()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			xmlDoc=xmlHttp.responseXML;	
				
			document.getElementById("mainWork").innerHTML='<object type="application/x-shockwave-flash" data="player.swf?cPath='+xmlDoc.getElementsByTagName("filePath")[0].childNodes[0].nodeValue+'" width="332" height="250"><param name="movie" value="player.swf?cPath='+xmlDoc.getElementsByTagName("filePath")[0].childNodes[0].nodeValue+'" /></object><h2 class="mainWorkTitle">'+xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue+'</h2><p class="mainWorkText">'+xmlDoc.getElementsByTagName("text")[0].childNodes[0].nodeValue+'</p>'								
		}
    }	
	var url = "getShowreel.php?id=" + showreelId; 
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}
