$(document).ready(function() {

//error
if( $('.error').position() != null ) { jsScrollToValue( ".error" ); }

//jsscloller
$('.jsscloller').click(function(eventObject) {

	eventObject.preventDefault();

	var scrollerName = $(this).attr('name');

	if( $('#'+scrollerName).position() != null ) { jsScrollToValue( "#"+scrollerName ); }

});

//calc site
$('#jsCalcSiteForm :checkbox').change(function() {

	var totalSum = 0;

	$('#jsCalcSiteForm :checkbox:checked').each(function() {
		if( $(this).val() ) totalSum += parseInt( $(this).val() );
	});

	$('#jsSiteCost span strong').html( parseFloat( totalSum ) + ' $' );
});


//count symbol
$('#text_counter').keyup(function() { jsTextCounter(); }).change(function() { jsTextCounter(); });


//confirm delete
$('.confirm_delete').click(function(eventObject)
{
	eventObject.preventDefault();
	if( confirm( eventObject.target.title + '?' ) ) window.location=$(this).attr('href');
});


//sh
if( $('.detTopicCodeBox').position() != null )
{
	SyntaxHighlighter.all();
	SyntaxHighlighter.config.clipboardSwf = '/design/images/sh/clipboard.swf';
}


//scroll to top
$(function()
{
	$('#linkToTop').click( function(eventObject)
	{
		eventObject.preventDefault();
		$('html, body').animate({ scrollTop: 0 }, 1000);
	});

	$(window).scroll( function()
	{
		if( $(this).scrollTop() > 1350 )
		{
			$('#linkToTop').fadeIn('slow');
		}
		else {
			$('#linkToTop').fadeOut('slow');
		}
	});
});


//Yandex Share
if( $('#ya_share1').position() != null )
{

	new Ya.share ({
		'element': 'ya_share1',
		'l10n': 'ru',
		'title': $('.detTopicName h1').text(),
		'image' : 'http://seoromin.com.ua/design/images/seoromin.gif',
		'elementStyle': {
			'type': 'button',
			'linkIcon': true,
			'border': false,
			'quickServices': ['vkontakte','twitter','facebook','yaru','odnoklassniki','moimir','friendfeed','lj','moikrug']
		},
		'description': $('.detTopicBrief').text(),
		'popupStyle': { 'copyPasteField': false }
	});
}


//* **** FUNCTIONS **** *//

function jsTextCounter()
{
	var jsTextArea   = $('#text_counter');
	var jsLimit      = parseInt( jsTextArea.attr('title') );
	var jsText       = jsTextArea.val();
	var jsTextLength = jsText.length;
	var jsInfo       = $('#limit-for-text');

	if( jsTextLength > jsLimit ) {
		jsInfo.addClass('red').html('Вы исчерпали допустимое количество символов');
		jsTextArea.val( jsText.substr( 0, jsLimit ));
	}
	else	{
		jsInfo.removeClass('red').html('осталось <span id="for-text">'+(jsLimit-jsTextLength)+'</span> символов');
	}
}


function jsScrollToValue( somevalue  )
{
	var jsPosTop = $(somevalue).position().top;
	var jsWinPosTop = $(window).scrollTop();
	if( $(somevalue).position() != null && ( jsPosTop > jsWinPosTop+350 || jsPosTop < jsWinPosTop ) ) $('html,body').animate({ scrollTop: jsPosTop-50}, 650);
}

}); //Конец Ready
