// JavaScript Document

jQuery(function () {
		//round corners classes
		if(!isIE) {
			//5px round classes
			DD_roundies.addRule('.allRound_5px', '5px', true);
			DD_roundies.addRule('.bottomRound_5px', '0 0 5px 5px', true);
			DD_roundies.addRule('.topRound_5px', '5px 5px 0 0', true);
			
			//3px round classes
			DD_roundies.addRule('.allRound_3px', '3px', true);
			
			//special round classes
			DD_roundies.addRule('#quicklogin', '3px 0px 3px 3px', true);
		}//end if - isIE condition for DD_roundies
		
		//scripts only for IE6
		if(typeof document.body.style.maxHeight === "undefined") {
			
		}//end if - IE6 condition
		
		//OSM bugs filter panel scripts & animations
		if(jQuery('#filters').length != 0) {
			var firstCall = true;
			jQuery('#filterToggle a').click(
				function () {
					jQuery('#filterBody').slideToggle();
					jQuery(this).next('.arrow').toggleClass('down');
					if(firstCall == true) {
						
						jQuery('.filter label img').each(
							function () {
								var filterIconHeight = 0;
								filterIconHeight = jQuery(this).attr('height');
								filterIconWidth = jQuery(this).attr('width');
								jQuery(this).css({position: 'absolute', top: '50%', left: '50%', 'margin-top': '-' + (filterIconHeight/2) + 'px', 'margin-left': '-' + (filterIconWidth/2) + 'px'})
							}
						);
						firstCall = false;
					}
				}
			);
		
			//adding class "checked" to the filters buttons that have attribute "checked" on osmBugs page filters module
			jQuery('.filter').each(
				function () {
					if (jQuery('input', this).attr('checked')) {
						jQuery(this).addClass('checked');
					}
				}
			);
			jQuery('.filter').click(
				function () {
					jQuery(this).toggleClass('checked');
					if (jQuery('input', this).attr('checked')) {
						jQuery('input', this).removeAttr('checked');
					}
					else {
						jQuery('input', this).attr('checked', 'checked');
					}
				}
			);
		}//end if - length condition
		
		//toggle animation for introduction text on bugs page
		if (jQuery('#introduction').length != 0) {
			jQuery('#introductionToggle a').click(
				function () {
					jQuery('#introduction .content').slideToggle();
					jQuery(this).next('.arrow').toggleClass('down');
				}
			);
		}
		
		//accordion efect for the right buttons on bug details page
		if(jQuery('#bugAcctionsMenu').length != 0) {
			jQuery('#bugAcctionsMenu').accordion({
				collapsible: true,
				active: false,
				autoHeight: false,
				header: 'h3.btnPopup'
			});
			if (action_name != "start_action") {
				jQuery('#bugAcctionsMenu').accordion( "option", "active", jQuery('#' + action_name).prev('.ui-accordion-header') );
			}

			jQuery('.btnReset').click(
				function (){
					jQuery(this).parents('.ui-accordion-content').prev('.ui-accordion-header').trigger('click');
				}
			);
			//applying same height to boxes on bug details page
			var bugDetailsBox_height = jQuery('#bugDetailsBox').innerHeight();
			jQuery('#bugDetailsBox').css('height', bugDetailsBox_height + 360 + 'px');
			if(jQuery('#bugDetailsBox').innerHeight()<jQuery('#bugCommentsBox').innerHeight()) {
				jQuery('#bugDetailsBox').css('height', jQuery('#bugCommentsBox').innerHeight());
			}
			else {
				jQuery('#bugCommentsBox').css('height', jQuery('#bugDetailsBox').innerHeight());
			}
		}//end if - length condition
		
		//privacy form manipulation and animations
		if(jQuery(".privacyGroup").length != 0) {
			
			//adding class checked to buttons that that have input with checked attribute and adding checked to all button on header if the col is all checked
			jQuery('.privacyGroup').each(
				function () {
					var visibility = { private : 0, shared : 0, registered : 0, public : 0 };
					jQuery(".privacyBtn", this).each(
						function () {
							if (jQuery("input", this).length != 0 && jQuery("input", this).attr('checked')) {
								jQuery(this).addClass('checked');
								var elm = jQuery(this);
								
								jQuery.each(visibility,
									function(key, value) {
										if (elm.hasClass(key)) {
											visibility[key]++;
											//console.log("after " + key + ":" + visibility[key]);
										}
									}
								);
							}
						}
					);
					var elm = jQuery(this);
					jQuery.each(visibility,
						function (key, value) {
							if(jQuery("." + key, elm).length == value) {
								jQuery("." + key + "_all", elm).addClass("checked");
							}
						}
					);
				}
			);
			
			//on click add class checked and remove it from other siblings
			jQuery('.privacyBtn').click(
				function () {
					elm = jQuery(this);
					elm.addClass('checked');
					elm.siblings().removeClass('checked');
					if (isIE) {
						jQuery("input", elm).attr("checked", true);
					}
					var visibility = { private : 0, shared : 0, registered : 0, public : 0 };
					jQuery.each(visibility,
						function(key, value) {
							if (elm.hasClass(key + "_all")) {
								elm.parents(".privacyGroup").find("." + key).trigger('click');
								elm.parents(".privacyGroup").find("." + key + " input").attr("checked", true);
							}
							else if (elm.hasClass(key)) {
								var allChecked = true;
								elm.parents(".privacyGroup").find("." + key).each(
									function () {
										if(!(jQuery(this).hasClass("checked"))) {
											allChecked = false;
										}
									}
								);
								if (allChecked) {
									elm.parents(".privacyGroup").find("." + key + "_all").addClass("checked");
								}
								else {
									elm.parents(".privacyGroup").find(".privacyGroupHeader .privacyBtn").removeClass("checked");
								}
							}
						}
					);
				}
			);
			
			//close/open the content of each section
			jQuery('.toggleGroup').click(
			 	function () {
					jQuery(".arrow_toggle", this).toggleClass("down");
					jQuery(this).parents(".privacyGroup").children(".groupContent").slideToggle();
				}
			 )
		}
		
		//toggle country_box
		if(jQuery(".country_box").length != 0) {			
			jQuery('.country_box .selected_country').click(
				function () {
					var elm = jQuery(this);
					elm.toggleClass("active");
					elm.next().toggle('blind',
						function () {
							if(elm.hasClass("active")) {
								jQuery('body').bind('click',
									function () {
										if(elm.hasClass("active")) {
											elm.toggleClass("active");
											elm.next().toggle('blind');
											jQuery(this).unbind();
										}
										else {
											jQuery(this).unbind();
										}
									}
								)
							}
						}, 500
					);
				}
			)
			jQuery('.country_box ul li a').click(
				function () {
					jQuery(".selected_country").toggleClass("active");
					jQuery(this).parents('ul').toggle('normal');
				}
			)
		}
		
		//login btn animation
		if(jQuery("#loginBtn").length != 0) {
			jQuery("#loginBtn").toggle(
				function () {
					var elm = jQuery(this);
					jQuery("body").prepend('<div class="overlay"></div>');
					jQuery(".overlay").css('height', jQuery('body').height() + 'px').animate({'opacity':'.5'});
					jQuery("#quicklogin").fadeIn('slow',
						function() {
							jQuery("#fe-login-username").focus();
							jQuery('.overlay').bind('click',
									function () {
										elm.trigger('click');
									}
							)
						}
					);
				},
				function () {
					var elm = jQuery(this);
					jQuery(".overlay").animate({'opacity':'0'});
					jQuery("#quicklogin").fadeOut('slow',
						function () {
							jQuery(".overlay").remove();
						}
					);
				}
			)
			jQuery('.loginTrigger').click(
				function () {
					if(jQuery("#js_modalwin:visible")) {
						jQuery(".js_modalwin_close").trigger('click');
					}
					jQuery('html').animate({scrollTop:0}, 'slow', function () {jQuery('#loginBtn').trigger('click');});
					return false;
				}
			)
		}
		/*jQuery(".js_secure").unbind();
		jQuery(".js_secure").removeAttr('onclick');
		jQuery(".js_secure").bind('click',
			function (event) {
				console.log(event);
				var elm = jQuery(this);
				var offset = elm.offset();
				jQuery("#js_modalwin").css({'left': offset.left + "px", 'top': (offset.top - 80) + "px"});
				jQuery("#js_modalwin").fadeIn("fast", 
					function () {
						jQuery("body").bind('click', 
							function () {
								jQuery("#js_modalwin").fadeOut('fast', function () {jQuery("body").unbind()});
							} 
						)
					}
				);
				jQuery(".js_modalwin_close").bind('click',
					function () {
						jQuery("#js_modalwin").fadeOut('fast', function () {jQuery("body").unbind()});
					}
				);
				return false;
			}
		)*/
		
		//change position animation
		if(jQuery("#change_position_btn").length != 0) {
			jQuery("#change_position_btn").click(
				function () {
					var elm = jQuery(this);
					elm.toggleClass("active");
					elm.next().toggle('blind',
						function () {
							if(elm.hasClass("active")) {
								jQuery('body').bind('click',
									function () {
										if(elm.hasClass("active")) {
											elm.toggleClass("active");
											elm.next().toggle('blind');
											jQuery(this).unbind();
										}
										else {
											jQuery(this).unbind();
										}
									}
								)
							}
						}
					);
				}
			);
			jQuery('#change_position_dropdown ul li a').click(
				function () {
					jQuery("#change_position_btn").trigger('click');
					jQuery("#change_position_btn").append('<strong class="preloader" style="display: block; position: absolute; right: 4px; top: 4px; width: 22px; height: 22px; background: url(img/misc/spinner.gif) center center no-repeat;"></strong>');
				}
			)
		}
		
		//forcing footer to bottom of page
		var window_H = jQuery(window).height();
		var body_H = jQuery('body').height();
		if(body_H < window_H) {
			jQuery('#content_wrapper').css('min-height', window_H-284+'px');
		}
		
});

Event.onReady(
function () {
$$('.filter').each(
	function (el) {
		Event.observe(el, 'mouseover', function() {
			var altImg = this.down('img').readAttribute('alt');
			var filterTooltip = new Element('div', { 'class': 'filterTooltip'})
			filterTooltip.innerHTML = '<span>' + altImg + '</span><div class="bg"></div><div class="arrow"></div';
			this.appendChild(filterTooltip);
			FTWidth = filterTooltip.getWidth();
			if (FTWidth > 240) {
				FTWidth = 240;
				filterTooltip.setStyle({whiteSpace: 'normal', width: FTWidth + 'px'});
			}
			FTHeight = filterTooltip.getHeight() + 5;
			filterTooltip.setStyle({
				marginLeft: '-' + (FTWidth/2) + 'px',
				marginTop: '-' + FTHeight + 'px'
			});
			filterTooltip.show();
		});
		Event.observe(el, 'mouseout', function() {
			if (this.down('.filterTooltip') != undefined) {
				this.down('.filterTooltip').hide().remove();
			}
		});
	}
);
});