/**
 * @author Nikolay Borisov (nikolay@borisov.me | http://nikolay.borisov.me/)
 * @copyright Notamedia (http://notamedia.ru/)
 * @version 0.3
 * @required jQuery
 * @date 2009-12-24
 */



cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


$(function(){
	
	(function(){

		var $li = $('#mainMenu > li');

		$li.each(function(){
			var $this = $(this);
			$this.hover(function(e){
				$this.addClass('hover');
			}, function(e){
				$this.removeClass('hover');
			});
		});
	
	})();
	
	(function(){
		
		var $labels = $('label.autohide');

		$labels.each(function() {

			var $label = $(this);
			var id = $label.attr('for');
			var $input = $('#'+id);
			
			$input.focus(function(){
				$label.hide();
			}).blur(function(){
				if(!$input.val()){
					$label.show();
				}
			}).change(function(){
				if($input.val()){
					$label.hide();
				}
			});
			
			if ($input.val()) {
				$label.hide();
			}
			
		});

	})();

	(function(){
		var $select = $('#select_okrug');
		var $link = $('.select_link', $select);
		var $cont = $('.cont', $select);
		var $list = $('.list', $select);
		var $items = $('ul > li', $list);
		var timer;
		
		var mapRus = document.getElementById('map_russia') || null;
		var mapRusMin = document.getElementById('map_russia2') || null;
		var isIndex = document.getElementById('p_index') ? true : false;
		
		var orderArray = [];
		
		if (isIndex) {
			
			var $mainAreas = $('#Map > area');
			var $infoTables = $('#infoTables > dl');

			$mainAreas.each(function(i){

				var $this = $(this);
				
				var order = $this.attr('rel');
				orderArray[i] = order;
				
				$this.hover(function(e){
					$items.eq(i).attr('class', 'hover');
					$infoTables.filter('#okruginf'+order).show();
					if (mapRus) {
						mapRus.className = 'okrug' + order;
					}
					if (mapRusMin) {
						mapRusMin.className = 'okrug' + order;
					}
				}, function(e){
					$items.eq(i).attr('class', '');
					$infoTables.filter('#okruginf'+order).hide();
					if (mapRus) {
						mapRus.className = 'okrug0';
					}
					if (mapRusMin) {
						mapRusMin.className = 'okrug0';
					}
				});
			});
			
		} else {
			
			var $menuAreas = $('#MapMenu > area');

			$menuAreas.each(function(i) {
				var $this = $(this);

				var order = $this.attr('rel');
				orderArray[i] = order;

				$this.hover(function(e) {
					$items.eq(i).attr('class', 'hover');
					if (mapRus) {
						mapRus.className = 'okrug' + order;
					}
					if (mapRusMin) {
						mapRusMin.className = 'okrug' + order;
					}
				}, function(e){
					$items.eq(i).attr('class', '');
					if (mapRus) {
						mapRus.className = 'okrug0';
					}
					if (mapRusMin) {
						mapRusMin.className = 'okrug0';
					}
				});
				
			});
			
			
			(function(){
				
				var $select2 = $('#select_region');
				var $link2 = $('.select_link', $select2);
				var $cont2 = $('.cont', $select2);
				var $in2 = $('.in', $cont2);
				var $items2 = $('ul > li', $in2);
				var timer2;
				
				var mapRegion = document.getElementById('map_region') || null;
				
				var orderArray2 = [];
				
				if(mapRegion){
					
					var $menuAreas2 = $('#Map_region_kontur > area');
					var $infoTables2 = $('#infoTables2 > dl');
					
					$menuAreas2.each(function(i) {
						var $this = $(this);
						
						var order2 = $this.attr('rel');
						orderArray2[i] = order2;
						
						$this.hover(function(e) {
							$items2.eq(i).attr('class', 'hover');
							$infoTables2.filter('#okruginf'+order2).show();
							mapRegion.className = 'region' + order2;
						}, function(e){
							$items2.eq(i).attr('class', '');
							$infoTables2.filter('#okruginf'+order2).hide();
							mapRegion.className = 'region0';
						});
						
					});
					
				}
				
				var inH = $in2.height() + 45;
				
				var onEnter2 = function(e) {
					clearTimeout(timer2);
					$cont2.stop().animate({ height:inH }, 300);
				};
		
				var onLeave2 = function(e){
					timer2 = setTimeout(function(){
						$cont2.stop().animate({ height:18 }, 300);
					}, 500);
				};
				
				$select2.hover(onEnter2, onLeave2);
		
				$items2.each(function(i){
					
					var $this = $(this);
					
					$this.hover(function(e){
						this.className = 'hover';
						if (mapRegion) {
							mapRegion.className = 'region' + orderArray2[i];
						}
					}, function(e){
						this.className = '';
						if (mapRegion) {
							mapRegion.className = 'region0';
						}
					});
				});
			
			})();
			
		}

		var onEnter = function(e) {
			clearTimeout(timer);
			$cont.stop().animate({ height:(isIndex ? 211 : 364) }, 300);
		};

		var onLeave = function(e){
			timer = setTimeout(function(){
				$cont.stop().animate({ height:18 }, 300);
			}, 500);
		};
		
		$select.hover(onEnter, onLeave);

		$items.each(function(i){
			
			var $this = $(this);

			$this.hover(function(e){
				this.className = 'hover';
				if (mapRus) {
					mapRus.className = 'okrug' + orderArray[i];
				}
				if (mapRusMin) {
					mapRusMin.className = 'okrug' + orderArray[i];
				}
			}, function(e) {
				this.className = '';
				if (mapRus) {
					mapRus.className = 'okrug0';
				}
				if (mapRusMin) {
					mapRusMin.className = 'okrug0';
				}
			});
		});
	})();

	(function() {
		var $togglers = $('._toggler');
		$togglers.each(function(i) {
			var $this = $(this);
			var $title = $('._title', this);
			var $block = $('._block', this),
				block = $block.get(0);
			$title.bind('click', function(e) {
				if (block.style.display == 'block') {
					$block.slideUp(300);
					$this.removeClass('show');
				} else {
					$block.slideDown(300);
					$this.addClass('show');
				}
			});
		});
	})();
	
	(function(){
		
		var $links = $('.sort');
		var spd = 300;
		
		$links.each(function(){
			
			var $this = $(this);
			var $ul = $('> ul', $this);
			
			if ($ul.length) {
				$this.hover(function(){
					$ul.stop().css({ display:'block', opacity:0 }).animate({ opacity:1 }, spd);
				}, function(){
					$ul.stop().animate({ opacity:0 }, spd, function(){
						$ul.hide();
					});
				});
			}
			
		});
		
	})();
	
	
	(function(){
		
		if(document.getElementById('p_index')){
			
			var $map = $('#map_block');
			var $link = $('.link', $map);
			
			$link.click(function(e){
				if($map.hasClass('map_inline')){
					$map.removeClass('map_inline');
					cookie('map', '', { path:'/' });
					$link.html('скрыть карту');
				}else{
					$map.addClass('map_inline');
					cookie('map', 'hide', { path:'/' });
					$link.html('показать карту');
				}
			});
			
			if (cookie('map') == 'hide') {
				$map.addClass('map_inline');
				cookie('map', 'hide', {
					path: '/'
				});
				$link.html('показать карту');
			} else {
				$map.removeClass('map_inline');
				cookie('map', '', {
					path: '/'
				});
				$link.html('скрыть карту');
			}
		}
		
	})();
	
	(function(){
		
		var spd = 300;
		
		var $body = $('body');
		var stageWidth = $body.width();
		
		$body.append(
			'<div id="popup">' +
				'<div class="cont">' +
					'<div class="t">&nbsp;</div>' +
					'<div class="in">' +
						'<div title="закрыть окно" class="close">закрыть окно</div>' +
						'<div id="popupContent">Красивое окошко</div>' +
					'</div>' +
				'</div>' +
				'<div class="b"><div>&nbsp;</div></div>' +
			'</div>'
		);
		
		var $popup = $('#popup');
		var $close = $('.close', $popup);
		var $content = $('#popupContent');
		var popupWidth = $popup.width();
		
		$close.click(function(e){
			$popup.hide();
		});
		
		var setPosition = function(){
			var l = parseInt((stageWidth-popupWidth)/2);
			$popup.css({ left:l });
		}
		
		setPosition();
		
		$(window).resize(function(e){
			stageWidth = $body.width();
			setPosition();
		});
		
		
		var $links = $('a.__graphdata');
		
		$links.each(function(){
			
			var $this = $(this);
			var $table = $('#'+$this.attr('rel'));
			var _type;
			
			if($table.hasClass('.graphdata_bar')){
				_type = 'bar';
			}else if($table.hasClass('.graphdata_area')){
				_type = 'area';
			}else if($table.hasClass('.graphdata_pie')){
				_type = 'pie';
			}else if($table.hasClass('.graphdata_line ')){
				_type = 'line';
			}
			
			$this.click(function(e){
				$popup.hide();
				
				$content.html('');
				var yScroll = $this.offset().top - 100;
				$popup.css({ top:yScroll });
				$popup.show();
				$table
					.visualize({
						type: _type,
						width: 402,
						height: 402,
						pieLabelPos: 'outside',
						colors: ['#be1e2d','#666699','#92d5ea','#ee8310','#8d10ee','#5a3b16','#26a4ed','#f45a90','#e9e744','#be1e2d','#666699','#92d5ea','#ee8310','#8d10ee','#5a3b16','#26a4ed','#f45a90','#e9e744','#be1e2d','#666699','#92d5ea','#ee8310','#8d10ee','#5a3b16','#26a4ed','#f45a90','#e9e744','#be1e2d','#666699','#92d5ea','#ee8310','#8d10ee','#5a3b16','#26a4ed','#f45a90','#e9e744']
					})
					.appendTo($content)
					.trigger('visualizeRefresh');
				
				/*var $info = $('.visualize-info');
				var $ul = $('> ul', $info);
				$ul.hide();
				
				$info.unbind().hover(function(e){
					$ul.show();
				}, function(e){
					$ul.hide();
				});*/
				
				return false;
				
			});
			
		});
		
	})();
	
	
	(function(){
		var $Dots = $('ul.tree b.marker');
		var spd = 300;
		
		if($Dots.length){
			$Dots.bind('click', function(e){
				$Dot = $(this);
				$Node = $Dot.parents('li').eq(0).find('> ul');
				if($Dot.hasClass('open')){
					if ($Node.length) {
						$Dot.removeClass('open');
						$Node.slideUp(spd);
					}
				}else{
					$Dot.addClass('open');
					$Node.slideDown(spd);
				}
			});
		}
	})();
	
	
	(function(){
		
		var $DatePickers = $('.date-pick');
		if ($DatePickers.length) {
			$DatePickers.datePicker();
		}
		
	})();
	
	
});

