/**
 * XQTools Plugin for jQuery
 * http://www.noname-media.com
 * 
 * Copyright 2011, Andi Heinkelein
 * 
 * Licensed under the MIT License
 * http://www.noname-media.com/license
 * 
 */
(function($) {
	
	$.fn.xqLayer = function(options) {
	
		var defaults = {
		
			api: true,
			sticky: 'fixed',
			posx: 'center',
			posy: '10%',
			offset: [0, 0],
			zindex: 99999,
			closeOnClick: false,
			autoClose: null,
			className: 'xqLayer',
			effect: 'default',
			
			template: '<div></div>',
			contentSelector: ''
		};
		
		var options = $.extend(defaults, options, {
		
			version: '0.1'
		});
		
		var autoCloseTimer;
		
		if (this.length > 0) {
		
			function xqLayer() {
			
				var self = this;
				var $layer;
				var effects = {};
			
				/* +---------- plugin author functions ----------+ */
			
				self.getConf = function(confName) {
				
					return options[confName];
				};
				
				self.setConf = function(confName, confValue) {

					options[confName] = confValue;
					return options[confName];
				};
				
				self.addEffect = function(effectName, fnShow, fnHide) {
					
					effects[effectName] = {};
					effects[effectName]['show'] = fnShow;
					effects[effectName]['hide'] = fnHide;
				};
				
				
				
				for (var f in options) {
				
					if (typeof options[f] == 'function') {
					
						self[f] = options[f];
					}
				}
				
				/* +---------- plugin properties ----------+ */
				
				
				/* +---------- plugin methods ----------+ */
				
				self.setup = function() {
				
					if ($selector.get(0) == document) {
						
						$selector = $('body');
					}
					
					$layer = $(options.template).appendTo($selector);
					
					//Add css class
					$layer.addClass(options.className);
					
					$layer.css({
						
						'position': options.sticky
					});
					
					if (options.closeOnClick) {
						
						$layer.click(function() {
							
							self.hide();
						});
					}
					
					self.show();
				};
				
				/**
				 * Show the layer
				 * 
				 * @param string layer content to replace the content
				 */
				self.show = function(layerContent) {
					
					//Insert layer content if any
					if (layerContent) {
						
						self.content(layerContent);
					}
					
					//calculate posLeft
					var posLeft = 0;
					var posTop = 0;

					if (options.sticky == 'fixed' || options.sticky == 'absolute') {
						
						//IE6 dosn't supports position:fixed
						if ($.browser.msie && $.browser.version <= 6)
							options.sticky = 'absolute';
						
						var sOffset = $selector.offset();
						
						switch (options.posx) {
							
							case 'left':
								posLeft = sOffset.left - (options.sticky == 'fixed' ? window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft : 0);
								break;
							case 'left-out':
								posLeft = sOffset.left - (options.sticky == 'fixed' ? window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft : 0) - $layer.outerWidth();
								break;
							case 'center':
							case 'middle':
								posLeft = (sOffset.left - (options.sticky == 'fixed' ? window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft : 0) + (($selector.width() - $layer.width()) / 2));
								break;
							case 'right':
								posLeft = sOffset.left - (options.sticky == 'fixed' ? window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft : 0) + $selector.width() - $layer.width();
								break;
							case 'right-out':
								posLeft = sOffset.left - (options.sticky == 'fixed' ? window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft : 0) + $selector.outerWidth();
								break;
							default:
								posLeft = 0;
						}
						
						switch (options.posy) {
							
							case 'top':
								posTop = sOffset.top - (options.sticky == 'fixed' ? window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop : 0);
								break;
							case 'top-out':
								posTop = sOffset.top - (options.sticky == 'fixed' ? window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop : 0) - $layer.outerHeight();
								break;
							case 'center':
								posTop = (sOffset.top - (options.sticky == 'fixed' ? window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop : 0) + (($selector.height() - $layer.height()) / 2));
								break;
							case 'bottom':
								posTop = sOffset.top - (options.sticky == 'fixed' ? window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop : 0) + $selector.height() - $layer.height();
								break;
							case 'bottom-out':
								posTop = sOffset.top - (options.sticky == 'fixed' ? window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop : 0) + $selector.outerHeight();
								break;
							default:
								posTop = 0;
						}
					}
						
					$layer.css({
						
						left: posLeft + options.offset[0],
						top: posTop + options.offset[1],
						position: options.sticky,
						zIndex: options.zindex
					});
					
					
					//$layer.show(effects['default'].show);
					effects[options.effect].show();
					
					//Set autoclose
					if (options.autoClose) {
						
						//Remove autoclose timer if any
						if (autoCloseTimer) {
							
							window.clearTimeout(autoCloseTimer);
							autoCloseTimer = null;
						}
						
						//Set autoclose timer
						autoCloseTimer = window.setTimeout(function() {

							self.hide();
						}, parseInt(options.autoClose) * 1000);
						
					}
				}
				
				/**
				 * Hide the layer
				 * 
				 * @return void
				 */
				self.hide = function() {
					
					effects[options.effect].hide();
					
					//Remove autoclose timer if any
					if (autoCloseTimer) {
						
						window.clearTimeout(autoCloseTimer);
						autoCloseTimer = null;
					}
				};
				
				/**
				 * Insert content into layer
				 * 
				 * @param string content to insert into the layer
				 * @return void
				 */
				self.content = function(layerContent) {
					
					//Check content selector
					if (options.contentSelector && $layer.find(options.contentSelector).length > 0) {
						
						var $content = $layer.find(options.contentSelector);
					}
					else {
						
						var $content = $layer;
					}
						
					$content.empty();
					$content.append(layerContent);
				};
				
				/**
				 * Add a custom show/hide effect
				 * 
				 * @param string effectName
				 * @param function show effect
				 * @param function hide effect
				 * @return void
				 */
				self.addEffect('default', function() {
					
					if ($layer.is(':hidden')) {
						
						$layer.animate({
							
							top: '-=10px'
						}, 0).animate({
							
							opacity: 1,
							top: '+=10px'
						}, 300);
					}
					
					$layer.show();
					
				}, function() {
					
					$layer.animate({
						
						opacity: 0,
						top: '-=10'
					}, 300, function() {
						
						$layer.hide();
					});
				});
				
				/* +---------- plugin construct ----------+ */
				
				self.setup();
			}
			
			
			var plugin;
			var $selector = this;
			
			this.each(function() {
				
				plugin = new xqLayer();
			});
		}
		
		return options.api ? plugin : this;
	
	};
	
 })(jQuery);

