// -------------------------------------------------------------------
// 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://www.cluntan.com/programs/rssbox/ajax_rss_sticker/lastrss/bridge.php"
var lastrssbridgeurl="programs/rssbox/ajax_rss_sticker/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.link=[], this.description=[], this.pubdate=[] //Arrays to hold each component of an RSS item
this.ajaxobj=createAjaxObj()
document.write('<div id="'+divId+'" class="'+divClass+'" >Initializing ticker...</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
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.title[i]=this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue

//Link needs change to open to _blank:
this.link[i]=this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue
//
this.description[i]=this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue
//this.description[i]= 'aaaaaaaaaaa';
//BEGIN: Cluntan change:
//RSS Item has image link format: [img]http://pub.clibrary.net/upload_files/20090131185155.jpg[/img]
var NewDescription = this.description[i];
//NewDescription = '<img src="http://www.cluntan.com/image/smoke.gif" style="width: 330px; height: 185px;">';
var $Pos1 = NewDescription.indexOf("[img]");
var $Pos2 = NewDescription.indexOf("[/img]");
var $ImageURL = NewDescription.substring($Pos1+5, $Pos2);
//var $ImageURL = 'http://www.cluntan.com/image/smoke.gif';
//NewDescription = $ImageURL;
//change the picture LINK according to path of the Title Link: one is Chinese BBS, another is Chinese cars:
//var carLink = this.link[i].match("www.cluntan.com\/en");
//var posEN = this.link[i].indexOf('en');
//Note the cars links contains a "en": http://www.cluntan.com/en/viewthread.php?tid=469
//But the photonews not: http://www.cluntan.com/bbs/viewthread.php?tid=23113


//http://www.cluntan.com/bbs/viewthread.php?tid=999cluntanvideo
var posCluntanVideo = this.link[i].indexOf('cluntanvideo');
//if (posEN == 23)
if (posCluntanVideo > 0)
{
//remove "cluntanvideo" from LINK:
var oldLink = this.link[i];
this.link[i] = oldLink.replace(/cluntanvideo/, "");


//create the new description (containing the video object code):
var $videoDescription = this.description[i];
var $pos1 = $videoDescription.indexOf("[code]");
var $pos2 = $videoDescription.indexOf("[/code]");
$videoDescription = $videoDescription.substring($pos1+6, $pos2);

//the first 425 does not matter, the width at the end does matter:
//NewDescription = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/TwGcuvMEugA&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/TwGcuvMEugA&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="360" height="300"></embed></object>';

//use my own width and height:
$pos1 = $videoDescription.indexOf("<embed");
$pos2 = $videoDescription.indexOf("width", $pos1);
var $pos3 = $videoDescription.indexOf("height", $pos1);
var $pos4 = $pos2;
//sometimes the height= part is put before width=:
if ($pos3 < $pos2)
{
$pos4 = $pos3;
}

//width="425" height="344"></embed>

//first part:
$FirstPart = $videoDescription.substring(0, $pos4);
$SecondPart = 'width="360" height="285"></embed></object>';

//combine:
NewDescription = $FirstPart + $SecondPart;

//create the final description code for displaying video:
//NewDescription = $videoDescription;

//NewDescription = 'aaaaaaaaaaaa';
}
else
{
	//check if this LINK comes from http://www.cluntan.com/auto/...
	var posAuto = this.link[i].indexOf('auto');

	if (posAuto > 0)
	{//for English AUTO site
		NewDescription = '<a target="_blank" href="' + this.link[i] + '">' + '<img src="' + $ImageURL + '"' + ' style="border: 6px solid; border-color: #cccccc; width: 296px; height: 176px;">' + '</a>';
	}
	else
	{//for Chinese site
		//OLD:
		//NewDescription = '<a target="_blank" href="' + this.link[i] + '">' + '<img src="' + $ImageURL + '"' + ' style="border: 6px solid; border-color: #cccccc; width: 330px; height: 185px;">' + '</a>';		
		NewDescription = '<a target="_blank" href="' + this.link[i] + '">' + '<img src="' + $ImageURL + '"' + ' style="border: 6px solid; border-color: #cccccc; width: 296px; height: 176px;">' + '</a>';
		
	}
}

//NewDescription = 'aaaaaaaaaaaa';
//re-set new data back:
this.description[i] = NewDescription;
//END: Cluntan change:

this.pubdate[i]=this.feeditems[i].getElementsByTagName("pubDate")[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)
//cluntan: change
//var linktitle='<div class="rsstitle"><a href="'+this.link[this.pointer]+'">'+this.title[this.pointer]+'</a></div>'
//var linktitle='<div><small>Today Photo:  <a target="_blank" style="color: #000000; text-decoration: none;" href="'+this.link[this.pointer]+'">'+this.title[this.pointer]+'</a></small></div>'
//var linktitle='<div><small> <a target="_blank" style="color: #000000; text-decoration: none;" href="'+this.link[this.pointer]+'">'+this.title[this.pointer]+'</a></small></div>'
var linktitle='<div><small> <a target="_blank" style="color: #000000; text-decoration: none;" href="'+this.link[this.pointer]+'">'+this.title[this.pointer].substring(0,35)+'</a></small></div>'
var description='<div class="rssdescription">'+this.description[this.pointer]+'</div>'
var feeddate='<div class="rssdate">'+this.pubdate[this.pointer]+'</div>'
if (this.logicswitch.indexOf("description")==-1) description=""
if (this.logicswitch.indexOf("date")==-1) feeddate=""
//cluntan: change
//var tickercontent=linktitle+feeddate+description //STRING FOR FEED CONTENTS 
var tickercontent=description+linktitle+feeddate //STRING FOR FEED CONTENTS 
this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
tickerDiv.innerHTML=tickercontent
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
}
if (fadetype=="up")
this.opacitysetting+=0.2
if (fadetype=="up" && this.opacitysetting>=1)
clearInterval(this[timerid])
}
