var fb_options = {appId: 145775475491272, cookie: true, xfbml: true, authResponse: true, oauth: true};

(function($){
	var timers = new Object;

	var methods = {
		_load_script: function(b,d,e){
			var a=document,c=a.body;if(!a.getElementById(b))a=a.createElement("script"),a.id=b,a.src=d,$(a).load(e),c.insertBefore(a,c.firstChild);
		},
		_timer: function(_options){
			var timer = timers[_options.id];

			++timer.time;

			if(timer.timeout - timer.time == 0){
				methods.stop_timer({id: _options.id});
			}else{
				if($.isFunction(timer.interval_callback)){
					timer.interval_callback(timer.timeout - timer.time);
				}
			}
		},
		_resize_textarea: function(_textarea){
			_textarea.height(0);

			var new_height = 0;
			var min_height = parseInt(_textarea.css("min-height"), 10);
			var calc_height = _textarea[0].scrollHeight + (_textarea.outerHeight() - _textarea.height());

			if(min_height > calc_height){
				new_height = min_height;
			}else{
				new_height = calc_height;
			}

			_textarea.height(new_height);

			if(_textarea.height() != _textarea[0].scrollHeight){
				new_height = _textarea[0].scrollHeight;

				_textarea.height(new_height);
			}

			if(_textarea.height() != _textarea[0].scrollHeight){
				new_height = _textarea[0].scrollHeight - (_textarea.outerHeight() - _textarea[0].scrollHeight);

				_textarea.height(new_height);
			}
/*
console.log(" h: "+_textarea.height());
console.log("oH: "+_textarea.outerHeight());
console.log("sH: "+_textarea[0].scrollHeight);
*/
		},
		init: function(){
			methods._load_script("facebook-sdk","//connect.facebook.net/de_DE/all.js",function(){FB.init(fb_options); FB.Event.subscribe('edge.create', function(targetUrl){_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);}); FB.Event.subscribe('edge.remove', function(targetUrl){_gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);}); FB.Event.subscribe('message.send', function(targetUrl){_gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);});});
			methods._load_script("google-sdk","//apis.google.com/js/plusone.js",function(){gapi.plusone.go()});

			Date.prototype.addDays=function(a){this.setDate(this.getDate()+a)};

			$("select").framework("custom_select");

			$("body").bind("click", function(_event){
				var $select = $(_event.target).parent(".select");

				if($select && $select.hasClass("active")){
					$(".select.active").removeClass("active");
				}else{
					$(".select.active").removeClass("active");
					$select.addClass("active");
				}
			});

			$("textarea").live("input", function(_event){
				var $this = $(this);

				methods._resize_textarea($this);
			});
		},
		uid: (function(){
			var a=0;return function(){var b = new Date(); a++; return a+""+b.getTime();};
		})(),
		start_timer: function(_options){
			var timer_id = methods.uid();

			timers[timer_id] = {timer: window.setInterval('$.framework("_timer", {id: '+timer_id+'})', 1000 * _options.interval), timeout: _options.timeout, interval_callback: _options.interval_callback, timeout_callback: _options.timeout_callback, time: 0};

			return timer_id;
		},
		stop_timer: function(_options){
			var timer = timers[_options.id];

			window.clearInterval(timer.timer);
			timers[_options.id] = null;
			if($.isFunction(timer.timeout_callback)){
				timer.timeout_callback();
			}
		},
		set_cookie: function(_options){
			var c=_options.name,d=_options.value,e=_options.timeout,b=new Date;b.addDays(e);document.cookie=c+"="+d+"; path=/; expires="+b.toGMTString();
		},
		get_cookie: function(_options){
			var a,c,e,f=_options.name,b=document.cookie.split(";");for(a=0;a<b.length;a++)if(c=b[a].substr(0,b[a].indexOf("=")),e=b[a].substr(b[a].indexOf("=")+1),c=c.replace(/^\s+|\s+$/g,""),c==f)return unescape(e);
		}
	};

	var fn_methods = {
		custom_select: function(){
			return this.each(function(){
				var $this = $(this);

				var name = $this.attr("name");
				var option_elements = "";

				$this.children("option").each(function(){
					var $child = $(this);

					option_elements += '<li data-value="'+$child.val()+'">'+$child.text()+'</li>';
				});

				var $selected = $this.children("option:selected");

				var $select = $("<div></div>").addClass("select");
				var $hidden_input = $('<input type="hidden"/>').attr("name", name);
				var $selection = $("<span></span>").addClass("selection");
				var $toggle = $("<span></span>").addClass("toggle");
				var $clear = $("<span></span>").addClass("clear");
				var $options = $("<ul></ul>").addClass("options").append(option_elements);

				if($selected.val() != ""){
					$hidden_input.attr("value", $selected.val());
					$selection.text($selected.text());
				}else{
					$selection.text($this.attr("title"));
				}

				$options.children("li").each(function(){
					var $this = $(this);

					$this.click(function(_event){
						var $this = $(this);

						var text = $this.text();
						var value = $this.attr("data-value");

						var select = $this.closest(".select");

						select.children(".selection").text(text);
						select.children("input").val(value);
						select.find("ul.options").children("li").removeClass("selected");

						$this.addClass("selected");
					});

					var value = $this.attr("data-value");

					if(value == $selected.val()){
						$this.addClass("selected");
					}

					if(value == ""){
						$this.remove();
					}
				});

				$hidden_input.appendTo($select);
				$selection.appendTo($select);
				$toggle.appendTo($select);
				$clear.appendTo($select);
				$options.appendTo($select);

				$this.replaceWith($select);
			});
		},
		error: function(_options){
			var error_settings = $.extend({"code": "generic", "show": true}, _options);

			return this.each(function(){
				$error = $(this).find(".error."+error_settings.code);

				if(error_settings.show){
					$error.show();
				}else{
					$error.hide();
				}
			});
		}
	};

	$.framework = function(_method){
		if(methods[_method]){
			return methods[_method].apply(this, Array.prototype.slice.call(arguments, 1));
		}else if(typeof _method === "object" || !_method){
			return methods.init.apply(this, arguments);
		}else{
			$.error("Method "+_method+" does not exist on jQuery.framework");
		}
	};

	$.fn.framework = function(_method){
		if(fn_methods[_method]){
			return fn_methods[_method].apply(this, Array.prototype.slice.call(arguments, 1));
		}else{
			$.error("Method "+_method+" does not exist on jQuery.framework");
		}
	};
})(jQuery);

$(document).ready(function(){
	$.framework();
});
