/******* Text Box Hints START ********/
/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {
	$.fn.hint = function (blurClass) {
	    if (!blurClass) blurClass = 'blur'; 	
	    return this.each(function () {
	        var $input = $(this),
	            title = $input.attr('title'),
	            $form = $(this.form),
	            $win = $(window);
	        function remove() {
	            if (this.value === title && $input.hasClass(blurClass)) {
	                $input.val('').removeClass(blurClass);
	            }
	        }
	        // only apply logic if the element has the attribute
	        if (title) { 
	            // on blur, set value to title attr if text is blank
	            $input.blur(function () {
	                if (this.value === '') {
	                    $input.val(title).addClass(blurClass);
	                }
	            }).focus(remove).blur(); // now change all inputs to title            
	            // clear the pre-defined text when form is submitted
	            $form.submit(remove);
	            $win.unload(remove); // handles Firefox's autocomplete
	        }
	    });
	};
})(jQuery);

$(function(){
	// elements with class 'blur'
	$('.blur').hint();
});
/******* Text Box Hints END ********/

/******* Tabs START ********/
		$(document).ready(function(){
			$('div#caTabs > div').hide(); // Hide all divs
			$('div#caTabs > div:first').show(); // Show the first div
			$('div#caTabs ul li a:first').addClass('select'); // Set the class of the first link to active
			$('div#caTabs ul li a').click(function(){ //When any link is clicked
				$('div#caTabs ul li a').removeClass('select'); // Remove active class from all links
				$(this).addClass('select'); //Set clicked link class to active
				var currentTab1 = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
				$('div#caTabs > div').hide(); // Hide all divs
				$(currentTab1).show(); // Show div with id equal to variable currentTab
				return false;
			});return false;
		});
		$(document).ready(function(){
			$('div#lcmTabs > div').hide(); // Hide all divs
			$('div#lcmTabs > div:first').show(); // Show the first div
			$('div#lcmTabs ul li a:first').addClass('select'); // Set the class of the first link to active
			$('div#lcmTabs ul li a').click(function(){ //When any link is clicked
				$('div#lcmTabs ul li a').removeClass('select'); // Remove active class from all links
				$(this).addClass('select'); //Set clicked link class to active
				var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
				$('div#lcmTabs > div').hide(); // Hide all divs
				$(currentTab).show(); // Show div with id equal to variable currentTab
				return false;
			});
		});
		$(document).ready(function(){
			//$('div.bpTabs > div').hide(); // Hide all divs
			//$('div.bpTabs > div:first').show(); // Show the first div
			$('div.bpTabs ul li a:first').addClass('select'); // Set the class of the first link to active
			$('div.bpTabs ul li a').click(function(){ //When any link is clicked
				$('div.bpTabs ul li a').removeClass('select'); // Remove active class from all links
				$(this).addClass('select'); //Set clicked link class to active
				//var currentTab1 = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
				//$('div.bpTabs > div').hide(); // Hide all divs
				//$(currentTab1).show(); // Show div with id equal to variable currentTab
				return false;
			});return false;
		});
/******* Tabs END ********/

/******* Change Font size START ********/
		$(document).ready(function(){
			// Reset Font Size
			var originalFontSize = $('#fullArticle').css('font-size');
			$("#resetFont").click(function(){
				$('#fullArticle').css('font-size', originalFontSize);
                $('#fullArticle p').css('font-size', originalFontSize);
				return false;
			});
			// Increase Font Size
			$("#plusFont").click(function(){
				var currentFontSize = $('#fullArticle').css('font-size');
				var currentFontSizeNum = parseFloat(currentFontSize);
				var newFontSize = currentFontSizeNum + 1;
				$('#fullArticle').css('font-size', newFontSize);
                $('#fullArticle p').css('font-size', newFontSize);
				return false;			
			});
			// Decrease Font Size
			$("#minusFont").click(function(){
				var currentFontSize = $('#fullArticle').css('font-size');
				var currentFontSizeNum = parseFloat(currentFontSize);
				var newFontSize = currentFontSizeNum - 1;
				$('#fullArticle').css('font-size', newFontSize);
                $('#fullArticle p').css('font-size', newFontSize);
			return false;
			});
		});
/******* Change Font size END ********/

/******* Share & Print START ********/
		function fbs_click() {
			u = location.href;
			t = document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&amp;t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
			return false;
		}
		$(document).ready(function(){
			$('.facebook').click(function(){
				return fbs_click();
			});
		});
		
		$(document).ready(function(){
			$('.print').click(function(){
				print();return false;
			});
		});
/******* Share & Print END ********/

/******* SMOOTH SCROLLING START ********/		
		$(document).ready(function(){
			$('.goTop a[href*=#]').click(function() {
				if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
					var $target = $(this.hash);
					$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
					if ($target.length) {
						var targetOffset = $target.offset().top;
						$('html, body').animate({scrollTop: targetOffset}, 1000);
						return false;
					}
				}
			});
		});
/******* SMOOTH SCROLLING END ********/

