﻿function bg(){
	var elBg = $('#backgroundBody'),
			elTable = $('#tableBody'),
			s = $(window).height() < $(document).height();
	if(s){elBg.height(elTable.height());}
	$(window).resize(function(){
		elBg.height(elTable.height());
	});
}

function selectButton(id){
	var htmlID;
	switch(id){
		case 0: htmlID = 3
		break
		case 1: htmlID = 2
		break
		case 2: htmlID = 4
		break
		case 3: htmlID = 0
		break
	}
	$('.leftmenu > li:eq('+htmlID+')').addClass('hover');
}

function deselectButton(){
	$('.leftmenu > li').removeClass('hover');
}

(function($){
	$.fn.accordion = function(options){
		r = $.extend({
			callback: function(){}
		}, options||{});
		var dl = $(this); 
		$('.accordion-head', dl).unbind().bind('click', function(e){
			var sibling = $(this).next().find('div:eq(0)');
			if($(this).hasClass('active')){
				sibling.slideUp(700, function(){r.callback();});
				$(this).removeClass('active');
			}else{
				$('.accordion-head', dl).removeClass('active');
				$('.accordion-content>div', dl).slideUp(700);
				sibling.slideDown(700, function(){r.callback();});
				$(this).addClass('active');
			}
		});
	};
})(jQuery);

(function($){
	$.preloadImages = function () {
		if (typeof arguments[arguments.length - 1] == 'function') {
			var callback = arguments[arguments.length - 1];
		} else {
			var callback = false;
		}
		if (typeof arguments[0] == 'object') {
			var images = arguments[0];
			var n = images.length;
		} else {
			var images = arguments;
			var n = images.length - 1;
		}
		var not_loaded = n;
		for (var i = 0; i < n; i++) {
			$(new Image()).load(function () {
				if (--not_loaded < 1 && typeof callback == 'function') { callback(); }
			}).attr('src', images[i]);
		}
	}
})(jQuery);

(function($){
	$.fn.angle = function(options){

		var o = $.extend({
			maxHeight: 0
		}, options||{});

		var ieOld = $.browser.msie && $.browser.version <= 7 ? true : false,
				el = $('.angleInner > *:eq(0)', $(this)),
				elW = el.width(),
				elH = el.height(),
				elT = -(o.maxHeight - el.height())/2,
				elL = Math.round((elW-(o.maxHeight*(elW/elH)))/2),
				image = {},
				title = {};

		return this.each(function(index){

			var parent = $('.angleInner', $(this)),
					elParent = $('.angleInner > *', $(this)),
					area = $('area', $(this));

			for(var i = 0; i <= elParent.length - 1; i++){
				var position = elParent.eq(i).position();
				elParent.eq(i).css({top: position.top + 'px', left: position.left + 'px'});
			}
			elParent.css({position:'absolute'});

			area.hover(
				function(){
					image = $('img', elParent.eq($(this).index()));
					title = image.next();
					image.css({zIndex: 1})
						.stop().animate({height: o.maxHeight, marginTop: elT, marginLeft: elL}, 400);
					title.stop().animate({opacity: 1}, 400);
					if(ieOld) ieFix(elParent.eq($(this).index()), 1);
				},
				function(){
					image.css({zIndex: 2})
						.stop().animate({height: elH, marginTop: 0, marginLeft: 0}, 200, function(){$(this).css({zIndex: 0});});
					title.stop().animate({opacity: 0}, 200);
					if(ieOld) ieFix(elParent.eq($(this).index()), 0);
				}
			);

			if($.browser.msie){area.focus(function(){this.blur();});}

		});

		function getCoords(step){
			var ar = o.coords.split(',');
			for(var i = 0; i <= ar.length - 1; i++){
				if((i+1)%2){ar[i] = Number(ar[i]) + step;}
			}
			return ar;
		}
		function ieFix(obj, num){obj.css({zIndex: num});}

	};
})(jQuery);

function showPopup(url){
	$.ajax({
		url: url,
		type: 'get',
		cache: false,
		success: function(body){
			var container = $('#popupContainer');
			container.html(body);
			initializeContext(container);
		}
	});
	return false;
}

function initializeContext(context){
	$('.checkboxList input,.radioList input, select.select', context).uniform();
	$('input:checkbox.input-validation-error, input:radio.input-validation-error', context).parents('.radioList, .checkboxList').addClass('list-error');
	$('select.input-validation-error', context).parents('.customSelect').addClass('select-error');
	$('a[rel^=modal]', context).modalbox();
	$('.modalbox.autoShow.centerModal', context).showModalBox({center: true});
}

$(document).ready(function(){
	initializeContext();
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'light_rounded',
		showTitle: false,
		markup: '<div class="pp_pic_holder"> \
						<div class="ppt">&nbsp;</div> \
						<div class="pp_content_container"> \
							<div class="pp_left"> \
							<div class="pp_right"> \
								<div class="pp_content"> \
									<div class="pp_loaderIcon"></div> \
									<div class="pp_fade"> \
										<div class="pp_details clearfix"><a class="pp_close" href="#">Закрыть</a></div> \
										<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
										<div class="pp_hoverContainer"> \
											<a class="pp_next" href="#">next</a> \
											<a class="pp_previous" href="#">previous</a> \
										</div> \
										<div id="pp_full_res"></div> \
									</div> \
								</div> \
							</div> \
							</div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>',
		gallery_markup: ''
	});
});
