function get_dimensions()
{
     var dimensions = {width: 0, height: 0};
     if (document.documentElement) 
     {
         dimensions.width = document.documentElement.offsetWidth;
         dimensions.height = document.documentElement.offsetHeight;
     }
     else if (window.innerWidth && window.innerHeight)
     {
         dimensions.width = window.innerWidth;
         dimensions.height = window.innerHeight;
     }
     return dimensions;

}

function get_scrolltop() 
{
    return sizeFilterResults(
        window.pageYOffset ? window.pageYOffset : 0, 
        document.documentElement ? document.documentElement.scrollTop : 0, 
        document.body ? document.body.scrollTop : 0	);
}

function sizeFilterResults(n_win, n_docel, n_body) 
{
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function get_clientheight() 
{
    return sizeFilterResults (
        window.innerHeight ? window.innerHeight : 0, 
        document.documentElement ? document.documentElement.clientHeight : 0, 
        document.body ? document.body.clientHeight : 0
    );
}

var popup = 
{
    show: function() 
    {
        var popup_height = $('#popup').height();
        var popup_width = $('#popup').width();
        var width = get_dimensions().width;
        var height = get_clientheight();
        var scroll_top = get_scrolltop();
        var top = Math.round((height / 2) - (popup_height / 2)) + scroll_top;
        var left = Math.round((width / 2) - (popup_width / 2));
        
        $('#popup').css({
            'top': top,
            'left': left
        });
        $('#popup, #curtain').show();
    },
    
    hide: function() {
        $('#popup, #curtain').hide();
    },
    
    text: function(text)
    {
        var text = text;
        $('#popup .text').html(text);
    }
}

/**
 * --------------------------------------------------------------------
 * jQuery customfileinput plugin
 * Author: Scott Jehl, scott@filamentgroup.com
 * Copyright (c) 2009 Filament Group 
 * licensed under MIT (filamentgroup.com/examples/mit-license.txt)
 * --------------------------------------------------------------------
 */
$.fn.customFileInput = function(){
	return $(this).each(function(){
	//apply events and styles for file input element
	var fileInput = $(this)
		.addClass('customfile-input') //add class for CSS
		.mouseover(function(){ upload.addClass('customfile-hover'); })
		.mouseout(function(){ upload.removeClass('customfile-hover'); })
		.focus(function(){
			upload.addClass('customfile-focus'); 
			fileInput.data('val', fileInput.val());
		})
		.blur(function(){ 
			upload.removeClass('customfile-focus');
			$(this).trigger('checkChange');
		 })
		 .bind('disable',function(){
		 	fileInput.attr('disabled',true);
			upload.addClass('customfile-disabled');
		})
		.bind('enable',function(){
			fileInput.removeAttr('disabled');
			upload.removeClass('customfile-disabled');
		})
		.bind('checkChange', function(){
			if(fileInput.val() && fileInput.val() != fileInput.data('val')){
				fileInput.trigger('change');
			}
		})
		.bind('change',function(){
			//get file name
			var fileName = $(this).val().split(/\\/).pop();
			//get file extension
			var fileExt = 'customfile-ext-' + fileName.split('.').pop().toLowerCase();
			//update the feedback
			uploadFeedback
				.text(fileName) //set feedback text to filename
				.removeClass(uploadFeedback.data('fileExt') || '') //remove any existing file extension class
				.addClass(fileExt) //add file extension class
				.data('fileExt', fileExt) //store file extension for class removal on next change
				.addClass('customfile-feedback-populated'); //add class to show populated state
			//change text of button	
			uploadButton.text('Обзор');	
		})
		.click(function(){ //for IE and Opera, make sure change fires after choosing a file, using an async callback
			fileInput.data('val', fileInput.val());
			setTimeout(function(){
				fileInput.trigger('checkChange');
			},100);
		});
		
	//create custom control container
	var upload = $('<div class="customfile"></div>');
	//create custom control button
	var uploadButton = $('<span class="customfile-button" aria-hidden="true">Обзор</span>').appendTo(upload);
	//create custom control feedback
	var uploadFeedback = $('<span class="customfile-feedback" aria-hidden="true"></span>').appendTo(upload);
	
	uploadFeedback.click(function(){
		fileInput.click();
	});
	
	uploadButton.click(function(){
		fileInput.click();
	});
	
	
	//match disabled state
	if(fileInput.is('[disabled]')){
		fileInput.trigger('disable');
	}
		
	
	//on mousemove, keep file input under the cursor to steal click
	upload
		.mousemove(function(e){
//			fileInput.css({
//				'left': e.pageX - upload.offset().left - fileInput.outerWidth() + 20, //position right side 20px right of cursor X)
//				'top': e.pageY - upload.offset().top - $(window).scrollTop() - 3
//			});	
		})
		.insertAfter(fileInput); //insert after the input
	
	fileInput.appendTo(upload);
		
	});
};

