
var core = {

	arrConfig: {},

	init: function()	{
		core.config('root', doc_root); // doc_root variable comes from /core/base/view.php viewContent(); from inline-javascript
		core.config('contextMenu', 'contextMenu');
		
	}, // end init

	config: function(strKey, strValue)	{
	
		if(strKey)	{
		
			if(!strValue) {
				return core.arrConfig[strKey];
			}
			else {
			
				core.arrConfig[strKey] = strValue;
				
				if(strKey == 'intUserId')	{
					jQuery(doc_root + 'includes/ajax/user.info.php', {
						intUserId : strValue
					}, function(data, status) {
						core.config('arrUser', data);
					});
				}
				
				if(strKey == 'intCompanyId')	{
					var strUrl = core.config('root') + "includes/ajax/company.info.php?intCompanyId=" + strValue;
					var resRequest = YAHOO.util.Connect.asyncRequest('GET', strUrl, {
						success: function(objData)	{
							core.config('arrCompany', objData.responseText);
						}
					});
				}
				
			}
		}
		else	{
			return false;
		}	
	
	}, // end config

	menu:	{
		remove_deleted_users: function() {
			
			var ok = confirm("Sind Sie sicher, dass die gelöschten Firmen und Benutzer entgültig aus der Datenbank entfernt werden sollen?");
			
			if (ok) {
				jQuery.post(doc_root + "includes/ajax/core.menu.remove_deleted_users.php", {
					
				}, function(data, status) {
					core.menu.get();
				});
			}
			
		},
		get: function()	{
			jQuery.post(doc_root + "includes/ajax/core.menu.get.php", {
				
			}, function(data, status) {
				jQuery('#boxMenu').html(data);
				
				
				// make a collapsible tree
				jQuery('#boxMenu').treeview({
					collapsed: false
//					toggle: function() {
//						alert('you');	
//					}
					//unique: true
				});
				
				// add contextmenu events
				jQuery('.tmg').contextMenu('context-tmg', {
					bindings: {
						'add-company': function(t) {
							company.create();
						},
						'add-administrator': function(t) { 
							user.create('administrator');
						},
						'add-moderator': function(t) {
							user.create('moderator');
						}						
					}
				});				
				jQuery('.tree-company').contextMenu('context-company', {
					bindings: {
						'add-senior_expert': function(t) { 
							user.create('senior_expert', t.id);
						},						
						'view-company': function(t) {
							company.get(t.id, 'view');
						},
						'edit-company': function(t) { 
							company.get(t.id, 'edit');
						},
						'delete-company': function(t) {
							company.Delete(t.id);
						}
					}
				});				
				jQuery('.tree-administrator-root').contextMenu('context-administrator-root', {
					bindings: {
						'add-administrator': function(t) {
							user.create('administrator');
						},
						'add-moderator': function(t) {
							user.create('moderator');
						}
					}
				});				
				jQuery('.tree-administrator').contextMenu('context-administrator', {
					bindings: {
						'view-administrator': function(t) {
							user.get(t.id, 'view');
						},
						'edit-administrator': function(t) {
							user.get(t.id, 'edit');
						},
						'delete-administrator': function(t) {
							user.Delete(t.id);
						}
					}
				});				
				jQuery('.tree-moderator-root').contextMenu('context-moderator-root', {
					bindings: {
						'add-moderator': function(t) {
							user.create('moderator');
						}
					}
				});				
				jQuery('.tree-moderator').contextMenu('context-moderator', {
					bindings: {
						'view-moderator': function(t) {
							user.get(t.id, 'view');
						},
						'edit-moderator': function(t) { 
							user.get(t.id, 'edit');
						},
						'delete-moderator': function(t) {
							user.Delete(t.id);
						}
					}
				});				
				jQuery('.tree-senior_expert').contextMenu('context-senior_expert', {
					bindings: {
						'add-expert': function(t) {
							user.create('expert', t.id);
						},
						'view-senior_expert': function(t) {
							user.get(t.id, 'view');
						},
						'edit-senior_expert': function(t) { 
							user.get(t.id, 'edit');
						},
						'delete-senior_expert': function(t) {
							user.Delete(t.id);
						}
					}
				});				
				jQuery('.tree-expert').contextMenu('context-expert', {
					bindings: {
						'view-expert': function(t) {
							user.get(t.id, 'view');
						},
						'edit-expert': function(t) { 
							user.get(t.id, 'edit');
						},
						'delete-expert': function(t) {
							user.Delete(t.id);
						}
					}
				});				
//				$('.company, .expert, .employee, .administrator').contextMenu('myMenu1', {
//					bindings: {
//						'open': function(t) {
//							alert('Trigger was '+t.id+'\nAction was Open');
//						},
//						'email': function(t) {
//							alert('Trigger was '+t.id+'\nAction was Email');
//						},
//						'save': function(t) {
//							alert('Trigger was '+t.id+'\nAction was Save');
//						},
//						'delete': function(t) {
//							alert('Trigger was '+t.id+'\nAction was Delete');
//						}
//					}
//				});				
				
			});
			
//			YAHOO.util.Connect.asyncRequest('GET', core.config('root') + "includes/ajax/core.menu.get.php", {
//				success: function(objData)	{
//					document.getElementById('boxMenu').innerHTML = objData.responseText;
//				}
//			});
		} // end get
	
	}, // end menu
	
	tree:	{
	
		toggle: function(strName)	{
			var objImage = strName.parentNode.getElementsByTagName('img')[0];
			var objElement = strName.parentNode.getElementsByTagName('ul')[0];
			var strElementDisplay = objElement.style.display;
						
			if(objElement.style.display == 'none')	{
				objImage.src = core.config('root') + 'misc/icons/minus.gif';
				objElement.style.display = 'inline';
			}
			else	{
				objImage.src = core.config('root') + 'misc/icons/plus.gif';
				objElement.style.display = 'none';
			}
		} // end toggle
	
	}, // end tree
	
	popup_box : {
		
		show : function(header, content, type) {

			var img_icon = 'information.png';
			if ('info' == type) {
				
			}
	
			var img_tag = '<img src="'+ doc_root + 'misc/icons/' + img_icon + '" border="0" ' +
								'style="float:left; margin-top:10px; margin-right:10px; border:0px solid red; " />';
			
			content = img_tag + 
					'<div style="float:left; width:85%; margin-top:10px; margin-bottom:10px; border:0px solid green;">' + content + '</div>';
					
			jQuery('#info-box').prependTo('#mainCol');
			
			jQuery('#info-box-header').html("");
			jQuery('#info-box-content').html("");
			
			jQuery('#info-box').css({
				'width': 300,
				'z-index': 9999,
				'position' : 'absolute'
			});	
			
			jQuery('#info-box-header').html(header);
			jQuery('#info-box-content').html(content);
			jQuery('#info-box').fadeIn('normal');

			center_div = 'info-box'; // defined global
			
			util.position();
			
			core.popup_box.start_count_down(7);
		},
		
		
		/**
		 * countd down from seconds to zero and closes the info-box 
		 * @param {INT} seconds - Start of count down
		 */
		start_count_down : function (seconds) {
			
			var intervalId = setInterval(function() {
				jQuery('#visible-seconds-left').text(''+seconds+'');
				seconds --;
				if (seconds < 0) {
					clearTimeout(intervalId);
					util.close('info-box');
				}
			}, 1000); 
		}	
			
		
	},
	wymconfig : {
        lang: 'de',
        basePath: doc_root + 'extern/wymeditor/wymeditor/',
        stylesheet: doc_root + "templates/base/misc/styles/wymeditor/wym_stylesheet.css",
//		styles:                                                            
//	      '/* PARA: Date */                                                    '+
//	      'body {                                                            '+
//	      '  background-color: #fbfbfc;              '+
//	      '}',
        skinPath: doc_root + "templates/base/misc/styles/wymeditor/ewiwym/",
        skin: 'ewiwym',
		classesItems: [],
		logoHtml: '',
//				statusHtml: '<b>status test</b>',
		postInit: function(wym) { // init the plugins here
//					wym.hovertools();
			window.setTimeout(function() {
				jQuery('.wym_iframe iframe').contents().find('body').css({
					'background' : '#fbfbfc',
					'color' : '#666666'
				});
			}, 500);
		},
//		preInit : function() {
//			alert('preinit');
//		},
		preInitDialog :function(wym,wdw) {
			//alert('preinit DIALOG!');
			filebrowser.init(wym, wdw);
		},	
//		postInit : function(wym) {
//			//jQuery(wym._options.iframeSelector).css('height', '50px');
////            jQuery(wym._box).find(wym._options.iframeSelector)
////                .css('background-color', '#00FF00');
//			//jQuery('.wym_iframe iframe').contentDocument.body.innerHTML = 'test test ';//document.body.innerHTML = 'ldldld';//.attr('src', 'http://www.google.de');
//			
//		},
		toolsItems: [
			{'name': 'Bold', 'title': 'Strong', 'css': 'wym_tools_strong'}, 
			{'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'},
			{'name': 'Superscript', 'title': 'Superscript', 'css': 'wym_tools_superscript'},
			{'name': 'Subscript', 'title': 'Subscript', 'css': 'wym_tools_subscript'},
			{'name': 'InsertOrderedList', 'title': 'Ordered_List', 'css': 'wym_tools_ordered_list'},
			{'name': 'InsertUnorderedList', 'title': 'Unordered_List', 'css': 'wym_tools_unordered_list'},
			{'name': 'Indent', 'title': 'Indent', 'css': 'wym_tools_indent'},
			{'name': 'Outdent', 'title': 'Outdent', 'css': 'wym_tools_outdent'},
			{'name': 'Undo', 'title': 'Undo', 'css': 'wym_tools_undo'},
			{'name': 'Redo', 'title': 'Redo', 'css': 'wym_tools_redo'},
			{'name': 'CreateLink', 'title': 'Link', 'css': 'wym_tools_link'},
			{'name': 'Unlink', 'title': 'Unlink', 'css': 'wym_tools_unlink'},
			{'name': 'InsertImage', 'title': 'Image', 'css': 'wym_tools_image'},
			{'name': 'InsertTable', 'title': 'Table', 'css': 'wym_tools_table'},
			{'name': 'Paste', 'title': 'Paste_From_Word', 'css': 'wym_tools_paste'},
			{'name': 'ToggleHtml', 'title': 'HTML', 'css': 'wym_tools_html'}
//			,
//			{'name': 'Justify', 'title': 'Justify_Text', 'css': 'wym_tools_newbtn'}
		]
	},
	
	/**
	 * checks if bits is a administrators bitstring
	 * @param {} bitstring
	 */
	isAdministrator : function(bitstring) {
		return bitstring == core.getConfigRightBitstring('administrator');
	},

	/**
	 * checks if bits are moderators bitstring
	 * @param {} bitstring
	 */
	isModerator: function(bitstring) {
		return bitstring == core.getConfigRightBitstring('moderator');
	},
	
	/**
	 * Retrieves the rights-bitstring from the config array of the passed role
	 * in $role.
	 * 
	 * @param {String} $role - key of the rights 
	 * @return {String} Bitstring with rights
	 */	
	getConfigRightBitstring : function(role) {
		for (bitstring in core.arrConfig.user_rights) {
			if (role == core.arrConfig.user_rights[bitstring].key) {
				return bitstring;	
			}
		}
		
		return false;
	},
	
	color_picker : {
	
	
	}

}; // end prototype

