// JavaScript Document

document.write('<link rel="stylesheet" type="text/css" href="fileadmin/template/jquery/css/custom-theme/css.css" media="all" />');

comPopup = function(url) {
  window.open(url,'','menubar=yes,scrollbars=yes,resizable=yes,width=720,height=540');
}

/* community */
comAll  = function() {
	jQuery('.accordion a.toggle').click(function() {
		jQuery(this).toggleClass("expanded");
		if (jQuery(this).hasClass("expanded")) {
			jQuery(this).find("input").val("1");
		} else {
			jQuery(this).find("input").val("0");
		}
		jQuery(this).next().toggle('slow');
		return false;
	}).next().hide();	
	//jQuery('.accordion a.toggle:first').toggleClass("expanded");	
	//jQuery('.accordion a.toggle:first').next().toggle();
	
	jQuery('.accordion a.switch').click(function() {
		jQuery(this).toggleClass("switchoff");	
		if (jQuery(this).hasClass("switchoff")) {
			jQuery(this).next().val("1");
		} else {
			jQuery(this).next().val("0");				
		}
		return false;
	});
	
	jQuery(function($){
		jQuery.datepicker.regional['de'] = {
			closeText: 'schließen',
			prevText: '&#x3c;zurück',
			nextText: 'Vor&#x3e;',
			currentText: 'heute',
			monthNames: ['Januar','Februar','März','April','Mai','Juni',
			'Juli','August','September','Oktober','November','Dezember'],
			monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
			'Jul','Aug','Sep','Okt','Nov','Dez'],
			dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
			dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			dateFormat: 'dd.mm.yy', firstDay: 1,
			isRTL: false};
		jQuery.datepicker.setDefaults($.datepicker.regional['de']);
	});	
	
	jQuery('.datepicker').datepicker();
	

}
comSimpleDialog = function() {
	jQuery('#comdialog').click(function() {
		jQuery('#comdialog-form').dialog('open');				
	});

	jQuery("#comdialog-form").dialog({
		autoOpen: false,
		height: 350,
		width: 350,
		modal: true,
		buttons: {
			'Speichern': function() {
				/*var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(name,"username",3,16);
				
				if (bValid) {
					$('#users tbody').append('<tr>' +
						'<td>' + name.val() + '</td>' + 
						'<td>' + email.val() + '</td>' + 
						'<td>' + password.val() + '</td>' +
						'</tr>'); 
					jQuery(this).dialog('close');
				}*/
				jQuery("#comdialog-form form").submit();
				jQuery(this).dialog('close');
			},
			'Abbrechen': function() {
				jQuery(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		},
		open: function(event, ui) { 
			jQuery(".ui-dialog-titlebar-close").hide(); 
		}

	});	
}
comExtendedDialog = function(openTo) {
	jQuery("#comdialoge-form").dialog({
		autoOpen: false,
		height: 600,
		width: 450,
		modal: true,
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		},
		open: function(event, ui) { 
			jQuery(".ui-dialog-titlebar-close").hide(); 
		}

	});
	
	if (openTo == "edit") {
		jQuery("#comdialoge-form").dialog("option", "buttons", {
			'Speichern': function() {
				jQuery("#comdialoge-form form").submit();
				jQuery(this).dialog('close');
			},
			'Abbrechen': function() {
				jQuery(this).dialog('close');
			},
			'Löschen': function() {
				jQuery("#comguestdelete").val("1");
				jQuery("#comdialoge-form form").submit();
				jQuery(this).dialog('close');
			}
		});	
	} else {
		jQuery("#comdialoge-form").dialog("option", "buttons", {
			'Speichern': function() {
				jQuery("#comdialoge-form form").submit();
				jQuery(this).dialog('close');
			},
			'Abbrechen': function() {
				jQuery(this).dialog('close');
			}
		});	
	}	
}
comCheck = function() {
	jQuery(document).ready(function() {
		comAll();
		comSimpleDialog();

		jQuery('.accordion a.switchhide').click(function() {
    		jQuery(this).next().val("1");
			jQuery(this).parent().parent().hide();
			return false;
		});

	});	
}
comGuest = function(openTo) {
	jQuery(document).ready(function() {
		comAll();
		comSimpleDialog();
		comExtendedDialog(openTo);
		
		if (openTo != 'no') {
			jQuery('#comdialoge-form').dialog('open');	
		}	
		
		jQuery(".clicker td").click(function() {
			url = jQuery(this).parent().find("a").attr("href");
			document.location.href = url;
		});
	});	
}
comBudget = function() {
	jQuery(document).ready(function() {
		comAll();
		comSimpleDialog();
	});	
}	
comMemo = function() {
	jQuery(document).ready(function() {
		comAll();

		jQuery('.accordion a.switchhide').click(function() {
			jQuery(this).next().val("1");
			jQuery(this).parent().parent().hide();
			return false;
		});
		jQuery('.accordion a.switchhide2').click(function() {
			jQuery(this).next().val("1");
			jQuery(this).next().next().hide();
			jQuery(this).hide();
			return false;
		});
	});	
}					

/* regular */
msBookmarks = function() {
	jQuery(".tellafriend").hide();
	jQuery(".toolbar").removeClass("toolbarhigher");
	jQuery(".toolbar").toggleClass("toolbarhigh", 500);		
	jQuery(".bookmarks").toggle("slow");	
}
msTellafriend = function() {
	jQuery(".bookmarks").hide();
	jQuery(".toolbar").removeClass("toolbarhigh");
	jQuery(".toolbar").toggleClass("toolbarhigher", 500);		
	jQuery(".tellafriend").toggle("slow");	
}
msMemorize = function() {
	target = jQuery(".memorize .target").val();		
	jQuery.post(target + '?source=memorize', jQuery(".memorize").serialize(), function(data) {
		response = nodeText(data.firstChild);
		if (response == "Please log in first") {
			window.location.href = "/de/planungstools/login.html";
		} else {
			alert(response);			
		}
		
	});	
}
msBusinessContact = function() {	
	jQuery(".businessquote").toggle("slow");	
}

nodeText = function(node) {
	children = node.childNodes;
	for (i = 0; i < children.length; i++) {
		if (children[i].nodeType == 3) {
			return children[i].nodeValue;	
		}
	}
}

msPopup = function(url) {
  window.open(url,'','menubar=yes,scrollbars=yes,resizable=yes,width=720,height=540');
}

function checkLength(o,n,min,max) {
	if ( o.val().length > max || o.val().length < min ) {
		o.addClass('ui-state-error');
		updateTips("Length of " + n + " must be between "+min+" and "+max+".");
		return false;
	} else {
		return true;
	}
}

jQuery(document).ready(function() {
	// date field of register form							
	jQuery(function($){
		$.datepicker.regional['de'] = {
			closeText: 'schließen',
			prevText: '&#x3c;zurück',
			nextText: 'Vor&#x3e;',
			currentText: 'heute',
			monthNames: ['Januar','Februar','März','April','Mai','Juni',
			'Juli','August','September','Oktober','November','Dezember'],
			monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
			'Jul','Aug','Sep','Okt','Nov','Dez'],
			dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
			dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			dateFormat: 'dd.mm.yy', firstDay: 1,
			isRTL: false};
		$.datepicker.setDefaults($.datepicker.regional['de']);
	});	
	
	jQuery('#tx-srfeuserregister-pi1-date_of_birth').datepicker();


	// tell a friend	
	jQuery(".tellafriend a").click(function() {
		target = jQuery(".tellafriend .target").val();		
		jQuery.post(target + '?source=tellafriend', jQuery(".tellafriend form").serialize(), function(data) {
			//$(this).addClass("done");
			alert("Vielen Dank. Die Nachricht wurde verschickt.");  
			jQuery(".tellafriend").toggle("slow");
			jQuery(".toolbar").toggleClass("toolbarhigher", 500);		
		});								
	});

	// business quote
	jQuery(".businessquote a").click(function() {
		target = jQuery(".businessquote .target").val();		
		jQuery.post(target + '?source=businessquote', jQuery(".businessquote form").serialize(), function(data) {
			response = nodeText(data.firstChild);
			if (response == "1") {																							  
				alert("Vielen Dank. Die Nachricht wurde verschickt.");  
				jQuery(".businessquote").toggle("slow");	
			} else {
				alert(response);
			}
		});								
	});


	// deprecated							
    /*jQuery.getFeed({
        url: '/fileadmin/proxy.php?url=http://www.hovinne.com/feed/',
        success: function(feed) {
            var html = '';
            for(var i = 0; i < feed.items.length && i <= 6; i++) {
                var item = feed.items[i];
                html += '<li><a href="' + item.link + '">' + item.title + '</a></li>';
                //html += '<div class="updated">' + item.updated + '</div>';
                //html += '<div>' + item.description + '</div>';
            }
			html += '</ul>';
            jQuery('#result').append(html);
        }    
    });*/


	// region selector on front page
	jQuery.widget("ui.combobox", {
		_create: function() {
			var self = this;
			var selecte = this.element.hide();
			var input = jQuery("<input>")
				.insertAfter(selecte)
				.autocomplete({
					source: function(request, response) {
						var matcher = new RegExp(request.term, "i");
						response(selecte.children("option").map(function() {
							var text = jQuery(this).text();
							if (this.value && (!request.term || matcher.test(text)))
								return {
									id: this.value,
									label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + jQuery.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
									value: text
								};
						}));
					},
					delay: 0,
					select: function(event, ui) {
						if (ui.item) {
							jQuery("#frontregionlink").val(ui.item.id);
							selecte.val(ui.item.id);
						}
					},
					change: function(event, ui) {
						if (!ui.item) {
							// remove invalid value, as it didn't match anything
							jQuery(this).val("");
							return false;
						}
						selecte.val(ui.item.id);
						self._trigger("selected", event, {
							item: selecte.find("[value='" + ui.item.id + "']")
						});
						
					},
					minLength: 0
				});
			//	.addClass("ui-widget ui-widget-content ui-corner-left");
			if (selecte.val() == "") {
				input.val(this.element.find("option").first().html());
			} else {
				input.val(selecte.find("option:selected").text());
				jQuery("#frontregionlink").val(selecte.val());
			}
			input.addClass("text-default-bold");
			
			var button = jQuery("<button>&nbsp;</button>")
			.attr("tabIndex", -1)
			.attr("title", "Show All Items")
			.insertAfter(input)
			.button({
				text: false
			});
			//
			button.removeClass("ui-corner-all");
			button.click(function() {
				// close if already visible
				if (input.autocomplete("widget").is(":visible")) {
					input.autocomplete("close");
					return false;
				}
				// pass empty string as value to search for, displaying all results
				input.autocomplete("search", "");
				input.focus();
				return false;
			});
		}
	});
	
	jQuery("#combobox").combobox();
	jQuery(".combobox").combobox();
	
	jQuery("#frontregionsearch").click(function() {
		goto = jQuery("#frontregionlink").val();
		if (goto != "") {
			document.location.href = "/" + goto;	
		}
	});
});