/**
 * XQ Request Link
 * 
 * 
 */
(function($) {
	
	$.fn.xqRequestLink = function(options) {
		
		try {
		
			var defaults = {
					
				'version': 			'0.2',
				'dumpmode': 		true,
				'requestURL': 		'',
				'sendMethod': 		'POST',
				'className':		'xqRequestLink',
				'loadAnim':			'/images/load1.gif',
				'showLayer': true,
				'layer': {
				
					
				},
				'layerParent': 'body',
				'messages': {
					
					'send': 'Send request ...',
					'error': 'Request failed!'
				},
				'onClick': 	function() {},
				'onResponse': function() {}
			};
			
			var options = $.extend(defaults, options);
			
			function setOption(name, value) {
				
				options[name] = value;
			}
			
			this.each(function() {
				
				var $self = $(this);
				var self = this;
				
				var $layer;
				
				$self.bind('click', function() {
					
					$current = $(this);
					
					$current['_onClick'] = options.onClick;
					$current['setOption'] = setOption;
					$current._onClick();
					
					var requestURL = options.requestURL || $current.attr('href');
					
					if (requestURL) {
						
						//Show layer
						if (options.showLayer && $.fn.xqLayer) {
							
							$layer = $(options.layerParent).xqLayer($.extend(options.layer, {
								
								posx: 'center',
								posy: 'center'
							}));
							
							$layer.content(options.messages.send);
						}
						
						function onResponse(data, textStatus) {
							
							if (data.error_message) {
								
								$.xqTools.error(data.error_message + ' in file ' + data.error_file + ' in line ' + data.error_line);
							}
							
							//Hide layer
							if ($layer)
								$layer.hide();
							
							//Call onResponse Function
							//options.onResponse($current, data, textStatus);
							$current['onResponse'] = options.onResponse;
							$current.onResponse(data, textStatus);
						}
						
						eval('var data = {link_name: "' + $current.attr('name') + '", link_url: "' + $current.attr('url') + '", link_id: "' + $current.attr('id') + '", link_css_class: "' + $current.attr('class') + '", link_rel: "' + $current.attr('rel') + '", link_rev: "' + $current.attr('rev') + '"};');
						
						if (options.sendMethod == 'POST') {
							
							$.post(requestURL, data, onResponse, 'json');
						}
						else {
							
							$.get(requestURL, data, onResponse, 'json');
						}
					}
					
					return false;
				});
			});
		}
		catch(e) {
			
			$.xqTools.error(e);
		}
	};
})(jQuery);

