//Main Lib                              | | |
//Copyright 2010, A.Trusov              | | |

window.engine={};
/*******      Er     *******/
window.engine.Er=new Function;
window.engine.Er.prototype = {
    debug_mode: false,
    all_errors: new Array(),
    add: function(message){
        if(this.debug_mode){
            this.all_errors.push(message);
        }
    }
};


/******* Search *******/


/******* Menu *******/
window.engine.Menu = new Function;
window.engine.Menu.prototype = {
	menu: window.menu_items,
    __init__: function(){
		var obj = this;
		$(".jtopmenu").bind({
			click: function() {  return true; },
			mouseenter: function(e) {
				 //if($(this).attr('class').search('active') != -1) {this.stopPropagation();}
				 $(this).addClass('active');
				 $("#menu li ul").remove();
				 $(this).append(obj.draw_menu_list($(this).attr('id'))); this.stopPropagation();
			},
			mouseleave: function(e) { $(this).toggleClass('active'); $("#menu li ul").remove(); }
		});
    },
	
	draw_menu_list: function(id){
		var id = parseInt(id.replace('m_',''));
		var list = this.menu[id], i=0, res=[];
		if(list == undefined) { return ''; }
		for(; i < list.length; i++){
			res.push('<li><a href="/'+list[i]['short_name']+'/'+list[i]['news_id']+'">'+list[i]['header']+'</a></li>');
		}
		return '<ul class="sub-menu">'+res.join('')+'</ul>';
	}
};

/******* Image preview *******/ 
window.engine.Image = new Function;
window.engine.Image.prototype = {
	
	_close_event: false,
	
	__init__: function(){
		//search elements
		var object = this;
		$('.lightview').each(function(){ 
		$(this).attr('rel', '#img_preview');
 
			$(this).bind({
				click: function() {
					$(this).overlay({
						top:20,
						left:'center'
					});
					var thumb = $(this).find('img').attr('src');
					var h = $(window).height();
					var w = $(window).width();
					var info = object.size_info(thumb,w,h);
					$('#img_preview, #img_preview img').css({'width':info['w']+'px', 'height':info['h']+'px'});
					$('#img_preview img').attr('src', $(this).attr('href'));
					$(this).data('overlay').load();
					object.close_event();
					return false;
				}
			}); 		
		});
		

	},
	
	close_event: function(){
		if(this._close_event) { return; }
		$('#img_preview .close').bind({
			click: function(){
				$('#img_preview img').attr('src', '');
			}
		});
		this._close_event = true;
	},
	
	size_info: function(url, w, h){
		var info={h:500, w:500};
		try {
			var a = url.split('_'), ratio = a[3], buff = '';
			buff = ratio.split('.');
			ratio = buff[0].replace('x', '.');
			info['ratio'] = (parseFloat(ratio) != 0)?parseFloat(ratio):1;
			info['h'] = parseInt(h - 60);
			info['w'] = (info['h'] * info['ratio']).toFixed();
			if (w < info['w']) {
				info['w'] = w - 60;
				info['h'] = (info['w'] / info['ratio']).toFixed();
			}
		}catch(e){}
		return info;
	}	
};

(function($){	
	var m = new window.engine.Menu();
	m.__init__();
	
	var i = new window.engine.Image();
	i.__init__();	
	
	$('#search-input').bind({
		click: function() { if($(this).attr('value') == 'поиск'){ $(this).attr('value', ''); } },
		blur:  function() { var q=$(this).attr('value'); if(q == '') { $(this).attr('value', 'поиск'); }}
	});
	
	$('#link_add').bind({
		click: function() { 
			//clone 
			$('#order_row').clone(false).show().appendTo('tbody');
		}
	});
	
	$(function() {
		if ($('.map11').is('*')) {
			$('.map11').maphilight({
				fillColor: '003399',
				strokeColor: '333333'
			});
			
			$('mapq > area').tooltip({
				predelay: 200,
				delay:1000,
				track: true,
				showBody: " - "
			});
			
			$('map > area').mouseover(function(e){
				$('#'+$(this).attr('p')).show();
				if($(this).attr('p') != 'main'){
					$('#main').hide();
		}
	});
	
			$('map > area').mouseout(function(e){
				$('#by, #ug, #zab, #sib, #ur, #main').hide();
				$('#main').show();
			});			
		}
	});
})(jQuery);

