$(function() {
	
	setTimeout("main.init()", 1000);
	
	
	
		});

var main = {
	init: function() {
		
		/*
		$(".shadow:not(.hidden)").dropShadow({left: 1, top: 1, blur: 2, opacity: 0.3});
		*/
		
		
		$(".corner").each(function() {
			$(this).corner($(this).attr('corner'));
		});
		
		
		$(".datepicker").datepicker();

		$('a[href*=#]').click(function() {
			 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
			 && location.hostname == this.hostname) {
			   var $target = $(this.hash);
			   $target = $target.length && $target
			   || $('[name=' + this.hash.slice(1) +']');
			   if ($target.length) {
			  var targetOffset = $target.offset().top;
			  $('html,body')
			  .animate({scrollTop: targetOffset}, 1000);
			    return false;
			   }
			 }
			  });
		
		this.fancy();
		
		if (auto_scroll)
			$('html,body').animate({scrollTop: $('#' + auto_scroll).offset().top}, 1000);
		
		
		
	},
	
	fancy: function() {
		$("a.fancy").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'titlePosition' 	: 'over',
			'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
			    return '';//'<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
			}
		});		
	}
};

/**
 * Фотографии программы
 * @param id
 * @return
 */
function program_load_images(id) {

	$("#link-load-images")
		.html('<img src="/i/ajax/5.gif" align="absmiddle"> загружаются фотографии')
		.addClass('none');
	
	$.get('/program/images/' + id, 
		function(d) {
			$('#list-images-2').append(d);
			$("#link-load-images")
				.html('Смотреть все фотографии')
				.removeClass('none')
				.attr('onclick', '')
				.click(function() {
					$('html,body').animate({scrollTop: $('#list-images-2').offset().top - 30}, 1000);
				});
			main.fancy();
			$('html,body').animate({scrollTop: $('#list-images-2').offset().top - 30}, 1000);
		});
}


var bonus = {
	show_form: function(id) {
		$('#pl_form_' + id)
			.addClass('pad_5')
			.css({textAlign: 'left'})
			.html('<img src="/i/ajax/8.gif" align="absmiddle"> загружается форма')
			.load('/bonus/form/?id=' + id);
	},
	
	send: function(id) {
		
		var form = $('#form-order-' + id);
		
		var data = form.serialize();
		$.post('/bonus/send/', data, function(d) {
			
			if (d.error) {
				if (typeof(timer_error) != 'undefined') clearTimeout(timer_error);

				if (!$('#kid-order-error').length)
					$('body').append('<div id="kid-order-error"></div>');

				var field = form.find('[name="' + d.code + '"]');

				$('#kid-order-error')
					.html(d.error)
					.css({left: field.offset().left, top: field.offset().top + field.height() + 7})							
					.show();	

				timer_error = setTimeout("$('#kid-order-error').hide()", 2000);
			}
			
			if (d.ok)
				$('#pl_form_' + id).html(d.ok);
		}, 'json');
	}
};

function create_mail2friend() {
	$("div.mail2friend a").fancybox({
		'width'				: 500,
		'height'			: 400,
        'autoScale'     	: true,
		'type'				: 'iframe',
		'autoDimensions'	: false
	});
}

function create_lw() {
	$("a.but-lw").fancybox({
		'width'				: 700,
		'height'			: 430,
        'autoScale'     	: true,
		'type'				: 'iframe',
		'autoDimensions'	: false
	});
}

var wmodal = {
		history: [],	
			
		close: function() {
			$.modal.close();
		},	
		
		open: function(url, size, callback) {
		
			if (typeof size == 'undefined') var size = {};	
			
			if (typeof size.width == 'undefined') size.width = 'auto';
			if (typeof size.height == 'undefined') size.height = 'auto';
			
			this.history.push({url: url, size: size});
			
			this.show_loader();
			
			var w 			= $(window);
			var modal		= $('#modal');
			var container 	= $('#simplemodal-container');
			
			modal
				.load(url,
					function () {
					
						container
							.find('.simplemodal-close')
							.show();
						
						container
							.css({width: ((size.width != 'auto') ? size.width + 'px' : 'auto'), height: ((size.height != 'auto') ? size.height + 'px' : 'auto')})
							.removeClass('aj-load');
						
						container
							.css({
								top: (w.height()-container.height())/2 + "px",
								left: (w.width()-container.width())/2+w.scrollLeft() + "px"
							});
						
						if (typeof callback != 'undefined') callback();					
					});
		},
		
		show_loader: function() {
			var w 			= $(window);
			var modal		= $('#modal');		
			
			if(!modal.length) {
				$('body').append('<div id="modal"></div>');
				var modal = $('#modal');
			}

			modal
			.html('')
			.show()
			.modal({opacity:40});		
			
			
			var container 	= $('#simplemodal-container');
			
			container
				.css({width: '40px', height: '40px'})
				.addClass('aj-load')
				.css({
					top: (w.height()-container.height())/2 + "px",
					left: (w.width()-container.width())/2+w.scrollLeft() + "px"
				});	
			
			container
				.find('.simplemodal-close')
				.hide();		
		},
		
		resize: function(width, height) {
			var w 			= $(window);
			var container 	= $('#simplemodal-container');
			
			container
				.animate({
					width: width + 'px', 
					height: ((typeof height != 'undefined') ? height + 'px' : 'auto')
				});
		},
		
		back: function() {
			if (this.history.length >= 2)
				this.open(this.history[this.history.length - 2].url, this.history[this.history.length - 2].size);
		}
	};



window.onerror = function(e){
	  return true;
}

