//Pop-up

var light_cur = 0;
//Listing photo interval
var light_interval_photo = 3000;
//Listing interval
var light_intervalNext = 30000;


light_gal = new Array();
light_images = new Array();

width_video=400;
height_video=300+24;
width_audio=800;
width_window=jQuery(window).width();

types = {'swf':
           '<div class = "photo_flash" style="width:400px;text-align:center">'+
		   '<embed '+
             'id="plvid" '+
             'name="plvid" '+
             'class="lg" '+
			 'src="$file" '+
            'width="'+width_video+'" '+
			 'height="'+height_video+'" '+
			 'wmode ="opaque" '+
             'allowscriptaccess="always" '+
             'allowfullscreen="true" '+
            '/> '+
		   '</div>',
         'video': 
           '<div class = "vid" style="width:400px;text-align:center">'+
		   '<embed '+
             'id="plvid" '+
             'name="plvid" '+
             'class="lg" '+
			 'src="$path" '+
            'width="'+width_video+'" '+
			 'height="'+height_video+'" '+
			 'wmode ="opaque" '+
             'allowscriptaccess="always" '+
             'allowfullscreen="true" '+
             'flashvars="file=$file&image=$tinsrc"'+
	        '/> '+
			'</div>',
		 "audio":
		   '<div class = "aud" style="width:400px;text-align:center">'+
		   '<embed '+
             'id="plaud" '+
             'name="plaud" '+
			 'class="lg" '+
             'src="$path" '+
             'width="320" '+
             'height="24" '+
			 'wmode ="opaque" '+
             'allowscriptaccess="always" '+
             'allowfullscreen="true" '+
             'flashvars="file=$file&image=$tinsrc" '+
	        '/> '+
			'</div>',
		 'photo':
		   '<div class="photo_lg" style="text-align:center">'+
			  '<a href="$ref" target="_blank">'+
			    '<img style="max-width:'+(width_window-200)+'px" class="lg" src="$file" alt="" title=""/>'+
			  '</a>'+
			'</div>',
		 'mixed': '<div class = "mixed" style="width:400px;text-align:left">$file</div>'
		};

//Add photos/videos/music to mas
//src - big photo/video/music
//tinsrc - little photo
//full_pic - full_picture to open
//alter - description
//namemas - unique identifier for storing data
//flashplayerpath - path to flash player
function lightAdd(src, tinsrc, full_pic, alter, newtype, namemas, flashplayerpath, filename)
{
   if (/\.gif$/i.test(src) ||
      /\.jpg$/i.test(src) ||
	  /\.jpeg$/i.test(src) ||
	  /\.png$/i.test(src)
	 )
  {
	light_images[light_images.length] = new Image();
	light_images[light_images.length - 1].src = src;
	light_images[light_images.length] = new Image();
	light_images[light_images.length - 1].src = tinsrc;  
  }
  
  eval("if (typeof "+namemas+" == 'undefined'){"+namemas+" = new Array();}");
  if (!full_pic) {full_pic = '';}
  
  var reg = /\.swf$/;
  if (reg.test(src))
  {
    newtype = 'swf';
  }
  var htmltemp = types[newtype].replace(/\$file/gi, src).replace(/\$tinsrc/gi, tinsrc).replace(/\$alt/gi, alter).replace(/\$path/gi, flashplayerpath).replace(/\$ref/gi, full_pic);
  if (typeof filename == 'undefined'){filename='';}
  //alert(filename);
  eval(namemas+"["+namemas+".length] = {html:'"+htmltemp+"', text:'"+alter+"', name:'"+filename+"', type: '"+newtype+"'};");
  setCurrentMas(namemas);
};

//Open foto by num and identifier
function open_light(num, namemas, func)
{
  if (namemas)
  {
    setCurrentMas(namemas);
  }
  light_cur = num;
  first_light_image();
  
  change_light_image(func); 
 
  //open_image(true);
}


