SG.namespace("FV");

(function() {
	var DOM = YAHOO.util.Dom;
	FV = {
		selectedClassName : "selected",
		selectedMarker : null,
		hiddenMarker : null,

		hideSelectedRows : function(root) {
			var elements = DOM.getElementsByClassName(this.selectedClassName, "tr", root);
			DOM.removeClass(elements, this.selectedClassName);
			if (this.selectedMarker) {
				SG.geo.geoMap.map.removeOverlay(this.selectedMarker);
			}
			if (this.hiddenMarker) {
				this.hiddenMarker.show();
			}
		},

		selectRow : function(elements) {
			DOM.addClass(elements, this.selectedClassName);
		},

		selectRowInTabelle : function(el) {
			DOM.addClass(el, "selected");
			this.showVereinInErgebnisse(el);
			this.showVereinInMap(el);
		},

		selectRowInErgebnisse : function(el) {
			this.hideSelectedRows(el.parentNode);
			DOM.addClass(el, "selected");
			this.showVereineInTabelle(el);
			this.showVereinInMap(el);
		},

		showVereinInMap : function(el) {
			if (FV.displayMaps()) {
				if (SG.geo != null && SG.geo.geoMap != null && SG.geo.geoMap.markers != null) {
					var markers = SG.geo.geoMap.markers;
					var markerIds = SG.geo.geoMap.markerIds;
					
					var map = SG.geo.geoMap;
					var className;
					for ( var i = 0; i < markerIds.length; i++) {
						className = el.className.split(" ")[1];
						if( markerIds[i].indexOf(className) != -1 ) {
							this.hiddenMarker = markers[i];
							this.selectedMarker = SG.geo.geoMap._createMarker(markers[i].getPoint(), "", "", "", "",
									markers[i].getIcon().image, 16, 16, true, false, false, null, 32);
							markers[i].hide();
							SG.geo.geoMap.map.addOverlay(this.selectedMarker);
						}
					}
				}
			}
		},

		showVereinInErgebnisse : function(el) {
			var isElement = DOM.get(el) || el;
			var className = isElement ? el.id : el;
			this.selectRow(DOM.getElementsByClassName(className, "tr", "ergebnisse"));
		},

		showVereinInTabellen : function(verein) {
			this.selectRow(DOM.getElementsByClassName(verein, "tr"));
		},

		showVereineInTabelle : function(el) {
			var classNames = el.className.split(" ");
			this.selectRow( [ DOM.get(classNames[1]), DOM.get(classNames[2]) ]);
		},

		activateGoogleMaps : function() {
			$.cookie('displayMaps', 'true', {
				expires : 9999
			});
			location.reload();
		},

		deactivateGoogleMaps : function() {
			$.cookie('displayMaps', 'false', {
				expires : 9999
			});
			location.reload();
		},

		tearDownPage : function() {
			if (FV.displayMaps()) {
				try {
					GUnload();
				} catch (e) {
					// Maps could have been loaded or not.
				}
			}
		},

		displayMaps : function() {
			if ($.cookie('displayMaps') == null && FV.isIE6()) {
				return false;
			}
			return $.cookie('displayMaps') == null || $.cookie('displayMaps') == 'true';
		},
		
		isIE6 : function() {
			return jQuery.browser.msie == true && jQuery.browser.version < 7;
		}
		
		/*
		 * -- portal werbungen
		 */
		
		
	};
})();

/**
 * Get the value of a cookie with the given name.
 * 
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 * 
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 * 
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		}
		// CAUTION: Needed to parenthesize options.path and options.domain
		// in the following expressions, otherwise they evaluate to undefined
		// in the packed version for some reason...
		var path = options.path ? '; path=' + (options.path) : '';
		var domain = options.domain ? '; domain=' + (options.domain) : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [ name, '=', encodeURIComponent(value), expires, path, domain, secure ].join('');
	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for ( var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};