/******* FORM LIMITATION START *********/
		function limitChars(textid, limit, infodiv){
			 var text = $('#'+textid).val(); 
			 var textlength = text.length;
			 if(textlength > limit){
				$('#' + infodiv).html('Не можете да пишете повече от '+limit+' символа!');
				$('#'+textid).val(text.substr(0,limit));
				return false;
			}
			else{
				$('#' + infodiv).html('Имате още '+ (limit - textlength) +' позволени символа');
				return true;
			}
		}	
		// start limitation
		$(function(){
			$('#userComment').keyup(function(){
				limitChars('userComment', 2000, 'charlimitinfo');
			})
		});
/******* FORM LIMITATION END *********/

/******* USEFUL LINKS START ********/
		$(document).ready(function(){
			$('.ulNav ul li').hover(
				function () {
					$(this).addClass('activeHover');
				}, 
				function () {
					$(this).removeClass('activeHover');		
				}
			)	
		});
/******* USEFUL LINKS END ********/

/******* POLL START ********/
function ShowPoll( action, pollId ) {

	var pollElement = $('#surveyContent');
	
	// Chech for selected answer
	if(action == 'answer'){
		
		for (var i=0; i < document.pollForm.answer.length; i++){
			
			if (document.pollForm.answer[i].checked) var selectedAnswer = document.pollForm.answer[i].value;
		}
		
	}
	
	//send request
	$.post("ajaxResponder.php", { polls: "1", action: action, pollId: pollId, answerId: selectedAnswer },
		  function(data){
		    pollElement.html(data);
	});
}
/******* POLL END ********/

function ShowTop5( category ){
	
	//show loding bar 
	$('#Top5Content').html('<div style="width:637px; height:19px; text-align:center; padding: 30px 0;"><img src="images/ajax-loader.gif" alt="" /></div>');
	
	//send request
	$.post("ajaxResponder.php", { show: "top5", category: category },
		  function(data){
		    $('#Top5Content').html(data);
	});	
}

function FrmSubmit() {
		var isErrors = 0;
		var elem = document.frm1.elements;
		for(var i = 0; i < elem.length; i++){
			if (elem[i].lang == 'notnull' ){
				
				if (elem[i].value == "") { elem[i].style.background = '#EECCBF'; isErrors += 1;}
				else { elem[i].style.background = '#FFFFFF'; }
			}
			if (elem[i].lang == 'numbers' ){
				if (elem[i].value == "" || checkNumeric(elem[i].value)){
					elem[i].style.background = '#EECCBF';
					isErrors += 1;
				}
				else { elem[i].style.background = '#FFFFFF'; }
			}
			if (elem[i].lang == 'testall' ){
				if(elem[i].value == "all") elem[i].name = "";
			}
		}
		
		// if no errors submit form

		if(isErrors == 0) { document.frm1.submit(); }
	}
	
	function checkNumeric(value){

      var anum=/(^\d+$)|(^\d+\.\d+$)/
  
      if (anum.test(value)) return false;
      else return true;
	}
/******* COMMENTS VOTE START *********/
function commentVote( comment, vote, rating, votes ) {
	
	if(vote == 'up') rating +=1;
	else rating -=1;
	
	//send request
	$.post("ajaxResponder.php", { cVote: "1", comment: comment, vote: vote });
	
	// change values
	$('#rating'+comment).html(rating);
	$('#votes'+comment).html(votes+1);
	$('#userVote'+comment).html('');
}
/******* COMMENTS VOTE END *********/

function delComment( cId , aId){
	
	var answer = confirm ("Сигурен ли си че искаш да изтриеш коментара?");

	if (answer){
		
		$.post("ajaxResponder.php", { delComment: cId, articleId: aId },
			  function(data) {
			  	
					$('#comment'+cId).css("background","#fb6c6c");
					$('#comment'+cId).slideUp(1000,function() {
					            $('#comment'+cId).remove();
          });

			});
	}
}

function isNumberKey(evt){
         var charCode = (evt.which) ? evt.which : event.keyCode
         if(charCode == 44) return true;
         if(charCode == 46) return true;
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
}

function CalcSubmit() {
		var isErrors = 0;
		var elem = document.frm1.elements;
		var url = '';
		
		for(var i = 0; i < elem.length; i++){
			if (elem[i].lang == 'notnull' ){
				
				if (elem[i].value == "") { elem[i].style.background = '#EECCBF'; isErrors += 1;}
				else { elem[i].style.background = '#FFFFFF'; }
			}
			if (elem[i].lang == 'numbers' ){
				if (elem[i].value == "" || checkNumeric(elem[i].value)){
					elem[i].style.background = '#EECCBF';
					isErrors += 1;
				}
				else { elem[i].style.background = '#FFFFFF'; }
			}
			if (elem[i].lang == 'testall' ){
				if(elem[i].value == "all") elem[i].name = "";
			}
			
			if(elem[i].name != ""){
				url += '&' + elem[i].name + '=' + elem[i].value;
			}
			
		}
					
		// if no errors submit form

		if(isErrors == 0) { 
				
				$("#CalcResult").addClass('moreCalc');
				document.getElementById("CalcResult").innerHTML = '<div class="loader"><image src="images/ajax-loader2.gif" alt="" /></div>';
				
				$.post("ajaxResponder.php", { calc: "1", url: url },
					  function(data){
					    document.getElementById("CalcResult").innerHTML = data;
				});
		}
	}
