// variable
var _imagePath = 'images/';

// filter
_filter = function(){
	var _filterStatus = null;

	function open(){
        if (_filterStatus == null) {
            _stype = $('#type_search').val();
            _filterStatus = (_stype == '2')? false : true;
            }

		_display = (_filterStatus)? 'none' : 'block';
		$('#advanced').css({display: _display});

		_stype = (_filterStatus)? '1' : '2';
        $('#type_search').val(_stype);

		_filterStatus = (_filterStatus)? false : true;
	}

	function okrug(_id, _type){
		if(_type){
			_input = $('#okrug').find('input');

			for(var i = 0; i < _input.length; i++){
				$(_input[i]).attr('checked', false);
				$(_input[i]).parent('div')[0].className='off2';
				_cbox = $(_input[i]).attr('id');
				if(_cbox == ''){
					$(_input[i]).parent('div')[0].className = 'off';
				}else{
					_children = _cbox.split('_');
					$('#' + _children[0])[0].className = 'schoolDistrictOff';
				}
			}
		}

		_check = true;
		if(!_type) _check = $('#' + _id + '_cbox').attr('checked');
		_class = (_check == true)? 'schoolDistrictOn' : 'schoolDistrictOff';
		_class2 = (_check == true)? 'on2' : 'off2';
        
        var jo;

        jo = $('#' + _id)[0];
        if (jo != null) {
            jo.className = _class;
        }

        jo = $('#' + _id + '_cbox').parent('div')[0];
        if (jo != null) {
            jo.className = _class2;
        }

		if(_check == false){
			_input = $('#' + _id).find('input');

			for(var i = 0; i < _input.length; i++){
				$(_input[i]).attr('checked', false);
				$(_input[i]).parent('div')[0].className = 'off';
			}
		}

		if(_type){
			$('#' + _id + '_cbox').attr('checked', true);
			_map.open();
		}

        _filter.set_metro();
        }

	function district(obj){
		_check = $(obj).attr('checked');
		_class = (_check == true)? 'on' : 'off';
		$(obj).parent('div')[0].className = _class;


        _filter.set_metro();
	}

	function set_metro(){
		_okrug = $('#okrug').find('input[rel=okrug]:checked');
		_arr1 = new Array();
		for(var i = 0; i < _okrug.length; i++){
			_arr1[_arr1.length] = $(_okrug[i]).val();
		}
		_district = $('#okrug').find('input[rel=district]:checked');
		_arr2 = new Array();
		for(var i = 0; i < _district.length; i++){
			_arr2[_arr2.length] = $(_district[i]).val();
		}
		if(_arr1.length == 0) _arr1 = null;
		if(_arr2.length == 0) _arr2 = null;

		_metro.json(_arr1, _arr2);
	}

	function cbox(obj, _name, _type){
		_checkObj = $(obj).attr('checked');
		_input = $('#' + _name).find('input');
		_check = $(_input[0]).attr('checked');

		if(_type){
			_class = (_check == true)? 'on' : 'off';

			for(var i = 0; i < _input.length; i++){
				$(_input[i]).attr('checked', _check);
				$(_input[i]).parent('div')[0].className = _class;
			}
		}else{
			_inputCheck = $('#' + _name).find('input:checked');

			if(_input.length == (_inputCheck.length + 1)){
				if(_check == true){
					$(_input[0]).attr('checked', false);
					$(_input[0]).parent('div')[0].className = 'off';
				}else{
					for(var i = 0; i < _input.length; i++){
						$(_input[i]).attr('checked', true);
					}
				}
			}

			_class = (_checkObj == true)? 'on' : 'off';
			$(obj).parent('div')[0].className = _class;
		}
	}

	function show(_obj, _name){
		_display = $('#' + _name).css('display');

		if(_display == 'block'){
			$('#' + _name).css({display: 'none'});
			$(_obj).attr('src', _imagePath + 'plus.gif');
		}else{
			$('#' + _name).css({display: 'block'});
			$(_obj).attr('src', _imagePath + 'minus.gif');
		}
	}

	return{
		open: open,
		okrug: okrug,
		district: district,
		set_metro: set_metro,
		cbox: cbox,
		show: show
	}
}();

