function correctPaddings(){
	var scrW = screen.width;
	switch (scrW){
		case 1024:
			$('c_wraper').setStyle('margin','0 22px 0 22px');			
			$('content_wrapper').setStyle('margin','0 22px 0 22px');			
			if ($('p_wraper')){
				$('p_wraper').setStyle('margin','0 7px 0 7px');	
			}
			if ($('main_menu_container')){
				$('main_menu_container').setStyle('width','480px');	
			}
			break;
		case 1280:
			$('c_wraper').setStyle('margin','0 58px 0 58px');			
			$('content_wrapper').setStyle('margin','0 58px 0 58px');			
			if ($('p_wraper')){
				$('p_wraper').setStyle('margin','0 43px 0 43px');		
			}
			break;
		case 1680:
			$('c_wraper').setStyle('margin','0 70px 0 70px');			
			$('content_wrapper').setStyle('margin','0 70px 0 70px');			
			if ($('p_wraper')){
				$('p_wraper').setStyle('margin','0 55px 0 55px');			
			}
			break;
	}
	
	if (scrW > 1680){
		$('c_wraper').setStyle('margin','0 70px 0 70px');			
		$('content_wrapper').setStyle('margin','0 70px 0 70px');	
		
		if ($('p_wraper')){
			$('p_wraper').setStyle('margin','0 55px 0 55px');			
		}
		
	}
}


window.addEvent('domready', function() {
	correctPaddings();
	imagesRollerMainMenu();
	imagesRoller();
	new Asset.images(['/images/loading_image.gif', '/images/ajax-loader2.gif', '/images/left_nav.png', '/images/right_nav_unactive.png', '/images/portfolio_loader.gif']);
	
	if ($('scroll_line')){
		scroller = new iScroller;
	}
	
	if ($('reviews_left_block')){
		initReviews();
	}
	
	pageScroller();
	
	InitBackLink();	
	
	InitTips();

	$(document.body).getElements('.frame').each(function(frame){		
		frame.addEvents({
			'mouseenter': function(){	
				this.removeClass('frame');
				this.addClass('frame_hover');
			},
			'mouseleave': function(){	
				this.removeClass('frame_hover');
				this.addClass('frame');
			}
		});
	});
	
	var thumbs = $(document.body).getElements('a.thumb_image');
	
	thumbs.set('tween', {duration: 100});
	
	thumbs.each(function(thumb){		
		thumb.addEvents({
			'mouseenter': function(){	
				this.tween('opacity', 0.5);
			},
			'mouseleave': function(){	
				this.tween('opacity', 1);
			}
		});
	});		
});

function InitBackLink(){
	if ($('back_roll')){
		var foo = $('color_text').getPosition();	
		
		var pagePos = window.getScroll().y;
		var pageSize = window.getSize().y;
		
		var offset = $('main_menu_container').getPosition().y;
		
		var newPos = (pagePos - offset) + (pageSize - 38) - 20;
		
		if (newPos < (foo.y - offset)){				
			$('back_roll').setStyle('top', newPos);	
		}
		else{
			$('back_roll').setStyle('top', foo.y - offset);
		}
		
		$('back_roll').set('morph', {duration: 500, transition: 'back:out'})	

		window.addEvent('scroll', function(){
			var pagePos = window.getScroll().y;			
			
			var newPos = (pagePos - offset) + (pageSize - 38) - 20;
			
			if (newPos < (foo.y - offset)){				
				var eff = $('back_roll').get('morph');
				
				if (eff){
					eff.cancel();
				}				
				
				$('back_roll').morph({'top': newPos});
			}
			else{
				$('back_roll').morph({'top': foo.y - offset});
			}
		});
	}
}

function InitTips(){
	$(document.body).getElements('.tips').each(function(tip){		
		tip.addEvents({
			'mouseenter': function(){	
				var pos = this.getPosition();
				$('tip').setStyle('display', 'inline-block')
				var center = $('tip').getElement('span.center_tip');
				center.set('text', this.title);
				var size = $('tip').getSize();				
				$('tip').setStyle('left', pos.x - size.x + 38);
				$('tip').setStyle('top', pos.y - 50);
			},
			'mouseleave': function(){	
				$('tip').setStyle('display', 'none')
			}
		});
	});
}

