// JavaScript Document
function AlbumPhoto(instName, ID, sysimg, basepic, pic_id, baselnk, pr_lnk, album_title, albNum){ 
  this.albName = instName ;
  this.albID = ID ;
  this.sysIMG = sysimg;
//  this.baseProfilepic = basepic ;
  this.pictureSrc  = pic_id; // pic_id = img src ID
  this.baseProfile = baselnk ;
  this.profileHref = pr_lnk;  // pr_lnk = profile anchor id 
  this.albTitle = album_title ;
  
  this.showplaybar = true;
  this.showrefresh = false;
  this.shownick = true;
  this.activelink = true;
  this.albmSize = 125 ;
  this.wtrmrk = false;
  
  this.pic_array = new Array();
  this.profile_array = new Array();
  this.nick_array = new Array();
  this.pic_index = 0;
  
  this.albNum = albNum;
  this.timerID = null;
  
  this.add_pic = function (userArry) { // profile = user numbers relating to the pics
    //this.pic_array[this.pic_index] = this.baseProfilepic + userArry.pic ; // full url to picture
    this.pic_array[this.pic_index] = userArry.pic ; // full url to picture
    this.profile_array[this.pic_index] = this.baseProfile + userArry.lnk ;
    this.nick_array[this.pic_index] = userArry.nick ;
    this.pic_index ++;
  }
  
  this.slide_x = function(x) {
    this.pic_index = x;
    this.setAttr();
  }


  this.pictureSrc = function () {
    return(this.pic_array[this.pic_index]);
  }
  
  this.profileHref = function() {
    return (this.profile_array[this.pic_index]);
  }
  
  this.nickHTML = function () {
    return(this.nick_array[this.pic_index]);
  }
  
  this.infoHTML = function() {
    return (this.info_array[this.pic_index]);
  }  
  
  
  this.setAttr = function() {
    $("#pic_pan" + this.albID).attr("src", this.pictureSrc() ) ;
    if (this.activelink == true ) { $("#pr_lnk" + this.albID).attr("href", this.profileHref() ) ; }
    if (this.shownick   == true ) { $("#nick" + this.albID).html(this.nickHTML() ) ;
                                    $("#nck_lnk" + this.albID).attr("href", this.profileHref() ) ; }
  }

  this.prev = function(pause) {
      if (pause == 1) { this.pause() }
      this.pic_index-- ;
	  this.pic_index = (this.pic_index < 0 ) ? this.pic_array.length -1 : this.pic_index;
	  this.setAttr();
  }
  
  this.next = function(pause)  {
      if (pause == 1) { this.pause() }
      this.pic_index++ ;
	  this.pic_index = (this.pic_index >= this.pic_array.length ) ? 0 : this.pic_index;      
	  this.setAttr();
      if (pause != 1) { this.play() }
  }
  
  this.pause = function() {
       window.clearTimeout(this.timerID);
  }
  
  this.refresh = function () {
       this.pause();
       this.empty();
       getAlbums(this.albNum, 1, this.albID );
  }
  
  this.play = function() {
       this.timerID = window.setTimeout(this.albName + '.next()', 5000);
  }
  
  this.skip = function() {
       if (this.pic_array.length == 0 ) { return ;} 
       this.pause()
       this.pic_array.splice(this.pic_index,1);
       this.profile_array.splice(this.pic_index,1);
       this.nick_array.splice(this.pic_index,1);      
       this.next();
  }

  this.empty = function(){
      this.pic_array.length = 0 ;
      this.profile_array.length = 0 ;
      this.nick_array.length = 0 ;
      this.pic_index = 0 ;
  }
  
  // add method
  this.build = build;
     
}


