var path="" //path ของโปรแกรมนับจากหน้าที่เรียกใช้ ต้องมี / ปิดท้าย ถ้าเป็น path เดียวกันไม่ต้องกำหนดอะไรเลย

function rssthainews(url, newsdiv, count) {
     this.newsdiv=newsdiv
     this.divcurr='div1_'+newsdiv
     this.divnext='div2_'+newsdiv
     this.url=url
     this.count=count
     this.news=Array ()
     this.req=this.Inint_AJAX()
     var HInstance=this
     this.req.onreadystatechange=function(){HInstance.initialize()}
     this.req.open("POST", path+"rssthai/reader.php", true); //สร้าง connection
     this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     this.req.send("url="+url+"&count="+count); //ส่งค่า
     if (navigator.userAgent.indexOf("MSIE")==-1) { //ถ้าไม่ใช่ IE
          var obj=document.getElementById(newsdiv);
          obj.style.position="relative";
          obj.innerHTML='<div id="div1_'+newsdiv+'" style="position:Absolute;Left:0px;Top:0px;"></div><div id="div2_'+newsdiv+'" style="position:Absolute;Left:0px;Top:0px;"></div>';
     }
}

rssthainews.prototype.initialize=function(){ 
     if (this.req.readyState == 4) {
          if (this.req.status==200) {
               var ret=this.req.responseText //รับค่ากลับมา
               this.news=ret.split(String.fromCharCode(2)) //แยกออกเป็นข่าวต่างๆ
                    if (this.news.length>0) {
                         document.getElementById(this.newsdiv).style.backgroundImage  = "url()"
                         //แสดงข่าวแรก
                         this.displaynews(0)
                         //จับเวลาแสดงข่าวครั้งต่อไป
                         var HInstance=this
                         this.newsinterval=window.setInterval(function(){HInstance.displaynews(HInstance.newsid)}, 10000)
                    }
          }
     }
}
     
rssthainews.prototype.displaynews=function(id) {
     if (id>=this.news.length-1) id=0 //ถึงข่าวสุดท้ายกลับไปแสดงข่าวแรก
     var datas=this.news[id].split(String.fromCharCode(1)) //แยกออกเป็นรายการต่างๆ
     var data="<div class='rss_image'><img src='rssthai/"+path+datas[0]+"' alt='' /></div><div class='rss_detail'><a href='"+datas[1]+"' target='_blank'>"+datas[2]+"</a><br />"+datas[3]+"</div>" //เนื้อหาข่าว
     this.displaynewsdata(data) //แสดงข่าวที่กำหนด
     this.newsid=id+1 //ข่าวต่อไป
}
     
rssthainews.prototype.displaynewsdata=function(val) {
     if (document.getElementById) {
          if (navigator.userAgent.indexOf("MSIE")>-1) { //ถ้าเป็น IE แสดง effect
               //สุ่ม effect
               switch (Math.floor(Math.random()*7)) {
               case 6: filterstring="progid:DXImageTransform.Microsoft.Checkerboard(squaresX=24, squaresY=16, direction='right', duration=1)"; break
               case 5: filterstring="progid:DXImageTransform.Microsoft.Pixelate(duration=2)"; break
               case 4: filterstring="progid:DXImageTransform.Microsoft.Fade(duration=3,overlap=1.0)"; break
               case 3: filterstring="progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.0, Duration=3, Motion=reverse, wipeStyle=1)"; break
               case 2: filterstring="progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.0, Duration=3, wipeStyle=1)"; break
               case 1: filterstring="progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.0, Duration=3, Motion=reverse)"; break
               default: filterstring="progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.0, Duration=3)"; break
               }
               var imgobj=document.getElementById(this.newsdiv)
               if (imgobj.filters && window.createPopup) {
                    imgobj.style.filter=filterstring
                    imgobj.filters[0].Apply()
               }
               imgobj.innerHTML=val
               if (imgobj.filters && window.createPopup) imgobj.filters[0].Play()
          } else { //Browser อื่นๆ แสดง ข่าวด้วย fade Effect
               this.blenddiv(val)
          }
     }
}

//แสดงผล จัดการแสดงผล
rssthainews.prototype.blenddiv=function(val) {
     var newsdiv=this.newsdiv

     //สลับ Layer
     this.divcurr=this.divnext
     this.divnext=(this.divcurr=='div1_'+newsdiv) ? 'div2_'+newsdiv : 'div1_'+newsdiv;

     //ซ่อน Layer divcurr ก่อน
     this.changeOpac(0, this.divcurr);
     document.getElementById(this.divcurr).innerHTML=val;
     document.getElementById(this.divcurr).style.visibility='visible'

     //fade in divcurr และ fade out divnext
     this.fade(0, this.divcurr, this.divnext)
}

//แสดง effect ไม่ใช่ ie
rssthainews.prototype.fade=function(val, divcurr, divnext) {
     this.changeOpac(val, divcurr);
     this.changeOpac(100-val, divnext);
     if (val<=100) {
          var HInstance=this
          HInstance.val=val+5
          HInstance.divcurr=divcurr
          HInstance.divnext=divnext
          setTimeout(function() {HInstance.fade(HInstance.val, HInstance.divcurr, HInstance.divnext)}, 50);
     } else {
          document.getElementById(divnext).style.visibility='hidden'
     }
}

//แสดงข่าวถัดไป +1 ก่อนหน้า -1
rssthainews.prototype.nextnews=function(id) {
     clearInterval(this.newsinterval) //ยกเลิกการแสดงข่าวเมื่อมีการเลือกข่าว
     if (id=='-1') { //แสดงข่าวถอยหลัง
          this.newsid=this.newsid-2
          if (this.newsid==-1) this.newsid=this.news.length-2 //ถอยหลังถึงรายการสุดท้ายกลับไปแสดงข่าวหลังสุด
     }
     this.displaynews(this.newsid)
     //จับเวลาแสดงข่าวครั้งต่อไป
     var HInstance=this
     this.newsinterval=window.setInterval(function(){HInstance.displaynews(HInstance.newsid)}, 10000)
}

//AJAX function
rssthainews.prototype.Inint_AJAX=function() {
     try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
     try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
     try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
     alert("XMLHttpRequest not supported");
     return null;
}

//change the opacity for different browsers
rssthainews.prototype.changeOpac=function(opacity, id) {
     var object=document.getElementById(id).style
     object.opacity=(opacity/100)
     object.MozOpacity=(opacity/100)
     object.KhtmlOpacity=(opacity/100)
     object.filter="alpha(opacity="+opacity+")"
}
     
