 $(document).ready(function() {
 	
  $('.box .content').append('<div class="corner top"></div><div class="corner bottom"></div>');
 
  fancyForm('body');
  
  $('.checkboxes .boxes .main input').click(function(){ $(this).parent('.main').next('.sub').children('input').attr('checked', $(this).is(':checked')); });
 	
  /*$('#categories dt span').click(function() {
   $(this).parent('dt').next('dd').toggle('fast');
  });*/
  
  $('.filter .selected').click(function() {
  	$box = $(this).next('.content');
  	if($box.is(":hidden")) { $box.slideDown('fast'); } else { $box.slideUp('fast'); }
  });

  $('#searchselect').click(function() {
  	if($('#searchoptions').is(":hidden")) { $('#searchoptions').slideDown('fast'); } else { $('#searchoptions').slideUp('fast'); }
  });
  
  $('input[title]').each(function() {
   if($(this).val() === '') { $(this).val($(this).attr('title').toUpperCase()); }
   $(this).focus(function() { if($(this).val() === $(this).attr('title').toUpperCase()) { $(this).val(''); } });
   $(this).blur(function() { if($(this).val() === '') { $(this).val($(this).attr('title').toUpperCase()); } });
  });
  
  $('form').submit(function() { 
   $(this).find('input[title]').each(function() { if($(this).val() === $(this).attr('title').toUpperCase()) { $(this).val(''); } });
  });
  
  $('#menu .login_btn').click(function() {
  	$(this).toggleClass('login_btn_open');
  	if($('#index .login').is(":hidden")) { $('#index .login').show(); } else { $('#index .login').hide(); }
  });
  
  $('#menu .tools_btn').click(function() {
  	$(this).toggleClass('tools_btn_open');
  });
  
  $("#start_date").datepicker({dateFormat: 'yy-mm-dd'});
  $("#end_date").datepicker({dateFormat: 'yy-mm-dd'});
  
  
  if(1) {
   newsSliderShowItem($('#newsitem1'));
   
   var slider = setInterval('newsSlider(0)', 5000); 
   
   $('#newsslider .list li').mouseenter(function(){ clearInterval(slider); newsSliderShow(this); });
  }
  
  $('.tests_intro .handle').click(function(){
  	$(this).siblings('.filter').toggle();
  });
  
  //quizzEvent();
  
 });
 
 function quizzEvent()
 {
	$('.quiz_group_question').click( function(){ $('#group_question_' + this.id).toggle(500); return false; } );
	answersEvent();
	groupsEvent();
 }
 
 function answersEvent()
 {
	$('.answer_text').unbind('blur', addNewAnswer);
	$('.answer_text:last').blur(addNewAnswer);
 }
 
 function addNewAnswer(e)
 {
	if(!this.value) return;
	var copy = this.cloneNode(true);
	copy.id = copy.name = (parseInt(this.id) + 1) + 'answer';
	copy.value = '';
	
	var lcopy = $("label[for='" + this.id + "']").get(0)
	{
		lcopy = lcopy.cloneNode(true);
		$(lcopy).attr('for', copy.id);
		$('span', lcopy).remove(); 
	}
	
	$(this.parentNode).append("<br/>").append(lcopy).append(copy);
	answersEvent();
 }
 
function groupsEvent()
 {
	$('.group_select').unbind('blur', addNewGroup);
	$('.group_select:last').blur(addNewGroup);
 }
 
 function addNewGroup(e)
 {
	if(!this.value) return;
	var copy = this.cloneNode(true);
	copy.id = copy.name = (parseInt(this.id) + 1) + 'group';
	copy.selectedIndex  = 0;
	
	var lcopy = $("label[for='" + this.id + "']").get(0);
	if(lcopy)
	{
		lcopy = lcopy.cloneNode(true);
		$(lcopy).attr('for', copy.id);
		$('span', lcopy).remove(); 
	}
	
	var ncopy = $("#" + this.id + "_num").get(0);
	if(ncopy = $("#" + this.id + "_num").get(0))
	{
		ncopy = ncopy.cloneNode(true);
		$(ncopy).attr('id', copy.name + "_num");
		$(ncopy).attr('value', '');
		$(ncopy).attr('name', copy.name + "_num");
	}
	
	$(this.parentNode).append("<br/>").append(lcopy).append(copy).append(ncopy);
	groupsEvent();
 }
 
 function fancyForm(element) {
  $(".fancyform .checkboxfield").each( function() {
    $(this).wrap('<div class="checkboxlist"><div class="checkboxes"></div></div>').addClass('boxes').removeClass('checkboxfield');
    $(this).parent().parent().prepend('<div class="holder"><div class="content">' + $(this).attr('title') + '</div><div class="select"></div></div>');
    $(this).parent('.checkboxes').append('<div class="bottom"></div>');
   }
  );
  
  $('#newwindow').click( function() {
  	 $(this).parents('form').attr('target', ($(this).is(':checked') ? '_blank' : '_self'));
  	} );
  	
  $('input[name="categories[]"]').click( function() {
  	
  	 var maincat = $(this);
  	 
  	 $('input[name="children_' + $(this).attr('id') + '[]"]').each( function() {
  	 	 $('input[id="categories_' + $(this).val() + '"]').attr('checked', maincat.is(':checked'));
  	 	} );
  	} );
  
  handleCheckBoxlist(element);
 }
 
 function handleCheckBoxlist(element) {
  $(element + ' .checkboxlist .select').click(function() {
  	$box = $(this).parent('.holder').next('.checkboxes');
  	if($box.is(":hidden")) { $box.slideDown('fast'); } else { $box.slideUp('fast'); }
  });
  $(element + ' .checkboxlist .select').blur(function() {
  	//$(this).parent('.holder').next('.checkboxes').slideUp('fast');
  });
 }


 var newsSliderLast;
 var newsSliderCurrent;
 
 function newsSlider(index) {
  
  newsSliderLast = $('#newsslider li.activeitem').length ? $('#newsslider li.activeitem') : $('#newsslider li:first');
  newsSliderCurrent = (newsSliderLast.next().length) ? newsSliderLast.next() : $('#newsslider li:first');
  
  newsSliderHideItem(newsSliderLast);
  newsSliderShowItem(newsSliderCurrent);
 }
 
 function newsSliderShow(item) {
 	newsSliderHideItem(newsSliderCurrent);
 	newsSliderShowItem($('#' + item.id.replace('small', '')));
 }
 
 function newsSliderHideItem(item) {
  item.fadeOut();
  item.removeClass('activeitem');
  $('#' + item.attr('id') + 'small').removeClass('active');
 }
 
 function newsSliderShowItem(item) {
  newsSliderCurrent = item;
  newsSliderCurrent.fadeIn();
  newsSliderCurrent.addClass('activeitem');
  $('#' + newsSliderCurrent.attr('id') + 'small').addClass('active');
 }

