var linkIcon = 1
var secs
var timerID = null
var timerRunning = false
var delay = 1000
var clic = 0
var voteCnt = 0

function playlist(){
	$(".videoPlayer .playlist").slideToggle('fast');
}

function comment_rate(commentId,vote){
	
	var count = parseInt($("div #"+commentId).text().replace('одобрения: ',''));

	$("div #"+commentId+" a").remove();
	$("div #"+commentId+" span").remove();
	
	$("div #"+commentId).append('<div class="disabled_buttons"></div>');
	if (vote == 'yes'){
		count = count + 1;
		$("div #"+commentId).append('<span>одобрения: '+count+'</span>');
	}
	if(vote == 'no'){
		count = count - 1;
		$("div #"+commentId).append('<span>одобрения: '+count+'</span>');
	}
	
	$.get('/getcatajax.php', { "commentID" : commentId,"vote": vote});
	
	return false;
}

function load(number){
   var bID = "news_" + number;
            //change img
            $("#hotNewsImages img").addClass("doNotShow");
            $("#hotNewsImages #img_" + bID).removeClass("doNotShow");
            
            //change title
            $("#hotNewsContent .titles div").addClass("doNotShow");
            $("#hotNewsContent #title_"+bID).removeClass("doNotShow");
            
            //change additional info
            $("#hotNewsContent .additional_info div").addClass("doNotShow");
            $("#hotNewsContent #add_"+bID).removeClass("doNotShow");
            
            //change buttons
            $("#hotNewsContent .buttons a").addClass("notSelect");
            $(" #hotNewsContent .buttons #"+bID).removeClass("notSelect");
            $(" #hotNewsContent .buttons #"+bID).addClass("select");
}
        
//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>14){FSz -= 1;increaseDecreaseFontSize(FSz)}	
		
	})
}

