$(document).ready(function() {

    function addMega(){   
        $(this).addClass("hovering");   
    }   
    
    function removeMega(){   
        $(this).removeClass("hovering");   
    } 
    
	var megaConfig = {
         interval: 100,
         sensitivity: 2,
         over: addMega,
         timeout: 100,
         out: removeMega
    };
 
    $("li.mega").hoverIntent(megaConfig);
	
	$('#searchbox').click(function(){
		if($(this).val()=='търсене в Ka6tata.com') {
			$(this).val('');
		}
	});
	
	$('#searchbox').focusout(function(){
		if($(this).val()=='') {
			$(this).val('търсене в Ka6tata.com');
		}
	});
	
	
		
	var loadInIframeModal = function(hash) {
		
		var $trigger = $(hash.t);
	
		var $modal = $(hash.w);
	
		var myUrl = $trigger.attr('href');
	
		var myTitle= $trigger.attr('title');
	
		var $modalContent = $("iframe", $modal);
		
		$modalContent.html('').attr('src', myUrl);

		//let's use the anchor "title" attribute as modal window title
	
		$('#jqmTitleText').text(myTitle);
	
		$modal.show();

	
	};
	
	var closeIfameModal = function(hash) {

		var $modal = $(hash.w);
		var $modalContent = $("iframe", $modal);
		
		$modalContent.html('').attr('src', 'http://localhost/ka6tata/httpdocs/index/blank');
		
		$modal.hide();
		hash.o.remove();
	};
	
	// initialise jqModal
	
	$('#dialog').jqm({
		modal: true,
		trigger: 'a.thickbox',
		target: '#jqmContent',
		onShow:  loadInIframeModal,
		onHide:  closeIfameModal
	});



});

//comments functions
function commentanswer(commentid,commentnumber,userid,username)
{
	var baseUrl = $('#baseUrl').val();
	//alert(commentid);
	//alert(commentnumber);
	//alert(userid);
	//alert(username);
	if(commentnumber<10) {
		commentnumber = "0"+commentnumber;
	}
	$('a#c-a-n').attr('href','#comment-'+commentid);
	$('a#c-a-n').text('{'+commentnumber+'}');
	$('a#c-a-u').attr('href',baseUrl + '/account/profile/userid/'+userid);
	$('a#c-a-u').attr('title','Профил на '+ username);
	$('a#c-a-u').text(username);
	$('#answer').css('display','block');
	$('#comment_answer_id').val(commentid);
}

function delcommentanswer()
{
	$('#answer').css('display','none');
	$('#comment_answer_id').val(0);
}

function deletecomment(commentid)
{
	var baseUrl = $('#baseUrl').val();
	
	var html = '';
	html += '<div class="actions">';
	html += '&nbsp;';
	html += '</div>';
	html += '<p>Този коментар беше изтрит от (неговия автор / администратор на сайта, заради неуместно и/или обидно съдържание)</p>';
					
	$('#comment-container-'+commentid).html(html);
	
	$.post(baseUrl + '/magazine/deletecommentajax', { commentid:commentid },
		function(data){
		
	}, "json");
}

function commentvote(commentid,vote)
{
	var baseUrl = $('#baseUrl').val();
	
	if(vote==0) {
		$("#actions-container-"+commentid).html('<p>Коментарът е докладван на администраторите.</p>');
		$.post(baseUrl + '/magazine/reportcommentajax', { commentid:commentid,vote:vote },
			function(data){	
		}, "json");
	} else {
		$.post(baseUrl + '/magazine/votecommentajax', { commentid:commentid,vote:vote },
			function(data){	
		}, "json");
		
		if(vote==1) {
			var vf = parseInt($('#votes-for-'+commentid).text());
			vf = vf+1;
			$('#votes-for-'+commentid).text(vf);
		} else if(vote==2) {
			var va = parseInt($('#votes-against-'+commentid).text());
			va = va+1;
			$('#votes-against-'+commentid).text(va);
		}
		
		$('#actions-buttons-'+commentid).css('display','none');
	}
}

function subscribeuser(articleid)
{
	var baseUrl = $('#baseUrl').val();
	
	var result = $('#article_email_subscription').is(':checked');
	//var result = $('#article_email_subscription').attr('checked'); 
	var s;
	if(result) {
		s = 1;
		$('#article-subscription-msg').text('Успешно се абонирахте за новите коментари по тази тема.');
	} else {
		s = 0;
		$('#article-subscription-msg').text('Успешно прекратихте абонамента си за новите коментари по тази тема. ');
	}
	
	$.post(baseUrl + '/magazine/subscribearticleajax', { articleid:articleid,subscribe:s },
		function(data){	
	}, "json");
}



//function add and remove firms 

function addItem() {
  source = document.addcompany.elements['l_category[]'];
  goal = document.addcompany.elements['r_category[]'];
  hidden = document.addcompany.categories;
  if ( (source.options[source.selectedIndex].value != '') && (!(source.options[source.selectedIndex].disabled)) ) {
    goal.options[goal.options.length] = new Option (source.options[source.selectedIndex].text, source.options[source.selectedIndex].value );
	//alert(source.options[source.selectedIndex].value);
	hidden.value = hidden.value + source.options[source.selectedIndex].value + ";";
	source.options[source.selectedIndex].disabled = true;
    source.options[source.selectedIndex].style.color = '#C0c0c0';
    source.options[source.selectedIndex].selected = false;
	
	
  }
}

function removeItem() {
  source = document.addcompany.elements['r_category[]'];
  goal = document.addcompany.elements['l_category[]'];

  for (i = 0; i < (goal.length); i++) {
    if (goal.options[i].value == source.options[source.selectedIndex].value) {
	  removeFromArray(source.options[source.selectedIndex].value);
      goal.options[i].disabled = false;
      goal.options[i].style.color = '';
    }
  }
  source.options[source.selectedIndex] = null;
}

function removeFromArray(categoryid)
{
	hidden = document.addcompany.categories;
	var newvalues="";

	var values = hidden.value.split(";");
	for(var i=0; i<values.length-1; i++)
	{
		if(values[i]!=categoryid)
		{
			newvalues +=values[i] + ";";
		}
	}
	
	hidden.value = newvalues;

}

//end function add and remove firms 
