// JavaScript Document
// class AlbumLoad
// code by Azer Manafov (azerman[at]hotmail[dot]com)

function clsAlbumParse( projectfilepath,fn_completion, quality )
{   
	var IMAGE_IMAGE  = 1;
	var IMAGE_SMALL  = 2;
	var IMAGE_MEDIUM = 3;
	
    var m_images     = new Array( );

	var m_xmlpath    = projectfilepath;

	var m_fncompletion = fn_completion;
	var m_header       = '';
	var m_this         = this;
	var m_tm           = new timerAction( tmCheckState, tmCheckState,tmCheckState);
	var m_imQuality    = ( quality != undefined )?quality:'';
	
	
	
	this.activateParser  = function( ) {  xmlparse.parseFile( m_xmlpath, processXmlAlbum); }
	this.getImgList      = function( ) { return m_images;	}
	this.getHeader       = function( ) { return m_header;	}
	
	function loadImages    ( )
	{   m_fncompletion( m_images );
	}
	
	function xmlParseImage   ( imgnode, prop, iKind )
	{   var nodes   = imgnode.childNodes;
	    
		for ( var i = 0; i < nodes.length; i++ )
		{   if ( nodes[i].nodeType == 1)
			{   switch( nodes[i].nodeName )
				{   case 'url':
					var path = visi.trim(nodes[i].childNodes[0].nodeValue," \t\n\r\f\"");
					if ( iKind == IMAGE_IMAGE )
					     prop.imgpth   = path;
					else 
					    if ( iKind == IMAGE_SMALL )
					         prop.thumbpth  = path;
					else     prop.mediumpth = path;
					break;
				}
			}
		}
	}

    function xmlParsePicture( picnode )
	{   var nodes = picnode.childNodes;
        var prop = { img: '',thumbpth:'', mediumpth:'',imgpth:'', en:'', he:'',ru:'', lnk:'' };
		for ( var i = 0; i < nodes.length; i++ )
		{   
		    if ( nodes[i].nodeType == 1)
			{   switch( nodes[i].nodeName )
				{   case 'title':
				        var nd  = nodes[i].childNodes;
						prop.en = '';
                        for ( var j = 0; j < nd.length; j++)
                        {   if ( nd[j].nodeType == 4 )
							     prop.en = visi.trim(nd[j].nodeValue);
                        }
						if ( prop.en  == '') prop.en = visi.trim(nd[0].nodeValue);
                    break;
					case 'russian':  prop.ru = visi.trim(nodes[i].childNodes[0].nodeValue); break;
					case 'hebrew':   
				        var nd  = nodes[i].childNodes;
						prop.he = '';
                        for ( var j = 0; j < nd.length; j++)
                        {   if ( nd[j].nodeType == 4 )
							     prop.he = visi.trim(nd[j].nodeValue);
                        }
						if ( prop.he  == '') prop.he = visi.trim(nd[0].nodeValue);
					break;
					case 'image':        xmlParseImage( nodes[i], prop,IMAGE_IMAGE  );   break;
					case 'mediumimage':  xmlParseImage( nodes[i], prop,IMAGE_MEDIUM ); break;
					case 'smallimage':   xmlParseImage( nodes[i], prop,IMAGE_SMALL  );  break;
					case 'link':         prop.lnk = visi.trim(nodes[i].childNodes[0].nodeValue); break;

				}
			}
		}
		return prop;
	}
    function tmCheckState    ( state,param )
	{
		switch( state )
		{
			case TM_STATE_START: break;
			break;
			case TM_STATE_RUN:
				var ok = true;
				for ( var i = 0; i < m_images.length; i++ )
				{   if ( m_images[i].img.state == 0 )
					{   ok = false;
						break;
					}
				}
			if ( ok == true )
			     m_tm.stoptm( );
			break;
			case TM_STATE_STOP:
			loadImages( );
			break;
		}
	}
	function processXmlAlbum( xml )
	{   var header  = xml.getElementsByTagName( 'header' );
	    var content = xml.getElementsByTagName( "picture" );
        var baseurl = visi.getbaseurl();

	    if ( header.length )
		     m_header = header[0].childNodes[0].nodeValue;

		if ( content.length )
		{   for ( var i = 0; i < content.length; i++ )
			{   if ( content[i].nodeType != 1) continue;
			    var prop = xmlParsePicture( content[i] );

				if ( prop.imgpth || prop.thumbpth || prop.mediumpth )
				{   var ind = m_images.length;
		            m_images[ind]              = prop;
					m_images[ind].img          = new Image();
					m_images[ind].img.state    = 0;
					m_images[ind].img.onload   = function( ) { this.state = 1; };
					m_images[ind].img.onerror  = function( ) { this.state = 2; };

                    var randomnumber = Math.floor(Math.random()*10000)+1;
					
					if ( m_imQuality == '' )
					{   if ( prop.mediumpth )
					         m_images[ind].img.src      = baseurl+prop.mediumpth+"?"+randomnumber;
					    else m_images[ind].img.src      = baseurl+prop.thumbpth +"?"+randomnumber;
					}
					else
					{   if ( m_imQuality == 'thumbs' )
						     m_images[ind].img.src      = baseurl+prop.thumbpth +"?"+randomnumber;
						else m_images[ind].img.src      = baseurl+prop.mediumpth+"?"+randomnumber;	 
					}

				}
			}
			m_tm.setBreakTime( 10000 );
	        m_tm.starttm     ( );
		}
	}
};
function parseXmlAlbumProject( xml )
{   var albums = new Array( );
    var a = xml.getElementsByTagName( "album");
	for ( var i = 0; i < a.length; i++ )
	{   if ( a[i].nodeType != 1) continue;
	    var prop = parseAlbumProperties( a[i]);
			 
	    albums.push( prop );
	}
	return albums;
}
function parseAlbumProperties( field)
{   var prop = { title:'', subtitle: '', description:'', thumburl: '', count: 0, contentxml: '', link: '' }; 
    var chl  = field.childNodes;
	for ( var i = 0; i < chl.length; i++ )
	{   if ( chl[i].nodeType != 1 ) continue;
	    switch( chl[i].nodeName )
		{   case 'title':         prop.title       = visi.trim(chl[i].childNodes[0].nodeValue," \t\n\r\f\"" + String.fromCharCode(8207) + String.fromCharCode(8206)); break;
			case 'subtitle':      prop.subtitle    = visi.trim(chl[i].childNodes[0].nodeValue," \t\n\r\f\"" + String.fromCharCode(8207) + String.fromCharCode(8206)); break;
			case 'description':   prop.description = visi.trim(chl[i].childNodes[0].nodeValue," \t\n\r\f\"" + String.fromCharCode(8207) + String.fromCharCode(8206)); break;
			case 'smallimage':
			    var url = chl[i].getElementsByTagName( 'url');
			    prop.thumburl = visi.trim(url[0].childNodes[0].nodeValue," \t\n\r\f\"" + String.fromCharCode(8207) + String.fromCharCode(8206)); 
				break;
			case 'contentnumber': prop.count      = visi.trim(chl[i].childNodes[0].nodeValue," \t\n\r\f\"" + String.fromCharCode(8207) + String.fromCharCode(8206)); break;
			case 'contentxmlurl': prop.contentxml = visi.trim(chl[i].childNodes[0].nodeValue," \t\n\r\f\"" + String.fromCharCode(8207) + String.fromCharCode(8206)); break;
			case 'link'      :    prop.link       = visi.trim(chl[i].childNodes[0].nodeValue," \t\n\r\f\"" + String.fromCharCode(8207) + String.fromCharCode(8206)); break;
        }
    }
	return prop;
}