function increaseDecreaseFontSize(sz) {
	var lh = sz+4
	$('#left .content').css('font-size',sz+'px')
	$('#left .content').css('line-height',lh+'px')
	
	if(sz==14){$('.small').css({'cursor':'default'})}
	else{$('.small').css({'cursor':'pointer'})}
	if(sz==19){$('.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)};
	}
}

//voting function
function vote(type){
	if(voteCnt == 0){
		voteCnt = 1;
          var cID = $(".paper").attr("id");
          if(type == "+1"){
             var txt = parseInt($(".paper .thumbUpCnt").text());
             var intTxt = txt+1;
          
             if(intTxt == 1){
                $(".paper .thumbUpCnt").text(intTxt + " одобрение");
             }else{$(".paper .thumbUpCnt").text(intTxt + " одобрения");}
          }else{
               var txt = parseInt($(".paper .thumbDownCnt").text());
               var intTxt = txt+1;
               
               if(intTxt == 1){
                   $(".paper .thumbDownCnt").text(intTxt + " неодобрение");
               }else{$(".paper .thumbDownCnt").text(intTxt + " неодобрения");}     
          }
          
          $.get('/getcatajax.php', { "paperID" : cID,"type":type });
	}
          
}

$(document).ready(function(){
        $(".searchBar").click(function(){
          $("#options").removeClass();
        });
          
        $("#close_btn").click(function(){
          $("#options").addClass("doNotShow");
        });
          
        $("body div").hover(function(){
          $("li .fallmenu").remove();
        })  
          
        $("#submenu li").hover(
        function(){
                $("li .fallmenu").remove();
                
                // get main cat ID
                var cID = $(this).attr("id");
                
                // call php function to retrieve cat html
                $.get('/getcatajax.php', { "catID" : cID }, function(data) {
                        $("li#" + cID).append(data);
                        $("li#" + cID + " .fallmenu").fadeIn("fast");
                }); 
        },
        function(){
                $("li .fallmenu").remove();
        }
    );
        $("#theMost .subButtons li").click(
          function(){
               //get button id 
               var bID = $("a",this).attr("id");
               var arrow = bID.substr(9);
               
               //move arrow
               $("#theMost .subButtons #smallArrow").removeClass();
               $("#theMost .subButtons #smallArrow").addClass(arrow);
               
               //display content     
               $("#theMost .middle ul").addClass("doNotShow");
               $("#theMost .middle #" + bID + "_submenu").removeClass("doNotShow");
               
          }
          );
        
        $("#theMost .header li").click(
        function(){
          //get button id and position arrow
          var bID = $(this).attr("id");
          $("#theMost .subButtons #smallArrow").removeClass();
          
          //show selected button
          $("#theMost .header li").addClass("notSelect");
          $(this).removeClass("notSelect");
          $(this).addClass("select");
          
          //show the needed submenu
          $("#theMost .subButtons  ul").addClass("doNotShow");
          $("#theMost .subButtons #" + bID + "_buttons" ).removeClass("doNotShow");
          
          //show the info
          $("#theMost .middle div").addClass("doNotShow");
          $("#theMost .middle #" + bID +"_menu").removeClass("doNotShow");
          $("#theMost .middle #" + bID +"_menu ul").addClass("doNotShow");
          $("#theMost .middle #" + bID +"_today_submenu").removeClass("doNotShow");
        }           
        );
        
        initFontSizeAndPrint();
        
        $("#outerRss .header li").click(
        function(){
             var bID = $(this).attr("id");
             
             $("#outerRss .header li").addClass("notSelect");
             $(this).removeClass("notSelect");
             $(this).addClass("select");
             
             $("#outerRss .middle div").addClass("doNotShow");
             $("#outerRss .middle #" + bID + "_info").removeClass("doNotShow");
             
        }
        );
        
        $(".rlabel").click(function(){
                    // for IE sake explicitly check the radio button
                    $("#" + $(this).attr("for")).attr("checked", "checked");
                    
                    $(".rlabel").removeClass("rlabelsel");
                    $(this).addClass("rlabelsel");
                });
        
        $("#hotNewsContent .buttons a").click(
          function(){
                    var bID = $(this).attr("id");
                    
                    //get the next attribute for timer
                    linkIcon=bID.substr(5);
                    
                    //change img
                    $("#hotNewsImages img").addClass("doNotShow");
                    $("#hotNewsImages #img_" + bID).removeClass("doNotShow");
                    
                    //change title
                    $("#hotNewsContent .titles div").addClass("doNotShow");
                    $("#hotNewsContent #title_"+bID).removeClass("doNotShow");
                    
                    //change buttons
                    $("#hotNewsContent .buttons a").addClass("notSelect");
                    $(" #hotNewsContent .buttons #"+bID).removeClass("notSelect");
                    $(" #hotNewsContent .buttons #"+bID).addClass("select");
        });
        
        $("#arrowDown").click(
         function(){
            if(clic == 0){
               $(".videoLink").animate(
               {top: '-=232'},function(){clic = 1});
            }
        });
         $("#arrowUp").click(
         function(){
            if(clic == 1){
               $(".videoLink").animate(
               {top: '+=232'},function(){clic = 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)
		}
	}
        
//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;
}

//check antispam
function anti_spam(field_1,field_2,alerttxt){
    
    var real= field_1.value;
    var hid = field_2.value;
    
    if(real!=(hid*3-159))
    {alert(alerttxt);  return false;}
    
   return true; 
}

//main function
function validate_form(thisform)
{
    with (thisform)
    {
        if (validate_email(email,"E-mail адресът, който сте въвели е невалиден!")==false)
            {email.focus();return false;}
        if(validate(from,"Не е въведено име!")==false)
            {from.focus(); return false;}
        if(validate(about,"Не е въведено заглавие на съобщението!")== false)
            {about.focus(); return false;}
        if(validate(comment,"Не е въведено съобщение!")== false)
            {comment.focus(); return false;}
	if(anti_spam(val,cs,"Кодът който сте въвели е грешен!")==false)
	    {val.focus(); return false;}
    }
}