function imagesRollerMainMenu(){
	var links = $(document.body).getElements('.roller');	
	links.each(function(link){			
		link.addEvents({		
			'mouseenter': function(){					
				this.setStyle('background-position','0% -65px');
				this.getChildren('span').setStyle('color', '#4d5050');	
			},
			'mouseleave': function(){		
				if (this.hasClass('active')){
					this.setStyle('background-position','0% -195px');				
				}
				else{
					this.setStyle('background-position','0% 0%');
				}
				this.getChildren('span').setStyle('color', '#4d5050');				
			},
			'mousedown': function(){					
				this.setStyle('background-position','0% -130px');
				
				if (this.href.test('portfolio')){
					action = 'portfolio';			
				}
				else
				{				
					var action = this.href.match(/[\/|\\]([^\\\/]+)$/);	
					action = action[1];	
				}
				switch (action){
					case "about_us.html":						
						var color = '#29789d';
						break;
					case "services.html":
						var color = '#a42929';
						break;
					case "portfolio":
						var color = '#966d16';
						break;
					case "reviews.html":
						var color = '#907f44';
						break;
					case "contacts.html":
						var color = '#3f8338';
						break;
				}				
				this.getChildren('span').setStyle('color', color);				
			},
			'mouseup': function(){
				this.setStyle('background-position','0% -65px');	
				this.getChildren('span').setStyle('color', '#4d5050');				
			}
		});
	});
}

function imagesRoller(){
	var links = $(document.body).getElements('.roller2');	
	links.each(function(link){			
		var height = link.name.toInt();				
		
		link.addEvents({		
			'mouseenter': function(){				
				this.setStyle('background-position','0% -' + height + 'px');	
			
				if (this.hasClass('slide')){
					this.slider = this.getElement('span');
					var tmp = this.slider.id.split('_');
					var SliderWidth = tmp[1];						

					this.SlideOutEffect = new Fx.Morph(this.slider, {duration: 400, transition: 'back:out'});
					this.SlideInEffect = new Fx.Morph(this.slider, {duration: 100, transition: 'sine:out'});
					this.SlideClickEffect = new Fx.Morph(this.slider, {duration: 300, transition: 'back:in'});					

					this.SlideOutEffect.start({'width' : SliderWidth});							
				}
				
				if (this.hasClass('am')){
					if ($('i'+this.id).hasClass('visited')){
						$('i'+this.id).addClass('iam_visited');
					}
					else{
						$('i'+this.id).addClass('iam_active');
					}
					$('i'+this.id).removeProperty('style');
				}
				
			},
			'mouseleave': function(){			
				this.setStyle('background-position','0% 0%');		
				
				if (this.hasClass('slide')){
					this.SlideOutEffect.cancel();
					this.SlideClickEffect.cancel();
					this.SlideInEffect.start({'width' : 0});
					
					if (this.slider.hasClass('active')){									
						this.setStyle('background-position','0% -' + (height * 3) + 'px');
					}	
				};

				if (this.hasClass('am')){
						$('i'+this.id).removeClass('iam_active');
						if ($('i'+this.id).hasClass('iam_visited')){
							$('i'+this.id).removeClass('iam_visited');
							$('i'+this.id).setStyle('background-position','0% -92px');
						}
				}
			},
			'mousedown': function(){					
				this.setStyle('background-position','0% -' + (height * 2) + 'px');	
				
				if (this.hasClass('slide')){
					this.SlideOutEffect.cancel();
					this.SlideClickEffect.start({'width' : 0});
					
					$$('.slideline').removeClass('active');			
					$$('.slide').setStyle('background-position','0% 0%');		
					
					this.slider.addClass('active');
					
					getPortfolioAJAX(this.id, this);						
				}	

				if (this.hasClass('service_link')){
					this.setStyle('color', '#fff');
				}				
			},
			'mouseup': function(){
				if (!this.hasClass('service_link')){
					this.setStyle('background-position','0% -' + height + 'px');
				}						
			}
		});
	});
}

