var linkIcon = 1;
var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;
var clic = 0;
var dID;
var voteCnt = 0;

//hotNews timer
function InitializeTimer(){
	// Set the length of the timer, in seconds
	secs = 5
	StopTheClock()
	StartTheTimer()
}
	
function StopTheClock(){
	if(linkIcon==6) linkIcon = 1
	if(timerRunning) load(linkIcon++);
	timerRunning = false
}
	
function StartTheTimer(){
	if (secs==0){
	    StopTheClock()
	    secs = 4
	    StartTheTimer()
	}else{
	    self.status = secs
	    secs = secs - 1
	    timerRunning = true
	    timerID = self.setTimeout("StartTheTimer()", delay)
	}
}

//load hot news content
function load(number){
   var bID = "n_" + number;
   
   $('.e_news').addClass('donotshow');
   $('#'  + bID).removeClass('donotshow');
   
   $('.background').addClass('donotshow');
   $('#b_'  + number).removeClass('donotshow');
    
   $('.nav_b').removeClass('select');
   $('#' + number).addClass('select');
}

//voting function
function vote(type){
    if(voteCnt == 0){
          var cID = $(".content").attr("id");
          if(type == "+1"){
             var txt = parseInt($(".thumbUpCnt").text());
             var intTxt = txt+1;
          
             if(intTxt == 1){
                $(".thumbUpCnt").text(intTxt + " одобрение");
             }else{$(".thumbUpCnt").text(intTxt + " одобрения");}
          }else{
               var txt = parseInt($(".thumbDownCnt").text());
               var intTxt = txt+1;
               
               if(intTxt == 1){
                   $(".thumbDownCnt").text(intTxt + " неодобрение");
               }else{$(".thumbDownCnt").text(intTxt + " неодобрения");}
          }
          voteCnt = 1;
          $.get('ajaxvote.php', { "paperID" : cID,"type":type });
    }
          
}

//change font functions
function initFontSizeAndPrint() {

	$('.large').click(function(){
		var FSz = parseFloat($('.content').css('font-size'));
		if(FSz<22){FSz += 1;increaseDecreaseFontSize(FSz)}
		
	})
	$('.small').click(function(){
		var FSz = parseFloat($('.content').css('font-size'));
		if(FSz>16){FSz -= 1;increaseDecreaseFontSize(FSz)}	
		
	})
}

function increaseDecreaseFontSize(sz) {
	var lh = sz+4
	$('.middle .content').css('font-size',sz+'px')
	$('.middle .content').css('line-height',lh+'px')
	
	if(sz==16){$('.small').css({'cursor':'default'})}
	else{$('.small').css({'cursor':'pointer'})}
	if(sz==22){$('.large').css({'cursor':'default'})}	
	else{$('.large').css({'cursor':'pointer'})}	

}

function fontResize(increase){

	if(increase){
		
		var FSz = parseFloat($('.content').css('font-size'));
		if(FSz<19){FSz += 2;increaseDecreaseFontSize(FSz)};
		
	} else {
		
		var FSz = parseFloat($('.content').css('font-size'));
		if(FSz>14){FSz -= 2;increaseDecreaseFontSize(FSz)};
	}
}

$(document).ready(function(){
    //toggle the most
    $("#mostholder #the_most li").click(function(){    
        var cID = $("a",this).attr("id");
        
        $("#mostholder #the_most li a").removeClass();
        $("#mostholder #the_most li a").addClass("notselect");
        $("#" + cID).removeClass("notselect");
        $("#" + cID).addClass("select");
        
        $("#mostholder .content ul").addClass("notshow");
        
        $("#" + cID + "_a").removeClass("notshow");
    });
    
    //from filter.js
    $(".newsarticles")
        .css("opacity","1")
       .hover(function(){
           $(this).css("opacity","1");
       }, function() {
           $(this).css("opacity","1");
       })
   
   //from filter .js
   $(".filter").click(function(){
        var thisFilter = $(this).attr("id");
        $(".newsarticles").slideUp();
        $("."+ thisFilter).slideDown();
        $("#catpicker a").removeClass("current");
        $(this).addClass("current");
        return false;
   });
   
   //show submenu
   /* $("#main_menu li").hover(
        function(){ $("ul", this).fadeIn(); },
        function(){ $("ul", this).fadeOut(); }
    );
    
    $("#main_menu li ul li").hover(
        function(){
            $("div", this).fadeIn("fast");
            $("a",this).addClass("selected");
            $("div a",this).removeClass("selected");
            },
        function(){
            $("div", this).fadeOut("fast");
            $("a",this).removeClass("selected");
        }
    );*/
    
    initFontSizeAndPrint();
    
    $(".dropdown").hover(
	function(){$('ul',this).slideDown('fast');},
	function(){$('ul',this).slideUp('fast');}
    );
    
    $(".nav_b").click(
          function(){
                    var bID = $(this).attr("id");
                    
                    //get the next attribute for timer
                    linkIcon=bID;
		    
		    $('.e_news').addClass('donotshow');
		    $('#n_'  + bID).removeClass('donotshow');
		    
		    $('.background').addClass('donotshow');
		    $('#b_'  + bID).removeClass('donotshow');
    
		    $('.nav_b').removeClass('select');
		    $('#' + bID).addClass('select');
        });
   
});
//form validation

//check e-mail
function validate_email(field,alerttxt)
{
    with (field)
    {
        apos=value.indexOf("@");
        dotpos=value.lastIndexOf(".");
        if (apos<1||dotpos-apos<2)
        {alert(alerttxt);return false;}
         else {return true;}
    }
}

//check fields
function validate(field,alerttxt){
    with(field)
    {
        if(value== null || value =="")
         {alert(alerttxt); return false;}
    }
    return true;
}

//author validation
function validate_author_form(thisform)
{
    with (thisform)
    {
        if (validate_email(yourmail,"E-mail адресът, който сте въвели е невалиден!")==false)
            {yourmail.focus();return false;}
        if(validate(yourname,"Не е въведено име!")==false)
            {yourname.focus(); return false;}
        if(validate(yourmessage,"Не е въведено съобщение!")== false)
            {yourmessage.focus(); return false;}
    }
    
}

//main function
function validate_form(thisform)
{
    with (thisform)
    {
        if (validate_email(email,"E-mail адресът, който сте въвели е невалиден!")==false)
            {email.focus();return false;}
        if(validate(name,"Не е въведено име!")==false)
            {name.focus(); return false;}
        if(validate(message,"Не е въведено съобщение!")== false)
            {message.focus(); return false;}
    }
}
