(function ($) {

    $.fn.sspCookieInfo = function (options) {

		function setCookie(c_name,value,exdays){
			var exdate=new Date();
			exdate.setDate(exdate.getDate() + exdays);
			var c_value=escape(value) + ((exdays===null) ? "" : "; expires="+exdate.toUTCString());
			document.cookie=c_name + "=" + c_value;
		}
		function getCookie(c_name){
			var c_value = document.cookie;
			var c_start = c_value.indexOf(" " + c_name + "=");
			if (c_start == -1){
				c_start = c_value.indexOf(c_name + "=");
			}
			if (c_start == -1){
				c_value = null;
			}
			else{
				c_start = c_value.indexOf("=", c_start) + 1;
				var c_end = c_value.indexOf(";", c_start);
			  	if (c_end == -1) {
					c_end = c_value.length;
				}
				c_value = unescape(c_value.substring(c_start,c_end));
			}
			return c_value;
		}
		function listCookies() {
			var theCookies = document.cookie.split(';');
			var aString = '<ul>';

			for (var i = 0 ; i < theCookies.length; i++) {

				aString += '<li> ' + theCookies[i] + "</li>";
			}
			aString += "</ul>";
			return aString;
		}


        if ($(this).length > 0) {

            var settings = $.extend({
                'html5': false, // default creates an <details> tag or alternativly an <div> tag.
                'appendTo': $("body"), // You can append this to whereever you want
                'textHtml': "cookie-default", // You may use difi-full, cookie-simple or your ovn html.
                'buttonHtml': "button-default", // Use button-default or custom html.
                'appendCSS': true, // Appending standard css
                'eventAction': "click",// options: mouseenter/mouseleave, click
                'animateIn': "slideDown", // options: show, fadeIn, slideDown
                'animateOut': "slideUp", // options: hide, fadeOut, slideUp
                'animateInTime': 250, // animate time in
                'animateOutTime': 250 // animate time out
            }, options);

            var d = (settings.html5 === true ? "details" : "div");
            var zoomtext = "";


            switch (settings.textHtml) {
                case "cookie-default":
                    zoomtext = "<h1>Cookie-informasjon</h1><p>Denne siden bruker informasjonskaker. Disse kan du spise så mye du vil av for du blir ikke feit.</p>" + listCookies();
                    break;
                case "cookie-simple":
                    zoomtext = "<p>Enkel cookie-info</p>";
                    break;
                default:
                    zoomtext = settings.textHtml;
                    break;
            }

            switch (settings.buttonHtml) {
                case "button-default":
                    buttonText = "<p><button class='cookie-close-button btn btn-info'>Lukk</button></p>";
                default:
                    buttonText = settings.buttonHtml;
                    break;
            }

            var contElm = $("<" + d + ">");
            contElm.append(zoomtext);
            contElm.addClass("cookie-info-container");
			var closeButton = $(buttonText);
			var BackgroundDim = $("<div style='height:100%; width:100%; position:fixed; z-index:14900; background:rgba(0,0,0,0.5); top:0px;'></div>");
			BackgroundDim.hide();

            console.log("setting appendTo Append background");
			settings.appendTo.append(BackgroundDim);
			contElm.append(closeButton);
            if (settings.appendCSS === true) {
                contElm.css({ "font-size": "1.5em",  padding: "30px", width: "100%", border: "1px solid #000", position: "fixed", bottom: 0, background: "rgba(255,255,255,0.98)", opacity: 0.8, "z-index" : 9998 });
            }
           	contElm.hide();

			closeButton.click(function () {
				$(".cookie-info-container").hide();
				if($(".cookie-info-container").hasClass("cookie-open")){
					$(".cookie-info-container").removeClass("cookie-open");
				}
				BackgroundDim.fadeOut(250);
    			setCookie("readCookieInfo",true,365);

			});

            settings.appendTo.append(contElm);



            var animateZoom = function (elm, wish, time) {
                console.log(elm,wish,time);
                switch (wish) {
                    case "show":
                        elm.show(time);
                        break;
                    case "hide":
                        elm.hide(time);
                        break;
                    case "fadeIn":
                        elm.fadeIn(time);
                        break;
                    case "fadeOut":
                        elm.fadeOut(time);
                        break;
                    case "slideDown":
                        elm.slideDown(time);
                        break;
                    case "slideUp":
                        elm.slideUp(time);
                        break;

                }
            };


            if (settings.convertContent === true) {
                var nt = "Les om vår bruk av informasjonskapsler";
                $(this).html(nt);
            }


			var c = $(".cookie-info-container");

			if(settings.eventAction == "click" || settings.eventAction == "mouseenter mouseleave"){
				$(this).on(settings.eventAction, function (e) {
					if (e.type == "click") {
						if (!c.hasClass("cookie-open")) {
							c.addClass("cookie-open");
							BackgroundDim.fadeIn(250, function(){
							animateZoom(c, settings.animateIn, settings.animateInTime);
							});
						}
						else {
							c.removeClass("cookie-open");
							animateZoom(c, settings.animateOut, settings.animateOutTime);
						}
					}
					else if (e.type == "mouseenter") {
						animateZoom(c, settings.animateIn, settings.animateInTime);
					}
					else if (e.type == "mouseleave") {
						animateZoom(c, settings.animateOut, settings.animateOutTime);
					}

				});
			}
			else if(settings.eventAction == "windowOpen" && getCookie("readCookieInfo") != "true"){
				animateZoom(c, settings.animateIn, settings.animateOut);
			}
            else {
                console.log(getCookie("readCookieInfo"));
            }
        }
    };
})(jQuery);
