﻿
	$j(document).ready(function() {
		$j("a._blank").attr('target', '_blank');align="absmiddle"
		$j("img.absmiddle").attr('align', 'absmiddle');		
		activateSearchInput();
		activateLeftSubMenu();
	});
	
	var CURR_LANG;
	function activateLanguage(lang){
		if(lang==1) {
			CURR_LANG = I18n.ru;
		} else if(lang==2) {
			CURR_LANG = I18n.ua;
		} else if(lang==3) {
			CURR_LANG = I18n.en;
		} else if(lang==4) {
			CURR_LANG = I18n.de;
		} else if(lang==5) {
			CURR_LANG = I18n.fr;
		} else if(lang==6) {
			CURR_LANG = I18n.it;
		}
	}
	
	// -- Menu --
	
	function activateMenu() {		
		$j('.menu div.menu_bg').bind('mouseenter', function(){
			var $this = $j(this);
			var $sub_menu = $j('.sub_menu', $this);			
			$j('.hover', $this).show();			
			// Устанавливаем минимальную ширину для submenu
			$sub_menu.css('min-width', $this.width()+50+'px' );				
			// for ie6
			if($j.browser.msie && $j.browser.version==6){				
				$j('.link_data', $sub_menu).css('width',$this.width()+50+'px');
			}
			$sub_menu.show();			
		}).bind('mouseleave', function(){
			var $this = $j(this);
			$j('.hover', $this).hide();
			$j('.sub_menu', $this).hide();
		});
		
		/*$j('.menu div.menu_bg').each(function(){
			var $this = $j(this);			
			var $sub_menu = $j('a.main', $this).next();
			if($sub_menu.is('.sub_menu')){				
				
				// Устанавливаем минимальную ширину для submenu
				$sub_menu.css('min-width', $this.width()+50+'px' );				
				// for ie6
				if($j.browser.msie && $j.browser.version==6){				
					$j('.link_data', $sub_menu).css('width',$this.width()+50+'px');
				}
				
				var $hover = $sub_menu.next();				
				$this.bind('mouseleave', function(){
					$sub_menu.add($hover).show();
				})
				.bind('mouseenter', function(){
					// FIXME: Нужно очищать стек действий
					$sub_menu.add($hover).hide();
				});
			}
		});*/
	}
	
	function activateLeftSubMenu(){
		$j('.left_menu_wrapper').each(function(){
			bindLeftSubMenu( $j(this) );
		});
	}
	function bindLeftSubMenu($this){
		$this.bind('mouseenter', function(){
			$this.unbind('mouseenter');
			$this.unbind('mouseleave');
			$j('ul', this).slideDown(function(){
				bindLeftSubMenu($this);
			});
		}).bind('mouseleave', function(){
			$this.unbind('mouseenter');
			$this.unbind('mouseleave');
			$j('ul', this).slideUp(function(){
				bindLeftSubMenu($this);
			});
		});
	}
	
	// -- Search --
	
	function activateSearchInput(){
		$j('#search_field').example(function() {
	        return $j(this).attr('title'); 
	    });
	}
	
	// -- Pages --
	
	function activateTesis(){		
		var url = window.location;
		var $tesis_div = $j('#tesis');
		var $tesis_wrapper = $j('#tesis .top_bg .bott_bg');		
		var $tesis = $j('#page_text h4');		
		if( $tesis.size()>0 ){			
			$tesis.each(function(){
				var $this = $j(this);
				var html = text = $this.html();
				$this.attr('id', html);				
				var tesis_link = '<div class="link"><a href="'+url+'#'+html+'">'+html+'</a></div>';
				$tesis_wrapper.append(tesis_link);
			});
			$tesis_wrapper.append('<br clear="all" />');
			$tesis_div.show();
		}
	}
	
	// -- Gallery --
	
	function activateFancyGallery(){
		$j("a.gallery_thumbs").fancybox({
			'zoomOpacity'			: true,
			//'overlayShow'			: false,
			'zoomSpeedIn'			: 500,
			'zoomSpeedOut'			: 500,
			'overlayColor'			: '#000',
			'hideOnContentClick'	: false,
			'frameHeight'			: 435,
			'callbackOnShow'		: function(){
				//$j('#fancy_frame, #fancy_div, #fancy_ajax, #fancy_outer, #fancy_inner').height($j('.media').height()+35);
				//$j('#fancy_ajax').css('clear','both');
				//$j('#fancy_frame, #fancy_div, #fancy_ajax').css('height','100%');
			}
        });		
	}
	
	/*function activateGalleryCarousel(){
		$object_carousel = $j('.gallery_carousel');
		$object_carousel.each(function(){
			$curr_carousel = $j(this);
			$curr_carousel.jcarousel({
				vertical: true,
				initCallback: function(carousel){ 
					$object_carousel.css({'visibility':'visible'}); 
					var $gallery_big_wrapper = $j('#gallery_big_wrapper');
					$j('.gallery_thumbs').bind('click', function(){
						var $thumb = $j(this);
						var id = $thumb.attr('id').split('_');
						var $big = $j('#big_'+id[1]).clone();
						
						// Гасим текущую картинку
						$j('img', $gallery_big_wrapper).fadeOut(700, function(){
							$j(this).remove();
						});			
						
						// Выводим новую картинку с еффектом наложения
						$gallery_big_wrapper.append( $big );
						$big.fadeIn(500);
						
						// Выделяем превью
						$j('.thumb_active').removeClass('thumb_active');
						$thumb.parent().addClass('thumb_active');						
					});
					
					$j('.gallery_thumbs:first').click();
				},				
				animation: "slow",
				scroll: 2
			});		
		});
	}*/
	
	// -- Contact --
	
	function activateContactForm(){
		var $contacts_form 	= $j('#ask_question_form');		
		var $errorContainer	= $j('div.errorContainer');
		
		$contacts_form.validate({			
			onkeyup: false,
			errorContainer: $errorContainer,
			errorLabelContainer: $j("ol", $errorContainer),		
			wrapper: 'li',
			errorPlacement: function(error, element) {},			
			rules: {	
				"user[fio]":"required",
				"user[email]": {
					required:true,
					email:true
				},
				"user[text]":"required"
			},
			messages: {				
				"user[fio]": {
					required: CURR_LANG.error.fio.required
				},
				"user[email]": {	
					required: CURR_LANG.error.email.required,
					email: CURR_LANG.error.email.email					
				},
				"user[text]": {
					required: CURR_LANG.error.text.required
				},
				securimage_code_value: {
					required: CURR_LANG.error.securimage_code_value.required
				}
			}
		});	
	}
	
	// -- Flash --
	
	function activateFlashBanners( wrapper, src, width, height ) {
		if ($j.browser.msie && ($j.browser.version < 8)) {
			return;
		}		
        $j('#'+wrapper).flash(
            { 
			  'wmode':'opaque',
			  src: src,
              width: width,
              height: height
			},
            { version: 8 }
        );
    }
	
	// -- Question --
	
	function activateQuestions(){
		$j(".question").bind('click',function(event) {
			event.preventDefault();
			$j(this).next().slideToggle();
		});

		$j(".close").click(function(event) {
			event.preventDefault();
			$j(this).parent().slideToggle();
		});
	}
	
	// -- Addithional --
		
	function ajaxError(){
		alert("Ошибка при передаче данных! Попробуйте снова");
	}
	
	function reloadPage(){
		window.location.reload();
		return;
	}
	
	function checkResponse( responseText ){
		if( typeof(responseText)=='undefined' ){				
			alert("Ошибка при передаче данных! Попробуйте снова");
			return false;
		}
		
		var json = eval( "(" + responseText + ")" );	
		if( typeof(json.error)=='undefined' ){
			alert("Ошибка при передаче данных! Попробуйте снова");
			return false;
		}
		
		return json;
	}
	
	function activateCalendar( range ){
		// Календарь
		$j(".calendar").datepicker({
			//mandatory: true,
			yearRange: range,
			dateFormat: $j.datepicker.ATOM
		}).attr("readonly", "readonly");
	}	
	
	function var_dump(obj) {
		  var result = null;
		  for (var i in obj)
		  result += 'object' + '.' + i + ' = ' + obj[i] + '\n';
		  return result;
	}
	 
	function activateblock(){
		$j.blockUI({ css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: '#000', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff'				
        }, overlayCSS:{'z-index': 1007} }); 
	}
	
	function deactivateblock(){
		$j.unblockUI();
	}
	
	function you_are_sure(str){
		return confirm(str);
	}
