$(document).ready(		
	function()
	{
		$("a.ext").each(function () {
			$(this).attr("target","_blank");
		});

		/*	
		$('.mainnav li').mouseenter(
		  function(event)
		  {
			$(this).addClass('active');
		  }
		);
		$('.mainnav li').mouseleave(
		  function(event)
		  {
			$(this).removeClass('active');
		  }
		);
		*/
		
		
		if($('.box.basket .items li').length < 3) {
			$('.basket .sbutton').hide();
			$('.basket .emptyText').show();
		}
		
		$('.basket li a[rel]').each(function() {
			$('.section-link a[rel^='+this.rel+']').next('.infobasket').addClass('disabled')
		})
		
		var AjaxRequestURL = '/media/internal_media/resources/php/InfoBasket.php';
		$('.section-link .infobasket').live('click', 
			function() {
			
				var link = $(this).prev('a.downloadlink');
				var path = link.attr('href');
				var file = link.attr('rel');
				var name = link.find('span').text();
				var bask = $(this);
				
				if($('.basket li a[rel^='+file+']').length==0) {
				
					$.post(AjaxRequestURL, { fileSrc: path, fileTitle: name, fileId: file, action: 'addFile' }, function() {
						$('.box.basket .items').children('.default').clone()
						.removeAttr('class')
						.children('.fileTitle').html(name)
						.next('a.remove').attr('rel', file)
						.parent('li')
						.insertAfter('.box.basket .items .default')
						.hide()
						.fadeIn();
						bask.addClass('disabled');
						$('.box.basket .sbutton').fadeIn()
						$('.basket .emptyText').hide();
					} );
				}
				else 
				{
					return false;
				}
			}
		)
		
		$('.box.basket .remove').live('click', 
			function() {
			
				var fileId = $(this).attr('rel');
				var elem = $(this).parent('li');
				$.post(AjaxRequestURL, { file: fileId, action: 'delFile' }, function() {
					elem.fadeOut().remove()
					
					$('.section-link a[rel^='+fileId+']').next('.infobasket').removeClass('disabled')
					
					if($('.box.basket .items li').length < 3) {
						$('.basket .sbutton').hide();	
						$('.basket .emptyText').show();
					}
				} );
				
				return false;
			}	
		);
		
		
		
		$('.location_wrapper li span').each(function() {
			var elem = $(this);
			switch(elem.attr('class')) {
				case "left": elem.css({ 'left':'-'+parseInt(elem.width())+'px', 'top':'2px' }); break;
				case "right": elem.css({ 'top':'2px' }); break;
				case "top": elem.css({ 'left':'-'+parseInt((elem.width()/2)-6)+'px' , 'top':'-'+parseInt(elem.height()-6)+'px' }); break;
				case "bottom": elem.css({ 'left':'-'+parseInt((elem.width()/2)-6)+'px' , 'top':parseInt(elem.height()-6)+'px' }); break;
			}
		})
		
		$('.location_wrapper li').click(function() {
			$('.location_wrapper li').removeClass('active');
			$(this).addClass('active');
			$('.info_wrapper ul').slideAnimate($(this).find('ul.corps').html(), 500);
		})
		
		$('.info_wrapper .close').click(function() {
			$(this).parent().animate({left: '800px'}, 500);
			$('.location_wrapper li').removeClass('active');
			return false;
		})
	
		$.fn.slideAnimate = function(data, time)
		{
			if($(this).hasClass('opened'))
			{
				$(this).parent().animate({left: '800px'}, time, function() 
					{ 
						$(this).find('ul').html(data).parent().animate({left: '450px'}, time) 
					} 
				);
			}
			else
			{
				$(this).addClass('opened')
					.html(data).parent().animate({left: '450px'}, time);
			}
		}
		
		$('form[name=find_contact] select').change(function() {
			$(this).closest('form').submit();
		})
	}
);
