/*
Copyright (c) 2008 Prasanna Kumar Nagasamudram - http://prasplugin.com

  This is a licenced software and is only intended to the person who purchased this software
  Under this licence it is NOT allowed to modify, distribute, sublicense, and/or sell copies of the Software
  If thr above is required one can go for the dev licence.
*/


function prasVideoScroller(divid,options)
{

  this.videoWidth=options.width;
  this.videoHeight = options.height;
  this.delay=options.delay;
  //this.delay=5;
  this.increment = this.savedincrement=1;
  this.url=options.url;
  this.divid=$(divid);
  this.title=options.title;
  
  this.viewPortdiv=$(divid).parentNode;
  //var numHeight = this.viewPortdiv.style.height.replace(/px/,"");
  //numHeight=parseInt(numHeight)+10;
  //this.viewPortdiv.style.height = numHeight+'px';
  
  $(divid+'_'+'title').innerHTML = "<B>"+this.title+"</B>";

  this.tmpid = $(divid+'_'+'tmp');
  this.divid.onmouseover=this.pauseScroll.bind(this);
  this.divid.onmouseout=this.resumeScroll.bind(this);
  
  //this.divid.onmouseout=this.resumeScroll.bind(this);
 // $(divid+'_'+'up').onmouseover=this.downScroll.bind(this);
  //$(divid+'_'+'down').onmouseover=this.upScroll.bind(this);
//  $(divid).onmouseover=this.pauseScroll.bind(this);


 //var button_ids = options.button_ids.split('|');
 
 //var default_catgs = (button_ids[0]).split('_')
 //this.catg = default_catgs[default_catgs.length -1];
 
// for (var i = 0; i < button_ids.length; i++) 
 //{
 //   $(button_ids[i]).onclick=this.setCategory.bind(this);
 //}

  this.loadData();
  var thispointer=this;
 
}


function getCount(str,pat)
{
var count=0;
for (var i=0;i<str.length;i++) {
   if (pat == str.substr(i,pat.length))
     count++;
 }
 return count;
}


prasVideoScroller.prototype = {
  setCategory: function(e)
  {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
      
    var catgs = targ.id.split('_');
    var catg = catgs[catgs.length-1];
    this.catg=catg;
    clearTimeout(this.timer);
    this.loadData();
  },
  loadData: function()
  {
    var thispointer = this;
    
        new Ajax.Request(thispointer.url+"?catg="+this.catg+"&height="+this.videoHeight+"&width="+this.videoWidth, 
        { 
          asynchronous:true, 
          onSuccess: function(transport)
          {
                var data=transport.responseText;
                //alert(getCount(data,"<img"));    
                //alert(data); 
                thispointer.divid.innerHTML = data;
 
                //thispointer.divWidth=thispointer.divid.offsetWidth;
                thispointer.divWidth=getCount(data,"<embed") * 100;
                //alert(thispointer.divWidth);
                thispointer.startScroll();
          }
        }
        
  );
  },
  startScroll: function()
  {
  //alert(this.divid.style.left);
    //this.tmpid.innerHTML = this.divid.style.left+"   "+this.divWidth+" "+this.viewPortdiv.offsetWidth;
    if(parseInt(this.divid.style.left) < this.divWidth * (-1))
    {
      this.divid.style.left = this.viewPortdiv.offsetWidth+"px";
    }
    else if(parseInt(this.divid.style.left) > this.viewPortdiv.offsetWidth)
    {
      this.divid.style.left = ((this.divWidth * (-1))+10)+'px';
    }
    else
    {
      this.divid.style.left = (parseInt(this.divid.style.left) - this.increment) + "px";
    }
    this.timer = setTimeout(this.startScroll.bind(this),this.delay*5);
  },
  pauseScroll: function(e)
  {
    this.savedincrement = this.increment;
    this.increment=0;
  },
  resumeScroll: function()
  {
    this.increment=this.savedincrement;
  },
  downScroll: function()
  {
    this.increment=-1;
  },
  upScroll: function()
  {
    this.increment=1;
  }
  
   
  };   