showTwitter = function(eid, user, maxitems) {
	jQuery(document).ready(function() {
		jQuery("#" + eid).getTwitter({
			userName: user,
			numTweets: maxitems,
			slideIn: false,
			slideDuration: 750,
			showHeading: false,
			showProfileLink: false,
			showTimestamp: false
		});
	});		
}

showFeed = function(eid, feed, maxitems) {
	jQuery(document).ready(function() {
		jQuery.getFeed({
			url: '/fileadmin/proxy.php?url=' + feed,
			success: function(feed) {
				var html = '';
				for(var i = 0; i < feed.items.length && i < maxitems; i++) {
					var item = feed.items[i];
					html += '<li><a href="' + item.link + '">' + item.title + '</a></li>';
					//html += '<div class="updated">' + item.updated + '</div>';
					//html += '<div>' + item.description + '</div>';
				}
				html += '</ul>';
				jQuery("#" + eid).append(html);
			}    
		});	
	});
}



diaShow = function(link, key, source, height) {
	html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="615" height="' + height + '" id="show" align="middle">';
	html = html + '<param name="allowScriptAccess" value="sameDomain" />';
	html = html + '<param name="allowFullScreen" value="false" />';
	html = html + '<param name="movie" value="/fileadmin/template/flash/show' + height + '.swf?link='+link+'&key='+key+'&src='+source+'" />';
	html = html + '<param name="quality" value="high" />';
	html = html + '<param name="bgcolor" value="#ffffff" />';
	html = html + '<param name="wmode" value="transparent" />';
	html = html + '<embed src="/fileadmin/template/flash/show' + height + '.swf?link='+link+'&key='+key+'&src='+source+'" quality="high" wmode="transparent" bgcolor="#ffffff" width="615" height="' + height + '" name="show" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	html = html + '</object>';
	document.write(html);
}

mapFlash = function() {
	html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="590" height="300" id="flash" align="left">';
	html = html + '<param name="allowScriptAccess" value="sameDomain" />';
	html = html + '<param name="movie" value="/fileadmin/template/fcmap/clientMap.swf" />';
	html = html + '<param name="menu" value="true" />';
	html = html + '<param name="quality" value="high" />';
	html = html + '<param name="scale" value="noscale" />';
	html = html + '<param name="salign" value="lt" />';
	html = html + '<embed src="/fileadmin/template/fcmap/clientMap.swf" menu="true" quality="high" scale="noscale" salign="lt"  width="590" height="300" name="flash" align="left" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	html = html + '</object>';
	document.write(html);
}