/**
 * XQTools - Category Tree plugin for jQuery
 * http://www.noname-media.com
 * 
 * Copyright 2011, Andi Heinkelein
 * 
 * Licensed under the MIT License
 * http://www.noname-media.com/license
 * 
 */
(function($) {
	
	$.fn.xqCategoryTree = function(options) {
	
		var defaults = {
		
			api: true,
			
			onClick: function() {},	//Call event on branch click, Params: selector
			onRename: function() {} //Call event on branch rename, Params: selector, oldName, newName
		};
		
		var options = $.extend(defaults, options, {
		
			version: '0.1'
		});
		
		
		if (this.length > 0) {
		
			function xqCategoryTree() {
			
				var self = this;
			
				/* +---------- plugin author functions ----------+ */
			
				self.getConf = function(confName) {
				
					return options[confName];
				};
				
				self.setConf = function(confName, confValue) {

					options[confName] = confValue;
					return options[confName];
				};
				
//				self.addEffect = function(effectName, fnShow, fnHide) {
//					
//					effects[effectName] = {};
//					effects[effectName]['show'] = fnShow;
//					effects[effectName]['hide'] = fnHide;
//				};
				
				
				
				for (var f in options) {
				
					if (typeof options[f] == 'function') {
					
						self[f] = options[f];
					}
				}
				
				/* +---------- plugin properties ----------+ */
				
				self.activeBranch = null;
				
				/* +---------- plugin methods ----------+ */
				
				self.setup = function() {

					$('li>span', $tree).live('click', function(e) {
						
						self.setStatus(this, 'active', !e.ctrlKey);
						
						//Call custom onClick function
						if (self.onClick)
							self.onClick(this);
					});
				};
				
				self.remove = function(selector) {
					
					if (selector == undefined)
						selector = 'li>span.active';
					
					$(selector, $tree).parent('li').remove();

					return self;
				};
				
				self.rename = function(selector, newName) {
					
					if (selector == undefined)
						selector = $('li>span.active');
					
					var oldName = $(selector).text();
					
					$(selector).text(newName);
					
					//Call custom event
					if (options.onRename)
						self.onRename(selector, oldName, newName);
					
					return self;
				};
				
				self.insert = function(selector, newName, insertMethod) {
					
					if (selector == undefined)
						selector = 'li>span.active';
					
					var $branch = $(selector).eq(0);
					if ($branch.get(0).nodeName.toLowerCase() == 'span') {
						
						$branch = $branch.parent('li:eq(0)');
					}
					
					if (insertMethod == 'after') {
						
						$('<li><span>' + newName + '</span></li>').insertAfter($branch);
					}
					else if (insertMethod == 'before') {
						
						$('<li><span>' + newName + '</span></li>').insertBefore($branch);
					}
					else if (insertMethod == 'prepend') {
					
						if ($branch.find('ul').length == 0) {
		
							$('<ul><li><span>' + newName + '</span></li></ul>').appendTo($branch);
						}
						else {
							
							$('<li><span>' + newName + '</span></li>').prependTo($branch.find('ul'));
						}
					}
					else {
					
						if ($branch.find('ul').length == 0) {
		
							$('<ul><li><span>' + newName + '</span></li></ul>').appendTo($branch);
						}
						else {
							
							$('<li><span>' + newName + '</span></li>').appendTo($branch.find('ul'));
						}
					}
					
					return self;
				};
				
				self.setStatus = function(selector, status, removeOther) {
					
					if (removeOther == undefined)
						removeOther = true;
					
					if (removeOther == true)
						$('li>span', $tree).removeClass(status);
					
					$(selector, $tree).addClass(status);
					
					return self;
				};
				
				self.removeStatus = function(selector, status, removeOther) {
					
					if (removeOther == true)
						$('li>span', $tree).removeClass(status);
					else
						$(selector, $tree).removeClass(status);
					
					return self;
				};
				
				/* +---------- plugin construct ----------+ */
				
				self.setup();
			}
			
			
			var plugin;
			var $selector = this;
			var tree;
			
			this.each(function() {
				
				$tree = $(this);
				plugin = new xqCategoryTree();
			});
		}
		
		return options.api ? plugin : this;
	
	};
	
 })(jQuery);




