var href = window.location.href;
if (href.split('#').length == 2){
	if (href.split('#')[1] != '') {
		var path = href.split('#')[1].split('/');
		if (path.length == 2) {
			window.location.href = path[0]+'.html#'+path[0] + '/' + path[1];
		} else {
			window.location.href = path[0]+'.html';
		}
	};
	// window.location.href = window.location.href.split('#')[1]+'.html';
}
$(document).ready(function(){

	load(module);
	$('.content-wrapper').addClass(module).addClass('active');
	$('.content-carousel').width(830*$('#main_nav li').length);
	$('.content-carousel').css("marginLeft", 0 - 830*$('#main_nav li.left').length);
	$('#main_nav li.left').each(function(i){
		if (i == 0)
			$('<div class="content-wrapper left '+$('a',this).attr('rel')+'"></div>').prependTo('.content-carousel');
		else
			$('<div class="content-wrapper left '+$('a',this).attr('rel')+'"></div>').insertAfter($('.content-wrapper.left').eq(i-1));
	});
	$('#main_nav li.right').each(function(i){
		if (i == 0)
			$('<div class="content-wrapper right '+$('a',this).attr('rel')+'"></div>').insertAfter('.content-wrapper.active');
		else
			$('<div class="content-wrapper right '+$('a',this).attr('rel')+'"></div>').insertAfter($('.content-wrapper.right').eq(i-1));
	});
	$('.content-wrapper:not(.active)').height($('.content-wrapper.active').height());
	$('#main_nav li:not(.active) a').each(function(){
		var self = this;
		$.post(this.href,{'html':'1'},function(html){
			$(html).appendTo($('.content-wrapper.'+ $(self).attr('rel')));
			load($(self).attr('rel'));
		});
	});
	
	$('#main_nav li a').live('click',function(){
		window.location.href = window.location.href.split('#')[0]+'#'+$(this).attr('rel');
		var index = $('#main_nav li').index($(this).parent());
		$('.content-wrapper').eq(index).height('auto');
		$('.content-wrapper').each(function(i){
			$(this).removeClass('left right active');
			if (i<index)
				$(this).addClass('left');
			else if(i>index)
				$(this).addClass('right');
			else
				$(this).addClass('active');
		});
		$('#main_nav li').each(function(i){
			$(this).removeClass('left right active');
			if (i<index)
				$(this).addClass('left');
			else if(i>index)
				$(this).addClass('right');
			else
				$(this).addClass('active');
		});
		$('.content-carousel').animate({"marginLeft":0 - 830*index},600, function(){
			$('.content-wrapper:not(.active)').height($('.content-wrapper.active').height());
		});
		$('title').html(titles[$(this).attr('rel')]);
		load($(this).attr('rel'));
		return false;
	});	
	$('#header h1 a').click(function(){
		$('#main_nav a[rel=index]').click();
		return false;
	});
});

var testimonials = "unloaded";


function load(module){
	switch(module){
		case "testimonials":
			if (testimonials == "unloaded")
				initTestimonials();
			testimonials = "loaded";
		break;
		case "license":
			initLicense();
		break;
		default:
			initPage(module);
		break;
	}
}


