var webshopUrl = "http://butiken.tryggahem.se";
var webshopRegexp = /^http:\/\/butiken\.tryggahem\.se?.+/;

(function($) {
    $.fn.popupmenu = function(options) {
        var opts = $.extend({}, $.fn.popupmenu.defaults, options);

        return this.each(function() {
            var $elements = $(this).find("ul li:has(div)");
            var $current = $elements.eq(0);
            $elements.hoverIntent({
                over: function() { $current.removeClass('hover'); $current = $(this); $(this).addClass('hover').find('a').addClass('hover'); },
                out: function() { $(this).removeClass('hover').find('a').removeClass('hover'); },
                interval: opts.interval,
                timeout: opts.timeout
            });
        });

        // Default options
        $.fn.popupmenu.defaults = {
            interval: 50,
            timeout: 400
        };
    };

    $.fn.shopPopup = function(options) {
        //var opts = $.extend({}, $.fn.shopPopup.defaults, options);

        return this.each(function(i) {
            //alert($(this).attr("href"));
            $(this).bind('click mousedown mouseup', function(e) {
                e.preventDefault();
                if (e.stopPropagation)
                    e.stopPropagation();

                e.cancelBubble = true;
                
                var relUrl;
                if ($(this).is("[href]"))
                    relUrl = $(this).attr('href').replace(/^http:\/\/[a-z|.]+/, '');
                else if ($(this).find("a").is("[href]"))
                    relUrl = $(this).find("a").attr('href').replace(/^http:\/\/[a-z|.]+/, '');
                $.getJSON('/verktyg/webshoppen/webshoplistajax', function(data) {
                    var content = "<div id=\"shop-container\">" + $("<div/>").html(data.shops.intro).text();
                    content += "<ul>";
                    $.each(data.shops.shop, function(i, item) {
                        content += "<li><a target=\"_blank\" href=\"" + item.link + relUrl + "\"><img src=\"" + item.logo + "\" alt=\"" + item.name + "\" /></a></li>";
                    });
                    content += "</ul>";
                    content += "<div id=\"default-shop\">" + $("<div/>").html(data.shops.nopartner).text().replace('/?relUrl', relUrl) + "</div>";
                    "</div>";
                    $.colorbox({ html: content, width: 630, opacity: 0.5 });
                });
                return false;
            });
            $(this)[0].oncontextmenu = function() {
                return false;
            }
            $("#cboxOverlay, #colorbox").bind('mouseup', function(e) {
                if (e.button == 2) {
                    e.preventDefault();
                    if (e.stopPropagation)
                        e.stopPropagation();
                    e.cancelBubble = true;
                    return false;
                }
            })[0].oncontextmenu = function() {
                return false;
            };

        });


        // Default options
        $.fn.shopPopup.defaults = {
            interval: 50,
            timeout: 400
        };
    };

    $(document).ready(function() {
	
	$(".contourButton").val("Skicka");
	$("#contour input.text").val("");
        // Init menu
        $("#main-menu").popupmenu();
        // Make webshop list items clickable
        $("#webshop-list li").click(function(e) {
            window.open($(this).find("a").attr('href'));
            return false;
        });
        //$('a[href^="' + webshopUrl + '"], #teasers .webbshoppen').shopPopup();
        $('a[href^="' + webshopUrl + '"], #teasers .webbshoppen').not('#teasers .no-popup a,.no-popup a').shopPopup();
        // Make start teasers clickable
        $("#teasers .t-container, #main-menu .nav-intro").each(function() {
            if ($(this).find("a").length > 0)
                $(this).addClass('clickable');
        }).click(function(e) {
            if ($(this).find("a").length > 0 && !$(this).find("a").attr('href').match(webshopRegexp)) {
                window.location = $(this).find("a").attr("href");
            }
            if ($(this).hasClass('no-popup')) {
                window.location = $(this).find("a").attr("href");
            }
            //console.log($(this).hasClass('no-popup'))
        });
        $("#faq-list-container")
                .addClass("faq-active")
                .find(".faq-list dt").click(function() {
                    $(this).next().andSelf().toggleClass("open");
                });
    });
})(jQuery);



// $(document).ready(function() {
//       console.log($(this).hasClass('no-popup'))
//    });