//this should be executed in the end of all scripts, after all of lightAdd
function light_init(num)
{
  if (!jQuery('.popupContent').length)
  { if (num) light_cur = num;   
       if (!jQuery('.abs').length)
       {
		 newitem = '<div class="shadow" style="display:none;position:absolute"></div>';
		 newitem += '<div class="abs" style="position:absolute">';
		 newitem +=   '<div class="popupContent">';
         newitem +=      '<div class="imageShadowWrap">';
         newitem += 		'<div class="close">';
         newitem +=				'<span class="closebut ie" title="Закрыть"></span>';
         newitem +=			'</div>';
		 newitem += 		'<div class="light_next">';
         newitem +=				'<span class="nextArrow ie" title="Следующая"></span>';
         newitem +=			'</div>';
         newitem +=			'<div class="light_prev">';
         newitem +=			    '<span class="prevArrow ie" title="Предыдущая"></span>';
         newitem +=			'</div>';
         newitem +=			'<table class="imageShadow">';
         newitem +=				'<tr>';
         newitem +=				  '<td class="tlt"><i class="ie"></i></td>';
         newitem +=   			  '<td class="tct"></td>';
         newitem +=				  '<td class="trt"><i></i></td>';
         newitem +=				'<tr>';
         newitem +=				'</tr>';
         newitem +=				  '<td class="tlm"></td>';
         newitem +=				  '<td class="name">'+''+'</td>';
         newitem +=				  '<td class="trm"></td>';
         newitem +=				'</tr>';
         newitem +=				'<tr>';
         newitem +=				  '<td class="tlm"></td>';
         newitem +=				  '<td class="tcm"><div class="lo">'+''+'</div></td>';
         newitem +=				  '<td class="trm"></td>';
         newitem +=				'</tr>';
         newitem +=				'<tr>';
         newitem +=				  '<td class="tlm"></td>';
         newitem +=				  '<td class="text">'+''+'</td>';
         newitem +=				  '<td class="trm"></td>';
         newitem +=				'</tr>';
         newitem +=				'<tr>';
         newitem +=				  '<td class="tlb"></td>';
         newitem +=				  '<td class="tcb"></td>';
         newitem +=				  '<td class="trb"></td>';
         newitem +=				'</tr>';
         newitem +=			'</table>';
         newitem +=		'</div>';
		 newitem +=	  '</div>';
         newitem +=	'</div>';
		 jQuery('body').append(newitem);
	   }
	   
	   jQuery('.shadow').css('opacity','0');
	   jQuery('.shadow, .close').click(function(){
		 jQuery('.abs').fadeOut(100,function(){
			jQuery('.shadow').fadeOut(100,function(){
				//jQuery('.abs').remove();
				//jQuery('.shadow').remove();
			});
		});
		 slide(true);
	   });
	   
	   //в IE 9 ошибки
	  jQuery(window).scroll(function(){
		
		if (jQuery('.abs').css('display')=='block' && !(jQuery.browser.msie && jQuery.browser.version == 9)){
			jQuery('.shadow').css('width',jQuery(document).width()+'px').css('height',jQuery('body').height()+'px').css('top',jQuery('body').css('padding-top'));
			
			if(typeof to_move != 'undefined' && to_move){
				abs_left=Math.round(jQuery(window).width()/2 - jQuery('.abs').width()/2 + jQuery(document).scrollLeft());
				abs_top=Math.round(jQuery(window).height()/2 - jQuery('.abs').height()/2 + parseInt(jQuery('body').css('padding-top'))/2 + jQuery(document).scrollTop());
				if (abs_top<20){abs_top=parseInt(jQuery('body').css('padding-top'))+20;}
				jQuery('.abs').stop();
				jQuery('.abs').animate({top:abs_top, left:abs_left},'20');
			}
			
		}
	  });
	  
	  jQuery(window).resize(function(){
			jQuery(window).scroll();
		});
	  
	  jQuery('.prevArrow').live('click',function()
	  {
		 light_cur*=1;
		 if (light_cur<=0) light_cur = light_gal.length - 1;
		 else light_cur-=1;
         change_light_image();
	    });
	   jQuery('.nextArrow, .imageShadow .tcm .lo').bind('click',function()
	    {
			light_cur*=1;
		    if (light_cur>=light_gal.length - 1) light_cur = 0;
			else light_cur+=1;
			change_light_image();
	    });
		
		first_light_image();
 }
}

function setCurrentMas(namemas)
{
  eval('light_gal = '+namemas);
  light_cur = 0;
}

function first_light_image()
{ 
  jQuery('.imageShadow .tcm .lo').html('');
}

function change_light_image(func)
{ 

  if (light_gal[light_cur].type=='mixed')
  {
    jQuery('.prevArrow, .nextArrow').css('display','none');
	jQuery('.imageShadow .tcm .lo').unbind('click');
  }
  else
  {
    jQuery('.prevArrow, .nextArrow').css('display','block');
	jQuery('.imageShadow .tcm .lo').unbind('click');
	//jQuery('.imageShadow .tcm .lo').bind('click', jQuery('.nextArrow').click());
  }
  jQuery('.imageShadow .tcm .lo').stop();
  jQuery('.imageShadow .tcm .lo').animate({opacity:1},500,function()
  {
  	// !!
	//alert(light_gal[light_cur].name); 

    jQuery('.imageShadow .tcm .lo').html(light_gal[light_cur].html);
	
    jQuery('.imageShadow td.text').html(light_gal[light_cur].text);
	jQuery('.imageShadow td.name').html(light_gal[light_cur].name);
  
	jQuery('.imageShadow .tcm .lo').animate({opacity:1},500,function(){
	  if (func) {func();}
	});
	
  open_image(true);
  
  });
}