function build () {
    with (this) {
        var output;
        output = '<div class="info" id="info' + this.albID + '">' + this.albTitle + '</div>' ;
            
        output += '<div class="album" id="album' + this.albID + '" style="width:' + this.albmSize + ';height:' + this.albmSize + ';">'  ;
        
        if (this.activelink == true) {
            output += '<a target="profile" id="pr_lnk' + this.albID + '" href="javascript:return false;" onClick="window.open(this, \'\', \'width=600,height=500,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes\'); return false;">'  ;
        }
        
        output += '<img id="pic_pan' + this.albID + '" src="' + this.sysIMG + '/beige.gif" height="' + this.albmSize + '" width="' + this.albmSize + '" border="0">'  ;
        
        if (this.activelink == true) {
            output += '</a>' ;
        }

        if (this.showplaybar == true) {
            output += '<div class="bwrp" id="bwrp' + this.albID + '">' ;
            output += '<center>'  ;
            output += '<div class="blck"><a href="javascript:return false;" onclick="' + this.albName + '.prev(1);return false;"><img src="' + this.sysIMG + '/rewind.gif" border="0"></a></div>'  ;
            output += '<div class="blck"><a href="javascript:return false;" onclick="' + this.albName + '.pause();return false;"><img src="' + this.sysIMG + '/stop.gif" border="0"></a></div>'  ;
            output += '<div class="blck"><a href="javascript:return false;" onclick="' + this.albName + '.play();return false;"><img src="' + this.sysIMG + '/play.gif" border="0"></a></div>'  ;
            output += '<div class="blck"><a href="javascript:return false;" onclick="' + this.albName + '.next(1);return false;"><img src="' + this.sysIMG + '/forward.gif" border="0"></a></div>'  ;
            if (this.showrefresh == true) {
                output += '<div class="blck"><a href="javascript:return false;" onclick="' + this.albName + '.refresh();return false;"><img src="' + this.sysIMG + '/refresh.gif" border="0"></a></div>'  ;
            }
            output += '</center>'  ;
            output += '</div>'  ;
        }
        
        output += '</div>'  ;
        
        if (this.shownick == true) {
            output += '<a target="profile" class="albm" id="nck_lnk' + this.albID + '" href="#" onClick="window.open(this, \'\', \'width=600,height=500,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes\'); return false;">';
        }
        
        output += '<div class="albm" id="nick' + this.albID + '"></div>' ;
        
        if (this.shownick == true) {
            output += '</a>' ;
        }
        output += '<script type="text/javascript">'  ;
        
        if (this.showplaybar == true) {
            output += '$("#album' + this.albID + '").mouseover(function () {'  ;
            output += '$("#bwrp' + this.albID + '").show();' ;
            output += '});' ;
            output += '$("#album' + this.albID + '").mouseout(function () {' ;
            output += ' $("#bwrp' + this.albID + '").hide();' ;
            output += '});' ;
        }
        output += '$("#pic_pan' + this.albID + '").error(function () {' ;
        output += 'myalbum' + this.albID + '.skip();' ;
        output += '});' ;
        output += '</script>' ;
        
        // add watermark
        if (this.wtrmrk == true) {
            output += '<div id="wtrmrk" style="position:absolute;top:60px;left:-3px;border:0;opacity: 0.3;filter: alpha(opacity=30);"><img src="' + this.sysIMG + '/coveralbum.png" height="50" width="50" onclick="document.location=\'/cgi-bin/system/index_page_steps.pl\'"></div>' ;
        }
        document.write(output);
    }
    
}

// called from albumphoto.tmpl
function getAlbums(albNum,nocache,albID) {
      $.ajax({
		url: '/cgi-bin/system/albumphotoFeed.pl?alb=' + albNum + '&nc=' + nocache,
		type: 'GET',
		dataType: 'html',
		timeout: 3000,
		cache: false,
		error: function(msg , Sts) {
			//alert('Error: '  +  msg + ' ' + Sts);
		},
		success: function(msgStr, Sts){
          if (msgStr)  {
            var myJsonObj = jsonParse(msgStr);
            if ((myJsonObj[0] && !nocache) || (nocache && albID == myalbum1.albID)) {    
                for (var i = 0; i < myJsonObj[0].length; i++) {
                    myalbum1.add_pic(myJsonObj[0][i]); 
                       //alert(myJsonObj[0][i].pic, myJsonObj[0][i].lnk) ;
                }
                myalbum1.slide_x(0) ;
                myalbum1.play();
            }
            if ((myJsonObj[1] && !nocache) || (nocache && albID == myalbum2.albID)) {
                for (var i = 0; i < myJsonObj[1].length; i++) {
                       myalbum2.add_pic(myJsonObj[1][i]);
                       //alert(myJsonObj[1][i].pic, myJsonObj[1][i].lnk) ;
                }
                myalbum2.slide_x(0) ;
                myalbum2.play();
            }
            if ((myJsonObj[2] && !nocache) || (nocache && albID == myalbum3.albID)) {    
                for (var i = 0; i < myJsonObj[2].length; i++) {
                       myalbum3.add_pic(myJsonObj[2][i]); 
                       //alert(myJsonObj[2][i].pic, myJsonObj[2][i].lnk) ;
                }
                myalbum3.slide_x(0) ;
                myalbum3.play();
            }
          }
		}
	  });
}

    