function show_div(id) {
	jQuery.post(doc_root + 'includes/ajax/debug.session.get.php',{
	}, function(data, status) {
		jQuery('#'+id).html(data);	
		jQuery('#'+id).show();	
	});
}




//        // set our global counter
//        var counter = 0;
//        // function to raise the counter and then store the change in the history
//        function raiseCounter() {
//            counter++;
//            // store the counter inside an object such as {counter:0} along with extra to test speed
//            $.history( {'counter':counter, 'counter1':counter, 'counter2':counter, 'counter3':counter, 'counter4':counter} );
//            $('#counter').html('{\'counter\':' + counter.toString() + '}');
//        }
//        


$().ready(function() {	


	// global feedback on links 
	$.address.change(function(event) {  
	
		var identifier = event.value.substr(1);
		
		if (jQuery.trim(identifier) != '') {
			
			if (identifier.match(/^href/)) {
				var href_link = identifier.replace(/^href=/, '');
				//window.location.href = href_link;
			}
			if (identifier.match(/^js:/)) {
				var identifier_parts = identifier.split(':');
				identifier = identifier_parts[1];
				
				 //alert(identifier);
				eval(identifier);
				
				//window.location.href = href_link;
			}
			else {
				//alert('javascript identifiert aus DOM');	
				$('#' + identifier).trigger('click');  
			}
		}
		

	});	
	
	
	// load arrConfig in core
	$.post(doc_root + 'includes/ajax/core.arrconfig.get.php',{
	}, function(arrConfig, status) {
		core.arrConfig = arrConfig;
	}, 'json');
	
	// global inits 
	window.setTimeout(function() {
		
		// tooltips
		$('img, a').tooltip({
			track: true, 
			delay: 0, 
			showURL: false, 
			showBody: " - ", 
			fade: 250 		
		});
		

		
	}, 1500);
});

