// JavaScript Document
// class AlbumLoad
// code by Azer Manafov (azerman[at]hotmail[dot]com)

var EVENT_PROJECT     = 1;
var EVENT_PICT_PREV   = 2;
var EVENT_PICT_NEXT   = 3;
var EVENT_SUBPRO_PREV = 4;
var EVENT_SUBPRO_NEXT = 5;

function clsAlbumLoad4( xmlprofilepath, projectname, placeholder )
{
    var CURR_IMAGE    = 0;
	var CURR_SUBPRO   = 0;
	var SUBPRO_ID     = -1;
	
    var PAGE_BTNS_NUM = 0;
    var SHIFT_NUM     = 1;
    var DEF_BTN_NUM   = 1;

	var m_num_node    = null;
	
    var m_xmlprofilepath  = xmlprofilepath;
	var m_projectname     = visi.trim(projectname);
	var m_placeholder     = placeholder;

/*
// event handler sample
function projectEventHandler(evt,param)
{   switch(evt)
	{   case EVENT_PROJECT:     break;
	    case EVENT_PICT_PREV:   break;
	    case EVENT_PICT_NEXT:   break;
	    case EVENT_SUBPRO_PREV: break;
        case EVENT_SUBPRO_NEXT: break;
    }
}

*/
	var m_prop            = 
	{
		clbkEventHandler: null,
		
        btnPictPrevId   : "picture_left",
        btnPictNextId   : "picture_right",
		ctrlPictTextCell: "picture_num",
		imgPictPrev     : "images/sys/x.gif",
		imgPictNext     : "images/sys/x.gif",
        btnSubproPrevId : "subpro_prev",
        btnSubproNextId : "subpro_next",
		
		borderclr       : "#e09132",
		
		language        : "en",
		defwidth        : 400,
		defheight       : 400,
		
		
		albumtblid      : 'albumTbl'    + (Math.floor(Math.random()*10000)+1),
		normimgid       : 'albumImage'  + (Math.floor(Math.random()*10000)+1),
		descriptid      : 'description1'+ (Math.floor(Math.random()*10000)+1),
		halfwhite       : "images/sys/bg_transp_50wht.png",
		halfblack       : "images/sys/bg_transp_50blk.png",
		thumb           : null
	};

    var m_smallimages = new Array( );
	var m_subprojects = new Array( );
	var m_this        = this;
	var m_parser      = null;
	var m_bigview     = new clsBigImgView( visi, albumNavigate );

	
    this.processAlbumPro = processAlbumPro;
	this.getProperty     = function( key ) { return m_prop[key]; }
	this.setProperties   = function( prop )
	{   for (var i in prop )
			 if ( m_prop[i] != undefined ) 
			      m_prop[i] = prop[i];
		m_bigview.setProperties( prop );
	}
	
	this.subproById      = subproById;
	this.start           = function( ) { if ( m_subprojects.length == 0 ) xmlparse.parseFile( m_xmlprofilepath, processAlbumPro); }
	this.subprjCount     = function( ) { return (m_subprojects.length)?m_subprojects[SUBPRO_ID].length:0;}
	this.subprjId        = function( ) { return (m_subprojects.length)?CURR_SUBPRO:-1;}
	this.createTable     = createTable;

	
	function albumNavigate( dir )// callback from bigimgview 
    {   
        if ( dir < 0 ) 
		     picturePrev();
		else pictureNext();
		doShowBigImage ();
	}
	
	function onThumbLoad   ( )
	{
			var wi = this.width;
			var hi = this.height;

			if ( m_prop.thumb.style.removeAttribute )
			{    m_prop.thumb.style.removeAttribute("height");
				 m_prop.thumb.style.removeAttribute("width");
			}
			else
			{    m_prop.thumb.style.removeProperty ("height");
				 m_prop.thumb.style.removeProperty ("width");
			}
			
			m_prop.thumb.removeAttribute("height");
			m_prop.thumb.removeAttribute("width");
			
				 
			if ( wi > hi )
				 m_prop.thumb.setAttribute( 'width',  wi);
			else m_prop.thumb.setAttribute( 'height', hi);
			
			if ( wi > hi )
				 m_prop.thumb.style.width  = wi +'px';
			else m_prop.thumb.style.height = hi +'px';
	}
	
    function testTable   ( imgs )
	{   CURR_IMAGE    = 0;
	    m_smallimages = imgs;
	    PAGE_BTNS_NUM = Math.min( DEF_BTN_NUM,m_smallimages.length);

        showAlbumImg(0);
		m_num_node.childNodes[0].nodeValue = (CURR_IMAGE+1) + " | " + m_smallimages.length;
	}
	function showAlbumImg ( index )
	{   CURR_IMAGE  = Math.min( m_smallimages.length-1, Math.max( 0,index));
		if ( CURR_IMAGE == m_smallimages.length-1 )
		     m_bigview.changeNavImg( clsBigImgView.BTNNEXT_NONE );
		else m_bigview.changeNavImg( clsBigImgView.BTNNEXT_OPEN );	 
		
		if ( CURR_IMAGE == 0 )
		     m_bigview.changeNavImg( clsBigImgView.BTNPREV_NONE );
		else m_bigview.changeNavImg( clsBigImgView.BTNPREV_OPEN );
	
	    var baseurl = visi.getbaseurl();
		if ( m_smallimages.length == 0 ) return;
		
		if ( m_prop.thumb == null )  m_prop.thumb = visi.getElement( m_prop.normimgid );

		if ( !m_prop.thumb ) return;
        if ( m_smallimages[CURR_IMAGE].img.state != 1 ) return;
        var randomnumber = Math.floor(Math.random()*10000)+1;
		m_prop.thumb.onload  = onThumbLoad;
		m_prop.thumb.onclick = doShowBigImage;
		
		
		if ( m_smallimages[CURR_IMAGE].mediumpth )
		     m_prop.thumb.src    = baseurl+m_smallimages[CURR_IMAGE].mediumpth+"?" + randomnumber;
		else m_prop.thumb.src    = baseurl+m_smallimages[CURR_IMAGE].thumbpth +"?" + randomnumber;
		

		var o = $( m_prop.descriptid );
		if ( o )
        {   
		    if ( m_smallimages[CURR_IMAGE][m_prop.language] == undefined ) m_smallimages[CURR_IMAGE][m_prop.language] = '';
		    if ( m_smallimages[CURR_IMAGE][m_prop.language] == "Photo"   ) m_smallimages[CURR_IMAGE][m_prop.language] = '';

			o.innerHTML = m_smallimages[CURR_IMAGE][m_prop.language];
			
        }
	}
	function createTable( )
	{   var s = '';
		var o =$(m_placeholder);
		if ( !o ) return;
		var width = visi.getStyle(o,'width',0 );
        if ( width.length )
		     width = parseInt(width);
		else width = m_prop.defwidth;
		
		width -= 8;
		
		
			 
		s += ' <table id="' + m_prop.albumtblid + '" width="98%" cellpadding="0" cellspacing="0" border="1" style="padding: 5px; border:0px solid #a39b91; table-layout:fixed;"><tbody>';
		s += '  <tr><td height="1" width="70%" style="width:70%;"></td><td width="30%" style="width:30%;"></td>';
		s += '  <tr>';
		s += '  <td colspan="2" align="center" valign="top">';
		s += '  <img id="' + m_prop.normimgid + '" align="absmiddle" width="' + width + '" src="images/sys/x.gif" alt="album image" style="cursor:pointer;"/>';
		s += '  </td></tr>';
		s += '  <tr>';
		s += '  <td align="left" style=" text-align:left; font-size:12px; color: #373526; font-weight:bolder;">';
		s += '     <div id="' + m_prop.descriptid + '" style="width:95%; background:url(' + m_prop.halfwhite+ ') repeat; margin:5px auto; padding:5px; border: 1px dotted yellow; border-bottom-color:black; border-right-color:black;"> description text </div>';
		s += '  </td>';
		s += '  <td align="left" style="text-align:left; font-size:11px; color: #373526; font-weight:normal;">';
		
        s += '    <table id="' + m_prop.albumtblid + '1" width="100%" align="right" border="0" style="margin:0; padding:0; text-align:right;"><tbody><tr>';
		s += ' 	  <td id="' + m_prop.btnPictPrevId + '" align="right" valign="middle" style=" border-right: 0px solid #373526; cursor:pointer;">';
		s += ' 	  <img src="' + m_prop.imgPictPrev + '" height="12" />';
		s += ' 	  </td>';
		s += ' 	  <td id="' + m_prop.ctrlPictTextCell + '" align="center" style="font-size:12px; border-right: 0px solid #373526; color: #373526; cursor:pointer; font-weight:bold; ">1 | 19</td>';
		s += ' 	  <td id="' + m_prop.btnPictNextId + '" align="left" valign="middle" style=" border-right: 0px solid #373526; cursor:pointer;">';
		s += ' 	  <img src="' + m_prop.imgPictNext  + '" height="12" />';
		s += ' 	  </td></tr>';
		s += ' 	  </tbody> </table>';
		
		s += '  </td> </tr> </tbody></table>';
		var o =$(m_placeholder);
		if (o )
		    o.innerHTML = s;
		
	}
                    
	function doShowBigImage ( )
	{   var randomnumber = Math.floor(Math.random()*10000)+1; // anti cashe
		m_bigview.changeImage( m_smallimages[CURR_IMAGE].imgpth+"?"+randomnumber);
	}
	
	function processAlbumPro( xml )
	{   
	    m_bigview.create( );
		
	    var s       = visi.trim( m_projectname.toLowerCase()," " + String.fromCharCode(8207) + String.fromCharCode(8206));

        SUBPRO_ID   = -1;
		CURR_SUBPRO = 0;
        if ( m_num_node == null ) m_num_node = $(m_prop.ctrlPictTextCell);

		var albset = parseXmlAlbumProject( xml );
		if ( !albset.length ) return;

		m_subprojects.push( new Array( ));
		m_subprojects[0].push(albset[0]);


		for ( var i = 1; i < albset.length; i++ )
		{   var bFound = false;
		    for ( var j = 0; j < m_subprojects.length; j++ )
			{   
			    if ( m_subprojects[j][0].title == albset[i].title )
				{   m_subprojects[j].push(albset[i]);
					bFound = true;
					break;
				}
			}

			if ( bFound == false )
			{   m_subprojects.push( new Array( ));
				m_subprojects[m_subprojects.length-1].push(albset[i]);
			}
		}
		
		for ( var i = 0; i < m_subprojects.length; i++ )
		{   var s1 = visi.trim( m_subprojects[i][0].title.toLowerCase()," " + String.fromCharCode(8207) + String.fromCharCode(8206));

            if ( s1 == s )
			{   SUBPRO_ID = i;
		        m_parser = new clsAlbumParse( m_subprojects[i][0].contentxml,  testTable );
				m_parser.activateParser( );
				break;
			}
		}

		var o = null;
/*
		if ( SUBPRO_ID != -1 )
		{   o = $( m_prop.descriptid );
			if ( o )
				 o.childNodes[0].nodeValue = m_subprojects[SUBPRO_ID][0].description;
		}
*/		
		o = $(m_prop.btnPictPrevId);
		if ( o ) o.onclick = picturePrev;
		o = $(m_prop.btnPictNextId);
        if ( o ) o.onclick = pictureNext;
		
        o = $(m_prop.btnSubproPrevId);
		if ( o ) o.onclick = subproPrev;
        o = $(m_prop.btnSubproNextId);
		if ( o ) o.onclick = subproNext;

		if ( m_prop.clbkEventHandler )
		     m_prop.clbkEventHandler( EVENT_PROJECT );
		
	}
	function picturePrev   ( ) 
	{   pictureById( CURR_IMAGE-SHIFT_NUM); 
		if ( m_prop.clbkEventHandler )
			 m_prop.clbkEventHandler( EVENT_PICT_PREV, CURR_IMAGE);
	}
	
	function pictureNext   ( ) 
	{    pictureById( CURR_IMAGE+SHIFT_NUM); 
		if ( m_prop.clbkEventHandler )
			 m_prop.clbkEventHandler( EVENT_PICT_NEXT, CURR_IMAGE);
	}
	
	function pictureById   ( id )
	{   showAlbumImg( id );
	    m_num_node.childNodes[0].nodeValue = (CURR_IMAGE+1) + " | " + m_smallimages.length;
		return CURR_IMAGE;
	}
	
	function subproPrev    ( ) 
	{   
	    subproById ( CURR_SUBPRO-1 ); 
		if ( m_prop.clbkEventHandler )
			 m_prop.clbkEventHandler( EVENT_SUBPRO_PREV, CURR_SUBPRO);
	}
	function subproNext    ( ) 
	{   
        subproById ( CURR_SUBPRO+1 );
        if ( m_prop.clbkEventHandler )
			 m_prop.clbkEventHandler( EVENT_SUBPRO_NEXT, CURR_SUBPRO);
	}
	
	function subproById    ( id ) 
	{   var curr = CURR_SUBPRO;
	    CURR_SUBPRO = Math.max( 0, id);
	    CURR_SUBPRO = Math.min( m_subprojects[SUBPRO_ID].length-1, CURR_SUBPRO);
		if ( curr == CURR_SUBPRO ) return -1;

        m_parser = new clsAlbumParse( m_subprojects[SUBPRO_ID][CURR_SUBPRO].contentxml, testTable );
		m_parser.activateParser( );
		return CURR_SUBPRO;
	}
	
};


