$(document).ready(function () {
    $(".leftcontrol").html("<img src='images/arrowleft.png' alt='' />").css("cursor", "pointer");
    $(".rightcontrol").html("<img src='images/arrowright.png' alt='' />").css("cursor", "pointer");
    $(".inslide>div").remove();
    $(".pagercover").css("visibility", "visible").fadeTo(0, .6).first().fadeTo(0, 0);
    var ind = 0;
    $(".pagercontrol a").click(function (e) {
        e.preventDefault();
        ind = $(this).index();
        $(".pagercover").stop().fadeTo(250, .6);
        $(this).find(".pagercover").stop().fadeTo(250, 0);
        $(".pagernator").stop().animate({ scrollTop: (ind * 307) }, 1000, "easeOutQuint");
    });
    var f = 1;
    function newsfeed() {
        if (f + 1 > $(".breadcrumbs>div>a").length) {
            f = 0;
        }
        $(".breadcrumbs>div").animate({ scrollTop: f * 30 }, 300);
        f += 1;
    }
    setInterval(newsfeed, 4000);
    /*
    var j = 1;
    function gonext() {
    $(".slidecontrol div").eq(j).trigger("click");
    if (j != 0) {
    j++;
    if (j == 4) { j = 0; }
    setTimeout(gonext, 7000);
    }
    }
    setTimeout(gonext, 8000);
    */
    var isHovered = false;
    var nextsPage = function () {
        if (isHovered == false) {
            if (ind < 3) {
                ind += 1
            } else {
                ind = 0
            }
            $(".pagercover").stop().fadeTo(500, .6);
            $(".pagercontrol a:eq(" + ind + ")").find(".pagercover").stop().fadeTo(500, 0);
            $(".pagernator").stop().animate({ scrollTop: (ind * 307) }, 2000, "easeOutQuint");
        }
    }
    setInterval(nextsPage, 6000);
    $(".headthree").mouseenter(function () {
        isHovered = true;
    });
    $(".headthree").mouseleave(function () {
        isHovered = false;
    });
    var playing = true;
    var dd = 2;
    $(".slidecontrol>div").click(function () {
        playing = false;
        $(this).addClass("active").siblings().removeClass("active");
        $(".slides>a.thisslidenow").removeClass("thisslidenow").stop().css("z-index", "3").animate({ "opacity": "0", "width": "1494px", "margin-left": "-249px", "height": "882px" }, 1500, function () { $(this).css({ "display": "none", "z-index": "2", "width": "", "margin-left": "", "height": "", "display": "block" }); });
        $(".slides>a#" + $(this).attr("for")).addClass("thisslidenow").stop(true, true).fadeTo(0, 1);
    });
    function autoplay() {
        if (playing == true) {
            $("div[for='s" + dd + "']").trigger("click");
            playing = true;
            dd = (dd == 4) ? 1 : dd + 1;
            setTimeout(autoplay, 5000);
        }
    }
    setTimeout(autoplay, 4000);

    $(".slides>a:not(:first)").css({ "display": "none", "z-index": "2" });
    $(".slides>a:first").addClass("thisslidenow").css({ "z-index": "3" });
    $(".lilslidecontrol>div").click(function () {
        $(".lilslide").css("background-position", $(this).attr("pos"));
        $(".descrip").html($(this).attr("tex"));
        $(".bluebotton").attr("href", $(this).attr("goto"));
        $(this).addClass("active").siblings().removeClass("active");
    });
    function shimmer() {
        $(".greenbar").append("<img src='images2/shimmer.png' class='shimmer nower' />").find(".nower").removeClass("nower").animate({ "left": "450px" }, 10000, "linear", function () {
            $(this).remove();
        });
        setTimeout(shimmer, Math.random() * 3000 + 5000);
    }
    shimmer();

    var pos = 0;
    $(".leftcontrol img").fadeTo(0, 0);
    $(".rightcontrol img").fadeTo(0, .6);
    $(".rightcontrol").bind("click", function () {
        var cownt = ($(this).siblings(".slide").children("a").length - 4) * 182;
        pos = $(this).siblings(".slide").scrollLeft();
        if (pos >= cownt - 100) {
            $(this).find("img").fadeTo(0, 0);
        } else {
            $(this).siblings(".leftcontrol").find("img").fadeTo(0, 1, function () {
                $(this).css('filter', '');
            });
        }
        $(this).siblings(".slide").stop().animate({ scrollLeft: ((Math.ceil(pos / 182) + 1) * 182) }, 1000, "easeOutQuint", function () {
            pos = $(this).scrollLeft();
            if (pos >= cownt - 100) {
                $(this).siblings(".rightcontrol").find("img").fadeTo(0, 0);
            } else {
                $(this).siblings(".leftcontrol").find("img").fadeTo(0, 1, function () {
                    $(this).css('filter', '');
                });
            }
        });
    })/*.bind("mouseenter", function () {
        var cownt = ($(this).siblings(".slide").children("a").length - 4) * 182;
        if (pos != cownt) {
            $(this).find("img").stop().fadeTo(250, 1);
        }
    }).bind("mouseleave", function () {
        var cownt = ($(this).siblings(".slide").children("a").length - 4) * 182;
        if (pos != cownt) {
            $(this).find("img").stop().fadeTo(250, .6);
        }
    });*/
    $(".leftcontrol").bind("click", function () {
        pos = $(this).siblings(".slide").scrollLeft();
        if (pos <= 0) {
            $(this).find("img").fadeTo(0, 0);
        } else {
            $(this).siblings(".rightcontrol").find("img").fadeTo(0, 1, function () {
                $(this).css('filter', '');
            });
        }
        if (pos > 0) {
            $(this).siblings(".slide").stop().animate({ scrollLeft: ((Math.floor(pos / 182) - 1) * 182) }, 1000, "easeOutQuint", function () {
                if (pos <= 0) {
                    $(this).siblings(".leftcontrol").find("img").fadeTo(0, 0);
                } else {
                    $(this).siblings(".rightcontrol").find("img").fadeTo(0, 1, function () {
                        $(this).css('filter', '');
                    });
                }
            });
        }
    })/*.bind("mouseenter", function () {
        if (pos != 0) {
            $(this).find("img").stop().fadeTo(250, 1);
        }
    }).bind("mouseleave", function () {
        if (pos != 0) {
            $(this).find("img").stop().fadeTo(250, .6);
        }
    });*/
    var initialPositionX;
    var currentPositionX;
    var pressed = false;
    var timeOut = false;
    var firstNum = false;
    var secondNum = false;
    var activeslide;
    $(".slide").bind("mousedown touchstart", function (e) {
        activeslide = $(this);
        e.preventDefault()
        initialPositionX = e.pageX;
        firstNum = false;
        pressed = true;
    });
    $(window).mouseup(function (e) {
        if (pressed == true) {
            theSweetness(initialPositionX, e.pageX);
            pressed = false;
            initialPositionX = e.pageX;
        }
    });
    $(window).bind("touchend", function (e) {
        if (pressed == true) {
            theSweetness(initialPositionX, e.pageX);
            pressed = false;
            initialPositionX = e.pageX;
        }
    });
    function settimevar() {
        timeOut = false;
    }
    $(".slide").bind("mousemove touchmove", function (e) {
        if (timeOut == false && pressed == true) {
            timeOut = true;
            posAni(e.pageX);
            setTimeout(settimevar, 50);
        }
    });
    function posAni(where) {
        if (firstNum == false) {
            firstNum = where;
            initialPositionX = where;
        } else if (secondNum == false) {
            secondNum = where;
        }
        if (firstNum != false && secondNum != false) {
            theSweetness(firstNum, secondNum);
            firstNum = secondNum;
            secondNum = false;
        }
    };
    function theSweetness(start, end, e) {
        pos = activeslide.scrollLeft();
        var change = (start - end) * 1;
        var cownt = (activeslide.children("a").length - 4) * 182;
        if (pos + change > cownt) {
            activeslide.stop().animate({ scrollLeft: (cownt + (change / 10)) }, 600, "easeOutCubic");
            /*$(".inslide").stop().animate({ "padding-right": (change / 10) + "px" }, 600, "easeOutCubic", function () {
            $(".rightcontrol img").fadeTo(200, 0);
            $(this).animate({ "padding-right": "0px" }, 400, "easeInQuint");
            });*/
        } else if (pos + change < 0) {
            activeslide.stop().animate({ scrollLeft: 0 }, 600, "easeOutCubic");
            /*$(".inslide").stop().animate({ "padding-left": Math.abs(change / 10) + "px" }, 600, "easeOutCubic", function () {
            $(".leftcontrol img").fadeTo(200, 0);
            $(this).animate({ "padding-left": "0px" }, 400, "easeInQuint");
            });*/
        } else {
            /*$(".inslide").stop().animate({ "padding-left": "0px", "padding-left": "0px" }, 300);*/
            activeslide.stop().animate({ scrollLeft: (pos + change) }, 800 + (change * 0.6), "easeOutCubic", function () {
                if (activeslide.scrollLeft() % 208 < 104) {
                    activeslide.animate({ scrollLeft: Math.floor($(".slide").scrollLeft() / 182) * 182 }, 400, "easeInQuad");
                } else {
                    activeslide.animate({ scrollLeft: Math.ceil($(".slide").scrollLeft() / 182) * 182 }, 400, "easeInQuad");
                }
                pos = activeslide.scrollLeft();
                if (pos > 0) {
                    activeslide.siblings(".leftcontrol").find("img").fadeTo(200, .6);
                }
                if (pos < cownt) {
                    activeslide.siblings(".rightcontrol").find("img").fadeTo(200, .6);
                }
            });
        }
    }
    $(".aftered").ivmodal({ classname: "bigmodal", callback: function (one, two) {
        one.append("<div><img src='" + two.attr("href") + "' alt='' /></div>");
        $("body").animate({ scrollTop: "0" }, 500);
    }
    });
    $(".vidpop").click(function () {
        $('<div class="cover"></div><div class="coverspan"><span></span><div class="vidmodal"><img src="images/modalclose.png" alt="Close Modal" class="vidclose" /><br /><iframe width="640" height="360" src="http://www.youtube.com/embed/E1sf5PNQ7gM?autoplay=1" frameborder="0" allowfullscreen></iframe></div></div>').appendTo("body");
        $(".cover").css("opacity", "0").fadeTo(500, .6);
    });
    $(".vidclose, .baclose").live("click", function () {
        $(".coverspan").remove();
        $(".cover").fadeTo(500, 0, function () {
            $(this).remove();
        });
    });
    $(".popups a").bind("click", function (e) {
        e.preventDefault();
        $('<div class="cover"></div><div class="coverspan"><span></span><div class="bamodal"><img src="images/modalclose.png" alt="Close Modal" class="baclose" /><img src="' + $(this).attr("href") + '" /></div></div>').appendTo("body");
        $(".cover").css("opacity", "0").fadeTo(500, .6);
    });

    if ($(".slide").length > 0) {
        var domNode = $(".slide");
        domNode.get(0).ondrag = function () { return false; };
        domNode.get(0).onselectstart = function () { return false; };
    }
    /*** QUOTE FORM ***/
    var package = "";
    $(".getaquote").live("click", function (e) {
        e.preventDefault();
        getaquote("content/get_a_quote.php", "quote");
        return false;
    });
    $(".prodheader").live("click", function () {
        getaquote("content/prods.php", "limited");
    })
    function getaquote(url, assi) {
        $.ajax({
            type: "GET",
            url: url,
            success: function (data) {
                $("<div class='cover'></div><div class='coverspan'><span></span><div class='" + assi + "modal'> " + data + "</div></div>").appendTo("body");
                $(".cover").css("opacity", "0").fadeTo(500, .6);
                $("." + assi + "modal").animate({ "margin-top": "0px" }, 800);
            }
        });
    }
    function closequote() {
        $(".coverspan>div").animate({ "margin-top": "-2000" }, 800, function () {
            $(".coverspan").remove();
        });
        $(".cover").fadeTo(500, 0, function () {
            $(this).remove();
        });
    }

    $(".getpackage").live("click", function (e) {
        package = $(this).attr("package");
        e.preventDefault();
        getpackage();
        return false;
    });
    function getpackage() {
        $.ajax({
            type: "GET",
            url: "content/package.php",
            success: function (data) {
                $("<div class='cover'></div><div class='coverspan'><span></span><div class='quotemodal'> " + data + "</div></div>").appendTo("body");
                $(".cover").css("opacity", "0").fadeTo(500, .6);
                $(".quotemodal").animate({ "margin-top": "0px" }, 800);
                $(".packaged").html(package);
                $("#package").val(package);
            }
        });
    }

    $(".close").live("click", function () {
        closequote();
    });
    $(".submit").live("click", function (e) {
        e.preventDefault();
        var sendstring = "content/" + $(this).parents("form").attr("action") + "?";
        if ($(this).parents("form").ivalidator()) {
            var replacement = $(this).parents("form");
            $(this).parents("form").find("input[type=text], input[type=hidden], textarea, input[type=radio]:checked, input[type=checkbox]:checked").each(function () {
                sendstring += $(this).attr("name") + "=" + $(this).val() + "&";
            });
            $(this).parents("form").find("select").each(function () {
                sendstring += $(this).attr("name") + "=" + $(this).find("option:selected").text() + "&";
            });
            replacement.html("Sending...");
            $.ajax({
                type: "GET",
                url: sendstring,
                success: function (data) {
                    replacement.html(data);
                }
            });
        }
    });
    if ($(".accordion").length > 0) {
        $(".accordion").accordion({ controller: ".controller", actual: ".accord", open: 0 });
    }
});
$(window).load(function () {
});
