﻿var userAgent = navigator.userAgent.toLowerCase();
$.browser.chrome = /chrome/.test(userAgent);

$(
    function() {
        cms.form();
        cms.orderForm();
        cms.spanLinks();
        cms.comments();
        cms.video();
        cms.homepageTeachers();
        cms.marquee();
        cms.fixIeHeight();
    }
)

var cms = {
	fixIeHeight: function() {
        var height = $(".container").height() - $(".footer").height() - $(".header").height() - $(".menu").height() - 85;
        $("div.right").css("height", height + "px");
    },
    
    fixChromeCSS: function() {
        if ($.browser.chrome) {
            $("div.contact-form textarea").css("width", "127px");
            $("div.inner-contact-form textarea").css("width", "127px");
        }
    },
    
    marquee: function() {
        if ($("div.news marquee").get(0)) {
            $("div.news marquee").marquee("pointer").mouseover(
                    function() {
                        $(this).trigger("stop");
                    }
                ).mouseout(
                    function() {
                        $(this).trigger("start");
                    }
                )
        }
    },
    
    homepageTeachers : function(){
        if ($("div.homepage-teachers").get(0)) {
            $("div.homepage-teachers-tabs").tabs("div.homepage-teachers > div.article-item", {
				rotate: true
                
            }).slideshow({
				autoplay: true,
				interval: 10000,
				clickable: false
            });

            var tabs = $("div.homepage-teachers-tabs").tabs();
            

        }
    },

    spanLinks: function() {
        $("div.ronin span[href]").each(
            function() {
                var $href = $(this).attr("href"),
                    $text = $(this).text();

                $(this).replaceWith("<a href=\"" + $href + "\" target=\"_blank\">" + $text + "</a>");
            }
        )
    },

    video: function() {
        $("div.gallery-picture[href]").find("span.play").show();
        $("div.gallery-picture[href]").one("click",
            function() {
                $(this).html(
                "<div class=\"player\"><a href=\"" + $(this).attr("href") + "\" style=\"display:block;width:500px;height:400px;\" id=\"player\"></a><div>"
                )
                flowplayer("player", "/swf/flowplayer-3.1.5.swf")
            }
        )
    },
    
    gallery: function() {
        if ($("div.gallery-navigation")) {
            $("div.gallery-navigation a").live("click",
            function(e) {
                e.preventDefault();
                $("div.right-content").load($(this).attr("href") + " div.right-content");

                }
            )
        }
    },

    comments: function() {
        if ($(".comments")) {
            $(".add-comment h3").click(
            function() {
                $(".add-comment form div").toggle();
            }
        )

        $(".comments span[href]").attr("title", function() { return $(this).attr("href"); }).addClass("link").click(function() { window.location = "mailto:" + $(this).attr("href"); });

        $(".comments li a").click(
                function() {
                    $(this).nextAll("div").toggle();
                }
            );
        }
    },

    form: function() {
        if ($("div.form")) {
            $("div.form form").submit(
                function(e) {
                    return false;
                }
            )
            $("div.form form button").click(
                function() {
                    if (CheckFrm($("div.form form").get(0))) {

                        var html = "";

                        $("div.form form input:text").each(
                            function(i) {
                                var $this = $(this);
                                html += $this.prev("label").text() + " " + $this.val() + "<br />";
                            }
                        )

                        $("div.form form textarea").each(
                            function(i) {
                                var $this = $(this);
                                html += $this.prev("label").text() + " " + $this.val() + "<br />";
                            }
                        )

                        $("div.form form :checked").each(
                            function(i) {
                                var $this = $(this);
                                html += $this.next("label").text() + "<br />";
                            }
                        )

                        $("div.form form").attr({ method: "post" });

                        var wrapper = $("div.form");

                        var formData = "SubmitContact=True&html=" + escape(html);

                        $(this).text(sending).attr("disabled", true);

                        $.post(url, formData, function(data) {
                            wrapper.html(data);
                            }
                        )
                    }
                }
            )
        }
    },

    orderForm: function() {
        if ($("div.order-form")) {
            $("div.order-form form").submit(
                function(e) {
                    return false;
                }
            )
            $("div.order-form form button").click(
                function() {
                if (CheckFrm($("div.order-form form").get(0))) {

                        var html = "";

                        $("div.order-form form input:text, div.order-form form input:hidden").each(
                            function(i) {
                                var $this = $(this);
                                html += $this.prev("label").text() + " " + $this.val() + "<br />";
                            }
                        )

                        $("div.order-form form textarea").each(
                            function(i) {
                                var $this = $(this);
                                html += $this.prev("label").text() + " " + $this.val() + "<br />";
                            }
                        )

                        $("div.order-form form :checked").each(
                            function(i) {
                                var $this = $(this);
                                html += $this.next("label").text() + "<br />";
                            }
                        )

                        $("div.order-form form").attr({ method: "post" });

                            var wrapper = $("div.order-form");

                        var formData = "SubmitContact=True&html=" + escape(html);

                        $(this).text(sending).attr("disabled", true);

                        $.post(url, formData, function(data) {
                            wrapper.html(data);
                        }
                        )
                    }
                }
            )
        }
    }
}


