// JavaScript Document

jQuery(function () {
		
		//tabs script
		if(jQuery('.tabsBox').length != 0) {
			jQuery('.tabsBox > ul').tabs('> .tabContent', { tabs: 'li' });		
		};//end if - tabs script
		
		//OSM bugs filter panel scripts & animations
		if(jQuery('#filters').length != 0) {
			jQuery('.filter input').change(
				function () {
					//submit changes on every click
					//jQuery('#bug_filter').trigger('click');
				}
			);
		};//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 dropdown_box used for LANGUAGE and COUNTRY boxes
		if(jQuery(".dropdown_box").length != 0) {
			jQuery('.dropdown_box .selected_item').click(
				function () {
					var elm = jQuery(this);
					if(!(elm.hasClass("animating"))) {
						elm.toggleClass("active");
						elm.addClass("animating");
						jQuery('.toggle_arrow', elm).toggleClass("active");
						elm.next().toggle('blind',
							function () {
								if(elm.hasClass("active")) {
									jQuery('body').bind('click',
										function () {
											if(elm.hasClass("active")) {
												elm.trigger('click');
												jQuery(this).unbind();
											}
											else {
												jQuery(this).unbind();
											}
										}
									)
								};
								elm.removeClass("animating");
							}, 500 
						);
						elm.next().find('li a').click(
							function () {
								elm.toggleClass("active");
								elm.next().toggle('normal');
							}
						);
					}
				}
			);
			
		};
		
		//login btn animation
		if(jQuery("#loginBtn").length != 0) {			
			jQuery("#quicklogin").dialog({
				autoOpen: false,
				draggable: false,
				zIndex: 3999,
				modal: true,
				position: ['center', 100],
				width: 420
			});
			jQuery("#loginBtn").click(
				function () {
					var elm = jQuery(this);
					jQuery('#quicklogin').dialog('open');
					jQuery('#quicklogin .dialog_bg').css({'height': jQuery('#quicklogin').outerHeight() + 'px'});
					jQuery("#fe-login-username").focus();
					jQuery('#quicklogin .dialogContent').css('z-index', '4100');
					jQuery('.ui-widget-overlay').bind('click',
							function () {
								jQuery(".ui-dialog-titlebar-close").trigger('click');
							}
					).animate({'opacity':'.3'});;
					return false;
				}
			);
			jQuery('.loginTrigger').click(
				function () {
					jQuery('#loginBtn').trigger('click');
					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");
					jQuery('#change_position_dropdown').toggle('blind',
						function () {
							if(elm.hasClass("active")) {
								jQuery('body').bind('click',
									function () {
										if(elm.hasClass("active")) {
											elm.toggleClass("active");
											jQuery('#change_position_dropdown').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>');
				}
			)
		};
		
		//intro section close/open slide
		if(jQuery("#handle").length != 0) {
			jQuery("#handle").click(
				function () {
					jQuery("span", this).toggleClass('close');
					jQuery("#intro_box").toggleClass('closeBox');
					jQuery("#intro_box").toggle('blind', 1000);
					if (jQuery("#intro_box").hasClass('closeBox')) {
						jQuery.cookie("cookieHomeIntroHide", 1, {expires: 7});
					}
					else {
						jQuery.cookie("cookieHomeIntroHide", 0, {expires: 7});
					}
					
				}
			);
		};
		
		//search bar input's width
		if(jQuery("#search").length != 0) {
			searchInputWidth();
			jQuery('.searchTab a').click(
				function () {
					if(!jQuery(this).parent().hasClass("searchTabOn")) {
						jQuery('.searchBox').toggle();
						jQuery(".searchTab").toggleClass("searchTabOn");
						searchInputWidth();
						jQuery(".searchResultsBox .btnClose").trigger('click');
						
					}
					return false;
				}
			);
		};
		
		//full width page scripts "/locations/" and "/OSMBugs/"
		if(jQuery(".full-width").length !=0) {
			
			//deactivating vertical scrollbar
			jQuery("html").css({
				'overflow-y': 'hidden'
			});
			
			if(jQuery(".MapDust").length != 0) {
				
				//adding heigh to content for full screen mode on MAPDUST
				jQuery(".full-width-container").css({
					'height': jQuery(window).height()- 144 + "px"
				});
				jQuery('.full-width-container #map').css({
					'width': jQuery(".full-width-container").outerWidth() - jQuery('.leftPan').outerWidth() - 2 /* border */ + 'px'
				});
				jQuery('.leftPan').css({
					'height': jQuery(window).height()- 144 - 4 + 'px'
				});
				jQuery('.leftPan-trigger').css({
					'height': jQuery(".leftPan").outerHeight() + 'px'
				});
				jQuery(window).resize(
					function () {
						jQuery(".full-width-container").css({
							'height': jQuery(window).height()-144 + "px"
						});
						jQuery('.full-width-container #map').css({
							'width': jQuery(".full-width-container").outerWidth() - jQuery('.leftPan').outerWidth() - 2 /* border */ + 'px'
						});
						jQuery('.leftPan').css({
							'height': jQuery(window).height()- 144 - 4 + 'px'
						});
						jQuery('.leftPan-trigger').css({
							'height': jQuery(".leftPan").outerHeight() + 'px'
						});
					}
				);
			}
			
			else {			
				//adding heigh to content for full screen mode
				jQuery(".full-width-container").css({
					'height': jQuery(window).height()- 144 + "px"
				});
				jQuery('.full-width-container #map').css({
					'width': jQuery(".full-width-container").outerWidth() - jQuery('.leftPan').outerWidth() - 2 /* border */ + 'px'
				});
				jQuery('.leftPan').css({
					'height': jQuery(window).height()- 144 - 4 + 'px'
				});
				jQuery('.leftPan-trigger').css({
					'height': jQuery(".leftPan").outerHeight() + 'px'
				});
				jQuery(window).resize(
					function () {
						jQuery(".full-width-container").css({
							'height': jQuery(window).height()-144 + "px"
						});
						jQuery('.full-width-container #map').css({
							'width': jQuery(".full-width-container").outerWidth() - jQuery('.leftPan').outerWidth() - 2 /* border */ + 'px'
						});
						jQuery('.leftPan').css({
							'height': jQuery(window).height()- 144 - 4 + 'px'
						});
						jQuery('.leftPan-trigger').css({
							'height': jQuery(".leftPan").outerHeight() + 'px'
						});
					}
				);
				//animate the header and footer on page load
				jQuery(".header").delay(800).animate({"top": "-110px"}, "slow", function () {jQuery(".header").hide();});
				jQuery(".footer").delay(800).animate({"bottom": "-316px"}, "slow", function () {jQuery(".footer").hide();});			
			
				//trigger the header and footer on menu btn click
				jQuery('.header-trigger').click(
					function () {
						var elm = jQuery(this);
						if(!elm.hasClass('active')) {
							jQuery("body").prepend('<div id="headerActiveOverlay" class="overlay" style="z-index: 999;"></div>');
							jQuery("#headerActiveOverlay").css('height', jQuery('body').height() + 'px').animate({'opacity':'.5'});
							elm.delay(100).animate({"top": "+=100px"}, "slow");
							jQuery(".footer").delay(1).animate({"bottom": "+=316px"}, "slow").show();
							jQuery(".header").delay(1).animate({"top": "+=110px"}, "slow",
								function () {
									if(elm.hasClass("active")) {
										jQuery('#headerActiveOverlay').bind('click',
											function () {
												if(elm.hasClass("active")) {
													elm.toggleClass("active");
													elm.find('.toggle_arrow').toggleClass('active');
													jQuery('.toggle_arrow', this).toggleClass('active');
													jQuery(".header").animate({"top": "-=110px"}, "slow", function () {jQuery(".header").hide();});
													jQuery(".footer").animate({"bottom": "-=316px"}, "slow", function () {jQuery(".footer").hide();});
													elm.animate({"top": "-=100px"}, "normal");
													jQuery("#headerActiveOverlay").animate({'opacity':'0'}).delay(800).remove();
												}
											}
										);
									}
								}
							).show();
						}
						else {
							jQuery(".header").animate({"top": "-=110px"}, "slow", function () {jQuery(".header").hide();});
							jQuery(".footer").animate({"bottom": "-=316px"}, "slow", function () {jQuery(".footer").hide();});
							elm.animate({"top": "-=100px"}, "normal");
							jQuery("#headerActiveOverlay").animate({'opacity':'0'}).delay(800).remove();
						}
						elm.toggleClass('active');
						jQuery('.toggle_arrow', this).toggleClass('active');
					}
				);
			}
			
			//Left Pan slide animation
			jQuery('.leftPan-trigger').click(
				function () {
					var map_center = new OpenLayers.LonLat();
					map_center = osb_map.getCenter();

					var map_bounds = new OpenLayers.Bounds();
					map_bounds = osb_map.calculateBounds();

					var zoom = osb_map.getZoom();

					if(jQuery(this).hasClass('active')) {
						jQuery('.full-width-container #map').css({
							'width': jQuery(".full-width-container").outerWidth()-25 /* leftPan-trigger width */ - 2 /* border */ + 'px'
						});
						jQuery('.leftPanContentContainer').animate({'width': '0'}, 800, function () {jQuery(this).css("display", "none");});
						osb_map.setCenter(map_center);
						refresh_osb();
						 

					}
					else {
						jQuery('.leftPanContentContainer').css("display", "block").animate({'width': '499px'}, 800,
							function () {
								jQuery('.full-width-container #map',
										function(){
											osb_map.zoomToExtent(map_bounds);
											osb_map.setCenter(map_center, zoom);
										}
									  ).css({'width': jQuery(".full-width-container").outerWidth() - jQuery('.leftPan').outerWidth() - 2 /* border */ + 'px'
								} );
							}
						);

						
					}
					jQuery(this).toggleClass('active');
					jQuery('.toggle_arrow', this).toggleClass('active');
					
					
				}
			);
			
			//Left Pan section animation
			var leftPan_sectionHeader = false;
			jQuery('.sectionHeader a.sectionHeaderTrigger').click(
				function () {
					if(!leftPan_sectionHeader) {
						leftPan_sectionHeader = true;
						jQuery(this).parent().next().slideToggle('normal', function () {leftPan_sectionHeader = false;});
						jQuery('.toggle_arrow', this).toggleClass('active');
					}
				}
			)
		};
		
		//animation for legend on bug details page
		if(jQuery('#legenda_btn').length != 0) {
			jQuery('#legenda_btn').click(
				function () {
					jQuery(this).prev().slideToggle('normal');
					jQuery('.toggle_arrow', this).toggleClass('active');
				}
			)
		};
		
		if(jQuery('.dialog').length != 0) {
			jQuery('.dialog').dialog({
				autoOpen: false,
				draggable: false,
				zIndex: 3999
			});
			jQuery('.opener').click(function() {
				var elm = jQuery(this).attr('href');

				jQuery('.dialog').dialog('close');
				jQuery(elm).dialog('open');
				jQuery(elm + ' .dialog_bg').css({'height': jQuery(elm).outerHeight() + 'px'});
				return false;
			});
			jQuery('.closer').click(function() {
					var elm = jQuery(this).attr('href');
				jQuery('.dialog').dialog('close');
				return false;
			});
		};

		jQuery('#authenticationForm').keydown(function(e){
			if (e.keyCode == 13) {
				jQuery(this).submit();
				return false;
			}

		});
});

//search bar input's width
function searchInputWidth() {
	jQuery("#search input.txt, #search input.text").each(
		function() {
			var thisWidth = parseInt(jQuery(this).parent().outerWidth());
			var thisPaddingLeft = parseInt(jQuery(this).prev().outerWidth());
			jQuery(this).css({
				'width': thisWidth - thisPaddingLeft -3 + "px",
				'padding-left': thisPaddingLeft + 2 + "px"
			});
		}
	);
};


//get new captcha
function  refreshCaptcha(captcha_url , elm)
{
 	if(jQuery(elm  + ' .js_captcha_img'))
	{
		jQuery(elm + ' .js_captcha_img').attr("src", captcha_url + '/'+ elm.replace(/#/g, '' ) +'?dummy='+parseInt(Math.random()*999666));
		jQuery(elm + ' .js_captcha_value').val("");
	}
 
	return false;
}
