$(document).ready(function(){
	var ind = location.href.indexOf('#');
	if (ind != -1 && parseInt(location.href.substring(ind+1))>1){
		var l = parseInt(location.href.substring(ind+1));
	}else{
		var l = 1;
	}

	var p1text = $($('dl.portfolio:eq(0) dd>div').get(0)).html();
	var p2text = $($('dl.portfolio:eq(0) dd>div').get(1)).html();
	$('dl.portfolio:not(.main) dd>div').html(p1text);
	$('dl.portfolio:not(.main) dd>div:odd').html(p2text);

	if (parseInt(l)>0){
		$('ul.local-menu li').removeClass('active');
		$('.block').css({display:'none'});

		menuj = $('ul.local-menu li:eq('+(l-1)+')');
		if (menuj.get(0)){
			menuj.addClass('active')
		}else{
			$('ul.local-menu li:eq(0)').addClass('active');
		}

		blockj = $('.block:eq('+(l-1)+')');
		if (blockj.get(0)){
			blockj.css({display:'block'});
		}else{
			$('.block:eq(0)').css({display:'block'});
		}
	}

	$('ul.local-menu a:not(#process)').click(function(){
		var index = $('ul.local-menu a').index(this);
		$('ul.local-menu li').removeClass('active');
		$(this).parent().addClass('active');
		$('.block').css({display:'none'});
		$('.block:eq('+index+')').css({display:'block'});
	});
	
	$('dl.portfolio:not(.main) dt img').click(function(){
		var i = $('dl.portfolio').index($(this).parent().parent().get(0));
		i++;
		if (i>=$('dl.portfolio').length){
			i=0;	
		}
		$('ul.local-menu a:eq('+i+')').click();
		document.location = $('ul.local-menu a:eq('+i+')').get(0).href;
	});

	/*
	 *	<- ctrl ->
	 */

	if ($('a.ctrl-next').length || $('a.ctrl-previous').length){
		$(document).keypress(function(e){
			if (e.ctrlKey && e.which==39 && $('a.ctrl-next').length){
				document.location = $('a.ctrl-next').get(0).href;
				return false;
			}
			if (e.ctrlKey && e.which==37 && $('a.ctrl-previous').length){
				document.location = $('a.ctrl-previous').get(0).href;
				return false;
			}
		});
		$(document).keydown(function(e){
			if (e.ctrlKey && e.which==39 && $('a.ctrl-next').length){
				document.location = $('a.ctrl-next').get(0).href;
				return false;
			}
			if (e.ctrlKey && e.which==37 && $('a.ctrl-previous').length){
				document.location = $('a.ctrl-previous').get(0).href;
				return false;
			}
		});
	}

	$('.fback .bg').css({'opacity': 0.9});
	$('.fback .bg').click(function(){$('.fback').hide();});

	$('#fback-show').click(function(){
		$('.fback').show();
		$('.fback form, .fback .thank').css({
			'display': 'block',
			'top': $(window).scrollTop()+$(window).height()/2
		});
		return false;
	});
	if (ind != -1 && location.href.substring(ind+1)=='message'){
		$('.fback').show();
		$('.fback form, .fback .thank').css({
			'display': 'block',
			'top': $(window).scrollTop()+$(window).height()/2
		});
	}

	$('#gor').hide();
	$('#mob a').click(function(){
		$('#mob').hide();
		$('#gor').show();
		return false;
	});
	$('#gor a').click(function(){
		$('#mob').show();
		$('#gor').hide();
		return false;
	});
	$('div.form-toggle').hide();
	$('h3.form-toggle').click(function(){
		$('div.form-toggle').toggle("slow",function(){
			$('.footer .nl').get(0).innerHTML+='';
		});
		return false;
	});
    if ($('div.files input.button').length){
    	var linp = 1;
		$('div.files input.button:eq(0)').click(function(){
			linp++;
			$('<p><input type="file" name="files[]" /> <input type="button" class="button" value="-" /></p>').appendTo('div.files');
			if (linp>=5){
				$(this).hide();
			}
			$('div.files input.button:gt(0)').click(function(){
				$(this).parent().remove();
				linp--;
				if (linp<5){
					$('div.files input.button:eq(0)').show();
				}
			});
		});
	}

	$('.header p span.ph2 a').click(function(){
		$('.header p span.ph2').hide();
		$('.header p span.ph1').show();
		return false;
	});

	$('.header p span.ph1 a').click(function(){
		$('.header p span.ph1').hide();
		$('.header p span.ph2').show();
		return false;
	});

	$('ul.col4 a.zoom').click(function(){
		var iurl = this.href;
		var zc = $('<div class="zoom-cnt"><div class="bg"></div><p></p></div>');
		zc.find('p').addClass('loading');
		zc.appendTo(document.body).css('height',$(document.body).height());
		zc.find('.bg').click(function(){
			$(this).parent().remove();
		});
		var zi = $('<img src="'+iurl+'" alt="" />').load(function(){
			zi.css({'margin-top':-9000,'margin-left':-9000}).appendTo(document.body);
			var zsw = this.width;
			var zsh = this.height;
			zc.find('p').animate({
				width: zsw,
				height: zsh,
				marginLeft: -(zsw+30)/2,
				marginTop: -(zsh+30)/2
			},500,function(){
				zc.find('p').removeClass('loading');
				zi.css({'margin-top':0,'margin-left':0}).appendTo(zc.find('p'));
			});
		});
		return false;
	});

	$('div.footer a.sl').click(function(){
		$('div.footer div.sl').toggle();
		return false;
	});
	
	if ($.browser.msie){
		$('form .brief p').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
	}

	if ($('#b-service').length){
		var prc = $('<cite></cite>').insertAfter('#b-service');

		$('#b-service').change(function(){
			if (!$(this).find('option.hint').attr('selected')){
				$(this).find('option.hint').remove();
				$(this).parent().find('.cloud').removeClass('chidden');
			}
						
			/*if (!$(this).find('option.hint').length){
				var price = $(this).find('option[selected]').attr('title');
				prc.html('от <span>'+price+'</span>');
			}*/
		});
	}
});
