var getSelTextWorked=function(){
	//alert('getSelText')
	var txt = '';
	if (window.getSelection!=undefined){
		txt = window.getSelection();
	}else if (document.getSelection!=undefined){
		txt = document.getSelection();
	}else if (document.selection!=undefined){
		txt = document.selection.createRange().text;
	}
	return txt;
}

var findErrorButton=function(event)
{
    $('#error_descr').val("ошибка в тексте \""+getSelTextWorked()+"\"");
	$('div.popup_overlay, #findError').css('display','block');

}
var ctrlEnter=function(event)
{
    if((event.ctrlKey) && ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
    	findErrorButton()
}
var findError = function(){
	var email = $('#error_email').val(),
		username = $('#error_username').val(),
		type = $('#error_type').val(),
		descr = $('#error_descr').val();
	if (type == "none" || descr == "" 
		//|| (email != "" && email.search(/\w+@\w+\.\w/))
	) {
		//alert('');
	} else {
		$.post('/senderror', {
				'error_type': type,
				'error_descr': descr,
				'error_username': username,
				'error_email': email
			}, function(data){
			if (data == '1'){
				$('#findError').hide();
				$('#findErrorMessage').css('display', 'block');
			}
		});
	}
}
var closeError = function(){
	$('#findError, #findErrorMessage, div.popup_overlay').hide();
}

// JavaScript Document

var urlGrabber = function(){
	var matches = new Array();
	matches = location.href.match(/\#(.*)$/);
	if (matches){
		return matches[1];
	}
	return false;
}



	$(document).ready(function() {
		$(document).keypress(function(event){ctrlEnter(event)})	
		var link = urlGrabber();
		if (link){
			$('#NewFeaturedNews div.tabsContainer ul.tabs').find('a.image').each(function(i){
				if ($(this).attr('href').replace('/ua/', '/') == link.replace('/ua/', '/')){
					// afterload all topnews images
					showAllTopnewsImages();
					var leftId = $(this).attr('rel');
					$('#NewFeaturedNews').find('div.TabContent').hide();
					$(leftId).show();
					
					var sdvig = 0;
					if (i > $('#NewFeaturedNews div.tabsContainer ul.tabs').find('a.image').size() - 3) {
						sdvig = i-2;
					} else {
						sdvig = i; 
					}
					
					$('#NewFeaturedNews div.tabsContainer ul.tabs').css('top', -92*sdvig);
					$(this).parent().parent().parent().find('li').removeClass('selected');
					$(this).parent().parent().addClass('selected');
				}
			});
		}
		
	/* MOUSE OVER AND PRESSDOWN*/
		var buttons = $('span.button_red');
		buttons.mouseover(function(){
			$(this).addClass('MouseOver');
		});
		buttons.mousedown(function(){
			$(this).addClass('MousePressDown');
		});
		buttons.mouseup(function(){
			$(this).removeClass('MousePressDown');
		});
		buttons.mouseleave(function(){
			$(this).removeClass('MouseOver');
			$(this).removeClass('MousePressDown');
		});
	/* MOUSE OVER AND PRESSDOWN*/
	
		var ActivePopUp = false;
		$('div.dropdown, div.DropDownList').live('mousemove',function(){
			ActivePopUp = true;
		});
		$(document).click(function(){
			if (ActivePopUp == true) {
				$('div.dropdown, div.DropDownList').filter('.activePopUp').hide().removeClass('activePopUp');
				ActivePopUp = false;
			}
		});

		$('#tochka_header .navigationBar .barMenu').mouseover(function(){
			$('#portal_sections').show().addClass('activePopUp');
		});
		$('#tochka_header .navigationBar .barMenu').mouseleave(function(){
			$('#portal_sections').hide().removeClass('activePopUp'); 
		});
		$('#tochka_header .navigationBar .startpage').mouseover(function(){
			$('#startpage_menu').show().addClass('activePopUp'); 
		});
		$('#tochka_header .navigationBar .startpage').mouseleave(function(){
			$('#startpage_menu').hide().removeClass('activePopUp'); 
		});
		
		
		// polls
		$('#poll_form li *').live('click',function(){
			$(this).parent().parent().find('li input').removeAttr('checked');
			$(this).parent().parent().find('li').removeClass('selected');
			$(this).parent().find('input').attr("checked", "checked");
			$(this).parent().addClass('selected');
			return false;
		});
	
		$('div.quickSearch ul.tabs>li').live('click', function(){
			$(this).parent().find('li').removeClass('current');
			$(this).addClass('current');
			if(!$(this).parent().hasClass("disable_pages")) {
				selTab = $(this).find('a').attr('href').replace('#', '');
				$(this).parents('div.quickSearch').find('div.TabContent').hide().filter('#'+selTab).show();
			}
			return false;
		});
		
		$('div.ContentBlock ul.tabs>li').live('click', function(){
			$(this).parent().find('li').removeClass('current');
			$(this).addClass('current');
			selTab = $(this).find('a').attr('href').replace('#', '');
			if ($(this).parents('div.ContentBlock').hasClass('TVWidget')){
				$(this).parents('div.ContentBlock').find('div.TabContent').hide()
				.filter('#'+selTab).show()
				.find('div.scrollHandleVertical').show()
				;
				$(window).resize();
				return false;
			} else if(!$(this).parent().hasClass("disable_pages")) {
				if ($(this).parents('div.ContentBlock').find('div.TabContent').size() > 0) {
					$(this).parents('div.ContentBlock').find('div.TabContent').hide().filter('#'+selTab).show();
				} else {					
					$(this).parents('div.ContentBlock').find('div.content').hide().filter('#'+selTab).show();
				}
				if(!$(this).parent().hasClass("href_action")) return false;
			}
		});
		

		$('div.quickSearch img.dd_button').live('click', function(){
			$('div.ContentBlock').css({'z-index':'100'});
			$(this).parents('div.ContentBlock').css({'z-index':'110'});
			$(this).parents('li.current').find('div.dropdown').show().addClass('activePopUp');
		});
		$('div.quickSearch div.dropdown img.dd_button').live('click', function(){
			$(this).parents('li.current').find('div.dropdown').hide().removeClass('activePopUp');
		});
		$('div.quickSearch div.dropdown li a').live('click',function(){
			$(this).parent().parent().find("li").show();
			$(this).parent().hide();
			$(this).parents('li.current').find('font').text($(this).text());
			$(this).parents('div.dropdown').hide();
			return false;
		});
	
		$('div.BestPlaces ul.tabs>li').live('click', function(){
			$(this).parent().find('li').removeClass('current');
			$(this).addClass('current')
			selTab = $(this).find('a').attr('href').replace('#', '');
			$(this).parents('div.BestPlaces').find('div.TabContent').hide().filter('#'+selTab).show();
			return false;
		});


		$('div.DropDownElement a.dd_button').live('click', function(){
			$('div.ContentBlock').css({'z-index':'100'});
			$(this).parents('div.ContentBlock').css({'z-index':'110'});
			$(this).parent().find('div.DropDownList').show().addClass('activePopUp');
			return false;
		});
		$('div.DropDownElement div.DropDownList li a').live('click',function(){
			$(this).parents('div.DropDownElement').find('font').text($(this).text());
			$(this).parents('div.DropDownList').hide();			
			if (!$(this).parent().parent().parent().hasClass('href_action')){
				return false;
			}
		});


		$('div.select input').click(function(){
			$(this).parent().find('ul').toggle();
			$('div.ContentBlock').css({'z-index':'100'});
			$(this).parents('div.ContentBlock').css({'z-index':'110'});
			$(this).parents('div.ContentBlock').find('div.select').css({'z-index':'200'});
			$(this).parents('div.select').css({'z-index':'210'});
		});
		$('div.select').mouseleave(function(){
			$(this).find('ul').hide();
		});
		$('div.select ul li').mouseenter(function(){
			$(this).addClass('current');
			if ($.browser.msie) {
				$(this).parent().animate({opacity: 1.0}, 50); 
			}
		});
		$('div.select ul li').mouseleave(function(){
			$(this).removeClass('current');
		});
		$('div.select ul li').click(function(){
			$(this).parents('div.select').find('input').val($(this).text());
			$(this).parent().hide();
		});
		
		
		$('#main:not(.frontpage, .news, .finance) div.Center_2inRow').each(function(){
			var maxHeight = 0;
			var itm = $(this).find('div.ContentBlock div.CB_inner');
			itm.each(function(){
				if ($(this).height() > maxHeight) {
					maxHeight = $(this).height();
				}
			});
			itm.height(maxHeight);
		});
		
		
		$('#main:not(.finance) div.ContentBlock').each(function(){
			if ($(this).hasClass('fl') && $(this).next('.fr') && !$(this).hasClass('.with_2_in_col')) {
				var maxHeight = 0;
				var itm = [$(this).find('div.CB_inner'), $(this).next().find('div.CB_inner')];
				$.each(itm, function(){
					if ($(this).height() > maxHeight) {
						maxHeight = $(this).height();
					}
				});
				$.each(itm, function(){
					$(this).height(maxHeight);
				});
			}
			if ($(this).hasClass('.with_2_in_col')) {
				var leftBlock = $(this).find('div.CB_inner'),
					right1 = $(this).next().find('div.CB_inner'),
					right2 = $(this).next().next().find('div.CB_inner');

				if (right2.height() + right1.height() + 44 < leftBlock.height()){
					right2.height(leftBlock.height() -  right1.height() - 44);
				} else if (right2.height() + right1.height() + 44 > leftBlock.height()){
					leftBlock.height(right1.height() + right2.height() + 44);
				}
			}
		});
		
		$('li.gadget').each(function(){
			//$(this).find('ul.tabs li:first').addClass('selected');
			i = 500;
			$(this).find('div.dropdown ul').each(function() {
				$(this).find('li').hide().filter(':first').show()
				$(this).parents('div.top').css('z-index',i);
				i = i-10;
			})
			$(this).find('div.selectBar ul').each(function() {
				$(this).find('li.selected').width($(this).find('a').width()+20);
				$(this).find('li').hide().filter(':first').show()
			})
			$(this).find('div.gadgetTab').hide().filter(':first').show();
		});
		$('li.gadget ul.tabs li').click(function(){
			$(this).parent().find('li').removeClass('selected');
			$(this).addClass('selected');
			var selTab = $(this).find('a').attr('href').replace('#', '');
			$(this).parents('div.content').find('div.gadgetTab').hide().filter('#'+selTab).show();
			return false;
		});


		$('div.NewFeaturedNews ul.tabs li').mouseenter(function(){
			// afterload all topnews images
			showAllTopnewsImages();
			$(this).parent().find('li').removeClass('selected');
			$(this).addClass('selected');
			var selTab = $(this).find('div').attr('id').substr(4);
			$(this).parents('div.NewFeaturedNews').find('div.TabContent').hide().filter('#'+selTab).fadeIn();
			return false;
		});

		$('div.NewFeaturedNews .button_dwn').live('click',function(){
			var FeatTabs = $('div.NewFeaturedNews ul.tabs'),
				countLi = $(this).parents('div.NewFeaturedNews').find('ul.tabs > li').size(),
				liHeight = 92,
				maxTop = (countLi-3)*-liHeight,
				FeatTabsTop = parseInt(FeatTabs.css('top')),
				numLiMoved = countLi-3+FeatTabsTop/liHeight >= 3 ? 3 : countLi-3+FeatTabsTop/liHeight;

			if (FeatTabs.is(':animated')) return false;
			if (FeatTabsTop > maxTop || FeatTabsTop == 0) {
				// afterload all topnews images
				showAllTopnewsImages();
				
				FeatTabs.animate({top: '-='+liHeight*numLiMoved}, 'slow', '', function(FeatTabsTop, maxTop){
					if (parseInt($('div.NewFeaturedNews ul.tabs').css('top')) <= ($(this).parents('div.NewFeaturedNews').find('ul.tabs > li').size()-3)*-liHeight) {
						$('div.NewFeaturedNews .button_dwn').addClass('button_dwn_na');
					}
				});
				$(this).parent().find('.button_up').removeClass('button_up_na');
			}
			FeatTabs.find('li.selected').removeClass('selected');
			return false;
		});
		$('div.NewFeaturedNews .button_up').live('click',function(){
			var FeatTabs = $('div.NewFeaturedNews ul.tabs'),
				countLi = $(this).parents('div.NewFeaturedNews').find('ul.tabs > li').size(),
				liHeight = 92,
				maxTop = (countLi-3)*-liHeight,
				FeatTabsTop = parseInt(FeatTabs.css('top')),
				numLiMoved = countLi-3+(maxTop-FeatTabsTop)/liHeight >= 3 ? 3 : countLi-3+(maxTop-FeatTabsTop)/liHeight;
			if (FeatTabs.is(':animated')) return false;
			if (FeatTabsTop < 0) {
				FeatTabs.animate({top: '+='+liHeight*numLiMoved}, 'slow', '', function(){
					if (parseInt($('div.NewFeaturedNews ul.tabs').css('top')) >= 0){
						$('div.NewFeaturedNews .button_up').addClass('button_up_na');
					}
				});
				$(this).parent().find('.button_dwn').removeClass('button_dwn_na');
			};
			FeatTabs.find('li.selected').removeClass('selected');
			return false;
		});

        $('#main div.GalleryBigImg').find('a.imageCont_wBG, a.btn_prev, a.btn_next').mouseover(function(){
                $(this).parent().find('a.btn_prev, a.btn_next').show();
        })
        $('#main div.GalleryBigImg').find('a.imageCont_wBG, a.btn_prev, a.btn_next').mouseout(function(){
                $(this).parent().find('a.btn_prev, a.btn_next').hide();
        })

        $('div.selectNew span').live('click', function(){
			$(this).parent().find('ul').toggle();
			$('div.ContentBlock').css({'z-index':'100'});
			$(this).parents('div.ContentBlock').css({'z-index':'110'});
			$(this).parents('div.ContentBlock').find('div.selectNew').css({'z-index':'200'});
			$(this).parents('div.selectNew').css({'z-index':'210'});
		});
		$('div.selectNew').bind('mouseleave', function(){
			$(this).find('ul').hide();
			$(this).find('li').removeClass('current');
		});
		$('div.selectNew ul li').bind('mouseenter', function(){
			$(this).addClass('current');
			if ($.browser.msie) {
				$(this).parent().animate({opacity: 1.0}, 50);
			}
		});
		$('div.selectNew ul li').bind('mouseleave', function(){
			$(this).removeClass('current');
		});
		$('div.selectNew ul li').live('click', function(){
			$(this).parents('div.selectNew').find('span.selectedValue').html($(this).html());
			$(this).parents('div.selectNew').find('input').val($(this).attr('key'));
			$(this).parent().hide();
		});

		$('#main .rightColumn .CurrencyWidget table.RatesDataTable').each(function(){
			$(this).find('tr:last').css({'background-image' : 'none'});
		});
		$('#main .rightColumn .CurrencyWidget table.switchableRows').each(function(){
			$(this).find('tr:first th:last').css({'border-right' : '#f8de75 1px solid'});
		});

		$('#main .rightColumn .CurrencyWidget table.switchableRows tr:not(.selected, .headerTR)').live('click',function(){
			selTab = $(this).attr('id').replace('T_', '');;
			$(this).parents('div.content').find('div.RatesGraphics').hide().filter('#'+selTab).show();

			$(this).parents('table').find('tr.selected').prev().removeClass('noneBG');
			$(this).parents('table').find('tr.selected').removeClass('selected');
			$(this).parents('table').find('tr td').css({'border-bottom' : '#cccccc 0px dashed'});

			$(this).addClass('selected');
			$('table.switchableRows tr.selected').each(function(){
				$(this).parents('table').find('tr').find('td:first').css({'border-left' : '#f8de75 0px solid'});
				$(this).parents('table').find('tr').find('td:last').css({'border-right' : '#f8de75 1px solid'});
				$(this).find('td:first').css({'border-left' : '#f8de75 1px solid'});
				$(this).find('td:last').css({'border-right' : '#fffcea 0px solid'});
				$(this).prev().addClass('noneBG');
				$(this).parents('table').find('tr:last td').css({'border-bottom' : '#fff 0px solid'});
			});
		});
		$('#main .rightColumn .CurrencyWidget table.switchableRows tr.selected').each(function(){
			$(this).parents('table').find('tr').find('td:first').css({'border-left' : '#f8de75 0px solid'});
			$(this).parents('table').find('tr').find('td:last, th:last').css({'border-right' : '#f8de75 1px solid'});
			$(this).find('td:first').css({'border-left' : '#f8de75 1px solid'});
			$(this).find('td:last').css({'border-right' : '#fffcea 0px solid'});
			$(this).prev().find('td').css({'border-bottom' : '#f8de75 1px solid'});
		});
		$('#main .rightColumn .CurrencyWidget').each(function(){
			if ($(this).find('table.RatesDataTable').height() > $(this).find('div.RatesGraphics').height()-2) {
				$(this).find('div.RatesGraphics').height($(this).find('table.RatesDataTable').height()-2);
			}
			if ($.browser.msie && $.browser.version < 7) {
				$(this).find('div.content').height($('.RatesGraphics').height()-4);
			}
		});

	});
	
	
var carousel = function(block){
	$('#'+block.id+' .btn_next').live('click',function(){
		var jokesCarusel = $(this).parent().parent().find('.carousel_container ul'),
			countLi = jokesCarusel.find('li').size(),
			liWidth = block.li_width,
			maxLeft = (countLi-1*block.li_in_row)*-liWidth,
			carusLeft = jokesCarusel.css('left') == 'auto' ? 0 :parseInt(jokesCarusel.css('left')),
			temp = jokesCarusel.html();
		if (jokesCarusel.is(':animated')) return false;
		if (carusLeft > maxLeft || (carusLeft == 0 && countLi != 1) || (carusLeft == -liWidth*block.li_in_row && countLi != 2)) {					
			jokesCarusel.animate({left: '-='+liWidth*block.li_in_row}, 'slow');
			
			$('#'+block.counter_id).text(parseInt($('#'+block.counter_id).text())+1);
						
			$(this).parent().find('a.notAble').removeClass('notAble');
		} else if (carusLeft < maxLeft) {
			jokesCarusel.animate({left: '-='+liWidth*block.li_in_row}, 'slow');

			$(this).addClass('notAble');
		} else {
			jokesCarusel
				.width(jokesCarusel.width()*2)
				.append(temp)
				.animate({left: '-='+liWidth*block.li_in_row}, 'slow', '', function(){
					jokesCarusel
						.width(jokesCarusel.width()/2)
						.css('left', '0px')
						.find('li:lt('+countLi+')').remove();
				});
			$('#'+block.counter_id).text(1);
		}
		if (typeof(block.function_next) == 'function'){
			(block.function_next)();
		}		
		return false;
	});
	$('#'+block.id+' .btn_prev').live('click',function(){
		var jokesCarusel = $(this).parent().parent().find('.carousel_container ul'),
			countLi = jokesCarusel.find('li').size()
			carusLeft = jokesCarusel.css('left') == 'auto' ? 0 :parseInt(jokesCarusel.css('left')),
			liWidth = block.li_width,
			temp = jokesCarusel.html();
		if (jokesCarusel.is(':animated')) return false;
		if (carusLeft == -liWidth) {
			jokesCarusel.animate({left: '+='+liWidth*block.li_in_row}, 'slow');

			$('#'+block.counter_id).text(parseInt($('#'+block.counter_id).text())-1);
			
			$(this).addClass('notAble');
		} else if (carusLeft < 0 || jokesCarusel.find('li:first').children().length == 0) {
			jokesCarusel.animate({left: '+='+liWidth*block.li_in_row}, 'slow');

			$('#'+block.counter_id).text(parseInt($('#'+block.counter_id).text())-1);

			$(this).parent().find('a.notAble').removeClass('notAble');
			if (carusLeft == -liWidth) {
				$(this).addClass('notAble');
			}
		} else {
			jokesCarusel
				.width(jokesCarusel.width()*2)
				.prepend(temp)
				.css('left', liWidth*(-countLi)+'px')
				.animate({left: '+='+liWidth*block.li_in_row}, 'slow', '', function(){
					jokesCarusel
						.width(jokesCarusel.width()/2)
						.find('li:gt('+(countLi-1)+')').remove();
				});
			$('#'+block.counter_id).text(countLi/block.li_in_row);
		}
		if (typeof(block.function_prev) == 'function'){
			(block.function_prev)();
		}			
		return false;
	});
}

var carouselAJAX = function(block){
    $('#'+block.id+' .btn_next').live('click',function(){
        var jokesCarusel = $(this).parent().parent().find('.carousel_container ul'),
        	countLi = jokesCarusel.find('li').size(),
        	liWidth = block.li_width,
        	maxLeft = (countLi-1*block.li_in_row)*-liWidth,
        	carusLeft = jokesCarusel.css('left') == 'auto' ? 0 :parseInt(jokesCarusel.css('left'));
        if (jokesCarusel.is(":animated")) return false;
        $(this).parent().find('a.btn_prev').show();
        if (carusLeft > maxLeft-liWidth || carusLeft == 0  || carusLeft == -liWidth) {
            if (jokesCarusel.find('li:last').children().length == 0) {
                $.post(block.url + (countLi - 1), {}, function(data){
                    jokesCarusel.find('li:last').replaceWith(data);
                    countLi = jokesCarusel.find('li').size();
                    jokesCarusel.width(liWidth*countLi);
                    if (carusLeft == maxLeft) {
                        $(this).hide();
                    }
                });
            }
            jokesCarusel.animate({left: '-='+liWidth*block.li_per_time}, 'slow');
        } else if (maxLeft - carusLeft == 2*liWidth) {
            jokesCarusel.animate({left: '-='+liWidth}, 'slow');
            $(this).hide();
        };
        return false;
    });

    $('#'+block.id+' .btn_prev').live('click',function(){
        var jokesCarusel = $(this).parent().parent().find('.carousel_container ul');
			carusLeft = parseInt(jokesCarusel.css('left'));
        	liWidth = block.li_width;
        if (jokesCarusel.is(":animated")) return false;
		$(this).parent().find('a.btn_next').show();
        if (carusLeft == -liWidth) {
            jokesCarusel.animate({left: '+='+liWidth}, 'slow');
			$(this).hide();
        } else if (carusLeft < 0 || jokesCarusel.find('li:first').children().length == 0) {
            jokesCarusel.animate({left: '+='+liWidth*block.li_per_time}, 'slow');
            if (carusLeft == -liWidth*block.li_per_time) {
                $(this).hide();
            }
        };
        return false;
    });	
}

var showAllTopnewsImages = function(){
	var block = $('div.NewFeaturedNews');
	if (block.find('div.TabContent:eq(1)').find('a img').attr('src') == ''){
		block.find('div.TabContent').each(function(i){
			$(this).find('a img').attr('src', topnewsImages[i]);
		});	
	}
}

var cssFix = function(){

  var u = navigator.userAgent.toLowerCase(),

  is = function(t){return (u.indexOf(t)!=-1)};

  $("html").addClass([

    (!(/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(''));

}();
