﻿function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){

					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

addEvent(window,'unload',EventCache.flush);

jQuery.fn.validForm = function(userOptions) {

    var options = {
    	errorContainer: "#errorList",
    	errorContainerCssClass: {
    		'text-color': "#F00"
    	}
    }

    $.extend(options, userOptions);

    $('head').append("<style>.inputError{border: 1px solid #F00!important; background: #FAA;}</style>");

    $(this).each( function() {
    	var id = "#" + $(this).attr('id');
        if ($(id + " input").length > 0) {
            Validate(id);
        } else {
        	return false;
        }
    });
    function Validate(form) {

        $(form + " input[type=submit]").click(function() {
        var errorAmount = 0;
        var collection = $(form + " input[type=text].required")
            .add(form +" input[type=password].required")
            .add(form +" input[type=file].required")
            .add(form +" textarea.required")
            .add(form +" select.required");
        collection.each(function() {
         	    var element = $(this);
         		var length = $.trim(element.attr('value'));

         		if (!length) {
         			element.addClass('inputError');
         			errorAmount++;
         		}
         		else {
                    element.removeClass('inputError');
         		}
     	});
     	collection.keypress( function (){
            $(this).removeClass('inputError');
            if ($(options.errorContainer).is(':visible')) {
        	$(options.errorContainer).hide();
            }
        });
        collection.change( function() {
            if (($(this).attr('value')).length < 1) {
                $(this).addClass('inputError');
            }
            else {
            	$(this).removeClass('inputError');
            }
        });

        if (errorAmount) {
        	$(options.errorContainer).html('Пожалуйста, заполните все обязательные поля');
        	$(options.errorContainer).show();
            return false;
        } else {
        	$(options.errorContainer).hide();
        }
        });
    }

}

$(document).ready(function(){

    $.blockUI.defaults.overlayCSS = {}; 
    $('.blockui').click(function() {
        $.blockUI({ message: $('#formask'),
            css: { 
            border: 'none',
            cursor: 'default', 
            width: '500px',
            '-webkit-border-radius': '5px', 
            '-moz-border-radius': '5px',
            'top': '100px',
            left: '50%',
            'margin-left': '-250px'
            },
            focusInput: true
        });
        return false;
    });

    $('.formclose').click(function() {
        $.unblockUI();
    });

    $('.opener').click(function() {
       $(this).next().slideToggle();
    });

	
	$("a[rel*='external']").click(function () {
		this.target = "_blank";
	});
	
	if (!$.browser.webkit) {
		
		$('INPUT[placeholder], TEXTAREA[placeholder]').blur(function(){ 
			
			if ($(this).val()=='') {
				$(this).val($(this).attr('placeholder'));
				$(this).addClass('placeholder');
			}
			
		}).focus(function(){
			
			$(this).removeClass('placeholder');
			if ($(this).val()==$(this).attr('placeholder'))
				$(this).val('');
			
		}).each(function(){
			
			if ( ($(this).val()=='') || ($(this).val()==$(this).attr('placeholder')) ) {
				$(this).val( $(this).attr('placeholder') );
				$(this).addClass('placeholder');
			}
			
			var form = $(this).closest('FORM');
			if (form.length)
				form.submit(function(){
					if ($(this).val()==$(this).attr('placeholder'))
						$(this).val('');
				});
			
		});
		
	}
	
});

