var application = {
    corner: function(){
        $('.corner').corner();
        $('.corner-5').corner("5px");
    },
    formValidate: {
        init:function()
        {
            var self = this;
            $('form').submit(function(){
                var hasError = false;
                self.clear($(this));
                $(this).find('.required input, .required select').each(function(){
                    if ($.trim($(this).val()) == ''){
                        $(this).addClass('error-empty-field');
                        hasError = true;
                    }
                });
                if (hasError){
                    alert('Вы заполнили форму не полностью');
                    return false;
                }
                return true;
            });
        },
        clear: function(node)
        {
            node.find('.error-empty-field').removeClass('error-empty-field');
        }
    },
    formsVals: 
    {
        init: function(){
            this.save();
            this.load();
            this.clear();
        },
        save: function() {
            $('form').submit(function(i){
                var list = '';
                $(this).find('input:text, input:hidden, select, textarea').each(function(){
                    if ((key = $(this).attr('id'))) {
                        $.cookie(key, $(this).val());
                        list += '|' + key;
                    }
                });
                if (list != '')
                    $.cookie('form_vals', list.substring(1));
                return true;
            });
        },
        load: function() {
            console.log($_GET['success']);
            if (!(fVals = $.cookie('form_vals')))
                return;
            for (i in (list = fVals.split('|'))) {
                $('#'+list[i]).val($.cookie(list[i]));
            }
            
        },
        clear: function() {
            if (!(fVals = $.cookie('form_vals')))
                return;
            $.cookie('form_vals', null);
            for (i in (list = fVals.split('|'))) 
               $.cookie(list[i], null);
        }
    },
    fancybox: function()
    {
        $('.facybox').fancybox({'titlePosition':'inside'});
    },
    spisok: function() {
        $(".dksub").collapsorz({
        minimum: 9
        , showText: "Все работы"
        , hideText: "Все работы"
        });
    },
    project: function() {
        $(".menu-item:odd").addClass("odd");
        $(".menu-item:even").addClass("even");
        $(".menu-item:even").append("<img class='square' src='/images/progect_background_centert1.png'>");
        $(".menu-item:last .square").css("display","none");
        $(".tabs li:last").css("background","none");
    },
    project_tabs: function() {
        var col = $('.tabs li');
        if(col.length=='0'){
            $('.tabs').hide();
            return
        }
        $('.submen li').hide();
        $('.tabs li:first').addClass('first');
        $('.tabs li:first a').addClass('active');
        var pos = $($('.tabs li:first a').attr('href'));
        $('.submen li').find(pos).parent().show();
        $('.tabs a').each(function(){
            $(this).click(function(){
                var pos = $($(this).attr('href'));
                $('.tabs').find('.active').removeClass('active');
                $('.submen li').hide();
                $(this).addClass('active');
                $('.submen li').find(pos).parent().show();
                return false;
            });
        });
    },
    location: function() {
        $('.location input').click(function(){
            location.href='http://www.exclusivediplom.ru/buy/';
            //return false;
        });
    }
}
$(document).ready(function(){
    (function() {
        this.project();
        this.corner();
        this.spisok();
        this.location();
        this.project_tabs();
        this.formValidate.init();
        this.formsVals.init();
        this.fancybox();
    }).call(application);
});
function $_GET(q,s) {
    s = s ? s : window.location.search;
    return (s=s.replace(/^\?/,'&').match(new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i'))) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined;
}
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
