/*	par = {		type:['NO_FILTER','PIC','VIDEO'],		name:'pics',		albumsPerPg:[10],		picsPerPg:[10],		maxLength:[0], - maksimālais skaits ar bildēm, ko var atzīmēt		showPreview:[true] - rādīt lielo bildi infoboksī uzspiežot uz mazās bildes		onclick:[function( pid ){}],		onchange:[function(){}],		withSelected:[true|false] - rādīt bildes, kuras ir atzīmētas,				albumSelect:[false|true],		albumOnclick:[function(){}]	};*/if( typeof D == 'undefined' ){	var D = {};}D.GalleryApi = function( par ){	D.GalleryApi.inst.push( this );	this.id = D.GalleryApi.inst.length - 1;	this.pids = {};	this.pidsObjects = {};	this.albumSelect = par.albumSelect || false;	this.aids = {};	this.aidsObjects = {};	this.albumType = par.type || 'NO_FILTER';	this.picType = par.type || 'NO_FILTER';	this.uid = par.uid || 0;	if( this.picType == 'VIDEO' ){		this.picType = 'ALL_VIDEO';	}	this.albumsPerPg = par.albumsPerPg || 20;	this.picsPerPg = par.picsPerPg || 20;	this.maxLength = par.maxLength || false;	this.showPreview = ( typeof par.showPreview == 'undefined' ? true : par.showPreview );	this.onclick = par.onclick;	this.onchange = par.onchange;	this.albumOnclick = par.albumOnclick;	this.node = mkE( {		tag:'div',		className:'galleryApi',		els:[			this.picsInput = mkE( {				tag:( $.browser.msie ? '<input name="' + ( par.name || 'pics' ) + '" />' : 'input' ),				prop:{					name:par.name || 'pics',					type:'hidden'				}			} ),			mkE( {				tag:'table',				prop:{					cellSpacing:'0',					style:{						width:'100%'					}				},				els:[					mkE( {						tag:'tbody',						els:[							this.tr = mkE( {								tag:'tr'							} )						]					} )				]			} )		]	} );	this.content = mkE( {		tag:'td',		prop:{			style:{				verticalAlign:'top'			}		},		attr:{			valign:'top'		},		els:[			mkE( {				tag:'img',				prop:{					src:pimg + 'img/load.gif'									}			} )		]	} ).append( this.tr );	if( par.withSelected ){		this.selectedContent = mkE( {			tag:'td',			prop:{				width:'170px',				style:{					verticalAlign:'top'				}			},			attr:{				valign:'top'			},			els:[				mkE( {					tag:'h6',					text:( par.albumSelect ? xGal[ 'api selected albums' ] : xGal[ 'api selected pics' ] )				} )			]		} ).append( this.tr );	}};D.GalleryApi.inst = [];D.GalleryApi.rpc = D.GalleryApi.prototype.rpc = new RPC( '/gallery/rq/app.php' );D.GalleryApi.ALBUM_TYPE = {	NO_FILTER:0,	PIC:1,	VIDEO:2};D.GalleryApi.PIC_TYPE = {	PIC:0,	VIDEO:1,	EMBED:2,	ALL_VIDEO:254,	NO_FILTER:255};D.GalleryApi.prototype.append = function( el ){		if( typeof el == 'string' ){		el = document.getElementById( el );	}	this.node.append( el );	this.getAlbums();};D.GalleryApi.prototype.remove = function(){	this.node.remove();};D.GalleryApi.prototype.getAlbums = function( pg ){	this.albumPg = pg;	var $galleryApi = this;	this.load();	this.rpc.send(		'apiGetAlbums',		{			apiId:this.id,			pg:pg || 1,			count:this.albumsPerPg,			type:D.GalleryApi.ALBUM_TYPE[ this.albumType ] || 0,			uid:this.uid		},		function( re ){			var checkboxs = {};			var ul = mkE( {				tag:'ul',				className:'inlineList galleryAlbumThumbs galleryAlbumThumbs0'			} );			var c = count( $galleryApi.aids );			for(  var k in re.items ){								var item = re.items[ k ];				mkE( {					tag:'li',					els:[						mkE( {							tag:'div',							className:'shadbox small',							els:[								mkE( {									tag:'div',									className:'borders',									els:[										mkE( {											tag:'div',											className:'shpic',											els:[												mkE( {													tag:'img',													prop:{														src:item.urlSmall,														_drItem:item,														onclick:function(){															if( $galleryApi.albumSelect ){																if( $galleryApi.albumOnclick && $galleryApi.albumOnclick.call( this, this._drItem.aid, this._drItem ) === false ){																	return false;																}																//checkboxs[ this._drItem.aid ].checked = ! checkboxs[ this._drItem.aid ].checked																//checkboxs[ this._drItem.aid ].onclick()															} else {																$galleryApi.getPics( this._drItem.aid, 1 );															}														} // onclick													}												} )											]										} ),										mkE( {											tag:'div',											className:'color2 galleryAlbumThumbTitle',											prop:{												innerHTML:item.title + '&nbsp;'											}										} ),										checkboxs[ item.aid ] = mkE( {											tag:'input',											prop:{												_drItem:item,												type:'checkbox',												style:{													display:( $galleryApi.albumSelect ? '' : 'none' )												},												checked:( $galleryApi.aids[ item.aid ] ? true : false ),												defaultChecked:( $galleryApi.aids[ item.aid ] ? true : false ),												disabled:( ! $galleryApi.aids[ item.aid ] && $galleryApi.maxLength !== false && $galleryApi.maxLength == c ),												onclick:function(){													if( this.checked ){														$galleryApi.aidsObjects[ this._drItem.aid ] = this._drItem;														$galleryApi.aids[ this._drItem.aid ] = true;														if( $galleryApi.selectedContent ){															$galleryApi.aids[ this._drItem.aid ] = mkE( {																tag:'div',																className:'galleryApiSelected',																style:{																	backgroundImage:"url('" + this._drItem.urlIcon + "')"																}															} ).append( $galleryApi.selectedContent );														}													} else {														if( typeof $galleryApi.aids[ this._drItem.aid ] == 'object' ){															$galleryApi.aids[ this._drItem.aid ].remove();														}														delete $galleryApi.aids[ this._drItem.aid ];														delete $galleryApi.aidsObjects[ this._drItem.aid ];													}													var c = count( $galleryApi.aids );													for( var k in checkboxs ){														checkboxs[ k ].disabled = ( ! checkboxs[ k ].checked && $galleryApi.maxLength !== false && $galleryApi.maxLength == c );													}													if( $galleryApi.albumOnclick ){																											}												} // onclick											}										} )									]								} ),								mkE( {									tag:'div',									className:'lc'								} ),								mkE( {									tag:'div',									className:'rc'								} )							]						} ) // shadbox					]				} ).append( ul );			} // for			clearNode( $galleryApi.content );			mkE( {				tag:'h6',				text:xGal[ 'api choose album' ]			} ).append( $galleryApi.content );			ul.append( $galleryApi.content );			mkE( {				tag:'div',				prop:{					innerHTML:re.navig				}			} ).append( $galleryApi.content );		}	);};D.GalleryApi.prototype.getPics = function( aid, pg ){	var $galleryApi = this;	this.load();	this.rpc.send(		'apiGetAlbum',		{			apiId:this.id,			aid:aid,			pg:pg || 1,			count:this.picsPerPg,			type:typeof D.GalleryApi.PIC_TYPE[ this.picType ] != 'undefined' ? D.GalleryApi.PIC_TYPE[ this.picType ] : 255		},		function( re ){			var checkboxs = {};			var ul = mkE( {				tag:'ul',				className:'inlineList galleryPicThumbs galleryPicThumbs0'			} );			var c = count( $galleryApi.pids );			var iconClassName;			for(  var k in re.items ){								var item = re.items[ k ];				iconClassName = '';				switch( item.type ){					case 0:						iconClassName = 'videoIcon';						break;					case 1:						iconClassName = 'videoOrangeIcon videoOrangeIconSize';						break;					case 2:						iconClassName = 'videoIcon videoIconSize';						break;				}				mkE( {					tag:'li',					els:[						mkE( {							tag:'div',							className:'shadbox small',							els:[								mkE( {									tag:'div',									className:'borders',									els:[										mkE( {											tag:'div',											className:'shpic',											els:[												mkE( {																										tag:'img',													prop:{														_drItem:item,														src:item.urlSmall,														onclick:function(){															if( $galleryApi.onclick && $galleryApi.onclick.call( this, this._drItem.pid, this._drItem ) === false ){																return false;															}															if( $galleryApi.showPreview && this._drItem.type == 0 ){																return InfoBox.open( '/gallery/pic_only.php?pid=' + this._drItem.pid, { width:720, height:530 } );															}															if( $galleryApi.albumSelect ){																return false;															}															if( ! checkboxs[ this._drItem.pid ].disabled || $galleryApi.maxLength == 1 ){																checkboxs[ this._drItem.pid ].checked = ! checkboxs[ this._drItem.pid ].checked;															}															checkboxs[ this._drItem.pid ].onclick();														}													}												} ),												mkE( {													tag:'div',													className:iconClassName												} )											]										} ),										mkE( {											tag:'div',											els:[												checkboxs[ item.pid ] = mkE( {													tag:'input',													prop:{														_drItem:item,														type:'checkbox',														onclick:function(){															if( $galleryApi.maxLength == 1 ){																for( var k in $galleryApi.pids ){																	var v = $galleryApi.pids[ k ];																	if( typeof v == 'object' ){																		v.remove();																	}																}																$galleryApi.pids = {};																$galleryApi.pidsObjects = {};															} else if( this.disabled ){																return false;															}															if( this.checked ){																$galleryApi.pidsObjects[ this._drItem.pid ] = this._drItem;																$galleryApi.pids[ this._drItem.pid ] = true;																if( $galleryApi.selectedContent ){																	$galleryApi.pids[ this._drItem.pid ] = mkE( {																		tag:'div',																		className:'galleryApiSelected',																		style:{																			backgroundImage:"url('" + this._drItem.urlIcon + "')"																		}																	} ).append( $galleryApi.selectedContent );																}															} else {																if( typeof $galleryApi.pids[ this._drItem.pid ] == 'object' ){																	$galleryApi.pids[ this._drItem.pid ].remove();																}																delete $galleryApi.pids[ this._drItem.pid ];																delete $galleryApi.pidsObjects[ this._drItem.pid ];															}															if( $galleryApi.onchange && $galleryApi.onchange.call( this ) === false ){															}															var c = count( $galleryApi.pids );															for( var k in checkboxs ){																checkboxs[ k ].disabled = ( ! checkboxs[ k ].checked && $galleryApi.maxLength !== false && $galleryApi.maxLength == c );															}															$galleryApi.picsInput.value = array_keys( $galleryApi.pids ).join( ',' );														},														disabled:( ! $galleryApi.pids[ item.pid ] && $galleryApi.maxLength !== false && $galleryApi.maxLength == c ),														defaultChecked:$galleryApi.pids[ item.pid ] ? true : false,														checked:$galleryApi.pids[ item.pid ] ? true : false,														style:{															display:( $galleryApi.maxLength == 1 ? 'none' : ( $galleryApi.albumSelect ? 'none' : '' ) )														}													}												} )											]																					} )									]								} ),								mkE( {									tag:'div',									className:'lc'								} ),								mkE( {									tag:'div',									className:'rc'								} )							]						} ) // shadbox					]				} ).append( ul );			} // for			clearNode( $galleryApi.content );						mkE( {				tag:'h6',				els:[					mkE( {						tag:'a',						text:xGal[ 'Back' ],						className:'leftArrow',						prop:{							href:'#',							onclick:function(){								$galleryApi.getAlbums( $galleryApi.albumPg );							}						}					} ) /*,					mkE( {						tag:'span',						text:htmlspecialchars_decode( re.album.title )					} ) */				]			} ).append( $galleryApi.content );			ul.append( $galleryApi.content );			mkE( {				tag:'div',				prop:{					innerHTML:re.navig				}			} ).append( $galleryApi.content );					}	);};D.GalleryApi.prototype.load = function(){	var position = getStyle( this.node, 'position' );	if( position == 'static' ){		this.node.style.position = 'relative';	}	this.overlay = mkE( {		tag:'div',		className:'rqOverlay',		style:{			height:this.node.offsetHeight + 'px',			width:this.node.offsetWidth + 'px'		}	} ).append( this.content );	if( ! $.browser.msie ){ 		$( this.overlay ).animate( { opacity:0.9 }, 6000 );	}};D.GalleryApi.prototype.value = function( v ){	if( v ){		for( var k in v ){			if( v[ k ].aid ){				this.aids[ v[ k ].aid ] = true;				this.aidsObjects[ v[ k ].aid ] = v[ k ];				if( this.selectedContent ){					this.aids[ v[ k ].aid ] = mkE( {						tag:'div',						className:'galleryApiSelected',						style:{							backgroundImage:"url('" + v[ k ].urlIcon + "')"						}					} ).append( this.selectedContent );				}			}			if( v[ k ].pid ){				this.pids[ v[ k ].pid ] = true;				this.pidsObjects[ v[ k ].pid ] = v[ k ];				if( this.selectedContent ){					this.pids[ v[ k ].pid ] = mkE( {						tag:'div',						className:'galleryApiSelected',						style:{							backgroundImage:"url('" + v[ k ].urlIcon + "')"						}					} ).append( this.selectedContent );				}			}		}	}		if( this.albumSelect ){		return array_keys( this.aids );	}	return array_keys( this.pids );};D.GalleryApi.prototype.valueObj = function(){	if( this.albumSelect ){		return this.aidsObjects;	}	return this.pidsObjects;};D.GalleryApi.prototype.addTextarea = function( textarea, size ){	var url = 'urlLarge';	if( ! size ){		size = 'large';	}	switch( size ){		case 'icon':			url = 'urlIcon';			break;		case 'small':			url = 'urlSmall';			break;		case 'gm':			url = 'urlGM';			break;		case 'middle':			url = 'urlMiddle';			break;	}	var html = '';	D.console.info( this.pidsObjects );	if( ! empty( this.pidsObjects ) ){		html += '<br />';	}	var row;	for( var k in this.pidsObjects ){		row = this.pidsObjects[ k ];		switch( row.type ){			case 0:							html += '<img class="TextAreaImg" src="' + row[ url ] + '" pid="' + row.pid + '" psize="' + size + '" />';				break;			case 1:			case 2:				html += '<img class="TextAreaVideo" src="' + pimg + 'img/new/textarea_video.png" pid="' + row.pid + '" style="background-image:url(\'' + row.urlGM + '\');" pid="' + row.pid + '" /><br />';				break;		}	}	if( ! empty( this.pidsObjects ) ){		html += '<br /><br />';		textarea.insertHTML( html );	}	parent.InfoBox.close();	return false;};