var _storage = {

};

var ie6 = 0;
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.search('MSIE 6') != -1){
	ie6 = 1;
}

var _bRules = {
	'#mypb_menu': function(el) {
		//var hidel = document.getElementsByClassName('select_box').shift();
		Event.observe(el, 'mouseover', function() { 
			el.addClassName('hover');
			$('mypb_menu_options').style.display = 'block';
			// verstop de Country pulldown in IE6
			if (ie6 && $('mypb_menu_options').getHeight() > 180) {
				if($('qs_country')){$('qs_country').style.visibility = "hidden";}
				if($('qs_state')){$('qs_state').style.visibility = "hidden";}
			}
			if (ie6 && $('mypb_menu_options').getHeight() > 230) if($('quick_search_city')){ $('quick_search_city').style.visibility = "hidden";}
			
		});
		Event.observe(el, 'mouseout', function() { 
			el.removeClassName('hover');
			$('mypb_menu_options').style.display = 'none';
			if (ie6) {
				if($('qs_country')){$('qs_country').style.visibility = "visible";}
				if($('qs_state')){$('qs_state').style.visibility = "visible";}
				if($('quick_search_city')){$('quick_search_city').style.visibility = "visible";}
			}
		});
	},
	'#mypb_menu_options a': function(el) {

		Event.observe(el, 'mouseover', function() { 
			el.addClassName('hover');
		});
		Event.observe(el, 'mouseout', function() { 
			el.removeClassName('hover');
		});

	},
	'div.menu_item ul li': function(el) {
		Event.observe(el, 'mouseover', function() { 
			el.addClassName('hover');
		});
		Event.observe(el, 'mouseout', function() { 
			el.removeClassName('hover');
		});
	},
	'div.menu_item': function(el) {

		Event.observe(el, 'mouseover', function() { 
			el.addClassName('hover');
		});
		Event.observe(el, 'mouseout', function() { 
			el.removeClassName('hover');
		});

		var lists = el.getElementsByTagName('div');

		if (lists.length == 0) return true;

		// only one level is supported
		var div = lists[0]
		var ul 	= el.getElementsByTagName('ul')[0];

		var ht = undefined; // HideTimeout

		div.style.display = "none";

		div.onmouseout = function() {
			div.style.display = 'none';
			//Effect.Fade(ul);
		}

		// support vertical scrollout
		el.onmouseout = function() {

			if (ht == undefined) {
				ht = setTimeout(function() {
					div.style.display = 'none';
				}, 500);
			}
		}

		el.onmouseover = function() {

			// hide all others
			var expanded = document.getElementsBySelector('div.menu_item div');
			$A(expanded).each(function(val, i) {
				val.style.display = "none";
			});

			if (ht != undefined) clearTimeout(ht);
			ht = undefined;

			div.style.display = 'block';
			//Effect.Appear(ul);

		}
	},
	'#member_search_results tbody tr': function(el) {

		try {

			el = $(el);

			el.onmouseover = function() {
				el.addClassName('hover');
			};

			el.onmouseout = function() {
				try {
					el.removeClassName('hover');
				} catch(e) {};
			};

			var id = 'res_' + Math.round(Math.random() * 1000);
			el.id = id;

			var link 	= document.getElementsBySelector('#' + id + ' a').shift();
			var cells 	= document.getElementsBySelector('#' + id + ' td');
			link.onclick = null;

		} catch (e) {};
	},
	'a.popup': function(el) {
		el.onclick = function() {
			return !openPopup(this, 466, 500);
		};
	},
	'#right_bar ul li': function(el) {
		el.innerHTML = ' - ' + el.innerHTML;
	},
	'td.auto_complete_field input.text_field': function(el) {
		var prep = '';

		try {
			prep = is_admin_page ? '/admin' : '';
		} catch(e) {};

		setTimeout(function() {
			new Ajax.Autocompleter(el.id, el.id + '_options', prep + '/'+ page_class_name +'/auto_'+el.id+'?_ajax=true', { });
		}, 1000);
	},
	'#edit_blog_message input#blog_category': function(el) {
		setTimeout(function() {
			new Ajax.Autocompleter('blog_category', 'blog_category_options', '/mypage/blog_categories?_ajax=true', { });
		}, 1000);
	},
	'#mem_search_sports': function(el) {
		setTimeout(function() {
			new Ajax.Autocompleter('mem_search_sports', 'mem_search_sports_options', '/members/sports_options?_ajax=true', { });
		}, 1000);
	},

	'#mem_language_skills': function(el) {
		setTimeout(function() {
			new Ajax.Autocompleter(el, 'mem_language_skills_options', '/members/language_options?_ajax=true', { });
		}, 1000);
	},
	'#qms_job_title': function(el) {
		setTimeout(function() {
			new Ajax.Autocompleter(el, el.id + '_options', '/members/job_title_options?_ajax=true', { });
		}, 1000);
	},
	'#fp_poll': function(el) {

		el.onsubmit = function() {
			try {
				$('poll_msg_area').innerHTML = '';

				var ok 		= false;
				var opts 	= this.getElementsByTagName('INPUT');
				for(var i = 0; i < opts.length; i++) {
					if (opts[i].name == 'option' && opts[i].checked == true) ok = true;
				}

				if (ok) {
					_a.postForm(this, 'fp_poll', _a.pollSubmitted);
				} else {
					$('poll_msg_area').innerHTML = 'Please select an option first';
				}
				
			} catch(e) {
				alert(e);
			}
			return false;
		};

	},

	'div.poll_results': function(el) {

		var res			= new Array();
		var children 	= getAllChildren(el);
		var max			= 0;
		for(var i = 0; i < children.length; i++) {
			if (children[i].className == 'result') {
				res.push(children[i]);	
				if (max < children[i].innerHTML) max = children[i].innerHTML;
			}
		}
		
		var space = el.offsetWidth;
		space = space - 4;
		var duration = 2000;
		$A(res).each(function(d) {

			var newWidth 		= Math.round(((d.innerHTML/100) * space));
			var newPercentage 	= (newWidth / d.offsetWidth) * 100;

			new Effect.Scale(d, newPercentage, { scaleY: false, scaleX: true, scaleContent: false, duration: duration/1000});

			// hide the percentage indicator untill the end of the effect
			var p = d.innerHTML;
			d.innerHTML = '';

			setTimeout(function() {
				d.innerHTML = '<div class="padje">' + p + '%</div>';
			}, duration);
		
		});
	},
	'div.collapse': function(el) {

		var content 		= null;
		var header 			= el.parentNode.getElementsByTagName('H3')[0];
		var state			= 'closed';

		header.style.cursor = 'pointer';

		Event.observe(header, 'click', function() {
			if (state == 'open') {
				state = 'closed'
				el.style.display 	= 'none';
			} else {
				state = 'open';
				el.style.display 	= 'block';
			}
		});

		if ($(el).hasClassName('initopen')) {
			el.style.display 	= 'block';
			state 				= 'open';
		}
	},
	'div#right_bar a.delete_button': function(el) {
		var oldInner = el.innerHTML;
		var img 	= Builder.node('img', { alt: 'X', 
											title: oldInner,
											border: 0,
											src: '/nspire/images/delete_icon.gif'});
		el.replaceChild(img, el.firstChild);
	},
	'div#right_bar ul li': function(el) {
		var origClassName = el.className;
		el.onmouseover = function() {
			el.className = 'hover';
		}
		el.onmouseout = function() {
			el.className = origClassName;
		}
	},
	'ul.booktip_cats a': function(el) {

		el.onclick = function() {

			var books = $A(document.getElementsBySelector('div.booktips'));
			var classNm = el.id;

			books.each(function(node) {

				if (Element.hasClassName(node, classNm)) {
					Element.setStyle(node, { display: 'block' });
				} else {
					Element.setStyle(node, { display: 'none' });
				}

			});

			$('book_show_all_cats').style.display = '';
			
			return false;
		};

	},

	'ul.digimag li a': function(el) {

		el.target = 'digimag';

		el.onclick = function() {
			openPopup(el, 1015, 715);
			return false;
		}
	},

	'a.digimag': function(el) {

		el.target = 'digimag';

		el.onclick = function() {
			openPopup(el, 1015, 715);
			return false;
		}
	},
	'ul.extras li a': function(el) {

		el.target = 'magazine-extra';

		el.onclick = function() {
			openPopup(el, 660, 520);
			return false;
		}
	},

	'input.delete_checkbox': function(el) {

		el.onchange = function() {
			
			if (el.checked == true) {
				var conf = 'Are you sure?';
				try {
					conf = $(el.id + '_confirm').innerHTML;
				} catch(e) {};
				if (!confirm(conf)) el.checked = false;
			};

		}

	},
	'select.quickCityPageSearch': function(el) {

		el.onchange = function() {

			if (el.selectedIndex > 0) {

				try {
					$(el.form).down('div.city_list').update('Please wait..');
					_a.call('/local/main', 'cityQuickSearch', { Country: $F(el) }, function(r) {
					

						$(el.form).down('div.city_list').update(r.responseText);


					});
				} catch(e) {
					alert(e);
				}

			}

		};
	},
	'a.video': function(el) {
		el.href += '&Title=' + el.innerHTML;
	},

	'_dummy': function(el) {}

}
Behaviour.register(_bRules);