/**
 * XQTools - IEFix plugin for jQuery
 * http://www.noname-media.com
 * 
 * Copyright 2011, Andi Heinkelein
 * 
 * Licensed under the MIT License
 * http://www.noname-media.com/license
 * 
 */
(function($) {
	
	$.fn.xqIEFix = function(options) {
	
		var defaults = {
		
			api: true,
			fixbox: true,
			fixpng: true,
			blankGif: '/images/blank.gif'
			
		};
		
		var options = $.extend(defaults, options, {
		
			version: '0.1'
		});
		
		
		if (this.length > 0) {
		
			function xqIEFix() {
			
				var self = this;
			
				/* +---------- plugin author functions ----------+ */
			
				self.getConf = function(confName) {
				
					return options[confName];
				};
				
				self.setConf = function(confName, confValue) {

					options[confName] = confValue;
					return options[confName];
				};
				
//				self.addEffect = function(effectName, fnShow, fnHide) {
//					
//					effects[effectName] = {};
//					effects[effectName]['show'] = fnShow;
//					effects[effectName]['hide'] = fnHide;
//				};
				
				
				
				for (var f in options) {
				
					if (typeof options[f] == 'function') {
					
						self[f] = options[f];
					}
				}
				
				/* +---------- plugin properties ----------+ */
				
				
				/* +---------- plugin methods ----------+ */
				
				self.setup = function() {

					var timer = new Date().getTime();
					var c = c2 = 0;
					
					var dump = [];
					for (var el in window.document.all) {
						
						var curEl = document.all[el];
						var $curEl = $(curEl);
							
						if (/^\d+$/.test(el)) {
							
							if (document.compatMode != 'CSS1Compat' && document.all[el].currentStyle && document.all[el].currentStyle.display == 'block') {
								
								++c2;
								$curEl.width($curEl.outerWidth());
								$curEl.height($curEl.outerHeight());
							}
							
							if (curEl.tagName == 'DIV' && curEl.currentStyle.display == 'inline-block') {
								
								curEl.style.display = 'inline';
							}
							
							if (options.fixpng && curEl.tagName == 'IMG' && /\.png(\?.*)?$/.test(curEl.src)) {
								
								var $newImg = $('<span style="display: inline-block">&nbsp;</span>');
								$newImg.width($curEl.width());
								$newImg.height($curEl.height());
								$curEl.replaceWith($newImg);
								
								$newImg.get(0).style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src="' + curEl.src + '", sizingMethod="scale")';
							}
							
							dump.push({
								
								'tag': document.all[el].tagName,
								'display': document.all[el].currentStyle.display,
								'padding-left': document.all[el].currentStyle.paddingLeft,
								'padding-right': document.all[el].currentStyle.display,
								'border-left': document.all[el].currentStyle.display,
								'border-right': document.all[el].currentStyle.display
							});
						}
							
						++c;
					}
					
					//XQDev.addDataTable(dump);
					//XQDev.addCompareTable($('.box3:eq(0)').get(0).style, $('.box3:eq(0)').get(0).currentStyle);
					
					//alert(((new Date().getTime() - timer) / 1000) + " Secounds " + c2 + $.support.boxModel);
				};
				
				
				/* +---------- plugin construct ----------+ */
				
				self.setup();
			}
			
			
			var plugin = new xqIEFix(this);
		}
		
		return options.api ? plugin : this;
	
	};
	
 })(jQuery);
