var images = new Array( 'http://www.medlegal-la.com/images/01.jpg',
  'http://www.medlegal-la.com/images/02.jpg',
  'http://www.medlegal-la.com/images/03.jpg',
  'http://www.medlegal-la.com/images/04.jpg',
  'http://www.medlegal-la.com/images/05.jpg'
);

var randomnumber = Math.floor(Math.random()*3) + 1;

var nextImage = randomnumber;
var imageWidth = 980;
var imageHeight = 222;
var imageTimeout = 10000;

function getE(e){
 if( e ){ 
   if( typeof(e) == 'string' ){
     return document.getElementById(e);
   }else{
     return e;
   }
 }
 return null;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function strpos (haystack, needle, offset) {
  var i = (haystack + '').indexOf(needle, (offset || 0));
  return i === -1 ? false : i;
}

function setOpacity(el, opacity) {

	//  Which opacity method does this browser support?

	opacity /= 100;

	el.style.opacity = opacity;
	el.style.MozOpacity = opacity;
	el.style.filter = "alpha(opacity=" + (opacity*100) + ")";

}

function fadeImage(el, currentOpacity) {

	currentOpacity += 5;

	if (currentOpacity > 100) {
		setOpacity(el, 100);
		var prevEl = el.previousSibling ? el.previousSibling : el.parentNode.lastChild;
		prevEl.style.visibility = 'hidden';
		el.style.zIndex = 1;
		window.setTimeout(startFading, imageTimeout);
	}
	else {
		setOpacity(el, currentOpacity);
		window.setTimeout(function() { fadeImage(el, currentOpacity); }, 50);
	}

}


function startFading() {

	var el = document.getElementById('image-container').childNodes[nextImage];
	
	if( el ){
  
  	el.style.visibility = 'visible';
  	el.style.zIndex = 2;
  	setOpacity(el, 0);
  	fadeImage(el,0);
  
  	nextImage = (nextImage < images.length-1) ? nextImage + 1 : 0;
	
	}

}


function pageLoad() {

	var el = document.getElementById('image-container');
	
	if( el ){

  	while (el.firstChild) { el.removeChild(el.firstChild); }
  
  	el.style.width = imageWidth + 'px';
  	el.style.height = imageHeight + 'px';
  
  	for(var i=0; i<images.length; i++) {
  
  		var t = document.createElement('IMG');
  		t.setAttribute('src',images[i]);
  		t.setAttribute('width',imageWidth);
  		t.setAttribute('height',imageHeight);
  		t.style.position = 'absolute';
  		t.style.visibility = 'hidden';
  		el.appendChild(t);
  
  	}
  
  	el.childNodes[nextImage-1].style.visibility = 'visible';
  
  	window.setTimeout(startFading, imageTimeout);
	
	}

}

function changeVid(id,src,w,h){

  var obj = getE(id);
 
  if( obj ){
    obj.innerHTML = '<object width="'+w+'" height="'+h+'" id=vidobj><param name="movie" value="'+src+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed swliveconnect="true" name=vidobj src="'+src+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+w+'" height="'+h+'"></embed></object>';
  }

}

var tObj = null;

function testimonial(){

  this.testims     = [];
  this.length      = 0;
  this.curKey      = -1;
  this.timer       = null;
  this.delay       = 25000;
  this.nextButtonD = '<img src="http://www.medlegal-la.com/images/nextButton.png" style="cursor:pointer;" onclick="tObj.next();">';
  this.nextButton  = this.nextButtonD;

  this.init = function(){
  
    var img, thisObj;

    img = new Image(); 
    img.src = "http://www.medlegal-la.com/images/nextButton.png";  
  
    thisObj = this;
    ajax('http://www.medlegal-la.com/testimonials',null,'GET', function(_html){ thisObj.load(_html); } );
    
  };
  
  this.load = function(_html){
  
    var txt, i, ii, iii, len, plen, tlen, obj, page, pages, path, explode, det, e;
    
    e = getE('testbox');
    
    if( e ){
    
      this.testims = []; 
      
      path = window.location.pathname.replace(/\//gi,'');
      page = path.substring(path.lastIndexOf('/') + 1).toLowerCase();  
      
      explode = _html.split('<!--start-->');
      det     = explode[1];
      explode = det.split('<!--stop-->');
      _html   = trim(explode[0]);
      
      explode = trim(_html).split('<!--pages-->');
      
      len = explode.length;
      
      for( i=0; i<len; i++ ){
        det   = trim(explode[i]).split('<!--html-->');
        pages = trim(det[0]).split(',');
        plen  = pages.length;
        //alert(det[0] + ' | len: '+plen);
        for( ii=0; ii<plen; ii++ ){
          //alert( 'THEPAGE: ' + pages[ii] ); 
          if( trim(pages[ii]) == page ){
            testis = trim(det[1]).split('<!--next-->');
            tlen   = testis.length;
            for( iii=0; iii<tlen; iii++ ){
              this.testims.push(trim(testis[iii]));
            }
            break;
          }
        }
      }
      
      this.length = this.testims.length;
      
      this.next();
    
    }
    
  };
  
  this.next = function(){
  
    var e = getE('testbox');
    
    if( e ){
  
      if( this.length > 0 ){
      
        this.length > 1 ? this.nextButton = this.nextButtonD : this.nextButton = '';
      
        this.curKey++;
        
        if( this.curKey == this.length ){
          this.curKey = 0;
        }  
        
        e.innerHTML = this.testims[this.curKey]+'<div style="clear:both;;height:35px;text-align:right;">'+this.nextButton+'</div>';
      
      }
    
    }
    
    this.resetTimer();
  
  };
  
  this.resetTimer = function(){
  
    if( this.timer ){
      clearTimeout(this.timer);
    }
    
    var thisObj = this;
    
    this.timer = setTimeout( function(){ thisObj.next(); }, this.delay );  
  
  };
  
  this.init();

}

function getNodesByTag(el,tag,recurse){

  var e, nodes, node, rnode, inodes, inode, rnodes = [], inodes = [], ilen, len, i, ii;
  
  e = getE(el);
  
  if( e ){
  
    nodes = e.childNodes;
    
    len = nodes.length;
    
    for( i=0; i<len; i++ ){
    
      node = nodes[i];
      
      if( node ){
     
         if( node.tagName == tag ){
         
           rnodes.push(node);
         
         }else if( node.childNodes.length && recurse ){
         
           inodes = getNodesByTag(node,tag,true);
           
           ilen = inodes.length;
           
           for( ii=0; ii<ilen; ii++ ){
          
             inode = inodes[ii];
            
             if( inode ){
           
               if( inode.tagName == tag ){
           
                 rnodes.push(inode);
             
               }
               
             }  
               
           }  
         
         }
      
      }
      
    }  
  
  }
  
  return rnodes;

}

function validateForm(form,msgBox){

  var e, nodes, node, len, i, reg, err, type, val, me;

  e = getE(form);
  
  if( e ){
    
    nodes = getNodesByTag(e,'INPUT',true);
    
    len = nodes.length;
    
    for( i=0; i<len; i++ ){
    
      node = nodes[i];
      
      if( node ){
      
        if( node.tagName == 'INPUT' ){
        
          type = node.type.toLowerCase();
        
          if( type != 'submit' && type != 'hidden' && type != 'button' && type != 'reset' ){
          
            if( strpos(node.className,'required') !== false ){
          
              val = node.value;
          
              if( val ){
            
                switch( node.name.toLowerCase() ){
              
                  case 'visitoremail':
                    reg  = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                    break;
                    
                  case 'visitorphone':
                    reg  = /^([\w\d\-\.\(\)]{2,35})$/i;
                    break;                    
                  
                  default:
                    reg  = /^([\w\d _\-]{2,150})$/i;
                    break;  
              
                }
                
                if( reg.test(val) === false ){
                  err = 'Invalid '+node.name+'.';
                  break;  
                }
              
              }else{
                err = 'All input fields must be filled out.';
                //err = node.name+' must have a value.';
                break;
              }
            
            }
          
          } 
        
        }
      
      }
    
    }
    
    if( err ){
      if( msgBox ){
        me = getE(msgBox);
        if( me ){
          me.innerHTML = err;  
        }
      }
      if( !me ){
        alert(err);
      }
      return err;
    }else{
      e.submit();
    }  
  
  }
  
  return false;

}

window.onload = function(){ pageLoad(); sObj = new slideObj(); tObj = new testimonial(); };
