function modal(object) {
	var speed = 150;
	$(object).modal({
		opacity: 50,
		overlayCss: {
			backgroundColor:"black"
		},
		overlayClose: true,
		onOpen: function (dialog) {
			dialog.overlay.fadeIn(speed);
			dialog.container.fadeIn(speed);
			dialog.data.fadeIn(speed);
		},
		onClose: function (dialog) {
			dialog.overlay.fadeOut(speed);
			dialog.container.fadeOut(speed);
			dialog.data.fadeOut(speed, function () {
				$.modal.close();
			});
		}
	});
}

var startcarusel = function() {
	$('#slider').jcarousel({
		wrap: 'circular',
		auto: 4,
		vertical: true,
		animation: 2000,
		scroll: 1
	});
	$('#slider-no').jcarousel({
		wrap: 'circular',
		auto: 0,
		vertical: true,
		animation: 2000,
		scroll: 1
	});
}
var valignblock = function(element){
	if(element instanceof jQuery) {
		var el = element;
	} else {
		el = $(element);
	}
	var diff = parseInt(el.height());
	el.css({
		'margin-top': '-' + (diff / 2) + 'px'
	});
}
var l_hash = '';
var w_hash = '';
var t_img;
var t_href;
var switchTab = function(hash, object) {
	if($(object).attr('rel') != undefined && $(object).attr('rel').length != 0){
		var prefix = $(object).attr('rel');
	} else {
		var prefix = '';
	}
	$(object).parent().parent().parent().find('.tab-content-show'+prefix).addClass('tab-content-hide').removeClass('tab-content-show'+prefix);
	$('#tab-content-' + hash).removeClass('tab-content-hide').addClass('tab-content-show'+prefix);
	$(object).parent().parent().find('li').each(function() {
		$(this).removeClass('on');
	});
	object.parent().addClass('on');
	object.blur();
	m_height = 0;
	$('.homecontent div:visible dl').each(function(){
		if($(this).height() > m_height){
			m_height = $(this).height();
		}
	});
	$('.homecontent div:visible dl').height(m_height);
	if($(object).closest('.vkl').find('.leftclick').length > 0 && $(object).closest('.vkl').find('.rightclick').length > 0) {
		if($(object).closest('.homevkl').find('li.on').prevAll('li').find('a').length > 0){
			$(object).closest('.vkl').find('.leftclick img').attr('src',$(object).closest('.vkl').find('.leftclick img').attr('src').replace('lrleft.','lrlefton.'));
		} else {
			$(object).closest('.vkl').find('.leftclick img').attr('src',$(object).closest('.vkl').find('.leftclick img').attr('src').replace('lrlefton.','lrleft.'));
		}
		if($(object).closest('.homevkl').find('li.on').nextAll('li').find('a').length > 0){
			$(object).closest('.vkl').find('.rightclick img').attr('src',$(object).closest('.vkl').find('.rightclick img').attr('src').replace('lrright.','lrrighton.'));
		} else {
			$(object).closest('.vkl').find('.rightclick img').attr('src',$(object).closest('.vkl').find('.rightclick img').attr('src').replace('lrrighton.','lrright.'));
		}
	}
};