// map
_map = function(){
	var _mapStatus = false;
	var _mapAnimate = false;

	function open(){
		if(_mapAnimate) return false;
		_mapAnimate = true;
		_w = (_mapStatus)? 70 : 600;
		_src = (_mapStatus)? 'map/open.png' : 'map/close.png';
		_mapStatus = (_mapStatus)? false : true;

		$('#map_button').attr('src', _imagePath + _src);
		$('#map_button').ifixpng();

		_shield.open();

		$('#map').animate({width: _w}, 1000, function(){
			_mapAnimate = false;
		});
	}

	return{
		open: open
	}
}();

// shield
_shield = function(){
	var _shieldStatus = false;

	function open(){
		if(_shieldStatus){
			$('#shield').css({visibility: 'hidden'});
			_shieldStatus = false;
		}else{
			_size = size();
			$('#shield').css({visibility: 'visible', width: _size[0], height: _size[1]});
			_shieldStatus = true;
		}
	}

	function close(){
		_map.open();
	}

	function size(){
		var _wPage, _hPage;
		if(window.innerHeight && window.scrollMaxY){
			_wPage = document.body.offsetWidth;
			_hPage = window.innerHeight + window.scrollMaxY;
		}else if(document.body.scrollHeight > document.body.offsetHeight){
			_wPage = document.body.scrollWidth;
			_hPage = document.body.scrollHeight;
		}else{
			_wPage = document.body.offsetWidth;
			_hPage = document.body.offsetHeight;
		}

		var _xScroll, _yScroll;
		if(self.pageYOffset){
			_yScroll = self.pageYOffset;
			_xScroll = self.pageXOffset;
		}else if(document.documentElement && document.documentElement.scrollTop){
			_yScroll = document.documentElement.scrollTop;
			_xScroll = document.documentElement.scrollLeft;
		}else if(document.body){
			_yScroll = document.body.scrollTop;
			_xScroll = document.body.scrollLeft;
		}

		var _wWindow, _hWindow;
		_wWindow = $(window).width();
		_hWindow = $(window).height();

		_size = new Array(_wPage, _hPage, _xScroll, _yScroll, _wWindow, _hWindow);
		return _size;
	}

	return{
		open: open,
		close: close,
		size: size
	}
}();
$(window).resize(function(){
	_size = _shield.size();
	$('#shield').css({width: _size[0], height: _size[1]});
});

// form
_form = function(){
	function clearForm(){
		_cbox = $('#searchFilter').find('input[type=checkbox]');
		for(var i = 0; i < _cbox.length; i++) $(_cbox[i]).attr('checked', false);

		_text = $('#searchFilter').find('input[type=text]');
		for(var i = 0; i < _text.length; i++) $(_text[i]).val('');

		_select = $('#searchFilter').find('select');
		for(var i = 0; i < _select.length; i++) $(_select[i]).find(':nth-child(1)').attr('selected', true);

		_on = $('#searchFilter').find('div[class=on]');
		for(var i = 0; i < _on.length; i++) $(_on[i])[0].className = 'off';

		_on2 = $('#searchFilter').find('div[class=on2]');
		for(var i = 0; i < _on2.length; i++) $(_on2[i])[0].className = 'off2';

		_district = $('#searchFilter').find('div[class=schoolDistrictOn]');
		for(var i = 0; i < _district.length; i++) $(_district[i])[0].className = 'schoolDistrictOff';

		_advanced = new Array('advanced', 'system', 'discipline', 'collaboration', 'additional', 'teache', 'base', 'security');
		for(var i = 0; i < _advanced.length; i++) $('#' + _advanced[i]).css({display: 'none'});

		_metro.init(null);

		return false;
	}

	function sendForm(){
		$('#searchFilter').submit();

		return false;
	}

	return{
		clearForm: clearForm,
		sendForm: sendForm
	}
}();

function checkKey (_e) { // _e is event object passed from function invocation

    var characterCode; // literal character code will be stored in this variable

    if (_e && _e.which) { // if which property of event object is supported (NN4)
        _e = _e;
        characterCode = _e.which; // character code is contained in NN4's which property
    }
    else {
        _e = event;
        characterCode = _e.keyCode; // character code is contained in IE's keyCode property
    }

    if (characterCode == 13) { // if generated character code is equal to ascii 13 (if enter key)
        _form.sendForm(); // submit the form
        return false;
    }
    return true;
}