function getPortfolioAJAX(section, btn){

	$('scroll_line').set('html','');
	scroller.setButtonsToUnactive();	
	var portfolio_loader = new Element('div',{'styles': {'width':'32px', 'margin':'0 auto'}});
	new Element('img', {'src':'/images/portfolio_loader.gif', 'styles': {'margin-top': '80px'}}).inject(portfolio_loader);
	portfolio_loader.inject($('scroll_container'));
	
	new Request.JSON({
		url: "/ajax/portfolio/" + section, 
		link: "cancel",		
		onComplete: function(res){
			portfolio_loader.dispose();

			if (screen.width==1280){
				var margin = 94;
				$('scroll_line').setStyle('width', (res.items.length*254)+((res.items.length)*94));
			}
			else{
				$('scroll_line').setStyle('width', (res.items.length*254)+((res.items.length)*20));			
			}			
			
			res.items.each(function(item,i){
				var block = new Element('div',{'class':'img_block'});
				
				if (i!=res.items.length){
					if (margin){
						block.setStyle('margin-right', margin +'px');
					}
				}
				else{
					block.addClass('no_margin_right');
				}				
				
				var wr = new Element('div', {'class':'main'}).inject(block);				
				var sh = new Element('div', {'class':'shadow'}).inject(block);
				
				new Element('div').set('text',item.title).inject(wr);
				var img_block = new Element('a',{'href':item.link});
			
				var loader = new Element('img', {'src':'/images/loading_image.gif', 'styles':{'margin-top':'57px'}}).inject(img_block);
				
				new Asset.image('/images/portfolio/' + item.image, {onload: function(){					
					loader.dispose();
					new Element('img', {'src': "/images/portfolio/" + item.image}).inject(img_block);					
				}});

				
				img_block.inject(wr);				
				block.inject($('scroll_line'));
			});
			
			scroller.stopScroll = false;
			scroller.counter = 0;
			scroller.count = res.items.length;
			
			scroller.limit = Math.round(scroller.count / (scroller.options.step));
			
			if ($('scroll_line').getStyle('left').toInt()!=0){				
				scroller.effect.start({'left':0});
			}
			
			if (scroller.count <= 3){
				scroller.setButtonsToUnactive();
			}
			else{			
				scroller.leftUnactive = true;
				if (scroller.rightUnactive){
					$('right_nav').setStyle('background', 'url(/images/right_nav.png) no-repeat left top');
				}
				$('left_nav').setStyle('background', 'url(/images/left_nav_unactive.png) no-repeat left top');
			}
			
			scroller.addInteractive();
		}
	}).send();
}

function initReviews(){
	var reviews = $('reviews_left_block').getElements('a.reviews_img_block');
	
		if (Browser.Engine.presto){
			var x=-3;		
		}
		else{
			var x=0;
		}
	
	var moveArrowTo = function(y, h){
		var blockY = $('reviews_arrow_area').getPosition().y;			
		var offset = y-blockY + (h/2) - 5;
		
		var mover = new Fx.Move($('reviews_arrow'), {
			relativeTo: $('reviews_arrow_area'),
			duration: 200, 
			transition: 'back:out'
		});
		
		mover.start({
			offset: {x: x, y: offset}
		});
	}
	
	$('reviews_arrow').position({
		'relativeTo': $('reviews_arrow_area'),
		'offset': {x: x, y: 35}
	});

	reviews.each(function(review){		
		review.addEvents({
			'click': function(){
				var pos = review.getPosition();				
				var size = review.getStyle('height');					
				moveArrowTo(pos.y, size.toInt());
				
				$('reviews_left_block').getElements('a.reviews_img_block').removeClass('active');
				
				$('review_text2').getElements('span.sele').setStyle('display', 'none');
				
				this.addClass('active');
				
				$('rreview_' + this.name).setStyle('display', 'block');
				
				return false;
			}
		});
	});
}


