var Slider = {
	container : null,

	init : function(container) {
		Slider.container = $(container);
		Slider.container.find('a').click(function() {
			var id = parseInt($(this).attr('data-id'));
			$('.main-image').cycle(id);
			Slider.container.find('li').removeClass('active-image');
			$(this).parent('li').addClass('active-image');
			return false;
		});
	}
};
var classMenu;
$(document).ready(function() {
	$('body').ajaxStart(function() {
		url = parent.location.href;
		var re = /trim/gi;
		$('body').find('button.button').each(function() {
			$(this).addClass('lock');
			$(this).attr('disabled', true);
		});
		if (!url.match(re)) {
			$('<div />').addClass('load').appendTo('.wrapper');
		}

	});
	$('body').ajaxStop(function() {
		setTimeout(function() {
			$('body').find('button.button').each(function() {
				$(this).removeClass('lock');
				$(this).attr('disabled', false);
			});
			$('.load').remove();
		}, 100);

	});

	$('.next-project').click(function() {
		// return false;
	});

	// Initialize Slider
	$('.main-image').cycle({
		fx : 'fade',
		timeout : 0,
		startingSlide : 0
	});
	$('.list-thumbnails li:first').addClass('active-image');
	Slider.init('.list-thumbnails');

	$(".contacts").overlay({
		mask : {
			color : '#333',
			loadSpeed : 200,
			opacity : 0.55
		},
		fixed : false,
		closeOnClick : true,
		onLoad:function(){
			xajax._x_chageCaptcha();
		},
		onBeforeClose: function(){
			clear_form('contacts');
		},
		onClose:function(){
			$('#contacts').data("validator").reset();
			$('.success-message').hide();
			$('.fail-message').hide();			
		}
	});
	
	$(".estimate").overlay({
		mask : {
			color : '#333',
			loadSpeed : 200,
			opacity : 0.55
		},
		fixed : false,
		closeOnClick : true,
		onLoad:function(){
			xajax._x_chageCaptcha();
			
		},
		onBeforeClose: function(){
			clear_form('estimate');
		},
		onClose:function(){
			$('#estimate').data("validator").reset();
			$('.success-message').hide();
			$('.fail-message').hide();
		}
	});
	
	
	$('#contacts').validator({position:'bottom left'}).submit(function(e){
			
		if (!e.isDefaultPrevented()) {
			xajax._x_sendContacts(xajax.getFormValue('contacts'));
			return false;
		}
		return false;
	});

	
	$('#estimate').validator({position:'bottom left'}).submit(function(e){
		
		if (!e.isDefaultPrevented()) {
			xajax._x_sendEstimate(xajax.getFormValue('estimate'));
			return false;
		}
		return false;
	});

	
	$(document).ready(function() {
		$('#sub-menu-list').hover(function() {
			$('.service-menu').fadeIn(200);
		}, function() {
			$('.service-menu').hide();
		});

		$('.service-menu li').hover(function() {
			classMenu = $(this).attr('data-class');
			$('.service-menu').addClass(classMenu);
		}, function() {
			$('.service-menu').removeClass(classMenu);
		});
	});

});

function showNotice(message, url, close) {
	$('.notice-content').html(message);
	$('.notice-content').fadeIn(300);
	if (url != undefined && url != false) {
		setTimeout(function() {
			parent.location.href = url;
		}, 2500);
	} else {
		if (close == undefined) {
			setTimeout(function() {
				$('.notice-content').fadeOut(300);
			}, 2000);
		}
	}
}

function setLocation(url) {

	window.location = url;
}

function decorateTable() {
	// $('table tbody tr:even').addClass('even');
	$('table tbody tr:nth-child(odd)').addClass('even');
}

function close_dialog(ele) {
	$('body').find('.' + ele).each(function() {
		$(this).data('overlay').close();
		if ($(this).overlay() != undefined) {
			// $(this).overlay().close();
		}

	});
}

function clear_form(ele) {
	//$(':input', '#' + ele).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
	$('#'+ele +" :input").not(":button, :submit, :reset, :hidden").each( function() {
	    this.value = this.defaultValue;     
	});
}

function reloadCaptcha(image){
	$('.cpblock').html(image);
}

function showErrorCaptcha(error){
	$('#estimate .success-message').hide();
	$('#estimate .fail-message').hide();
	$('#contacts .success-message').hide();
	$('#contacts .fail-message').hide();
	$('#contacts').data("validator").invalidate(error);
}
function showErrorCaptchaEstimate(error){
	$('#estimate').data("validator").invalidate(error);
}

function sendSuccess(){
	$('.success-message-contact').fadeIn();
}

function sendFail(){
	$('.fail-message-contact').fadeIn();
}

function sendSuccessEstimate(){
	$('.success-message-estimate').fadeIn();
}

function sendFailEstimate(){
	$('.fail-message-estimate').fadeIn();
}

