﻿jQuery.fn.random = function () {
    var randomIndex = Math.floor(Math.random() * this.length);
    return jQuery(this[randomIndex]);
};

$(document).ready(function () {
    var themes = new Array("accessories", "boutique", "menswear", "suits", "shoes", "bikes", "watches", "shoes3", "furniture1", "handbags1", "heels2", "women2", "jeans1", "jewellry1", "jewellry2", "furniture2", "bike2");
    var themeI = Math.floor(Math.random() * themes.length);
    $(".tab").hide();
    $(".tab div.bk").css({ opacity: 0.85 });


    $(".box").css({ opacity: 0 });
    $(".box div.boxbk").css({ opacity: 0.85 });
    $(".box").each(function (i) {
        $(this).css({ backgroundImage: "url('images/" + themes[themeI] + "/" + i.toString() + ".jpg')" });
    });



    $(".box").show();
    var hidden = $(".box");
    var shown = new Array();
    var linkys = $(".box div.caption");
    function slideDownNextText() {
        linkys.fadeOut();
    }

    function fadeInNextBox() {
        var e = hidden.not(shown).random()
        shown.push(e[0])
        if (shown.length <= 12) {
            e.animate({ opacity: 1 }, 100, "swing", fadeInNextBox);
        } else {
            setTimeout(slideDownNextText, 2000);
        }
    }
    fadeInNextBox();
    function changeNext() {
        var e = hidden.not(shown).random()
        shown.push(e[0])
        if (shown.length <= 12) {
            e.animate({ opacity: 0 }, 100, "swing", function () {
                var i = hidden.index(this);
                $(this).css({ backgroundImage: "url('images/" + themes[themeI] + "/" + i.toString() + ".jpg')" });
                $(this).animate({ opacity: 1 }, 100, "swing", changeNext);
            });
        } else {
            clearTimeout(switchThemeTimeout);
            switchThemeTimeout = setTimeout(switchTheme, 7000);
        }
    }
    function switchTheme() {
        clearTimeout(switchThemeTimeout);
        shown = new Array();
        var newI = Math.floor(Math.random() * themes.length);
        while (newI == themeI) {
            newI = Math.floor(Math.random() * themes.length);
        }
        themeI = newI;
        changeNext();
    }
    $("#logo").click(function () {
        switchTheme();
    });
    switchThemeTimeout = setTimeout(switchTheme, 7000);
    var currentT = null
    var closeTimeout = 0;

    function closecurrent() {
        $("#" + currentT + "Box").find("div.caption").stop(true, true).fadeOut("fast");
        $("#" + currentT).stop(true, true).slideUp("fast");
    }
    var contactusopen = false;
    $("div.tab").each(function () {
        var id = $(this).attr("id")

        $("#" + id + "Box").hover(function () {
            if (contactusopen)
                return;
            $(this).find("div.caption").stop(true, true).fadeIn("fast");
            $("#" + id).stop(true, true).slideDown("fast");
            currentT = id;
            if (id == "contactus")
                contactusopen = true;
        }, function () {
            if (id != "contactus") {
                $(this).find("div.caption").stop(true, true).fadeOut("fast");
                $("#" + id).stop(true, true).slideUp("fast");
            }
        });


    });
    $("#animateBox").click(function () {
        switchTheme()
    });
    $("#contactusLink").click(function () {

        $("#contactusBox").find("div.caption").stop(true, true).fadeIn("fast");
        $("#contactus").stop(true, true).slideDown("fast");
        contactusopen = true;
    });
    $("#backButton").click(function () {

        $("#contactusBox").find("div.caption").stop(true, true).fadeOut("fast");
        $("#contactus").stop(true, true).slideUp("fast");
        contactusopen = false;
    })
    $("#submitQuery").click(function () {
        var missing = new Array();

        $("input, textarea").css("border", "");
        if (jQuery.trim($("#nametextbox").val()).length < 3) {
            missing.push("name");
            $("#nametextbox").css("border", "solid 2px Red");
        }
        if (jQuery.trim($("#emailtextbox").val()).length + jQuery.trim($("#telephonetextbox").val()).length < 3) {
            missing.push("contact details");
            $("#emailtextbox").css("border", "solid 2px Red");
            $("#telephonetextbox").css("border", "solid 2px Red");
        }
        if (jQuery.trim($("#querytextbox").val()).length < 3) {
            missing.push("a query");
            $("#querytextbox").css("border", "solid 2px Red");
        }

        $("#submitQuery").text("Submit Query");
        if (missing.length > 0) {
            if (missing.length > 1) {
                var last = missing.pop();
                $("#message").text("Please enter your " + missing.join(", ") + " and " + last)
            } else {
                if (missing[0] == "a query")
                    missing[0] = "query";
                $("#message").text("Please enter your " + missing.join(", "))
            }

        } else {
            $("#submitQuery").text("Sending...");
            $.get("submit.aspx", { Name: $("#nametextbox").val(), Business: $("#businesstextbox").val(), Email: $("#emailtextbox").val(), Telephone: $("#telephonetextbox").val(), Query: $("#querytextbox").val() }, function (data) {
                if (data == "SENT") {
                    $("#message").text("Your enquiry has been received, and we'll be in touch shortly.\nThanks!");

                    $("#submitQuery").text("Sent.");
                } else {
                    $("#message").text("Your enquiry could not be sent, please call or email us.");
                    $("#submitQuery").text("Submit Query");
                }
            })
          .error(function () {
              $("#message").text("Your enquiry could not be sent, please call or email us.");
              $("#submitQuery").text("Submit Query");
          })

        }
    })

});