$(document).ready(function() {

	$('a[rel="fancybox_gallery"]').fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		}
	});

	$("a.fancybox_video").fancybox({
		ajax : {
			type	: "GET"
		}
	});

	$('a.fancybox').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic'
	});

	w_hash = window.location.hash.substr(1);
	if (w_hash != '') {
		switchTab(w_hash, $(".tab-nav-link").filter(function() {
			return this.hash == window.location.hash;
		}));
	} else {
		if($('.tab-nav-list li.on').length == 0){
			$('.tab-nav-list li:first').addClass('on');
		}
	}
	$('.tab-nav-link').click(function() {
		l_hash = this.hash.substr(1);
		switchTab(l_hash, $(this));
		return false;
	});
	$('.homecontent .links a').click(function(){
		$(this).parent().find('a').each(function(){
			$(this).removeClass('on');
		});
		$(this).addClass('on');
		$(this).parent().parent().find('.tab-sub-content').hide();
		$(this).parent().parent().find('#tab-sub-content-'+$(this).attr('rel')).show();
	});
	$("#slider-hor").easySlider({
		auto: true,
		continuous: true,
		speed: 1500,
		pause: 3000
	});
	$('#tableMain input:text').each(function(){
		$(this).addClass('inp');
	});
	$('#tableMain textarea').each(function(){
		$(this).addClass('inp2');
	});
	$('.homevkl a').each(function(){
		valignblock($(this));
	});
	$('.sel').live('click',function(){
		$(this).parent().find('.selmenu').slideToggle('slow');
	});
	$('.select').mouseleave(function(){
		$('.selmenu',this).slideUp('slow');
	});
	$('.selmenu a').click(function(){
		var p_form = $(this).closest('.select').find('.hidden-form form');
		var get_var = ($(this).attr('rel') != undefined && $(this).attr('rel').length > 0) ? $(this).attr('rel') : $(this).text();
		$('input',p_form).val(get_var);
		p_form.submit();
	});
	startcarusel();
	
	$("#menu table td").mouseover(function(){
		var prev = $('#menu table div.menuin:visible');
		$('.menuin.menuin-show').hide();
		if(prev != $('.menuin',this)){
			prev.hide();
		}
		$(".menuin",this).show();
	}).mouseleave(function(){
		$('.menuin.menuin-show').show();
	});
	$('#menu table').mouseleave(function(){
		$('.menuin:visible:not(.menuin-show)').hide();
	});
	
	$('.rightclick').click(function(){
		var el = $($(this).closest('div.vkl').find('ul.homevkl li.on').nextAll('li').find('a')[0]);
		if(el.length == 0){
			return false;
		}
		if(el.attr('href').indexOf('#') == '0'){
			el.trigger('click');
		} else {
			document.location.href = el.attr('href');
		}
		if($(this).closest('div.vkl').find('ul.homevkl li.on').prevAll('li').find('a').length > 0){
			$(this).parent().find('.leftclick img').attr('src',$(this).parent().find('.leftclick img').attr('src').replace('lrleft.','lrlefton.'));
		} else {
			$(this).parent().find('.leftclick img').attr('src',$(this).parent().find('.leftclick img').attr('src').replace('lrlefton.','lrleft.'));
		}
		if($(this).closest('div.vkl').find('ul.homevkl li.on').nextAll('li').find('a').length > 0){
			$(this).parent().find('.rightclick img').attr('src',$(this).parent().find('.rightclick img').attr('src').replace('lrright.','lrrighton.'));
		} else {
			$(this).parent().find('.rightclick img').attr('src',$(this).parent().find('.rightclick img').attr('src').replace('lrrighton.','lrright.'));
		}
	});
	$('.leftclick').click(function(){
		var el = $($(this).closest('div.vkl').find('ul.homevkl li.on').prevAll('li').find('a')[0]);
		if(el.length == 0){
			return false;
		}
		if(el.attr('href').indexOf('#') == '0'){
			el.trigger('click');
		} else {
			document.location.href = el.attr('href');
		}
		if($(this).closest('div.vkl').find('ul.homevkl li.on').prevAll('li').find('a').length > 0){
			$(this).parent().find('.leftclick img').attr('src',$(this).parent().find('.leftclick img').attr('src').replace('lrleft.','lrlefton.'));
		} else {
			$(this).parent().find('.leftclick img').attr('src',$(this).parent().find('.leftclick img').attr('src').replace('lrlefton.','lrleft.'));
		}
		if($(this).closest('div.vkl').find('ul.homevkl li.on').nextAll('li').find('a').length > 0){
			$(this).parent().find('.rightclick img').attr('src',$(this).parent().find('.rightclick img').attr('src').replace('lrright.','lrrighton.'));
		} else {
			$(this).parent().find('.rightclick img').attr('src',$(this).parent().find('.rightclick img').attr('src').replace('lrrighton.','lrright.'));
		}
	});
	if($('.leftclick').closest('div.vkl').find('ul.homevkl li.on').prevAll('li').find('a').length > 0){
		$('.leftclick img').attr('src',$('.leftclick img').attr('src').replace('lrleft.','lrlefton.'));
	}
	if($('.rightclick').closest('div.vkl').find('ul.homevkl li.on').nextAll('li').find('a').length > 0){
		$('.rightclick img').attr('src',$('.rightclick img').attr('src').replace('lrright.','lrrighton.'));
	}
});


var services = {
	application : function  ()
	{
		if (!$('div.okno').size())
		{
			$('body').append('<div class="okno" style="width:480px;"><div><input type="submit" onclick="$.modal.close();" class="butclose" value=""></div><p class="title">Заказать</p><div class="form plusservice"></div></div>');
		}

		var href = window.location.href.replace('http://','').split('/')
		services_url = 'http://'

		for (var key in href)
		{
			s = href[key]
			if (s != 'services')
				services_url += s+'/'
			else
			{
				 services_url += s+'/'
				break
			}
		}

		$.post(services_url + 'ajax.php',{type : 'get_application_form'},function(data){
			$('div.okno div.form').html(data)
			modal('div.okno');

			services.init_form()
		})
	},
	init_form : function (){
		$('div.okno form[name="USER_RENT_FORM"]').submit(function(){
			$('div.okno div.form').slideUp()

			var data = $(this).serialize()

			$.post(services_url + 'ajax.php',data + '&type=get_application_form&' + $("input:submit", this).attr('name') + '=' + $("input:submit", this).val(),function(data){

				if (data.length < 4)
				{
					data = 'Заявка успешно принята.';
				}

				$('div.okno div.form').html(data)
				$('div.okno div.form').slideDown()
				services.init_form()
			})

			return false;
		})
	}
}
