﻿$(
    function() {
        if ($("div.services marquee").get(0)) {
            $("div.services marquee").marquee("pointer").mouseover(
                    function() {
                        $(this).trigger("stop");
                    }
                ).mouseout(
                    function() {
                        $(this).trigger("start");
                    }
                )
        }

        if ($("div.Gallery-Picture[href]")) {
            checkIsVideo();
        }

        if ($("div.tabs").get(0)) {
            $("div.tabs").tabs(".homepage-images > div", {
                // enable "cross-fading" effect
                effect: "fade",
                fadeInSpeed: 1000,
                fadeOutSpeed: 0,
                // start from the beginning after the last tab
                rotate: true

            }).slideshow(
				{
					autoplay: true,
					interval: 3000,
					clickable: false
				});
        }

        $("div.Gallery-Navigation a, div.Gallery-Thumbs a").live("click",
            function(e) {
                e.preventDefault();
                $("div.Gallery").load($(this).attr("href") + " div.Gallery", function() { checkIsVideo() });
            }
        )

        $("button[href]").click(
            function() {
                var $href = $(this).attr("href"),
                    $target = $(this).attr("target");

                window.open($href, $target);
            }
        )

        $("div.ronin span[href]").each(
            function() {
                var $href = $(this).attr("href"),
                    $text = $(this).text();

                $(this).replaceWith("<a href=\"" + $href + "\" target=\"_blank\">" + $text + "</a>");
            }
        )

        $("div.Comments ol a").toggle(
            function() {
                $(this).parent().addClass("Selected");
                $(this).nextAll("div").fadeIn("fast");
            },
            function() {
                $(this).parent().removeClass("Selected");
                $(this).nextAll("div").hide();
            }
        )

        $("div.Comments span[href]").attr("title", function() { return $(this).attr("href"); }).addClass("Link").click(function() { window.location = "mailto:" + $(this).attr("href"); });

        $("div.AddComment h3").click(
            function() {
                $("div#AddCommentHid").toggle();
            }

        );

        $("div.Comment-Title a").click(
            function() {
                $("div.Comment-Hidden", $(this).parent().parent().toggleClass("Comment-Wrapper")).toggle();
            }
        );

        $("div.contact form").submit(
            function(e) {
                return false;
            }
        )

        $("div.contact form button").click(
            function() {
                if (CheckFrm($("div.contact form").get(0))) {

                    var html = "";

                    $("div.contact form input:text").each(
                        function(i) {
                            var $this = $(this);
                            html += $this.prev("label").text() + " " + $this.val() + "<br />";
                        }
                    )

                    $("div.contact form textarea").each(
                        function(i) {
                            var $this = $(this);
                            html += $this.prev("label").text() + " " + $this.val() + "<br />";
                        }
                    )

                    $("div.contact form :checked").each(
                        function(i) {
                            var $this = $(this);
                            html += $this.next("label").text() + "<br />";
                        }
                    )

                    $("div.contact form").attr({ method: "post" });

                    var wrapper = $("div.contact form");

                    var formData = "SubmitContact=True&html=" + escape(html);

                    $(this).text(sending).attr("disabled", true);

                    $.post(url, formData, function(data) {
                        wrapper.html(data);
                    }
                    )
                }
            }
        )
    }
)

    function checkIsVideo() {
    $("div.Gallery-Picture[href]").addClass("pointer").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:640px;height:480px;\" id=\"player\"></a><div>"    
            )
            flowplayer("player", "/swf/flowplayer-3.1.5.swf")
        }
    )

    return;
}
    
var Validate = {
    Email: function(s) {
        var re = /^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/i;
        return re.test(s);
    },
    ILPhone: function(s) {
        var re = /^0(?:77|72|52|54|50|57|3|4|8|9)-?\d{7}$/;
        return re.test(s);
    },
    ILDate: function(s) {
        var re = /^((29\/0?2(?=\/(\d{2}(0[48]|[13579][26]|[2468][048])|([13579][26]|[2468][048])00))|([01]?\d|2[0-8])\/(0?\d|1[0-2])|(29|30)\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/\d{4})$/;
        return re.test(s);
    },
    ILSocialID: function(s) {
        var tot = 0;
        for (i = 0; i < 8; i++) {
            x = (((i % 2) + 1) * s.charAt(i));
            if (x > 9) {
                x = x.toString();
                x = parseInt(x.charAt(0)) + parseInt(x.charAt(1))
            }

            tot += x;
        }

        if ((tot + parseInt(s.charAt(8))) % 10 == 0) {
            return true;
        } else {
            return false;
        }
    }
}
