
$(document).ready(function(){

        blocks();
		$("#dataInputs").hide();
		$(".articles_of_avtor").hide();
			
			$("#nav-one li").hover(
				function() { $("ul", this).fadeIn("fast"); }, 
				function() { $("ul", this).fadeOut("fast"); } 
			);
	  	if (document.all) {
				$("#nav-one li").hoverClass ("sfHover");
			}
	  });
	  
		$.fn.hoverClass = function(c) {
			return this.each(function(){
				$(this).hover( 
					function() { $(this).addClass(c);  },
					function() { $(this).removeClass(c); }
				);
			});
		
	$("#loading").bind("ajaxSend", function(){
        $(this).show(); 
    }).bind("ajaxComplete", function(){
        $(this).hide(); 
    });
		}  

function openDateInputs() {
      var checkElement = $("#dataInputs");
      if(checkElement.is(':visible')) {
	  	$(checkElement).slideUp('fast');
        return false;
        }
      if(!checkElement.is(':visible')) {
        checkElement.slideDown('fast');
        return false;
        }
 }

 
 function openDiv() {
	 $('.blocklink').click(
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('div')) && (checkElement.is(':visible'))) {
	  	$(checkElement).slideUp('normal');
        return false;
        }
      if((checkElement.is('div')) && (!checkElement.is(':visible'))) {
        //$('#accordion div:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
        }
      }
    );

 }
 
function showAllArticles(avID, number) {
	checkElement = '#all'+avID;
	closeElement = '#close'+avID;
	heightWin = parseInt(number) * 3.5 + 3;
	if (heightWin > 25) heightWin = 25;  
	heightWin = heightWin + 'em';  
	  if($(checkElement).is(':visible')) {
	  	$(checkElement).slideUp('normal');
	  	$(closeElement).hide();
        return false;
        }
      if(!$(checkElement).is(':visible')) {
		$(checkElement).load("/articles/all_of_author/", {avtor_id:avID});
		$(checkElement).css( "height", heightWin);
        $(checkElement).slideDown('normal');
	  	$(closeElement).show();
        return false;
        }

}

function onAjaxSuccess(avID, data)
{
//$(data).appendTo(div);
alert(data);
}
 
 function blocks() {
  $('#content-hp h2').click(
    function(event) {
      var checkElement = $(this).next();
      if((checkElement.is('div')) && (checkElement.is(':visible'))) {
	  	$(checkElement).slideUp('normal');
		$(this).css( "background-image", "url(/images/rusk/plus.gif)" );
        return false;
        }
      if((checkElement.is('div')) && (!checkElement.is(':visible'))) {
        //$('#accordion div:visible').slideUp('normal');
        checkElement.slideDown('normal');
		$(this).css( "background-image", "url(/images/rusk/minus.gif)" );
        return false;
        }
      }
    );
	$('#content-hp h2 a').click(
		function(event) {
			event.stopPropagation();
			return true;
		}
	);
  }
  
  
  function showDayArticles(razd, day, elem) {
	checkElement = elem;
	showElement = '#DayArticles';
	closeElement = '#NewsOfDay';
	
	$("#news-hplink a").attr('class', ''); 
	$(checkElement).attr('class', 'active');
	$("#news-hplink a:last[@class=active]").attr('class', 'active leftlink');
	$(closeElement).hide('normal');
	$(showElement).show('normal');
	$(showElement).html('<div id="loading">Идет загрузка...</div>');
	$(showElement).load("/service/articles_of_day/", {razd:razd, day:day});
      return false;
}
  
  function showArticlesOfDay(daypath, elem, template, razd, showElement, closeElement, limit, ball_min, ball_max, newsofday) {
	checkElement = elem;
	checkElements = razd+' a';
	checkElementLast = razd+' a:last[@class=active]';
	$(checkElements).attr('class', ''); 
	$(checkElement).attr('class', 'active');
	$(checkElementLast).attr('class', 'active leftlink');
	$(closeElement).hide('normal');
	$(showElement).show('normal');
	$(showElement).html('<div id="loading">Идет загрузка...</div>');
	$(showElement).load("/service/articles_of_day/", {daypath:daypath, template:template, limit:limit, ball_min:ball_min, ball_max:ball_max, newsofday:newsofday});
      return false;
}

  function showNewsOfDay(daypath, elem, template, template2, template3, razd, showElement, closeElement, limit, ball_min, ball_max, newsofday, limit2, ball_min2, ball_max2, newsofday2, limit3, ball_min3, ball_max3) {
	checkElement = elem;
	checkElements = razd+' a';
	checkElementLast = razd+' a:last[@class=active]';
	$(checkElements).attr('class', ''); 
	$(checkElement).attr('class', 'active');
	$(checkElementLast).attr('class', 'active leftlink');
	$(closeElement).hide('normal');
	$(showElement).show('normal');
	$(showElement).html('<div id="loading">Идет загрузка...</div>');
	$(showElement).load("/service/news_of_day/", {daypath:daypath, template:template, template2:template2, template3:template3, limit:limit, ball_min:ball_min, ball_max:ball_max, newsofday:newsofday, limit2:limit2, ball_min2:ball_min2, ball_max2:ball_max2, newsofday2:newsofday2, limit3:limit3, ball_min3:ball_min3, ball_max3:ball_max3});
      return false;
}



function BlindUp() {
	$("#print_bar").hide();
	}


function initCookie() {
    known = readCookie('ruskcom');
    alert(known);
    if (!known) 
        return false;
    if (is_array(known)) {
        for (element in known) {
            umiElement = 'a[umielement|="'+element+'"]';
            $(umiElement).addClass("ajaxlink");
        }
        return true;
    }
    else {
        umiElement = 'a[umielement|="'+element+'"]';
        $(umiElement).html('Строка! Уже поддержали!');
        return true;
    }
}

function forum_vote(comment_id,path) {
    known = readCookie('ruskcom');
//    alert(known);
    voteElement = '#votes_' + comment_id.toString();
    if (!known) 
        known = new Array();
    if (in_array(comment_id,known,false)) {
        votes_id = 'votes_' + comment_id.toString();         
        $(voteElement).html('Ваш голос уже учтен...');
        return false;
    }
    else {
        known.push(comment_id);
        array_unique(known);
        setCookie('ruskcom',known,10,path);
    }

    $(voteElement).html('Идет учет мнения...');    
    $(voteElement).load("/pokrov/vote_com.php", {comment_id:comment_id});
    return true;
}

function setCookie(name, value, days, path) {
    if (is_array(value)) {
        value = value.toString();
    }
        
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path="+path;
}

function eraseCookie(name) {
	setCookie(name,"",-1,"/");
}

function is_array( mixed_var ) {     
    return ( mixed_var instanceof Array );
}


function in_array(needle, haystack, strict) {	// Checks if a value exists in an array
	// 
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

	var found = false, key, strict = !!strict;

	for (key in haystack) {
		if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
			found = true;
			break;
		}
	}

	return found;
}

function array_unique( array ) {	// Removes duplicate values from an array
	// 
	// +   original by: Carlos R. L. Rodrigues

	var p, i, j;
	for(i = array.length; i;){
		for(p = --i; p > 0;){
			if(array[i] === array[--p]){
				for(j = p; --p && array[i] === array[p];);
				i -= array.splice(p + 1, j - p).length;
			}
		}
	}

	return true;
}


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) { 
            value = c.substring(nameEQ.length,c.length);
            value_arr = value.split(',');
            return value_arr;
        }
                
	}
    
	return null;
}