function open_image(open)
{
   //jQuery('html').css('overflow','hidden');
   

  if (open)
  {
    var pad_top=parseInt(jQuery('body').css('padding-top'));
	
	
    jQuery(document).scrollTop(0).scrollLeft(0);
    jQuery('.shadow').css('width',jQuery(document).width()+'px').css('height',jQuery('body').height()+'px').css('top',pad_top);
	
	abs_left=Math.round(jQuery(window).width()/2 - jQuery('.abs').width()/2);
	abs_top=Math.round(jQuery(window).height()/2 - jQuery('.abs').height()/2 + pad_top/2);
	
	//if (types[newtype]=='photo'){
		//alert("!");
	//}
	if (abs_top<pad_top){
		abs_top=pad_top+30; to_move=0;}
	else{
		to_move=1;
	}
	
	if (abs_left<0){
		abs_left=30; to_move=0;}
	else{
		to_move=to_move*1;
	}
	
	jQuery('.abs').css('left',abs_left+'px').css('top',abs_top+'px');
	
	if(jQuery.browser.msie && jQuery.browser.version == 9){
		jQuery('.shadow').css('display','block').css('opacity','0.7');
		jQuery('.abs').css('display','block').css('opacity','1');
		//в IE9 дергается изображение
		jQuery(document).scrollTop(0).scrollTop(50);
	}
	else{
		jQuery('.shadow').css('display','block').animate({opacity:0.7},200,function(){
		   jQuery('.abs').css('display','block').animate({opacity:1},200, function(){
		   });
		});
	}
    

	//jQuery('.light_prev').css('margin-top',parseInt(jQuery('.lg').height() / 2 - jQuery('.light_prev span').height()/2 ));
	//jQuery('.light_next').css('margin-top',parseInt(jQuery('.lg').height() / 2) - jQuery('.light_next span').height()/2 );
	if (jQuery('.photo_lg').length>0){
		jQuery('.light_prev').css('margin-top',parseInt(jQuery('.abs').height() / 2 - jQuery('.light_prev span').height()/2 ));
		jQuery('.light_next').css('margin-top',parseInt(jQuery('.abs').height() / 2) - jQuery('.light_next span').height()/2 );
	}
	else{
		jQuery('.light_prev').css('margin-top',parseInt(jQuery('.lg').height() / 2 ));
		jQuery('.light_next').css('margin-top',parseInt(jQuery('.lg').height() / 2));
	}
	
	
	if (jQuery('input[type="file"]').hasClass('error')){
		jQuery('input[type="file"].error').next('.description').after('<div class="error_text">(Неправильный тип или размер файла!)</div>');
	}
	
	jQuery('input[type="file"]').attr('size','1');
	jQuery('input[type="file"]').bind('change',function(){
		if (jQuery(this).next('.file_value').length>0){
			jQuery(this).next('.file_value').remove();
		}
		jQuery(this).after('<div class="file_value">Файл для загрузки - '+jQuery(this).attr('value')+'</div>');
		jQuery(this).parent().children('.error_text').remove();
	})
	/*if(jQuery.browser.msie){
		jQuery('.form-type-file label').css("margin-bottom",'7px');
		jQuery('input[type="file"]').css("display",'none');
		jQuery('.form-type-file label').live('click',function(){
			jQuery(this).next('input[type="file"]').click();
		});
	}*/
  }
}


var c_id = -1;

function slide(stop,rand, namemas)
{
  if (stop) {if (c_id){clearInterval(c_id);} c_id = -1;}
  else
    { 
	  var temp; 
	  if (jQuery('.shadow').css('display') == 'none')
	  {
	    temp = light_cur; 
	  }
	  else
	  {
	    temp = light_cur+1; 
	  }
	  
	  if (namemas)
	  {
	    setCurrentMas(namemas);
		eval('var flag = ('+temp+'<'+namemas+'.length);');
		if (temp>=0 && flag)
		{
		  light_cur = temp;
		}
	  }
	  open_image(true);
      change_light_image();
	  if (rand)
        c_id = setInterval(function(){light_cur = Math.round(Math.random()*light_gal.length);change_light_image();},light_interval_photo);
      else
        c_id = setInterval(function(){jQuery('.nextArrow').click()},light_interval_photo);
	}
}

idlight_monitor = -1;
function light_monitor(flag)
{
   if (idlight_monitor!=-1)
   {
	 clearInterval(idlight_monitor);
	 idlight_monitor = -1;
   }
   slide(true, false);
   if (flag)
   {
     idlight_monitor = setInterval(function()
                           {
                             clearInterval(idlight_monitor);
							 slide(false, false, slider_cur_id);
				           },light_intervalNext);
   }   
};
(function ($) {

Drupal.behaviors.textarea = {
  attach: function (context, settings) {
    $('.form-textarea-wrapper.resizable', context).once('textarea', function () {
      var staticOffset = null;
      var textarea = $(this).addClass('resizable-textarea').find('textarea');
      var grippie = $('<div class="grippie"></div>').mousedown(startDrag);

      grippie.insertAfter(textarea);

      function startDrag(e) {
        staticOffset = textarea.height() - e.pageY;
        textarea.css('opacity', 0.25);
        $(document).mousemove(performDrag).mouseup(endDrag);
        return false;
      }

      function performDrag(e) {
        textarea.height(Math.max(32, staticOffset + e.pageY) + 'px');
        return false;
      }

      function endDrag(e) {
        $(document).unbind('mousemove', performDrag).unbind('mouseup', endDrag);
        textarea.css('opacity', 1);
      }
    });
  }
};

})(jQuery);
;