function pageScroller(){
	if ($('main_menu_container')){
		var elms = $('main_menu_container').getElements('a.ii');
	
		var colors = ['#E64100', '#FBBD0A', '#00A547', '#006CB1'];
		
		elms.each(function(elm){
			elm.addEvent('click', function(){
				var target = elm.id + '_block';
				new Fx.Scroll(window, { 
				onComplete: function(){
					var rand_no = Math.random();
					rand_no = Math.ceil(rand_no * 4);				
					$(target).highlight(colors[rand_no]);
				}}).toElement(target);
				this.addClass('visited');
				this.addEvent('mouseleave', function(){
					this.setStyle('background-position','0% -92px');			
				});
			});
		});
	}
}

function changeBigImage(path, id, index){
	var img = $('big_image');	
	var block = $('thumb_block_' + id);	
	var container = $('big_image_container');
	
	$('img_loader_' + id).setStyle('display', 'block');

	container.set('morph', {duration: 100, transition: 'sine:out'});
	
	img.set('morph', {duration: 200, transition: 'sine:out'})	
	img.morph({'opacity': 0});
	
	$$('.thumb_block').removeClass('thumb_block_active');	
	block.addClass('thumb_block_active');
	
	img.get('morph').addEvent('complete', function(){
		new Asset.image('/images/portfolio/mid_' + path, {
			onload: function(){
				$('img_loader_' + id).setStyle('display', 'none');
				var aParent = img.getParent('a');
				aParent.href = '/images/portfolio/' + path;
				aParent.removeProperty('onclick');
				
				var text = $('slim_images').get('text');
				
				var img_arr = text.split('|');
				
				var garray = new Array();
				
				img_arr.each(function(a, i){
					var img_array = new Array();
					
					img_array[0] = a;
					
					garray[i] = img_array;
				});

				
				aParent.addEvent('click', function(){		
					Slimbox.open(garray, index);
					return false;
				});
				
				container.morph({'height': this.height});
				img.src = '/images/portfolio/mid_' + path;	
				img.morph({'opacity': 1});
			}
		});
	})
}

function ajaxRequest(elm, url, query_str){
	
	var result_elm = $(elm);
	
	new Request({
		url: url, 
		onComplete: function(res){
			result_elm.set('html', res);		
		},
		onFailure: function(){
			document.location=url;
		}
	}).send(query_str);
}

function GetReview(link){

	var elmPos = $(link).getPosition(); 	
	var reviewBlockPos = $('reviews_menu').getPosition(); 
	var block = $('review_block');	
	
	var allLinks = $('reviews_menu').getElements('a')
	
	allLinks.removeClass('active_rev');
	
	link.addClass('active_rev');
	
	var NewTop = elmPos.y - reviewBlockPos.y - 48;
	
	block.set('morph', {duration: 500, transition: 'back:out'});	
	block.morph({'top': NewTop});
	
	$('review_title').set('html', link.title);

	$('review_text').set('html', '<div style="text-align: center;">ЗАГРУЗКА...</div>');	
	$('review_text').setStyle('height', 'auto');
	
	var sHeight = $('review_text').getSize().y;		
	
	new Request.JSON({
		url: link.href, 
		onComplete: function(res){				
			$('review_text').set('html', res.text);
			auto_anonymize();
			var tHeight = $('review_text').getSize().y;		
			
			$('review_text').setStyle('height', sHeight);
			
			$('review_text').set('morph', {duration: 300, transition: 'sine:out'});
			$('review_text').morph({'height': tHeight});	
			
		},
		onFailure: function(){
			document.location=link.href;
		}
	}).send();

	return false;
}

function SetActiveReview(id){
	var block = $('review_block');		

	if (id != 0){
		var reviewBlockPos = $('reviews_menu').getPosition(); 		
		var rev = $('rev_' + id);		
		var elmPos = rev.getPosition(); 		
		var NewTop = elmPos.y - reviewBlockPos.y - 48;			
		block.setStyle('top', NewTop);		
	}
	else{
		block.setStyle('top', 0);		
	}
}


