jQuery(document).ready(function($){

	$.fn.carouselH=function(){
		function resizeModalBackground(){
			setTimeout(function(){
				var modal=$("#carouselModal")
				var bodyHeight=document.body.clientHeight;
				var modalTop=parseInt(modal.css("top"));
				var modalHeight=parseInt(modal.css("height"));					
				modal.next().css("height", Math.max(modalTop+modalHeight+3, bodyHeight)+"px");
				alert([modalTop+modalHeight, bodyHeight])
			},1000)
		}
		$.fn.extend({
			iconClick:function($car){
				if($(this).parent().hasClass('current')) return $(this);
				$car.icons.find('li.current').removeClass('current');
				var n=Number($(this).attr('id').substr(8));
				$car.carouselRotate(n);
				$(this).parent().addClass('current');
				$car.hideArrows();
				return $(this);
			},
			arrowsClick:function($car){
				if($(this).parent().hasClass('next')){
					var next=$car.icons.find('li.current').next();
					if(next.length){
						next.find('a').click();
					} else {
						$car.icons.find('li.carouselLi:first').find('a').click();
					}
				}
				if($(this).parent().hasClass('prev')){
					var prev=$car.icons.find('li.current').prev();
					if(prev.length){
						prev.find('a').click();
					} else {
						$car.icons.find('li.carouselLi:last').find('a').click();
					}
				}
				return this;
			},
			hideArrows:function(){
				if(this.icons.find('li:first').hasClass('current')) this.arrows.find('.prev:visible').hide();
					else this.arrows.find('.prev:hidden').show();
				if(this.icons.find('li:last').hasClass('current')) this.arrows.find('.next:visible').hide();
					else this.arrows.find('.next:hidden').show();
				return this;
			},
			carouselRotate:function(n){
				var $this=this;
				var $newImg=$this.animationBlock.find('li:eq('+n+')');
				var $curImg=$this.animationBlock.find('li.current').fadeOut(500);
				$('#carouselModal img.carouselImg').attr('src',$newImg.find('img').attr('alt'));
				$newImg.css('z-index','2').fadeIn(500,function(){
					$curImg.removeClass('current');
					$newImg.addClass('current').removeAttr('css');
				});
				/*if(n!=0) n='-'+Number(n*100)+'%';
				else{
					if($.browser.msie) n='-0.01%';
				};
				$this.animationBlock.animate({left:n},700);*/
				return $this;
				//alert(n);
			},
			bindEvents:function(){
				var $this=this;
				$this.icons.find('a').click(function(event){
					event.preventDefault();
					$(this).iconClick($this);
				});
				$this.arrows.find('a').click(function(event){
					event.preventDefault();
					$(this).arrowsClick($this);
				});
				/*$('#carouselModal .next').click(function(e){
					e.preventDefault();
					$this.arrows.find('li.next a').click();
					$this.animationBlock.find('img').data('overlay').load();
					
				});
				$('#carouselModal .prev').click(function(e){
					e.preventDefault();
					$this.arrows.find('li.prev a').click();
				});*/
				$(document).keydown(function(e){
					switch (e.keyCode){
						case 37: //left
							$this.arrows.find('li.prev a').click();
							break;
						case 39: //right
							$this.arrows.find('li.next a').click();
							break;
					}
				});
				return $this;
			},
			unbindEvents:function(){
				this.icons.find('a').unbind('click').click(function(event){
					event.preventDefault();
				});
				this.arrows.find('a').unbind('click').click(function(event){
					event.preventDefault();
				});
				return this;
			},
			startAuto:function(){
				var $this=this;
				clearInterval($this.interval);
				$this.interval=setInterval("$('#"+$this.car_id+" ul.arrows li.next a').click()",$this.autoSpeed);
			},
			stopAuto:function(){
				var $this=this;
				clearInterval($this.interval);
			}
		});

		var isFloated=$(this).parent().parent().attr("class").search(/\bf(Right|Left)\b/)!=-1;

		$(this).each(function(){
			var $this=$(this);
			var count=0;
			$this.car_id = 'car_id_'+String(Math.random()).substr(2);
			$this.attr('id',$this.car_id)
			$this.auto=$this.find('.carSetAuto').val();
			$this.autoSpeed=3000;
			//$this.autoSpeed=(13-Number($this.find('.carSetSpeed').val()))*1000;
			$this.animationBlock=$this.find('.carouselInit ul');
			$this.icons=$($this.find('.carouselInit').html()).addClass('smallThumbs');
			$this.icons.find('img').each(function(){
				$(this).wrap('<a href="#" id="thumbId_'+Number(count++)+'"></a>');
			});
			$this.icons.find('img[preview_list]').each(function(){
				$(this).attr('src',$(this).attr('preview_list'));
			});
			$this.animationBlock.find('li:first').addClass('current');
			
						
			if(!$('#carouselModal').length)	$('body').append(
				'<div class="simple_overlay" style="width:auto" id="carouselModal">'+
					'<a class="prev">&lt;</a>'+
					'<a class="next">&gt;</a>'+
					'<img class="progress" src="/css/admin/img/loading_overlay.gif" />'+
					//'<img class="carouselImg" src="'+$this.animationBlock.find('li.current img').attr('src')+'" />'+
				'</div>'
			);
			$this.animationBlock.find('img').each(function(){
				var $i=$(this);
				$i.attr("src",$i.attr(isFloated ? "preview_large" : "alt")).wrap('<a href="'+$(this).attr('alt')+'"></a>');
			});
			$this.animationBlock.find('a').overlay({
				target: '#carouselModal',
				expose: '#f1f1f1',
				speed: 'fast',
				onBeforeLoad:function(){
					if($this.auto) $this.stopAuto();
				}
				,onLoad:function(){
					if($this.auto) $this.stopAuto();
					$('#carouselModal').next('div').addClass('modalFadeBox');
				}
				//,onLoad: resizeModalBackground
				,onClose:function(){
					if($this.auto) $this.startAuto();
				}
			}).gallery({
				speed: 800,
				template: ''
			});
			$('#carouselModal').width('50px');
			$this.find('ul.arrows, ul.smallThumbs').remove();

			$this.icons.find('li:first').addClass('current');
			$this.icons=$this.append($this.icons).find('.smallThumbs');
			$this.arrows=$this.append('<ul class="arrows"><li class="prev"><a href="#"><span>&larr;</span></a></li><li class="next"><a href="#"><span>&rarr;</span></a></li></ul>');
			$this.hideArrows();
			$this.bindEvents();
			if($this.auto){
				$this.startAuto();
				$this .bind('mouseenter',function(){
					$this.stopAuto();
				}).bind('mouseleave',function(){
					$this.startAuto();
				});
			}
		});
	}

	//$('.openNyroModal').nyroModal();
	if ($.datepicker) {
		$.datepicker.setDefaults({
			showOn: 'button',
			buttonImage: 'css/img/calendar.gif',
			buttonImageOnly: true,
			dateFormat: 'dd/mm/yy',
			zIndex:6000
		});

		$('.alternateInput').each(function(){
			if($(this).parents('li').find(':hidden').val()=='') $(this).html('&nbsp;');
			else $(this).text($(this).parents('li').find(':hidden').val());
		});
		$('input.datepicker').datepicker({
			maxDate: '+100y',
			minDate: '-100y' ,
			yearRange: '-80:+80',
			changeYear: true,
			changeMonth: true,
			onSelect: function(){
				$(this).focus();
				$(this).parents('li').find('.alternateInput').text($(this).val());
				$(this).parents('li').find('label.error').remove();
			}
		}).keydown(function(e){e.preventDefault();});
	 }

	//$("input[class*='number{']").each(function(){
		//alert($(this).attr('class'));
	//});
	$.fn.setVideoImage=function($elem){
		$(this).each(function(){
			var $this=$(this);
			//$this.imgOld=$this.find("img");
			//$elem.prepend($this.imgOld.clone().addClass("previewImage"));
			$this.img=$this.find('img:first');
			$this.parent=$this.img.parent();
			if($this.img.height()>$this.parent.height()) $this.img.css({width:'auto',height:'100%'});
			if($this.img.width()>$this.parent.width()){
				$this.img.css({width:'100%', height:'auto'});
				$this.img.css({paddingTop:Math.round(Number($this.img.parent().height()-$this.img.height())/2+1)+'px'});
			}
			$this.img.hide();
			$this.img.bind('load',function(){
				$this.img.show();
				if($this.img.height()>$this.parent.height()) $this.img.css({width:'auto',height:'100%'});
				if($this.img.width()>$this.parent.width()){
					$this.img.css({width:'100%', height:'auto'});
					$this.img.css({paddingTop:Math.round(Number($this.img.parent().height()-$this.img.height())/2+1)+'px'});
				}
			});
		});
		return $(this);
	}

	jQuery.extend({
		filterNumber:function(){
			$('input.filterNumber').blur(function(){
				var curVal=$(this).val();
				var patt=/\D/g;
				var newVal=curVal.replace(patt,"");
				$(this).val(newVal);
			});
		},
		addAnimation: function(){
			$.fn.extend({
				render:function(){
					$this=this;
					$this.wraper.attr('id',$this.hashId);
					var swfUrl=$this.image.attr('alt');
					var so = new SWFObject(swfUrl, $this.hashId, $this.width, $this.height, "8", "#ffffff");
					so.addParam("allowFullScreen", "false");
					so.addParam("wmode","opaque");
					so.write($this.hashId);
					$this.wraper.removeAttr('id');
					//$this.append('<img src="" alt="'+swfUrl+'" style="display:none;" />');
				}
			});
			$('.animationSet').each(function(){
				var $this=$(this).addClass('animationContainer').removeClass('animationSet');
				$this.hashId='anim_id'+String(Math.random()).substr(2);
				$this.mode=0;
				$this.width=vPlayerDefW;
				$this.height=vPlayerDefH;
				$this.iWidth=vPlayerDefW;
				$this.iHeight=vPlayerDefH;
				$this.image=$this.find('img').addClass('hidden');
				$this.imageUrl='';
				if($this.find('.animSetMode').length)  $this.mode=Number($this.find('.animSetMode').val());
				if($this.find('.animSetWidth').length)  $this.width=$this.find('.animSetWidth').val();
				if($this.find('.animSetHeight').length)  $this.height=$this.find('.animSetHeight').val();
				if($this.find('.animSetAltWidth').length)  $this.iWidth=$this.find('.animSetAltWidth').val();
				if($this.find('.animSetAltHeight').length)  $this.iHeight=$this.find('.animSetAltHeight').val();
				if($this.find('.animSetImage').length)  $this.imageUrl=$this.find('.animSetImage').val();
				if($this.mode){
					$this.image.wrap('<a href="#"></a>');
					$this.modal=$this.image.parent('a');
					if($this.imageUrl==''){
						$this.css({width:$this.iWidth+'px',height:$this.iHeight+'px'});
						$this.modal.css({width:$this.iWidth+'px',height:$this.iHeight+'px'}).addClass('playAnimation').show()
					}else{
						$this.image.attr('src',$this.imageUrl).css({width:$this.iWidth+'px',height:$this.iHeight+'px'}).show();
					}
					
					if(!$('#animationModal'+$this.hashId).length)	$('body').append(
						'<div class="simple_overlay" id="animationModal'+$this.hashId+'">'+
						'</div>'
					);
					$this.wraper=$('<div class="animationWrap"></div>').css({width:$this.width+'px'}).prependTo($('#animationModal'+$this.hashId).css({width:$this.width+'px'}));
					$this.modal.overlay({
						target: '#animationModal'+$this.hashId,
						expose: '#f1f1f1',
						onBeforeLoad: function(){
							$this.render();
						}
					});
				}else{
					$this.css({width:$this.width+'px',height:$this.height+'px'});
					$this.wraper=$('<div class="animationWrap"></div>').prependTo($this);
					if($this.imageUrl==''){
						$this.render();
					}else{
						$this.wraper.append('<a href=""><img src="'+$this.imageUrl+'" style="width:'+$this.width+'px; height:'+$this.height+'px; display:inline;" /></a>');
						$this.wraper.find('a').click(function(event){
							event.preventDefault();
							$this.render();
						});
					}
				}
			});
		},
		addMedia: function(options){
			/*var settings={
				newW:vPlayerDefW,
				newH:vPlayerDefH
			};
			if(options){$.extend(settings, options);}*/
			if ($.fn.media) {
				$.fn.media.defaults.flvPlayer = baseUrl + '/swf/mediaplayer.swf';
				$.fn.media.defaults.mp3Player = baseUrl + '/swf/mediaplayer.swf';
				$(".cms-link-img").each(function(){
					/*height for mp3*/
					var md=this.className.match(/\{.*\}/);
					md=(md&&md.length) ? md[0] : "";
					var mdHash=md.charAt("0")=="{"?eval("("+md+")"):{};
					
					var mediaW=vPlayerDefW, mediaH=vPlayerDefH;

					if($(this).hasClass('mp3')){
						mediaH=35;
					}else{
						if(mdHash.videoW!=0) mediaW=mdHash.videoW;
						if(mdHash.videoW!=0) mediaH=mdHash.videoH;
					}
					var alignClass='';
					if($(this).hasClass('fRight')) alignClass='fRight ';
					if($(this).hasClass('fLeft')) alignClass='fLeft ';

					var d=$('<div class="'+alignClass+'mediaContainer" href="'+this.href+'"><a href="javascript:;" class="mediaLink" style="position:absolute; left:0; top:0;"></a></div>');
					d.addClass(md).width(mediaW+"px").height(mediaH+"px");
					if($(this).find("img").length && ($(this).find("img").attr('src')!='')){
						//d.css("backgroundImage","url('"+$(this).find("img").attr('src')+"')");
						d.prepend($(this).find("img").clone().addClass("previewImage"));
						d.setVideoImage();
						$(this).next('.hiddenImg').remove();
						$(this).after('<img class="hiddenImg" style="display:none;" src="'+$(this).find("img").attr('src')+'" />')
					}else d.css("background","#000");


					//d.width(mediaW+"px").height(mediaH+"px");
					$(this).replaceWith(d);

					if(mdHash.modal){
						d.hide().before('<div class="videoModalBox '+md+'" style="width:'+mdHash.inlineW+'px; height:'+mdHash.inlineH+'px; overflow:hidden;"><a href="#" class="openVideoModal mediaLink" style="display:block; width:'+mdHash.inlineW+'px; height:'+mdHash.inlineH+'px;"></a></div>');
						if(d.next('img').length && (d.next('img').attr('src')!='')) d.prev('div').css('background-image','url('+d.next('img').attr('src')+')');
							else d.prev('div').css('background-image','none');
						
						if(!$('#videoModal').length)	$('body').append(
							'<div class="simple_overlay" style="width:auto; padding:12px" id="videoModal">'+
								'<div class="videoModalContent"></div>'+
							'</div>'
						);
						content=d.clone(true).show();
						$('#videoModal .videoModalContent').html(content);
						d.prev('div').find('.openVideoModal').overlay({
							target: '#videoModal',
							expose: '#f1f1f1',
							closeOnClick: false,
							onLoad: function(){
								var d=$('#videoModal .mediaContainer');
								d.click(function(event){
									event.preventDefault();
									clickEvent($(this));
								});
								if(mdHash.autostart) d.click();
							}
						});
					}
					var clickEvent=function(div){
						//var div=$(this);
						var m=div.metadata();
						var	useCustomControls=false;
						var madiaHref=div.attr('href');
						if(useCustomControls){
							div.after(
								'<ul class="mediaControls" style="width:'+mediaW+'px">'+
									'<li class="mediaPlay"><a href="#">Play</a></li>'+
									'<li class="mediaPause"><a href="#">Pause</a></li>'+
									'<li class="mediaStop"><a href="#">Stop</a></li>'+
									'<li class="mediaScroll"><a href="#"><span><!--Scroll--></span></a></li>'+
								'</ul>'
							);
							$('.mediaControls a').click(function(event){
								event.preventDefault();
								var media=$(this).parents('ul.mediaControls').prev('.mediaContainer').find('embed').get(0);
								if($(this).parent('li').hasClass('mediaPlay')) media.startPlayback();
								if($(this).parent('li').hasClass('mediaPause')) media.pausePlayback();
								if($(this).parent('li').hasClass('mediaStop')) media.stopPlayback();
								if($(this).parent('li').hasClass('mediaScroll')){
									var pers=(event.clientX-$(this).offset().left)/$(this).width();
									$(this).find('span').width(pers*100+'%');
									media.seek(pers);
								}

							});
						}
						var controlH=0;
						if(div.next('.mediaControls').length) controlH=Number(div.next('.mediaControls').height());
						var options={
							width:mediaW,
							height:mediaH-controlH,
							attrs:{style:'width:'+mediaW+';height:'+Number(mediaH-controlH)+';'}
						};

						$.extend(options,div.metadata());
						var $ddd=div.parent()
						div.media(options);
						$ddd.find('.mediaContainer').attr('href',madiaHref).height(mediaH+"px");
						var tmp=$ddd.find('embed').attr('name');
						mp1proxy[tmp]=$ddd.find('embed').attr('name');
					}
					d.click(function(event){
						event.preventDefault();
						clickEvent($(this));
					});
					if(mdHash.autostart && !mdHash.modal) d.click();
				});
			}
		},
		addCarousel: function(options){
			$('.carouselContainer').each(function(){
				var $t=$(this);
				if($t.find('.toDrop').length) return $t;
				var type="linear";
				var align="0";
				var repeat="true";
				var auto="true";
				var space="0";
				var direction="h-l";
				var background;
				var previewImg="true";
				var width=300;
				var height=200;
				var borderSize=0;
				var borderColor=0xff9099AE;
				
				if($t.find('.carSetWidth').length) width=$t.find('.carSetWidth').val();
				if($t.find('.carSetHeight').length) height=$t.find('.carSetHeight').val();

                var group_id = String(Math.random()).substr(2);
				
				$t.find('.carouselInit').attr('id','carouselContent').width(width+'px').height(height+'px');
				if($t.parent('.carouselWrapper').length) $t.parent('.carouselWrapper').width(width+'px').height(height+'px');
				var images = new Array();
				$t.find('.carouselUl img').each(function(){
					if($(this).attr('src')==$(this).attr('alt')) images.push({url:$(this).attr('src'),hash:group_id});
					else images.push({url:$(this).attr('src'),link:$(this).attr('alt'),hash:group_id});
				});
				if($t.find('.carSetType').length) type=$t.find('.carSetType').val();
				if($t.find('.carSetAlign').length) align=$t.find('.carSetAlign').val();
				if($t.find('.carSetRepeat').length) repeat=$t.find('.carSetRepeat').val();
				if($t.find('.carSetAuto').length) auto=$t.find('.carSetAuto').val();
				if($t.find('.carSetSpace').length) space=$t.find('.carSetSpace').val();
				if($t.find('.carSetDirection').length) direction=$t.find('.carSetDirection').val();
				if($t.find('.carSetPreview').length) previewImg=$t.find('.carSetPreview').val();
				if($t.find('.carSetBg').length){
					background=$t.find('.carSetBg').val();
					if(String(background).length!=6){
						background="transparent";
						$t.find('.carSetBg').val('');
					}
				}
				var bigCarousel=false;
				if(type=='round4'){
					type='withThumbs';
					width=700;
					$t.parent('.carouselWrapper').width(width+'px').find('.carouselInit').width(width+'px');
					bigCarousel=true;
					auto=0;
				}
				if(type=='wagram'){
					type='withThumbs';
					height=232;
					$t.parent('.carouselWrapper').height(height+'px').find('.carouselInit').height(height+'px');
				}
				switch (type){
					case 'wagram':
						space="8";
						var $carWrapper = $t.parent('div.carouselWrapper').addClass('wagram-carousel');
						
						$carWrapper.find('.currentImage').remove();
						$t.prepend('<div class="currentImage"></div>');
						
						if ($carWrapper.length) {
							$carWrapper.find('div.ui-resizable-handle').remove();
							$carWrapper.height(312+'px');
						}
						
						if(!$('#carouselModal').length)	$('body').append(
							'<div class="simple_overlay" style="width:auto" id="carouselModal">'+
								'<img class="carouselImg" src="'+$carWrapper.find('ul.carouselUl li:first img').attr('src')+'" />'+
							'</div>'
						);
						var so = new SWFObject("/swf/carousel.swf", "group_"+group_id, width+'px', '74px', "8", background);
						so.addVariable("borderSize", borderSize);
						so.addVariable("borderColor", borderColor);
						$carWrapper.find('ul.carouselUl li:first img')
							.clone()
							.appendTo($carWrapper.find('div.currentImage'))
							.addClass('style1')
							.css({height:'224px', width: width+'px'})
							.overlay({
								target: '#carouselModal',
								expose: '#f1f1f1'
						});
						if(bigCarousel){
							$carWrapper.height(height+'px').addClass('bigCarousel');
							$carWrapper.find('.carouselInit').height('auto')
							$carWrapper.find('div.currentImage').css({height:Number(height-74)+'px', width: 'auto'});
						}
						
						break;
					case 'withThumbs':
						$t.find('p, div.preventDrag').remove();
						$t.parent('.carouselWrapper').height('auto');
						$t.parent('.carouselWrapper').addClass('carouselWithThumbs').find('ul.carouselUl').clone().attr('class','').appendTo($('#carouselContent'));
						$('#carouselContent').removeAttr('id');
						
						$t.carouselH();
						if(width<335){
							$t.find('div.carouselInit ul img[preview_large]').each(function(){
								$(this).attr('src',$(this).attr('preview_large'));
							});
						}

						return $(this);
						break;
					/*case 'pikachoose':
						$t.find('.carouselInit p').remove();
						$t.find('.carouselUl').clone('false').removeClass('carouselUl').attr('id','group_'+group_id).appendTo($('#carouselContent')).find('.dragHandle').remove();
						$('#carouselContent').removeAttr('id').parent().addClass('pikaCarousel').parent('.carouselWrapper').addClass('pikaWrapper').height('auto').width('auto');
						var newSpeed=Number($t.find('.carSetSpeed').val())*800+2200;
						jQuery5('#group_'+group_id).PikaChoose({
							carousel:true,
							transition:[-1],
							speed:newSpeed
						});
						$t.find('div.jcarousel-clip-horizontal').width(width+'px');
						return $(this);
						break;*/
					case 'fading':
						var fadingId='fading_id'+String(Math.random()).substr(2);
						$('#carouselContent p').remove();
						$(this).find('.carouselUl').clone('false').height('').removeClass('carouselUl').addClass('fadingCarousel').appendTo($('#carouselContent')).find('.dragHandle').remove();
						var  $fadingCarousel=$('#carouselContent .fadingCarousel');
						$('#carouselContent').css('overflow', 'hidden');
						if(background=="transparent") background="fff";
						$('#carouselContent, #carouselContent li').css('background','#'+background).removeAttr('id');
						$fadingCarousel.attr('id',fadingId).find('li.carouselLi:not(:first)').hide();
						$fadingCarousel.find('img').each(function(){
							var $Img=$(this);
							if($Img.attr('alt')!=$Img.attr('src')) $Img.wrap('<a href="'+$Img.attr('alt')+'" target="_blank" style="border:none; margin:0; padding:0;"></a>');
						});
						var fadeInt=setTimeout('fadeImages("'+fadingId+'")',3000);
						return $(this);
						break;
					case 'linear':
						var so = new SWFObject("/swf/carousel.swf", "group_"+group_id, width+'px', height+'px', "8", background);
						space="5";
						break;
					case 'diagonal':
						var so = new SWFObject("/js/libs/coverflow/test.swf", "group_"+group_id, width+'px', height+'px', "8", background);
						so.addVariable("preview", previewImg);
						break;
					case 'diaporama':
						var so = new SWFObject("/js/libs/coverflow/test.swf", "group_"+group_id, width+'px', height+'px', "8", background);
						so.addVariable("preview", previewImg);
						break;
					default:
						var so = new SWFObject("/js/libs/coverflow/test.swf", "group_"+group_id, width+'px', height+'px', "8", background);
						so.addVariable("type", type);
						so.addVariable("camera","{z:0,y:-200}");
				}
				/*if(type=='linear'){
					var so = new SWFObject("/swf/carousel.swf", "group_"+group_id, "100%", "100%", "8", background);
				}else{
					var so = new SWFObject("/js/libs/coverflow/test.swf", "group_"+group_id, "100%", "100%", "8", background);
					so.addVariable("type", type);
					so.addVariable("camera","{z:0,y:0:y:-200}");
				}*/
				so.addVariable("speed", $(this).find('.carSetSpeed').val());
				so.addVariable("images", escape($.toJSON(images)));
				so.addVariable("align", align);
				so.addVariable("space", space);
				so.addVariable("direction", direction);
				so.addVariable("repeat", repeat);
				so.addVariable("auto", auto);
				so.addVariable("group", "group_"+group_id);
				if (type == 'diaporama') {
					so.addVariable("type", "diaporama");
					so.addVariable("arrow_y", "0.3");
				}
				so.addParam("allowFullScreen", "false");
				if(background=="transparent") so.addParam("wmode","transparent");
					else so.addParam("wmode","opaque");
				so.write("carouselContent");
				$('#carouselContent').removeAttr('id');
				$t.hide();
				$t.fadeIn(300);
			});
		},
		addModal: function($element){
			//alert($(this).html());
			$('.imgWrapper').each(function(){
				if(!$(this).hasClass('openNyroModal') && $(this).attr('href')=='') $(this).unbind('click').click(function(e){ e.preventDefault(); });
				//else $(this).unbind('click');
			});
			if($('.openNyroModal img').length){
				if($element==undefined) $element=$('a.openNyroModal');
				if($element.length==0) $element=$('a.openNyroModal');
				if($('div.cms[id]:has(a.openNyroModal)').length>1){
					var arr=[];
					$('div.cms:has(a.openNyroModal)').each(function(){
						arr.push($(this).attr('id'));
					});
					for(i in arr){
						if(!$('#newGalery_'+arr[i]).length)	$('body').append(
								'<div class="simple_overlay" id="newGalery_'+arr[i]+'">'+
									'<a class="prev">'+translate('Prev')+'</a>'+
									'<a class="next">'+translate('Next')+'</a>'+
									'<div class="info"></div>'+
									'<img class="progress" src="/css/admin/img/loading_overlay.gif" />'+
								'</div>'
						);
						//alert('#'+arr[i]+' a.openNyroModal');
						$('#'+arr[i]+' a.openNyroModal').overlay({
							target: '#newGalery_'+arr[i],
							expose: '#f1f1f1'
						}).gallery({
							speed: 800,
							template: '<span>'+translate('Image ${index} of ${total}')+'</span>'
						});
					}
				}else{
					if(!$('#newGalery').length){
							$('body').append(
							'<div class="simple_overlay" id="newGalery">'+
								'<a class="prev">'+translate('Prev')+'</a>'+
								'<a class="next">'+translate('Next')+'</a>'+
								'<div class="info"></div>'+
								'<img class="progress" src="/css/admin/img/loading_overlay.gif" />'+
							'</div>'
						);
					};
					if($('#mainContentTabs').length) $element.overlay({
							target: '#newGalery',
							expose: '#f1f1f1'
						});
					else $element.overlay({
							target: '#newGalery',
							expose: '#f1f1f1'
						}).gallery({
							speed: 800,
							template: '<span>'+translate('Image ${index} of ${total}')+'</span>'
						});
				}
			}
		},
		proxiColorPicker: function(){
			$('.proxiColorPicker:not(".hasProxiColorPicker")').each(function(){
				var colors=['ffffff','000000','ff0000','00ff00','0000ff','ffff00','ff00ff','00ffff'];
				var $input=$(this);
				$input.after('<ul class="proxiColors"><li title="transparent" class="transparentColor">X</li></ul>');
				var $picker=$input.next('.proxiColors')
				for(i in colors){
					$picker.append('<li title="'+colors[i]+'" style="background:#'+colors[i]+'"></li>');
				}
				$picker.find('li').hover(
					function(){
						$(this).addClass('hoverColor');
					},
					function(){
						$(this).removeClass('hoverColor');
					}
				).click(function(){
					if($(this).hasClass('transparentColor')) $input.val('');
						else $input.val($(this).attr('title'));
				});
				$input.addClass('hasProxiColorPicker');
			});
		}
	});
	$.addMedia();
	$.addCarousel();
	$.addModal($('a.openNyroModal'));
	$.addAnimation();
	$.proxiColorPicker();
	$.filterNumber();
});

