(function($) {
	$(document).ready(function() {	
		
		
		// $('#catalogMenu h2').bind('click',function(){
			// var object = $(this).parent();
			// if($(object).hasClass('active'))
				// return false;
			// $('#catalogMenu .active .contentCatalogMenu').fadeOut();
			// $('#catalogMenu .active .preview').fadeOut();
			// $('#catalogMenu .active').animate({
				// width:'82px'
			// },{
				// quque:false,
				// duration:800,
				// complete: function(){
					// $(this).removeClass('active');
					// $(object).animate({
						// width:'330px'
					// },{
						// quque:false,
						// duration:800,
						// complete:function(){
							// $(object).addClass('active');
							// $('#catalogMenu .active .contentCatalogMenu').fadeIn();
							// $('#catalogMenu .active .preview').fadeIn();
						// }
					// })
				// }
			// })
		// })		
		
		$('#catalogMenu h2').bind('click',function(){
		if($.browser.msie != true){
		// if(false){
			var object = $(this).parent();
			if($(object).hasClass('active'))
				return false;
			$('#catalogMenu .active .contentCatalogMenu').fadeOut();
			$('#catalogMenu .active .preview').fadeOut();
			$('#catalogMenu .active').animate({
				width:'82px'
			},{
				quque:false,
				duration:800,
				complete: function(){
					$(this).removeClass('active');
					$(object).animate({
						width:'330px'
					},{
						quque:false,
						duration:800,
						complete:function(){
							$(object).addClass('active');
							$('#catalogMenu .active .contentCatalogMenu').fadeIn();
							$('#catalogMenu .active .preview').fadeIn();
						}
					})
				}
			})
		}
		else{
			var object = $(this).parent();
			if($(object).hasClass('active'))
				return false;
			$('#catalogMenu .active .contentCatalogMenu').hide();
			$('#catalogMenu .active .preview').hide();			
			$('#catalogMenu .active').css({width:'82px'});
			$('#catalogMenu .active').removeClass('active');
			$(object).css({width:'330px'});
			$(object).addClass('active');
			$('#catalogMenu .active .contentCatalogMenu').show();
			$('#catalogMenu .active .preview').show();	
		}			
		});
		
		$('table tr[valign="bottom"] td').css('vertical-align','bottom');
		
		if($('#leftMenu li').length == 0){
			$('#leftMenu').addClass('hidden');
		}
		
		$('#catalogMenuDefault li').bind('click',function(){
			var location = $(this).find('a').attr('href');
			window.location = location;
		})
		
		// $('#smallMenu>ul>li>a').bind('mouseover',function(){	
			// $('#smallMenu ul ul').fadeOut();
			// var offset = $(this).parent().offset();
			// var offsetLeft = (offset.left - 324) - (($(window).width() - 1280)/2);
			// var ul = $(this).parent().find('ul');
			// $(ul).css('left',offsetLeft+'px');
			// $(ul).fadeIn();
		// });
		
		// $('#smallMenu ul ul').bind('mouseleave',function(){
			// $(this).fadeOut();
		// })
		
			$('#smallMenu>ul>li>a').bind('mouseover',function(){	
			$('#smallMenu ul ul').hide();
			var offset = $(this).parent().offset();
			//var offsetLeft = (parseInt(offset.left,10)-324) - (($(window).width() - 1280)/2);
			var offsetLeft = (offset.left - 324) - (($(window).width() - 1280)/2);
			//alert(offsetLeft);
			//var offsetLeft = parseInt(offset.left,10)-533;
			var ul = $(this).parent().find('ul');
			$(ul).css('left',offsetLeft+'px');
			$(ul).show();
		});
		
		 $('#smallMenu ul ul').bind('mouseleave',function(){
			$(this).hide();
		});
				
		colorsArray = new Array('about','stone','kamins','chimneys','bbq','ourWorks');
		var i = 0;
		$('#topMenu li a').each(function() {
			$(this).addClass(colorsArray[i]);
			i++;
		})
		
		$('#galleryBlockLink').bind('mouseover',function() {
			$('#galleryLinks').show();
		})
		
		$('#galleryBlockLink').bind('mouseout',function() {
			$('#galleryLinks').hide();
		})
		
		var contentClass = $('#topMenu .active a').attr('class');
		$('body').addClass(contentClass);
		
		$('#sendRecallsButton').bind('click',function() {
			sendForm('recallsForm');
		});
		
		$('#sendQuestionButton').bind('click',function() {
			sendForm('sendQuestion');
		});
		
		$('#text tr:nth-child(2n)').addClass('matroskaTR');
		
				
		$(document).bind('keypress',function(e) {
			var code = (e.charCode) ? e.charCode : e.keyCode;
			if($('#galleryOpen').css('display') == 'block') {
				var src = $('#mainGalleryImg').attr('src');
				src = src.split("/");
				var index = src[src.length-1].split('.')[0];
				if(code == 37) {
					getPhoto(index,'back');
				} else if(code == 39) {
					getPhoto(index,'next');
				} /*else if(code == 13) {
					slideInterval = setInterval(function() {
						var src = $('#mainGalleryImg').attr('src');
						src = src.split("/");
						var index = src[src.length-1].split('.')[0];
						getPhoto(index,'next');
					},3000);
				} else if(code == 27) {
					clearInterval(slideInterval);
				}*/
			}
			if($('#shadow').css('display') == 'block') {
				if(code == 27) {
					$('.popupWindow').fadeOut();
					$('#shadow').hide();
					if(slideInterval !== undefined) {
						clearInterval(slideInterval);
					}
				}
			}			
		
		})
		
		
		$('.scrollLeftArrow').bind('click',function() {
			if(typeof(counterClicks) == 'undefined') 
				counterClicks = 0;
			var object = $(this).parent().find('ul');
			var liW = $(this).parent().find('li:first').width()+8;
			var liL = $(this).parent().find('li').length;
			var widthScroll = liW * liL;
			if(counterClicks !== 0) {
				counterClicks--;
				$(object).animate({
					'marginLeft':'-'+(liW*3*counterClicks)+'px'
				});
			} else {
				var countsClick = Math.ceil(liL/3);
				if(countsClick !== 0)
					countsClick--;
				counterClicks = countsClick-1;
				$(object).animate({
					'marginLeft':'-'+(liW*3*countsClick)+'px'
				});
			}
		})
		
		$('.scrollRightArrow').bind('click',function() {
			if(typeof(counterClicks) == 'undefined') 
				counterClicks = 0;
			var object = $(this).parent().find('ul');
			var liW = $(this).parent().find('li:first').width()+8;
			var liL = $(this).parent().find('li').length;
			var widthScroll = liW * liL;
			var countsClick = Math.ceil(liL/3);
			if(countsClick !== 0)
				countsClick--;
			if(counterClicks !== countsClick) {
				counterClicks++;
				$(object).animate({
					'marginLeft':'-'+(liW*3*counterClicks)+'px'
				});
			} else {
				counterClicks = 0; 
				$(object).animate({
					'marginLeft':'0px'
				});
			}
		})
		
		$('#gallery a').bind('click',function() {
			var height = $(document).height();
			$('#shadow').css('height',(height+20)+'px');
			
			var src = $(this).next().find('img').attr('src');
			if(src == undefined) 
				src = $(this).parent().find('img:first').attr('src');
			src = src.split("/");
			index = src[src.length-1].split('.')[0];
			$('#shadow').show();

			var numberImg = parseInt($('#gallery a').index(this))+2;
			// var offsetTop = parseInt(window.pageYOffset);
			$('#galleryOpen #number').html(numberImg);
			$('#galleryOpen').fadeIn();
			getPhoto(index,'back');
		});
		
		$('a[rel="scrollGallery"]').fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	false
		});
		
		$('#mainGalleryImg').live('click',function() {
			var src = $(this).attr('src');
			src = src.split("/");
			var index = src[src.length-1].split('.')[0];
			getPhoto(index,'next');
		})
		
		$('#mainGalleryImg').live('mouseover',function() {
			var offset = $('#mainImgBlock').offset();
			$('#next').css('right','-80px').show();
			$('#prev').css('left','-80px').show();
		})
		
		$('#next').live('click',function() {
			var src = $('#mainGalleryImg').attr('src');
			src = src.split("/");
			var index = src[src.length-1].split('.')[0];
			getPhoto(index,'next');
		})
		
		$('#prev').live('click',function() {
			var src = $('#mainGalleryImg').attr('src');
			src = src.split("/");
			var index = src[src.length-1].split('.')[0];
			getPhoto(index,'back');
		})
		
		$('#arrowNext').bind('click',function() {
			var src = $('#mainGalleryImg').attr('src');
			src = src.split("/");
			var index = src[src.length-1].split('.')[0];
			getPhoto(index,'next');
		})
		
		$('#arrowBack').bind('click',function() {
			var src = $('#mainGalleryImg').attr('src');
			src = src.split("/");
			var index = src[src.length-1].split('.')[0];
			getPhoto(index,'back');
		})
		
		$('#shadow').live('click',function() {
			$('.popupWindow').fadeOut();
			$(this).hide();
		})
		
		$('.activeTitlePhoto').live('click',function() {
			if($('#galleryOpen #titlePhoto').hasClass('activeOpenTitlePhoto')) {
				$(this).removeClass('activeOpenTitlePhoto');
				$('#description').fadeOut();
			} else {
				var offset = $('#galleryOpen #mainGalleryImg').offset();
				var offset2 = $('#galleryOpen').offset();
				var left = parseInt(offset.left-offset2.left-15);
				var width = parseInt($('#mainGalleryImg').width())-15;
				$('#galleryOpen #description').css({
					'left':left+'px',
					'width':width+'px'
				}).fadeIn();
				$(this).addClass('activeOpenTitlePhoto');
			}
		})
		
	
	/*	var height = $(document).height();
		$('#shadow').css('height',height+'px');
		
		count_visible = 5;
		
		$('.listOffers .itemOffer:first').addClass('first');
		$('.listOffers .itemOffer:last').addClass('last');
		$('.listOffers .itemOffer').addClass('hidden_item');
		$('.listOffers .itemOffer').slice(0,count_visible).removeClass('hidden_item').addClass('visible_item');
		$('.prevOffer').click(prevOffer);
		$('.nextOffer').click(nextOffer);
		
		function prevOffer(){			
			if(!$('.listOffers .itemOffer.visible_item:first').is('.first'))
				$('.listOffers .itemOffer.visible_item').removeClass('visible_item').addClass('hidden_item').prev().removeClass('hidden_item').addClass('visible_item');
		}
		
		function nextOffer(){
			if(!$('.listOffers .itemOffer.visible_item:last').is('.last'))
			$('.listOffers .itemOffer.visible_item').removeClass('visible_item').addClass('hidden_item').next().removeClass('hidden_item').addClass('visible_item');
		}
	})*/
	
	
	
	var count_visible = 5;		
	var curent_item = 0;
	var counter = 0;
	$('.listOffers').each(function (){
		counter = counter + 1;
		$(this).attr('rel',counter);
		$('div[rel='+$(this).attr('rel')+'].listOffers .itemOffer:first').addClass('first');
		$('div[rel='+$(this).attr('rel')+'].listOffers .itemOffer:last').addClass('last');
		$('div[rel='+$(this).attr('rel')+'].listOffers .itemOffer').addClass('hidden_item');
		$('div[rel='+$(this).attr('rel')+'].listOffers .itemOffer').slice(0,count_visible).addClass('visible_item');
	})	
	$('.listOffers .prevOffer').click(prevOffer);
	$('.listOffers .nextOffer').click(nextOffer);



function prevOffer(){		
	if(!$('div[rel='+$(this).parent().attr('rel')+'].listOffers .itemOffer.visible_item:first').hasClass('first'))
		$('div[rel='+$(this).parent().attr('rel')+'].listOffers .itemOffer.visible_item').removeClass('visible_item').addClass('hidden_item').prev().removeClass('hidden_item').addClass('visible_item');
}

function nextOffer(){
	if(!$('div[rel='+$(this).parent().attr('rel')+'].listOffers .itemOffer.visible_item:last').hasClass('last'))
		$('div[rel='+$(this).parent().attr('rel')+'].listOffers .itemOffer.visible_item').removeClass('visible_item').addClass('hidden_item').next().removeClass('hidden_item').addClass('visible_item');;	
}
})	
	
	addSwf = function(name,width,height) {
		document.write('<object type="application/x-shockwave-flash" data="'+name+'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" align="center">'+
'<param name="src" value="'+name+'" />'+
'<param name="allowscriptaccess" value="always" />'+
'<param name="quality" value="high" />'+
'<param name="wmode" value="transparent" />'+
'<embed src="'+name+'" wmode="transparent" menu="false" quality="high" width="'+width+'" height="'+height+'" align="center" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
'</object>');
	}
	
	sendForm = function(name) {
		var error = 0;
		var regMail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var regPhone = /^([0-9]{6,12})$/;
		var action = $('#'+name).attr('action');
		var type = $('#'+name).attr('method');
		var offset = $('#'+name).offset();
		var top = offset.top - 50;
 		$('#'+name+' .required').each(function() {
			if($(this).val() == '' || $(this).val() == 'Имя' ||  $(this).val() == 'Ваш вопрос' || $(this).val() == 'Код' || $(this).val() == 'Контактный телефон') {
				$(this).addClass('notValid');
				error++;
			} else if($(this).attr('name') == 'email' && !regMail.test($(this).val())) {
				$(this).addClass('notValid');
				error++;
			} else if($(this).attr('name') == 'phone' && !regPhone.test($(this).val())) {
				$(this).addClass('notValid');
				error++;
			}
			else 
				$(this).removeClass('notValid');
		});
		
		if(error !== 0) {
			//$('#'+name+' .info').remove();
			$('#'+name+' .info').addClass('error').html('Проверьте введеные данные');
		} else {
			//$('body').prepend('<div id="shadow"></div><div id="messageBox"></div>');	
			//$('#shadow').css('height',height+'px');
			var data = $('#'+name).serialize();
			$.ajax({
				url: action,
				type: type,
				data: data,
				success: function(response) {
					//$('#shadow').show();
					//$('#messageBox').html(response).css('top',top+'px').fadeIn();
					$('#'+name+' .info').html(response);
				}
			})
		}
	}
	
	closeWindow = function() {
		$('.popupWindow').fadeOut();
		$('#shadow').hide();
	}
	
	getPhoto = function(index,direction) {
		var pid = $('#galleryPid').text();
		var number = parseInt($('#number').text());
		var shadowHeight = $('#shadow').height();
		$('#next').hide();
		$('#prev').hide();
		$('#mainImgBlock').append('<div id="loaderDiv">&nbsp;</div>');
		$.getJSON('/application/getPhoto/'+index+'/'+pid+'/'+direction+'/',function(response) {
			$('#galleryOpen #titlePhoto').html(response.title).removeClass('activeOpenTitlePhoto');
			if(direction == 'next' && number != response.count)  
				number++;
			else if(direction == 'next' && number == response.count)
				number = 1;
			else if(direction == 'back' && (number-1) != 0)
				number--;
			else if(direction == 'back' && (number-1) == 0) 
				number = response.count;
			$('#galleryOpen #imageNumber').html('<span id="number">'+number+'</span> из '+response.count);
			$('#galleryOpen #description').hide();
			if(response.description != '') {
				$('#galleryOpen #description').html(response.description).show();
				$('#galleryOpen #galleryMainImg').attr('alt',response.title);
				$('#galleryOpen #titlePhoto').addClass('activeTitlePhoto').addClass('activeOpenTitlePhoto');
			} else {
				$('#galleryOpen #titlePhoto').removeClass('activeTitlePhoto');
				$('#galleryOpen #galleryMainImg').attr('alt',response.title);
			}
			image = new Image();
			image.src = response.src;
			if($(image).load(function() {
				$('#galleryOpen #mainGalleryImg').attr('src',response.src);
				$('#loaderDiv').remove();
				setTimeout(function() {
					var width = $('#mainGalleryImg').width();
					var height = $('#mainGalleryImg').height()+35;
					var left = parseInt(width/2);
					var delta = parseInt((window.innerHeight-height)/2);
					var offsetTop = parseInt(window.pageYOffset)+delta;
					$('#galleryOpen').
					css('top',offsetTop+'px').
					animate({
						'width':width+'px',
						'height':height+'px',
						'marginLeft':'-'+left+'px'
					}).fadeIn();
				},200)
			})) {
				$(image).attr('src',response.src);
			}
		});
	}
	
	getPhotoGallery = function(pid,module) {
		$.ajax({
			url:'/application/getPhotoGallery/'+pid+'/'+module+'/',
			type: 'GET',
			beforeSend: function() {
				//$('#shadow').show();
			},
			success: function(response) {
				var offsetTop = parseInt(window.pageYOffset)+50;
				$('#galleryOpen').html(response);
				$('#galleryOpen').css({
					'top':offsetTop+'px'
				}).show();
				if($('#galleryOpen #description').text() !='') {
						$('#galleryOpen #titlePhoto').addClass('activeTitlePhoto');
				}
			}
		})
	}
		
})(jQuery);
