/* scripts */
var cssFix = function() {
	var u = navigator.userAgent.toLowerCase(), addClass = function(el, val) {
		if (!el.className) {
			el.className = val;
		} else {
			var newCl = el.className;
			newCl += (" " + val);
			el.className = newCl;
		}
	}, is = function(t) {
		return (u.indexOf(t) != -1)
	};
	addClass(
			document.getElementsByTagName('html')[0],
			[
					(!(/opera|webtv/i.test(u)) && /msie (\d)/.test(u)) ? ('ie ie' + RegExp.$1)
							: is('firefox/2') ? 'gecko ff2'
									: is('firefox/3') ? 'gecko ff3'
											: is('gecko/') ? 'gecko'
													: is('opera/9') ? 'opera opera9'
															: /opera (\d)/
																	.test(u) ? 'opera opera' + RegExp.$1
																	: is('konqueror') ? 'konqueror'
																			: is('applewebkit/') ? 'webkit safari'
																					: is('mozilla/') ? 'gecko'
																							: '',
					(is('x11') || is('linux')) ? ' linux' : is('mac') ? ' mac'
							: is('win') ? ' win' : '' ].join(" "));
}();

var is_chrome = false, chrome_version = false;
if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
	is_chrome = true;
	chrome_version = navigator.userAgent.replace(/^.*Chrome\/([\d\.]+).*$/i,
			'$1')
}

var body='';
var antiSpanDiv='';
var ajaxLoader='';

startScript(function() {
			body = $('body');
			antiSpanDiv = $('#antiSpanDiv');
			ajaxLoader = $('#ajax-loader');
			$('.quick_search').submit(
					function() {
						var search_word = $(this).children(
								"input[name=search_str]").val();
						location.href = '/search/string_' + search_word + '/';
						return false;
					});

			function SubmitSearchForm() {
				var search_structure = $("#search_structure").val();
				var search_word = $("#search_word").val();

				if (search_word == "Поиск" || search_word.length < 3) {
					alert('Укажите параметр поиска!');
					return false;
				}

				link = '/search/string_' + search_word + '/';
				if (search_structure > 0) {
					link += 'structure_' + search_structure + '/'
				}

				location.href = link;
			}
			$('#search_button').click(function() {
				SubmitSearchForm();
				return false;
			});
			
			$('p.media5').click(function(){
				window.location = 'http://www.media5.com';
			});

			$("#main_search_form").submit(function() {
				SubmitSearchForm();
				return false;
			});

			$('.showFormLogin').live("click", function(event) {
				$('body').overlay( {
					opacity : 0.7
				}).show();
				$('#loginform').middle('window', {
					position : 'fixed'
				}).show();
				return false;
			});
			$('.closelogin').live('click', function() {
				$('#loginform').hide();
				$('#m5_overlay').hide();
			});

			$('.showFormReg').live("click", function(event) {
					
						$("#regform .capcha").attr(
								'src',
								'/securimage/securimage_show.php/?' + Math
										.random());
						$('body').overlay( {
							opacity : 0.7
						}).show();
						$('#regform').middle('window', {
							position : 'fixed'
						}).show();
						return false;
			});
			$('.closereg').live('click', function() {
				$('#regform').hide();
				$('#m5_overlay').hide();
			});
			$('#m5_overlay').live('click', function() {
				$('#loginform').hide();
				$('#regform').hide();
				$('#m5_overlay').hide();
			});
			$("#regform .capcha").click(
					function() {

						src = document.captcha.src;
						$(this).attr(
								'src',
								'/securimage/securimage_show.php/?' + Math
										.random());
					});

			maincomment();
			poportionsImage();
			hideZiroComments();
			showtextbox();
			spoiler();
		});
        
 
function browserDetect(){

   var isiPad = navigator.userAgent.match(/iPad/i) != null;
   var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
   var isiPod = navigator.userAgent.match(/iPod/i) != null;

   if($.browser.msie && $.browser.version == 6)
		return false;

   if(isiPad || isiPhone || isiPod) 
	   return false;

   return true;
}

function spoiler() {
	$('.spoiler').before('<a href="#" class="spoiler_link">Отобразить</a>');

	$('.spoiler_link').click(function() {
		var text = $(this).text();
		if (text == 'Отобразить')
			$(this).text('Скрыть').toggleClass('sp_open2');
		else
			$(this).text('Отобразить').toggleClass('sp_open2');
		$(this).next('.spoiler').toggleClass('sp_open');
		return false;
	});
}

function textIndent(i) {
	$('.fullcontent p:first').css('text-indent', i);
}

