
(function (window) {
    "use strict";
    var $ = window.jQuery,
	view = {},
	control = {};

    view = {

        init: function () {
            view.slideshow.init();
            view.pseudoTabs.init();
            view.inputField.init();
        },
        locateOnMapToolTip: {
            /**
            * Get data from .net via input elements inside #net_js_bridge element. All data is stored in the properties object.
            * Automaticlly invoked by $.runInit($.view) on dom:ready event
            */
            init: function () {
                var enableTooltips = function enableTooltips() {
                    var links,
                            i,
                            h;
                    // Create tooltip element
                    h = document.createElement("span");
                    h.id = "btc";
                    h.setAttribute("id", "btc");
                    h.style.position = "absolute";
                    document.getElementsByTagName("body")[0].appendChild(h);
                    // Get all event to use as triggers
                    links = $(".coords");
                    for (i = 0; i < links.length; i++) {
                        prepare(links.get(i));
                    }
                },
                    prepare = function prepare(el) {
                        var tooltip,
                            t,
                            b,
                            s,
                            l,
                            texttooltip;

                        t = el.getAttribute("title");

                        if (t == null || t.length == 0) {
                            t = "Locate on map:";
                        }

                        el.removeAttribute("title");
                        tooltip = createEl("span", "tooltip");
                        s = createEl("span", "top");
                        //document.createElement("span").innerHTML = t
                        //document.getElementById("yourContainerDiv").innerHTML= answer;
                        //s.appendChild(document.createTextNode(t));
                        var newdiv = document.createElement('div');
                        newdiv.innerHTML = t;

                        //s.appendChild(document.createTextNode(t));
                        s.appendChild(newdiv);
                        tooltip.appendChild(s);
                        b = createEl("b", "bottom");
                        l = el.getAttribute("href");
                        tooltip.appendChild(b);
                        el.tooltip = tooltip;
                        el.onmouseover = showTooltip;
                        el.onmouseout = hideTooltip;
                        el.onmousemove = locate;
                    },
                    showTooltip = function showTooltip(e) {
                        document.getElementById("btc").appendChild(this.tooltip);
                        locate(e);
                    },
                    hideTooltip = function hideTooltip(e) {
                        var d = document.getElementById("btc");
                        if (d.childNodes.length > 0) {
                            d.removeChild(d.firstChild);
                        }
                    },
                    createEl = function createEl(t, c) {
                        var x = document.createElement(t);
                        x.className = c;
                        x.style.display = "block";
                        return (x);
                    },
                    locate = function locate(e) {
                        var posx = 0,
                            posy = 0;

                        if (e == null) {
                            e = window.event;
                        }
                        if (e.pageX || e.pageY) {
                            posx = e.pageX; posy = e.pageY;
                        } else if (e.clientX || e.clientY) {
                            if (document.documentElement.scrollTop) {
                                posx = e.clientX + document.documentElement.scrollLeft;
                                posy = e.clientY + document.documentElement.scrollTop;
                            } else {
                                posx = e.clientX + document.body.scrollLeft;
                                posy = e.clientY + document.body.scrollTop;
                            }
                        }
                        document.getElementById("btc").style.top = (posy + 10) + "px";
                        document.getElementById("btc").style.left = (posx - 20) + "px";
                    }

                enableTooltips();
            }
        },
        slideshow: {

            init: function () {
                var itemClicked = {},
                        currentSlideInt = 0,
                        currentSlide = {},
                        switchSlide = {},
                        startSlideshow = {},
                        intVal = {},
                        stopSlideshow = {},
                        determineSlide = {},
                        numPics = 0;

                if (document.getElementById("splashWrapper")) {

                    $(".splash").each(function (i) {
                        if (i !== 0) {
                            $(this).find(".splashNavWrapper").remove();

                        } else {
                            $("#splashWrapper").append($(this).find(".splashNavWrapper"));

                        }
                        if (i !== $(".splash").length - 1) {
                            $(this).addClass("splashLayer");
                        }
                        numPics = i;
                    });

                    $(".splash").each(function (i) {
                        $(".splashNav li:first").addClass("selected");
                        if (i !== 0) {
                            $(this).animate({ opacity: 0 }, 1);
                        } else {
                            $(this).get(0).style.zIndex = 1;
                        }
                    });
                }

                $(".splashNav li").click(function (e) {
                    stopSlideshow();
                    e.preventDefault();

                    var itemClicked = $(this).get(0);

                    $(".splashNav li").each(function (i) {
                        if (itemClicked === $(this).get(0) && i !== $(".splashNav li").length) {
                            currentSlide = $(".splash").eq(i);
                            currentSlideInt = i;
                            switchSlide(currentSlide);
                        }
                    });
                });

                $(".next").click(function () {
                    stopSlideshow();
                    determineSlide();
                    switchSlide(currentSlide);
                });

                $(".prev").click(function () {
                    stopSlideshow();
                    if (currentSlideInt > 0) {
                        currentSlideInt -= 1;
                    } else {
                        currentSlideInt = $(".splash").length - 1;
                    }
                    currentSlide = $(".splash").eq(currentSlideInt);
                    switchSlide(currentSlide);
                });

                var determineSlide = function () {

                    if (currentSlideInt < $(".splash").length - 1) {
                        currentSlideInt += 1;
                    } else {
                        currentSlideInt = 0;
                    }

                    currentSlide = $(".splash").eq(currentSlideInt);
                },

                switchSlide = function (_currentSlide) {

                    $(".splashNav li").removeClass("selected").eq(currentSlideInt).addClass("selected");

                    $(".splash").each(function (i) {
                        if (i === currentSlideInt) {
                            $(this).get(0).style.zIndex = 1;
                        } else {
                            $(this).get(0).style.zIndex = 0;
                        }
                    });

                    //_currentSlide.get(0).removeAttribute("filter");
                    $(_currentSlide).animate({
                        opacity: 1
                    }, 1000);
                },

                startSlideshow = function () {

                    intVal = setInterval(function () {
                        determineSlide();
                        switchSlide(currentSlide);
                    }, 6000);
                },

                stopSlideshow = function () {
                    clearInterval(intVal);
                };

                if (document.getElementById("splashWrapper")) {

                    window.switchSlide = function () {
                        determineSlide();

                        switchSlide(currentSlide);
                    };

                    startSlideshow();
                }
            }
        },
        pseudoTabs: {
            init: function () {
                var itemClicked = {};
                $("#tabBox").find("div").each(function (i) {
                    if (i !== 0) {
                        $(this).hide();
                    }
                });
                $(".tabs li").click(function (e) {
                    e.preventDefault();
                    itemClicked = $(this).get(0);

                    $(".tabs li").each(function (i) {

                        if ($(this).get(0) === itemClicked) {
                            //$( this ).parent().addClass( "selected" );
                            $(this).addClass("active");
                            $("#tabBox div").eq(i).show();
                        } else {
                            //$( this ).parent().removeClass( "selected" );
                            $("#tabBox div").eq(i).hide();
                            if ($(this).hasClass("active")) {
                                $(this).removeClass("active");
                            }
                        }
                    });
                });
            }
        },
        inputField: {
            init: function () {
                var leadText = "",
                currentField = null;
                $("input[type=text]").focus(function () {
                    currentField = $(this);
                    leadText = $(this).attr("class").split("-")[1];
                    if ($(this).val() === leadText) {
                        $(this).val("");
                    }
                });

                $(document).click(function (e) {
                    if ($(e.target)[0] === $(currentField)[0]) {
                        return;
                    }
                    if ($(currentField).val() === "") {
                        $(currentField).val(leadText);
                    }
                });
            }
        }
    },

	control = {
	    init: function () {

	    }
	}

    $.extend(view, view);
    $(document).ready(function () {
        //view.init();
        $.runInit(view);
    });
} (window));
