$(document).ready(function(){

	var timeout = 0;
			
	$('.level').hover(function(){
	
		clear_level();
		$(this).children('ul').show();
		$(this).find('a.drop').addClass('selected');
		
	}).mouseleave(function(){
		timeout = setTimeout(function(){
			clear_level();
		}, 100);
	});
	
	function clear_level(){
		clearTimeout(timeout);
		$('.level').children('ul').hide();
		$('.level').find('a.drop').removeClass('selected');
	}	
    
	$('dl.top_news_list dt').next('dd').addClass('first-child');
	$('.top_news_level_list li:first-child, .vlist li:first-child').addClass('first-child');
	$('.top_news_level_list li:first-child, .vlist li:first-child').next().addClass('first-child');	
	$('.top_news_level_list li:nth-child(2n+1), .svl dd:nth-child(2n-2), .vlist li:nth-child(2n-1)').addClass('clear_left');
    
	$('#toggle_comment').click(function(){
		$('.write_comment').slideToggle(0);
		return false;
	});
	
	$('#show_others').click(function(){
		$(this).toggleClass('open');
		$('.others_list').slideToggle('normal');
		return false;	
	});
	
	$('.others_list li:first-child').addClass('first-child');
	$('.others_list li:first-child').next().addClass('first-child');
	$('.others_list li:first-child').next().next().addClass('first-child');

	if ($.browser.msie && $.browser.version == 6){
		$('#main_menu li ul li:first-child, .mlist li:first-child').addClass('first-child');
	}
    
	initPagination();
});

function initPagination(){
		var show_per_page = 10; 
		var commentscountec = parseInt($('#commentscount').html());
		if(commentscountec > 10 ) {
			var number_of_items = $('#comments_list').children('li').size();
			var number_of_pages = Math.ceil(number_of_items/show_per_page);
			
			$('#current_page').val(0);
			
			$('#show_per_page').val(show_per_page);
			
			var navigation_html = '<a class="previous_link" href="javascript:previous();">Prev</a>';
			
			var current_link = 0;
				
			while(number_of_pages > current_link){
				navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
				current_link++;
			}
			navigation_html += '<a class="next_link" href="javascript:next();">Next</a>';
			
			$('#page_navigation').html(navigation_html);
			
			$('#page_navigation .page_link').eq(0).addClass('active_page');
			
			$('#comments_list').children('li').css('display', 'none');
			
			$('#comments_list').children('li').slice(0, show_per_page).css('display', 'block');
		}
		else {
			$('#page_navigation').css('display','none');
		}
	}

function previous(){
	
	new_page = parseInt($('#current_page').val()) - 1;
	if($('.active_page').prev('.page_link').length==true){
		go_to_page(new_page);
	}
	
}

function next(){
	new_page = parseInt($('#current_page').val()) + 1;
	if($('.active_page').next('.page_link').length==true){
		go_to_page(new_page);
	}
	
}
function go_to_page(page_num){
	var show_per_page = parseInt($('#show_per_page').val());
	start_from = page_num * show_per_page;
	end_on = start_from + show_per_page;
	$('#comments_list').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
	$('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');
	$('#current_page').val(page_num);
}


var win = null;
//popup window ;)
function popUpPlayer(mypage, myname, w, h, scroll){
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
  win = window.open(mypage, myname, settings);
  if (win.opener == null) {  // for Nav 2.0x
      win.opener = self  // this creates and sets a new property
   }
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}


function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, true); 
		return true; 
	}else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	}else{ 
		return false; 
	} 
}

function hideFocusBorders(){
var theahrefs = document.getElementsByTagName("a");
	if (!theahrefs){return;}
		for(var x=0;x!=theahrefs.length;x++){
		theahrefs[x].onfocus = function stopLinkFocus(){this.hideFocus=true;};
	}
}

addEvent(window, 'load', hideFocusBorders);

function voteForBusiness(bid, vote) {
sendVoteRequest(bid, vote);
}

function sendVoteRequest(id, vote){
    var req734 = null;
    if (window.XMLHttpRequest) {
        req734 = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req734 = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var url = "http://www.chalga.bg/music/vote/" + id + "/" + vote;
    req734.open("GET", url, true);
    req734.onreadystatechange = callback_sendvote( req734 );
    req734.send(null);
}

function callback_sendvote(req) {
    return function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                // do nothing :)
            }
        }
    }
}