var vPlayerDefW=300, vPlayerDefH=160,vPlayerCtrlsH=50;

var mp1proxy = {};

function showItem(items) {
	$(function(){
		if($.evalJSON(items).link!=undefined && $.evalJSON(items).link!=''){
			window.open($.evalJSON(items).link,'_blank');
		}else{
			if(!$('#carouselModal').length)	$('body').append(
				'<div class="simple_overlay" style="width:auto" id="carouselModal">'+
				'<img class="carouselImg" src="" />'+
				'</div>'
			);
			$('#carouselModal .carouselImg').attr('src',$.evalJSON(items).url);
			var img= $('<img src="'+$.evalJSON(items).url+'" />');
			img.overlay({
				target: '#carouselModal',
				expose: '#f1f1f1'
			});
			img.click();
		}
	});
}

function showCarouselItem(items){
	$(function(){
	
		var obj = $.evalJSON(items)
		
		var $cc = $("#group_"+obj.hash).parents('div.carouselContainer');

		if ($cc.find("div.currentImage").length == 0){
			$cc.prepend('<div class="currentImage" style="text-align: center; border: 4px solid #9b978f;"></div>');
		}
		var $curus = $cc.find('div.currentImage');	
		if ($curus.find('img').attr('src') == obj.url) return;
		$curus
			.find('img:first')
			//.css({position:'relative', margin-top: '0 auto';})
			.fadeOut('fast', function(){
				$curus.find('img:first').remove();
				$curus.append('<img src="'+obj.url+'" alt="" class="style1" style="height: 224px; width: auto;" />');
				$('#carouselModal img').attr('src',obj.url);
				$curus.find('img:first').overlay({
					target: '#carouselModal',
					expose: '#f1f1f1'
				});
			});
	});
}

/*
function showImage(items){
	$( function () {
		showCarouselItem(items);
	});
}
*/

function carouselClick(n){
	return false;
}

function fadeImages(id){
	var $this=$('#'+id);
	var $cur=$this.find('.carouselLi:visible');
	var $next;
	if($cur.next('.carouselLi').length) $next=$cur.next('.carouselLi');
	else  $next=$this.find('.carouselLi:first');
	$cur.addClass('static');
	$next.fadeIn(1000,function(){
		$cur.hide().removeClass('static');
		var fadeInt=setTimeout('fadeImages("'+id+'")',3000);
	});
}

var playerSkin='/swf/proxi_skin.swf';

