﻿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!important;}</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(){
	
	$("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('');
				});
			
		});
		
	}
	
});

(function($) {
  var cache = [];
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(document).ready(function(){
	jQuery(function(){
	jQuery(".gallery").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
		mouseWheel: true
    	});
	});
});

$(document).ready(function() {

	/* This is basic - uses default settings */
	
	$("a.single_image").fancybox();
	
	/* Using custom settings */
	
	$("a#inline").fancybox({
		'hideOnContentClick': true
	});

	/* Apply fancybox to multiple items */
	
	$("a.grouped_elements").fancybox({
		'transitionIn'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'titlePosition' :   'over'
									 });
	
	$("a.grouped_sertif").fancybox({
		'transitionIn'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'titlePosition' :   'over',
		'autoScale'		:   false,
		'height'        :   900,
		'width'         :   'auto'
									 });
	
});
