function Project(d){
	this.data={};
	this.pixFilter=[];
	if (d){
		if (is_object(d)){
			this.setData(d);
			if (!isNaN(d.id)){
				this.build(d.id);
				}
			}
		else if (!isNaN(d)){
			this.build(d);
			}
		}
	}
Project.prototype={
	
	/* Build
	================================================*/
		build:function(id){
			alert('build '+id);
			},
		
	/* Base
	================================================*/
		getData:function(property){
			if (property){
				return this.data[property];
				}
			else{
				return this.data;
				}
			},
		getPic:function(picId){
			var pix=this.getData('pix');
			return pix[picId];
			},
		setData:function(data,val){
			if (!data){
				return false;
				}
			if (is_object(data)){
				for (var opt in data){
					this.data[opt]=data[opt];
					}
				}
			else{
				this.data[data]=val;
				}
			return this.data;
			},
		isBuilded:function(){
			return !!this.getData('id');
			}
		
	}
	

function ProjectPic(d){
	this.initialize(d);
	}
	ProjectPic.PIX_SIZES={
		1:{
			width:77,
			height:51,
			caption:'Маленькая'
			},
		2:{
			width:150,
			height:100,
			caption:'Средняя'
			},
		3:{
			width:500,
			/* height:300, */
			caption:'Большая'
			}
		}
	ProjectPic.untouchableTypes=['swf','gif'];
	ProjectPic.prototype={
			initialize:function(d){
				this.data={};
				this.container=null
				this.eventsContainers=['wrap','overlay','handler','overhead'];				
				this.size=2;
				if (d){
					if (is_object(d)){
						this.setData(d);
						}
					else if (!isNaN(d)){
						this.setData('id',d);
						}
					}	
				},
		/* Base
		================================================*/
			getData:function(property){
				if (property){
					return this.data[property];
					}
				else{
					return this.data;
					}
				},
			
			setData:function(data,val){
				if (!data){
					return false;
					}
				if (is_object(data)){
					for (var opt in data){
						this.data[opt]=data[opt];
						}
					}
				else{
					this.data[data]=val;
					}
				return this.data;
				},
			isBuilded:function(){
				return !!this.getData('id');
				},
			getRevision:function(){
				return getRevision();
				},
			render:function(size,container){
				if (container){
					container=$(container);					
					}
				else{
					container=this.container;
					}
				if (!container.length){					
					return false;
					}
				var $this=this;				
				if (size){
					this.size=size;
					}
				if (!this.getData('id')){					
					return this.renderEmpty(container);
					}
				var data=$.extend(
					duplicateObject(this.getData()),
					{
						path:getPicturePath(this.data.id),
						size:this.size,						
						addClass:(Number(this.data.is_title)?'pic-title':'')+(!Number(this.data.is_show)?' pic-hide':''),
						width:ProjectPic.PIX_SIZES[$this.size].width,
						height:ProjectPic.PIX_SIZES[$this.size].height||undefined,
						revision:this.getRevision()
						}
					);				
				if (data.height){
					data.heightStr='height="'+data.height+'"';
					}
				if (this.size==3){
					if (data.type=='swf'){
						data.picHtml=$flash(
							{
								src:fetchTemplate('picSrc',data),
								width:data.size_3_width,
								height:data.size_3_height,
								_write:false
								}
							);
						container.html(fetchTemplate('picPreview_swf',data));
						this.picOverlayIframe=$('<iframe frameborder="0"></iframe>')							
							.css(
								{
									width:data.size_3_width+'px',
									height:data.size_3_height+'px'
									}
								)
							.appendTo(container);
						this.picOverlay=$('<div></div>')
							.addClass('pic-overlay')
							.css(
								{
									position:'absolute',
									top:0,
									left:'5px',
									zIndex:1000,
									width:data.size_3_width+'px',
									height:data.size_3_height+'px',
									background:BROWSER.isIE?"url('/s/img/0.gif')":''
									}
								)
							.appendTo(container);
						
							
						}
					else {
						this.picOverlay=this.picOverlayIframe=null;
						if (!data.heightStr && data.size_3_height && Number(data.size_3_height)){
							data.heightStr='height="'+data.size_3_height+'"';
							}
						if (data.size_3_width && Number(data.size_3_width)){
							data.width=data.size_3_width;
							}
						data.src=fetchTemplate('picSrc',data);						
						container.html(fetchTemplate('picPreview',data));
						}
					}
				else{
					data.src=fetchTemplate(
						'picSrc',
						$.extend(
							data,
							{
								type:'jpg'
								}							
							)
						);
					container.html(fetchTemplate('picPreview',data));
					}				
				this.handleRendered();				
				/* this.setContainers();
				this.putEvents();
				if ((isset(this.data.is_show_project) && !Number(this.data.is_show_project)) || (!Number(this.data.is_show) && this.size!=3)){
					this.img.fadeTo('fast',0.3);
					} */
				},
			
			handleRendered:function(container){				
				if (container){
					container=$(container);
					if (container.length){
						this.container=container;
						}
					}
				if (this.setContainers()){
					this.putEvents();
					if (
						(isset(this.data.is_show_project) && !Number(this.data.is_show_project)) ||
						(Number(this.data.is_presentation_project)) ||
						(!Number(this.data.is_show) && this.size!=3)){
						this.img.fadeTo('fast',0.3);
						}
					}
				},
			
			renderEmpty:function(container){
				container.html(fetchTemplate('picPreview_empty',{size:this.size}));
				this.handleRendered();
				},
			setContainers:function(){				
				if (this.container && this.container.length){
					this.img=$('img',this.container);
					this.wrap=$('.pic',this.container);				
					this.over=$('.over',this.container);
					this.handler=$('.pic-handler',this.over);
					this.overhead=$('.h',this.over);
					return true;
					}
				return false;
				},		
			
			putEvents:function(){
				var $this=this;
				if (is_array(this.eventsContainers)){
					$.each(
						this.eventsContainers,
						function(){
							var method='putEvents_'+this;
							if (method_exists($this,method)){
								$this[method]();
								}
							}
						);
					}
				},			
			putEvents_wrap:function(){
				var $this=this;
				this.wrap
					.mouseover(
						function(){
							$(this).addClass('pic-active');
							}
						)
					.mouseout(
						function(){
							$(this).removeClass('pic-active');
							}
						)
					.click(
						function(){
							$function($this.getData('onselect'));
							return false;
							}
						);
				},
			putEvents_overlay:function(){
				var $this=this;
				if (this.picOverlay){
					this.picOverlay
						.click(
							function(){
								$function($this.getData('onselect'));
								return false;
								}
							)
						.css('cursor','pointer')
					}
				},
			putEvents_handler:function(){				
				var $this=this;
				this.handler
					.mousedown(
						function(e){
							e.stopPropagation();
							}
						)
					.mouseup(
						function(e){
							e.stopPropagation();
							}
						)
					.click(
						function(e){
							e.stopPropagation();							
							$function(
								$this.getData('onhandlerclick'),
								$this
								);
							}
						);				
				},
			putEvents_overhead:function(){
				var $this=this;
				this.overhead
					.mousedown(
						 function(e){
							e.stopPropagation();
							var clone=$(getOverClone($this.wrap))
								.html($this.wrap.html())
								.fadeTo('fast',0.5);
							var geometry=$this.getPixPreviewsGeometry();							
						 	startDrag(
								e,this,clone,
								{
									onmove:function(el,left,top){										
										$.each(
											geometry,
											function(){
												if (left>this.left && left<this.right && top>this.top && top<this.bottom){
													this.pic.wrap.addClass('pic-hover');
													//window.AdminPage.status('on '+this.pic.getData('id'));
													}
												else{
													this.pic.wrap.removeClass('pic-hover');
													}
												}
											);
										},
									oncomplete:function(){										
										var order=0;
										//dumpObject(geometry);
										$.each(
											geometry,
											function(){												
												order++;
												if (this.pic.wrap.hasClass('pic-hover')){
													$function(
														$this.getData('onchangeorder'),
														order														
														);
													}
												this.pic.wrap.removeClass('pic-hover');
												}
											);
										unsetChild(clone);
										}
									}
								);
							}
						 );
				},
			remove:function(){
				this.container.remove();
				},
			isDisplaying:function(){
				return (this.container && this.container.length && $displaying(this.container));
				}
		}
		
function getPicturePath(id){
	id=String(id);
	var d1=id.substr(0,1);
	var d2=id.length>1?id.substr(1,1):'0';
	return PATH_FILES+d1+'/'+d2+'/';
	}