function initTestimonials(){
	$('ul.testimonials li').each(function(){
		if ($('p',this).length > 1) {
			$('<div class="more"></div>').appendTo($('blockquote',this));
			$('p',this).appendTo($('div.more',this));
			$('p',this).eq(0).insertBefore($('div.more',this));
			$('<a class="more" href="#"><span class="dotted">Развернуть</span></a>').appendTo($('blockquote',this));
			$('<a class="less" href="#"><span class="dotted">Cвернуть</span></a></div>').appendTo($('div.more',this));
			
		};
	});
	$('ul.testimonials a.more,ul.technology a.more').live('click',function(){
		$(this).prev('div.more').slideDown('fast');
		$(this).hide();
		return false;
	});
	$('ul.testimonials a.less,ul.technology a.less').live('click',function(){
		$(this).parent().slideUp('fast');
		$(this).parent().next('a.more').show();
		return false;
	});
	
	$('a.leave_comment,a.tryagain').live('click',function(){
		$(this).next('div.comment_expand').show('fast');
		if ($('.tryagain').length) $('.tryagain').hide();
		return false;
	});
	$('div.comment_expand a.close').live('click',function(){
		$(this).parent().hide('fast');
		if ($('.tryagain').length) $('.tryagain').show();
		return false;
	});
	$('.comment_expand form').live('submit',function(){
		$.post('/testimonials/send.html', $('.comment_expand form').serialize() ,function(r){
			if (r.type == 'error') {
				$('.message').html('<p class="error">'+r.text+'</p>'
		+		'<a class="tryagain"><span class="dotted">Попробовать еще раз</span></a>'
		+		'<div class="comment_expand">'
		+			'<form action="#">'
		+				'<fieldset>'
		+					'<textarea rows="10" cols="45" name="text"></textarea>'
		+					'<div class="form-items">'
		+						'<p class="captcha"><label for="name">Ваше имя:</label>'
		+						'<input class="text" id="name" name="name" type="text" alt="" />'
		+						'</p>'
		+						'<input class="submit" type="submit" value="Отправить" />'
		+					'</div>'
		+				'</fieldset>'
		+			'</form>'
		+			'<a class="close"><span class="dotted">Закрыть</span></a>'
		+		'</div>');
			} else {
				$('.message').html('<p class="thanks">Ваш отзыв отправлен, спасибо!</p>');
			}
			
		},'json');
		$('.message').html('<p class="thanks">Отправка сообщения!</p>');
		return false;
	});
	$('.message').html('<a class="leave_comment">Оставить отзыв</a>'
	+		'<div class="comment_expand">'
	+			'<form action="#">'
	+				'<fieldset>'
	+					'<textarea rows="10" cols="45" name="text"></textarea>'
	+					'<div class="form-items">'
	+						'<p class="captcha"><label for="name">Ваше имя:</label>'
	+						'<input class="text" id="name" name="name" type="text" alt="" />'
	+						'</p>'
	+						'<input class="submit" type="submit" value="Отправить" />'
	+					'</div>'
	+				'</fieldset>'
	+			'</form>'
	+			'<a class="close"><span class="dotted">Закрыть</span></a>'
	+		'</div>');	
}

function initPage(module){
	$('.content-carousel img:not(.wraped)').each(function(){
		var img = this.src.split('/')[this.src.split('/').length - 1];
		$(this).addClass('wraped');
		$(this).wrap('<a class="imagebox" href="/media/uploads/cache/900x480xResizeAutoxFFFFFF/'+img+'"></a>');
	});
	$('ul.gallery a').removeClass('imagebox');
	$('ul.gallery a,a.imagebox').lightbox();
	$.Lightbox.construct({
	show_helper_text:false,
			show_info:true,
			show_extended_info:true,
			download_link:false,
			show_linkback:false,
			opacity:0.3,
			text: {
			image: '',
			close:''
			}
	});
	$('ul.submenu a').live('click',function(){
		$('.r-content',$(this).parent().parent().parent()).hide();
		$('#'+$(this).attr('rel')).show();
		var href = window.location.href;
		window.location.href = href.split('#')[0] + "#" + $('#main_nav .active a').attr('rel') + '/' + $(this).attr('rel');
		return false;
	});
	// var href = window.location.href;
	// var klass = href.split('#')[1];

	// if (typeof klass != "undefined") {
	// 	$('.content-wrapper.'+klass+' .r-content').hide();
	// 	$('#'+$('.content-wrapper.'+klass+' ul.submenu a').eq(0).attr('rel')).show();
	// };
	// $('.r-content',$('ul.submenu a').eq(0).parent().parent().parent()).hide();
	// $('#'+$('ul.submenu a').eq(0).attr('rel')).show();


		$('.content-wrapper.active .r-content').hide();
		$('#'+$('.content-wrapper.active ul.submenu a').eq(0).attr('rel')).show();

	if (href.split('#').length == 2){
		if (href.split('#')[1] != '') {
			var path = href.split('#')[1].split('/');
			if (path.length == 2) {
				$('.content-wrapper.'+path[0]+' .r-content').hide();
				$('.r-content#'+path[1]).show();
			}
		}
	}	

}

function initLicense(){
	$('ul.license img:not(.wraped)').each(function(){
		var img = this.src.split('/')[this.src.split('/').length - 1];
		$(this).width('auto').height('auto').addClass('wraped');
		$(this).attr('src','/media/uploads/cache/150x200xResizeAutoxFFFFFF/'+img);
		$(this).wrap('<a class="imagebox" href="/media/uploads/orig/'+img+'"></a>');
	});
	$('ul.license img.wraped').each(function(){
		var img = this.src.split('/')[this.src.split('/').length - 1];
		$(this).width('auto').height('auto').attr('src','/media/uploads/cache/150x200xResizeAutoxFFFFFF/'+img);
		$(this).parent().removeClass('imagebox');
	});
	$('ul.license a').lightbox();
	$.Lightbox.construct({
	show_helper_text:false,
			show_info:true,
			show_extended_info:true,
			download_link:false,
			show_linkback:false,
			auto_resize:false,
			opacity:0.3,
			text: {
			image: '',
			close:''
			}
	});
	$('ul.license li:even').css('clear',"left");
}