$.fn.scaleImage = function(w, h) {
	this.each(function() {
		var src = $(this).attr('src');
		$(this).removeAttr('src').unbind('load').load(function() {
			if (h > 0 && $(this).height() < h)
				$(this).height(h).width('auto');
			$(this).css( {
				marginLeft : -$(this).width() / 2
			}).show();
		}).attr('src', src);
	});
	return this;
}

function poportionsImage() {
	//$('img.js-blog-foto').scaleImage(0, 250);
	$('.lastnews-img img').scaleImage();
}

function hideZiroComments() {
	$('.con-tal').each(function() {
		if ($(this).html() == "0") {
			$(this).parent('.commentpop').css('display', 'none');
		}
	});
}

function maincomment() {
	$('div.this_comment_count').live('hover', function() {
		var $parents = $(this).parents('.commentsConteiner');
		var speed = $.browser.msie ? null : 200;

		window['comment_user'] = $parents.children('.this_comment_user').show(speed);
		window['comment_count'] = $parents.children('.this_comment_count').hide(speed);
	});

	$('img.this_comment_close').live('click', function() {
		var $parents = $(this).parents('.commentsConteiner');
		if ($.browser.msie)
			var speed = 0;
		else
			var speed = 200;
		$parents.children('.this_comment_count').show();
		$parents.children('.this_comment_user').hide();
	});

	$('.commentsConteiner').live('mouseleave', function() {
		window['comment_count'].show();
		window['comment_user'].hide();
	});
	$('.block-user-rating').live('hover', function() {
		$('.this_comment_count').show();
		$('.this_comment_user').hide();
	});
}
function showtextbox() {
	$('div.prevcomments').hover(function() {
		if ($(this).text().length > 69 && !$(this).is('.noslide')) {
			$(this).stop().animate( {
				height : 166
			}, 300);
		}
	}, function() {
		if (!$(this).is('.noslide')) {
			$(this).stop().animate( {
				height : 44
			}, 300);
		}
	});
}
function WaitingResponse(param){
	if(!antiSpanDiv.length)
	{
		body.append('<div style="position:absolute; opacity:0; left:0; top:0 ;z-index:999; width:'+body.width()+'px; height:'+body.height()+'px;" id="antiSpanDiv"></div><img src="/images/ajax-loader.gif" width="66" height="66" id="ajax-loader"/>');
		antiSpanDiv = $('#antiSpanDiv');
		ajaxLoader = $('#ajax-loader');
	}
	switch(param){
		case 'start':
			antiSpanDiv.show();
			ajaxLoader.show();
			/*
			if($('#m5_overlay').is(':visible'))
			{
				$('#m5_overlay').css({opacity:0});
				antiSpanDiv.css({background:'#000',opacity:0.7});
			}
			*/
		break;
		case 'end':
			antiSpanDiv.hide();
			ajaxLoader.hide();
		break;
	}	
}


		var al_b_width = $('.blue_container').width();
		var blue_a = $('#blue_accordion a');
		var blue_box = $('#blue_box');

		blue_a.hover(function(){

			if(window['blue_a_open']) window['blue_a_open'].css({zIndex:1});
			blue_a.removeClass('a_t_left')/*.removeClass('a_t_right')*/;
			blue_box.hide();

			var title = $(this).attr('title');
			var autor = $(this).children().attr('title');
			var uri = $(this).attr('href');
			var link = $(this).attr('autor');
			var index = blue_a.index($(this));
			var p_left = $(this).position().left;

			if(window['blue_a_open']) window['blue_a_open'].css({zIndex:1});
			
			window['blue_a_open'] = $(this).css({zIndex:3});

			
			blue_box.find('span').children('a').text(autor).attr('href',link);
			blue_box.find('div').children('a').text(title).attr('href',uri);

			//if(index > 3)
			//{
				//$(this).addClass('a_t_right');
				//var right = al_b_width-(p_left+65);
				//blue_box.addClass('t_right').show().css({right:right+65-260,left:'auto'});
			//} else {
				$(this).addClass('a_t_left');
				blue_box/*.removeClass('t_right')*/.show().css({left:p_left+65,right:'auto'});
			//}
		}).eq(0).trigger('mouseover');
			
			/*
			
		$('#blue_box, .blue_container').mouseleave(function(){
			window['blue_a_open'].css({zIndex:1});
			blue_a.removeClass('a_t_left').removeClass('a_t_right');
			blue_box.hide();
		});
		*/


$(window).load(function(){

	var default_padding = $('.default_padding'),
		default_padding_width = default_padding.width()-25,
		default_padding_image = default_padding.find('img');
		
	default_padding_image.each(function(){
		
		if($(this).width() > default_padding_width)
			$(this).width(default_padding_width).height('auto');
	});
});
