/*global $, window, alert, google*/
"use strict";

(function() {


    if ((/iframe\=true/).test(document.location.toString())) {
        $("body").addClass("iframe");
    }

    var view = {
        "0": {
            init: function() {
                $("table:not(.sitemap)").each(function() {
                    this.cellPadding = 0;
                    this.cellSpacing = 0;
                });
            }
        },
        blueBox: {
            init: function() {
                if ($.isIE7) {
                    $("div.startImgText div.rcMiddle").each(function() {
                        var that = this,
                        t = $(this);

                        t.parent().css({
                            width: that.offsetWidth + "px"
                        });
                    });
                }

                $("div.click a.useLink").each(function() {
                    var t = $(this),
                        h = this.href;

                    t.click(function(e) {
                        e.preventDefault();
                    });

                    t.closest("div.click").click(function() {
                        document.location = h;
                    });
                });
            }
        },
        tables: {
            init: {
                headerstyle: function() {
                    $("table:not(.noFix) tr.headerstyle").each(function() {
                        var t = $(this);

                        t.parent().before("<thead><tr></tr></thead>");

                        t.find("> td").each(function() {
                            t.parent().parent().find("thead tr").append("<th>" + this.innerHTML + "</th>");
                        });

                        t.remove();
                    });
                },
                firstLeftAlign: function() {
                    $("table:not(.noFix) tr > *:nth-child(1)").css({
                        textAlign: "left"
                    });
                }
            }
        },
        imageRotator: {
            init: function() {
                var ir = $("div.imageRotator"),
                    ul = null,
                    json = null,
                    i,
                    ac = [],
                    acObj = {};

                if (ir.length) {
                    json = $.parseJSON(ir.find("input[type=hidden]").get(0).value);

                    ul = $("div#imageRotator").append("<ul />").find("ul");

                    for (i = 0; i < json.length; i++) {
                        ac = [];
                        acObj = {};

                        if (json[i].imgSrc) {
                            acObj = {
                                tagName: "img",
                                src: json[i].imgSrc,
                                alt: json[i].imgAlt ? json[i].imgAlt : "",
                                title: json[i].imgSrc
                            };

                            ac.push(acObj);
                        }

                        if (json[i].href) {
                            acObj = {
                                tagName: "h2",
                                appendChildren: {
                                    tagName: "a",
                                    href: json[i].href,
                                    innerHTML: json[i].textLink
                                }
                            };

                            ac.push(acObj);
                        }

                        if (json[i].ingress) {
                            acObj = {
                                tagName: "p",
                                className: "ingress",
                                innerHTML: json[i].ingress.replace(/(^<p>|<\/p>$)/gi, "")
                            };

                            ac.push(acObj);
                        }

                        if (json[i].phone) {
                            acObj = {
                                tagName: "p",
                                className: "phone",
                                innerHTML: json[i].phone
                            };

                            ac.push(acObj);
                        }

                        if (json[i].email) {
                            acObj = {
                                tagName: "p",
                                className: "email",
                                innerHTML: "<a href=\"mailto:" + json[i].email + "\">" + json[i].email + "</a>"
                            };

                            ac.push(acObj);
                        }

                        ul.append($.create({
                            tagName: "li",
                            appendChildren: ac
                        }));

                        (function() {
                            ul.find("img").each(function() {
                                var that = this;
                                if (!that.src) {
                                    that.src = that.title;
                                }
                                that.removeAttribute("title");
                            });
                        } ());
                    }

                    this.init_0();
                }
            },
            init_0: function() {
                if ($("div.imageRotator").length) {
                    var ir = $("div.imageRotator"),
                    employeeList,
                    currentSlideIndex = 0,
                    currentSlide,
                    url,
                    width,
                    height,
                    isSliding = false,
                    hf,
                    randomStartIndex = function(el) {
                        var si = parseInt(Math.random() * 100, 10);

                        while (si > el.length - 1) {
                            si = parseInt(Math.random() * 100, 10);
                        }

                        return parseInt(si, 10);
                    },
                    slide = function(prevNext) {

                        if (!employeeList && !isSliding) {
                            employeeList = $("div#imageRotator ul li:not(.doNotShow)");

                            currentSlideIndex = randomStartIndex(employeeList);
                        }

                        if (prevNext && !isSliding) {
                            if (prevNext === "next") {
                                currentSlideIndex += 1;
                            } else if (prevNext === "prev") {
                                currentSlideIndex -= 1;
                            }

                            if (currentSlideIndex < 0) {
                                currentSlideIndex = employeeList.length - 1;
                            } else if (currentSlideIndex > employeeList.length - 1) {
                                currentSlideIndex = 0;
                            }
                        }

                        if (!isSliding) {
                            currentSlide = employeeList.eq(currentSlideIndex);
                        }

                        if (!prevNext && !isSliding) {

                            width = ir.find("div.slide")[0].offsetWidth;

                            ir.find("div.slide").css({
                                position: "relative",
                                left: 0
                            }).append(currentSlide.find("img, h2, p:not(.link)").clone());
                            url = currentSlide.find("h2 a").attr("href");

                            ir.find("div.slide").find("img").wrap("<a href=\"" + url + "\"></a>");

                            ir.prepend($.create({
                                tagName: "span",
                                className: "prev"
                            })).prepend($.create({
                                tagName: "span",
                                className: "next"
                            }));

                            ir.find("span.prev").click(function() {
                                slide("next");
                            });

                            ir.find("span.next").click(function() {
                                slide("prev");
                            });

                        } else if (!isSliding) {

                            if (prevNext === "next") {
                                ir.find(" > .slideWrap").append($.create({
                                    className: "slide newSlide",
                                    position: "absolute",
                                    left: "-" + width + "px",
                                    top: 0
                                }));
                            } else {
                                ir.find(" > .slideWrap").prepend($.create({
                                    className: "slide newSlide",
                                    position: "absolute",
                                    left: width + "px",
                                    top: 0
                                }));
                            }

                            ir.find("div.newSlide").append(currentSlide.find("img, h2, p:not(.link)").clone());

                            url = currentSlide.find("h2 a").attr("href");

                            ir.find("div.newSlide").find("img").wrap("<a href=\"" + url + "\"></a>");

                            height = ir.find("div.newSlide")[0].offsetHeight;

                            if (prevNext === "next") {
                                ir.find(".slide:not(.newSlide)").animate({
                                    left: width + "px",
                                    height: height + "px"
                                }, "slow");
                            } else {
                                ir.find(".slide:not(.newSlide)").animate({
                                    left: "-" + width + "px",
                                    height: height + "px"
                                }, "slow");
                            }

                            isSliding = true;

                            ir.find(".newSlide").animate({
                                left: 0
                            }, "slow", function() {
                                ir.find(".slide:not(.newSlide)").remove();
                                ir.find(".newSlide").removeClass("newSlide").css({
                                    position: "relative",
                                    top: "",
                                    left: ""
                                });
                                setTimeout(function() {
                                    isSliding = false;
                                }, 1);
                            });
                        }
                    };

                    hf = document.getElementById("employeeContainer");

                    slide();
                }
            }
        },
        fundTabs: {
            toggle: function(cTab, containerId) {
                $(".fundTop li").removeClass("active");
                cTab.parent().addClass("active");
                $("#omFondenInfo, #Defaultframework1_fundMainContent_PPMPanel").hide();
                $("#" + containerId).show();
            },
            init: function() {
                var that = this;

                $("#tabFondensUtveckling a[href]").click(function(e) {
                    var href = this.href,
                    t = $(this);

                    e.preventDefault();

                    $("#omFondenInfo").load(href + " #graphPageWrap", function() {

                        var omFondenInfo = this,
                            ofi = $(omFondenInfo);

                        ofi.find("span.label").remove();

                        if (document.getElementById("appletStore")) {
                            ofi.prepend($.create({
                                tagName: "div",
                                className: "row graphCheckBox",
                                appendChildren: {
                                    tagName: "label",
                                    appendChildren: [{
                                        tagName: "input",
                                        type: "checkbox",
                                        click: function() {
                                            try {
                                                if (this.checked) {
                                                    $("#navApplet").addClass("hidden");
                                                    $("#totApplet").removeClass("hidden");
                                                } else {
                                                    $("#totApplet").addClass("hidden");
                                                    $("#navApplet").removeClass("hidden");
                                                }
                                            } catch (error) {
                                                alert(error);
                                            }
                                        }
                                    }, {
                                        tagName: "span",
                                        innerHTML: "Inkl. utdelningar"
}]
                                    }
                                }));
                            }

                            t.click(function(e) {
                                e.preventDefault();
                                that.toggle(t, "omFondenInfo");
                            });
                        });

                        t.unbind("click");
                        that.toggle(t, "omFondenInfo");

                        t.click(function(e) {
                            e.preventDefault();
                            that.toggle(t, "omFondenInfo");
                        });
                    });

                    $("#tabOmFonden a").click(function(e) {
                        e.preventDefault();
                        var t = $(this);
                        that.toggle(t, "Defaultframework1_fundMainContent_PPMPanel");
                    });
                }
            },
            init: {
                webKit: function() {
                    if ($.isWebKit) {
                        $("body").addClass("webKit");
                    }
                },
                js: function() {
                    $("body").addClass("js");
                },
                misc: function() {
                    if ($.isIE678) {
                        var tb = $("table:not(.sitemap):not(#id_matrix):not(.noFix) tbody");

                        tb.find("tr:nth-child(odd)").addClass("odd");
                        tb.find("tr:nth-child(even)").addClass("even");
                    }
                },
                popUp: function() {
                    $("a[rel~=popUp]").attr("target", "_blank");
                },
                docLinks: function() {
                    var supportedDocs = "pdf",
                re = new RegExp("\\.(" + supportedDocs.replace(/\s?,\s?/gi, "|") + ")$", "gi");

                    $("a").each(function() {
                        var m = this.href.match(re),
                    t = $(this);
                        if (m) {
                            t.addClass("sprite docIcon " + m.toString().replace(/[\W]/g, ""));
                            t.find("br").remove();
                            t.find("+ br").remove();
                            t.find("+ br").remove();
                        }
                    });
                },
                tableStuff: function() {

                    $("table:not(.sitemap):not(.noFix)").find("tr").each(function() {
                        var t = $(this),
                    url,
                    tLinks = t.find("a");

                        if (tLinks.length && tLinks.length === 1) {
                            url = tLinks.attr("href");
                        }

                        if (url) {
                            tLinks.click(function(e) {
                                e.preventDefault();
                            });

                            t.closest("table").addClass("hasOmo");

                            t.click(function() {
                                document.location = url;
                            }).mouseover(function() {
                                t.addClass("omo");
                            }).mouseout(function() {
                                t.removeClass("omo");
                            });
                        }
                    });
                },
                subMenu: function() {
                    $("#subMenu a").each(function() {
                        var that = this,
                    ih = that.innerHTML;

                        that.innerHTML = "<span>" + ih + "</span>";

                    });
                },
                fundTab: function() {
                    $("#tabFondensUtveckling a").each(function() {
                        var that = this;

                        if (!that.href) {
                            $(that).parent().hide();
                        }
                    });
                },
                /*test : function () {
                $("div.startImgText").find("*").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                    
                var obj = this.currentStyle,
                k,
                str = "";
                    
                for (k in obj) {
                if (obj[k]) {
                str += "<li>" + k + " : " + obj[k] + "</li>";
                }
                }
                    
                $("body > ul").remove();
                    
                $("body").prepend("<ul>" + str + "</ul>");
                    
                $("body > ul").css({
                position : "absolute"
                });
                    
                });
                },*/
                modules: function() {
                    $("div.module:not(.click) > :first-child:not(h3)").each(function() {

                        var t = $(this);

                        t.css({
                            marginBottom: "10px"
                        });

                        t.closest("div.module").prev("div.module").css({
                            marginBottom: "10px"
                        });
                    });
                },
                gmaps: function() {

                    var latlng,
                    myOptions,
                    map,
                    icon,
                    marker,
                    gm = document.getElementById("lanneboGMap");

                    if (gm) {

                        $(gm).css({
                            width: "500px",
                            height: "300px"
                        });

                        latlng = new google.maps.LatLng(59.33481462711241, 18.073574602603912);

                        myOptions = {
                            zoom: 17,
                            center: latlng,
                            navigationControl: true,
                            navigationControlOptions: {
                                style: google.maps.NavigationControlStyle.ZOOM_PAN
                            },
                            mapTypeId: google.maps.MapTypeId.ROADMAP
                        };

                        map = new google.maps.Map(gm, myOptions);

                        icon = new google.maps.MarkerImage('/LFImages/lanneboPOI.png', new google.maps.Size(75, 56), new google.maps.Point(0, 0), new google.maps.Point(40, 47));

                        marker = new google.maps.Marker({
                            position: latlng,
                            map: map,
                            icon: icon
                        });
                    }
                }
            }
        };

        $.extend({ view: view });

        $(document).ready(function() {
            $.runInit($.view);
        });

    } ());
