$(document).ready(function(){
	// auth form fields events manipulations
	loginFieldsEvents('#inp_loginForm_login', 'Логин');
	loginFieldsEvents('#inp_loginForm_password', 'Пароль');

	// code for banners
	$('.small_banner .baner label').click(function(){
		var src = $(this).find('img').attr('src');
		var width = $(this).find('img').attr('width');
		var height = $(this).find('img').attr('height');
		var new_code = '<a href="http://www.site.com/"><img src="'+ src +'" width="'+ width +'" height="'+ height +'" border="0" alt="Название сайта" title="Название сайта"></a>';
		$('.small_banner #html_code_small').text(new_code);
	});


	$('.big_banner .baner label').click(function(){
		var src = $(this).find('img').attr('src');
		var width = $(this).find('img').attr('width');
		var height = $(this).find('img').attr('height');
		var new_code = '<a href="http://www.site.com/"><img src="'+ src +'" width="'+ width +'" height="'+ height +'" border="0" alt="Название сайта" title="Название сайта" /></a>';
		$('.big_banner #html_code_big').text(new_code);
	});
	
	// navmenu toggler
	$('#mainmenu > li').click(function(){
		var width__ = 0;
		var el = $('#mainmenu').find('li.active');
		if (el) {
			var class__ = el.attr('class');
			if (class__ == 'company active') {
				width__ = 164;
			} else if (class__ == 'vps active') {
				width__ = 91;
			} else if (class__ == 'services active') {
				width__ = 119;
			} else if (class__ == 'documents active') {
				width__ = 156;
			} else if (class__ == 'support active') {
				width__ = 156;
			}
			width__ = width__ + "px";
			el.animate({
			   width:width__
			},300).removeClass('active');
		} else {
		}
		var class_ = $(this).attr('class');
		var width_ = 0;
		if (class_ == 'company') {
			width_ = 371 + 164;
		} else if (class_ == 'vps') {
			width_ = 0 + 91;
		} else if (class_ == 'services') {
			width_ = 169 + 119;
		} else if (class_ == 'documents') {
			width_ = 268 + 156 ;
		} else if (class_ == 'support') {
			width_ = 320 + 156;
		}
		width_ = width_+"px";
		$(this).animate({
		   width:width_
		},"normall");
		$(this).addClass('active');
	});

	// setup ul#plans to work as tabs for each div directly under div.plans_info
	$('ul#plans li').click(function(){$('.homepage #content .text').hide()});
	$('ul#plans li.plan1').click(function(){$('.plans_info').find('> div').hide().end().find('.plan1').show()});
	$('ul#plans li.plan2').click(function(){$('.plans_info').find('> div').hide().end().find('.plan2').show()});
	$('ul#plans li.plan3').click(function(){$('.plans_info').find('> div').hide().end().find('.plan3').show()});
	
	// setup ul.tabs to work as tabs for each div directly under div.panes
	$("ul.tabs").tabs("div.panes > div"); 
	
	//features
	  $('.features a').click(function(){
		$('.features a').removeClass('selected');
		$(this).addClass('selected');
		var name = $(this).attr('href').split('#')[1];
		location = '#' + name;
		$('.featuresText > div').fadeOut(200);
		setTimeout(function(){
		  $('.featuresText #' + name).fadeIn(300);
		}, 200);
	  });
	  $('.featuresText .arrowLeft').click(function(){
		var elements = $('.features a');
		var next = 0;
		$('.features a').each(function(i){
		  if($(this).hasClass('selected')) {
			next = i - 1;
			if (next < 0) {
			  next = elements.length - 1;
			}
			$('.features a').removeClass('selected');
		  }
		});
		$(elements[next]).addClass('selected');
		var name = $(elements[next]).attr('href').split('#')[1];
		location = '#' + name;
		$('.features a').each(function(){
		  $(this).removeClass('selected');
		  if ($(this).attr('href').split('#')[1] == name) {
			$(this).addClass('selected');
		  }
		});
		$('.featuresText > div').fadeOut(200);
		setTimeout(function(){
		  $('.featuresText #' + name).fadeIn(300);
		}, 200);
	  });
	  $('.featuresText .arrowRight').click(function(){
		var elements = $('.features a');
		var next = 0;
		$('.features a').each(function(i){
		  if($(this).hasClass('selected')) {
			next = i + 1;
			if (next > elements.length - 1) {
			  next = 0;
			}
			$('.features a').removeClass('selected');
		  }
		});
		$(elements[next]).addClass('selected');
		var name = $(elements[next]).attr('href').split('#')[1];
		location = '#' + name;
		$('.features a').each(function(){
		  $(this).removeClass('selected');
		  if ($(this).attr('href').split('#')[1] == name) {
			$(this).addClass('selected');
		  }
		});
		$('.featuresText > div').fadeOut(200);
		setTimeout(function(){
		  $('.featuresText #' + name).fadeIn(300);
		}, 200);
	  });
	
	  if (location.href.split('#').length > 1) {
		var name = location.href.split('#')[1];
		$('.features a').each(function(){
		  $(this).removeClass('selected');
		  if ($(this).attr('href').split('#')[1] == name) {
			$(this).addClass('selected');
		  }
		});
		$('.featuresText > div').fadeOut(200);
		setTimeout(function(){
		  $('.featuresText #' + name).fadeIn(300);
		}, 200);
	  }
	  
	// toggler
	$('.toggler').next().hide();
	$('.toggler').next().end().click(function(){
		$(this).next().slideToggle("slow").end().find('> span').toggleClass('collapse');
	});

	// faq toggler
	$('.faqpage .toggle').next().hide();
	$('.faqpage .toggle').next().end().click(function(){
		$(this).next().slideToggle('slow').end().find('> span').toggleClass('collapse');
	});
	
	// oferta toggler
	$('.ofertapage .toggle').next().hide();
	$('.ofertapage .toggle').next().end().click(function(){
		$(this).next().slideToggle('slow').end().find('> span').toggleClass('collapse');
	});

	// partnerpage toggler
	$('.partnerpage .toggle').next().hide();
	$('.partnerpage .toggle').next().end().click(function(){
		$(this).next().slideToggle('slow').end().find('> span').toggleClass('collapse');
	});

	// helper
	$('.helper').mouseover(function(){
		$(this).find('> span').show();
	}).mouseout(function(){
		$(this).find('> span').hide();
	});
	
	// fancybox for license
	$(".licensepage #content .image a").fancybox({
		'titleShow'	: false
	});
	
	$(".wrap_payments a").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titleShow'	: false
	});

	$("#login").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titleShow'	: false
	});
});

var loginFieldsEvents = function (field, text) {
	$(field).click(function(){
		if ($(this).val() == text) $(this).val('');
	}).blur(function(){
		$(this).val( ($(this).val().length>0)?$(this).val():text );
	});
}
