//document ready
(function($) {
    $(function() {

        var date = $(":date");
        if (date.size() > 0) {
            if (date.hasClass('bigRange')) {
                date.dateinput({selectors: true, yearRange: [-100,1]});
            }
            else
                date.dateinput();
        }


        var gallery = $("a.photo");
        if (gallery.size()) {
            gallery.colorbox({current:""});
        }


        $('.showhide').click(function() {

            var selector = $(this).attr('data-selector');
            var focus = $(this).attr('data-focus');

            if ($(selector).size() > 0) {
                var parent = $(this).attr('data-parent');
                if (parent)
                    $(this).parents(parent).find(selector).slideToggle("slow");
                else
                    selector.slideToggle("slow");

                var text = $(this).text();

                if (text.indexOf('Show') != -1)
                    text = text.replace("Show", "Hide");
                else
                    text = text.replace("Hide", "Show");

                $(this).text(text);

                if ($(focus).length)
                    $(focus).focus();


            }

            return false;
        });


        //reg form  client validation
        var regform = $("#registration-form");
        regform.validator().submit(function(e) {

            var form = $(this);

            // client-side validation OK.
            if (!e.isDefaultPrevented()) {

                // submit with AJAX
                $.postJSON(form.attr('action'), form.serialize(), function(json) {
                    if (json.result == 0) {
                        form.data('validator').invalidate(json);
                    }
                    else {
                        if (json.link) {
                            location.href = json.link;
                        }
                    }
                });

                // prevent default form submission logic
                e.preventDefault();
            }
        });

        $.tools.validator.fn("[data-equals]", "Value not equal with the $1 field", function(input) {
            var name = input.attr("data-equals"),
                field = this.getInputs().filter("[name=" + name + "]");
            return input.val() == field.val() ? true : [name];
        });


        // enable tabs on page wih expose for panes
        var tabs = $("ul.tabs");

        if (tabs.length) {
            var panes = $(".panes");

            panes.click(function() {
                $(this).expose({color: '#789'});
            });

            tabs.tabs("div.panes > div");
        }


        // check selected option and show/hide phone field
        setPhone = function (option, phone) {
            if (option.val() == "D") {
                phone.show();
                phone.find("input").attr("disabled", false).attr("required", "required");
            }
            else {
                phone.hide();
                phone.find("input").attr("disabled", true);
                regform.data("validator").reset();
            }

        };

        var profile_type = $("#id_profile_type");
        if (profile_type.length) {
            var phone = profile_type.parents("tr").next();
            var option = $("option:selected", $(this));

            setPhone(option, phone);

            profile_type.change(function() {
                var option = $("option:selected", $(this));

                setPhone(option, phone);

            });
        }


        // hide messages from user
        var messages = $('div.messages');

        if (messages.length)
            messages.delay(4000).slideUp(500);


//	var video = $(".video:not(html)");
//	if(video.length)
//		video.colorbox({iframe:true, innerWidth:700, innerHeight:500});

        //$('.head_menu .items').align();


        $('a.read_more[data-selector]').click(function() {

            var selector = $(this).attr('data-selector');

            if ($(selector).size() > 0) {
                var text = $(this).next('div.read_more').text();

                var parent = $(this).attr('data-parent');
                if (parent)
                    $(this).parents(parent).find(selector).text(text);
                else
                    $(selector).text(text);
            }

            $(this).remove();

            return false;
        });
    });

    function loading_start() {
        $("#loading").slideDown("fast");
    }

    $(document).ajaxSend(function(event, xhr, settings) {
        loading_start();

        function getCookie(name) {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }

        function sameOrigin(url) {
            // url could be relative or scheme relative or absolute
            var host = document.location.host; // host + port
            var protocol = document.location.protocol;
            var sr_origin = '//' + host;
            var origin = protocol + sr_origin;
            // Allow absolute or scheme relative URLs to same origin
            return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
                (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
                // or any other URL that isn't scheme relative or absolute i.e relative.
                !(/^(\/\/|http:|https:).*/.test(url));
        }

        function safeMethod(method) {
            return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
        }

        if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
            xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
        }
    });

    function loading_finish() {
        $("#loading").slideUp("fast");
    }

    $(document).ajaxComplete(function() {
        loading_finish();
    });
})(jQuery);

