// Common JavaScript Codes For All Pages

$(function () {
	// - footer
	var layout = function () 
	{
		var wh = $(window).height() - $('#header').height() - $('#footer').height() - 2;
		
		var ch = $('#contents').height();
		if (ch < wh) $('#contents').css('height', wh);
	}

	layout();
	window.onresize = layout;

	// - logomenu
	$('#logo').click(function () {
		$('#logo').toggleClass('opened');
		if ($('#logo').hasClass('opened')) $('#logoMenu').fadeIn('fast'); else $('#logoMenu').fadeOut('fast')
		return false;
	});
	
	// - hovering related links
/*	
	$('.category a').mouseover(function(){
		$(this).offsetParent().find('a').addClass('hover');
	});
	
	$('.category a').mouseout(function(){
		$(this).offsetParent().find('a').removeClass('hover');
	});

	$('.topic .videoColumn a').mouseover(function(){
		$(this).offsetParent().find('a').addClass('hover');
	});
	
	$('.topic .videoColumn a').mouseout(function(){
		$(this).offsetParent().find('a').removeClass('hover');
	});

	$('.question a').mouseover(function(){
		$(this).offsetParent().find('a').addClass('hover');
	});
	
	$('.question a').mouseout(function(){
		$(this).offsetParent().find('a').removeClass('hover');
	});

	$('.topicBlock a').mouseover(function(){
		$(this).offsetParent().find('a').addClass('hover');
	});
	
	$('.topicBlock a').mouseout(function(){
		$(this).offsetParent().find('a').removeClass('hover');
	});
*/	

	$('.contentBarHead a').click(function(){
		$(this).parent().next().slideToggle('fast');
		if($(this).html() == 'HIDE'){
			$(this).html('SHOW');
		} else {
			$(this).html('HIDE');
		}
		return false;
	});
	
	//textarea character count
	$('textarea[maxlength]').keyup(function(){
	var max=parseInt($(this).attr('maxlength'));
	if($(this).val().length>max)$(this).val($(this).val().substr(0,$(this).attr('maxlength')));
		$(this).parent().find('.charsRemaining').html((max-$(this).val().length)+' characters remaining');
	});
	
	//register checkbox on askQuestionForm
	
	$('#askQuestionForm .regCheckBox').click(function(){
		$('#askQuestionForm  .register').toggle();
		layout();
		$('#askQuestionForm  .register').find('input').toggleClass('req');
	});
	
	$('input').checkBox();
	
	// tools js
	
	$('#toolSearch').click(function(){
		closeTools();
		$('#searchPopup').show();
		$('#searchPopup .close').click(function(){
			$(this).parent().hide();
		});
		
		return false;
	});
	
	$('#toolMember').click(function(){
		closeTools();
		
		if ($('#loginPopup').length > 0)
		{
			$('#loginPopup').show();

			$('#loginPopup .close').click(function(){
				$(this).parent().hide();
			});		
		}
		
		else
		{
			$('#logoutPopup').show();

			$('#logoutPopup .close').click(function(){
				$(this).parent().hide();
			});	
		}
		
		return false;
	});
	

	// PopupSelectBox
	$('.PopupSelectBox').click(function(event){	
		$(this).find('.optionsList').toggle();					
		event.stopPropagation();
	});
	

	$('.PopupSelectBox').bind('clickoutside',function(event){
		if($(this).find('.optionsList:visible').size()>0)$(this).trigger('click');
	});
	

	$(document).bind('click', function(e){
        var $clicked = $(e.target);
        if (!($clicked.is('.PopupSelectBox') || $clicked.parents().is
('.PopupSelectBox'))) {
         	$('.PopupSelectBox').find('.optionsList').hide();
        }
    });
	

	$('.PopupSelectBox').each(function(event){
		$('<input type="hidden" value="" />').attr('id',$(this).attr('id')).attr('name',$(this).attr('id')).appendTo(this);
		$(this).attr('id','Container_'+$(this).attr('id'));
		
		if($(this).find('.Value').text()=='-'){
			$(this).find('.Value').text($(this).find('.optionsList a:first').text());
			$(this).find('input').val($(this).find('.optionsList a:first').attr('id'));
		}
		
		else
			$(this).find('input').val($(this).find("a.SmallArrow").attr("rel"));
	});
	
	$('.PopupSelectBox .optionsList a').click(function(event){
		$(this).parents('.PopupSelectBox').find('.Value').text($(this).text());
		$(this).parents('.PopupSelectBox').find('.optionsList').hide();
		$(this).parents('.PopupSelectBox').find('input').val($(this).attr('href').replace(/^.*#/,''));

		return false;
	});
	
	// for fronpage settings effect
	$('#Container_frontpageForm-NumQuestion01 .optionsList a').click(function(){
		qType = $('#Container_frontpageForm-QuestionType01').find('.Value').text();
		if(qType=='Custom'){
			$('.selectCats01 .cat').hide();
			numCats = $(this).text(); 
			$('.selectCats01 .cat:lt('+ numCats +')').show();
		}
	});
	
	$('#Container_frontpageForm-QuestionType01 .optionsList a').click(function(){
		qType = $(this).text();
		if(qType=='Custom'){
			$('.selectCats01 .cat').hide();
			numCats = $('#Container_frontpageForm-NumQuestion01').find('.Value').text();
			$('.selectCats01 .cat:lt('+ numCats +')').show();
			$('.selectCats01').show();	
		} else {
			$('.selectCats01').hide();
		}
		
	});
	
	$('#Container_frontpageForm-NumQuestion02 .optionsList a').click(function(){
		qType = $('#Container_frontpageForm-QuestionType02').find('.Value').text();
		if(qType=='Custom'){
			$('.selectCats02 .cat').hide();
			numCats = $(this).text(); 
			$('.selectCats02 .cat:lt('+ numCats +')').show();
		}
	});
	
	$('#Container_frontpageForm-QuestionType02 .optionsList a').click(function(){
		qType = $(this).text();
		if(qType=='Custom'){
			$('.selectCats02 .cat').hide();
			numCats = $('#Container_frontpageForm-NumQuestion02').find('.Value').text();
			$('.selectCats02 .cat:lt('+ numCats +')').show();
			$('.selectCats02').show();	
		} else {
			$('.selectCats02').hide();
		}
		
	});
	
	$('#Container_frontpageForm-NumQuestion03 .optionsList a').click(function(){
		qType = $('#Container_frontpageForm-QuestionType03').find('.Value').text();
		if(qType=='Custom'){
			$('.selectCats03 .cat').hide();
			numCats = $(this).text(); 
			$('.selectCats03 .cat:lt('+ numCats +')').show();
		}
	});
	
	$('#Container_frontpageForm-QuestionType03 .optionsList a').click(function(){
		qType = $(this).text();
		if(qType=='Custom'){
			$('.selectCats03 .cat').hide();
			numCats = $('#Container_frontpageForm-NumQuestion03').find('.Value').text();
			$('.selectCats03 .cat:lt('+ numCats +')').show();
			$('.selectCats03').show();	
		} else {
			$('.selectCats03').hide();
		}
		
	});
	
	$('input[type=text]').click(function(){
		var defaulttxt = $(this).attr('alt');
		
		if(defaulttxt == $(this).val()){
			$(this).val("");
		}
	});
	
	$('input[type=text]').focusout(function(){
		var defaulttxt = $(this).attr('alt');
		if($(this).val() == ''){
			$(this).val(defaulttxt);
		}
	});
		
	$('.validateForm').submit(function(){		
		$('.errorMessage').hide();
		$(this).find('.req').each(function(index) {
			if($(this).val() == ''){
				var errmsg = $(this).next('.errmsg').html();
				
				if(errmsg){
					$('.errorMessage').html(errmsg);
					$('.errorMessage').show();				
					return false;
				}
			} else {
				if($(this).attr('name') == 'email'){
					if(!validEmail($(this).val())){	
						$('.errorMessage').html('Please enter a valid email address.');
						$('.errorMessage').show();		
						return false;
					}
				}
				
				if($(this).attr('alt') == $(this).val()){
					var errmsg = $(this).next('.errmsg').html();
				
					if(errmsg){
						$('.errorMessage').html(errmsg);
						$('.errorMessage').show();				
						return false;
					}
				}
			}
		});
  
		return false;
	});
	
	//function for adding and removing categories
	$('.flexicats').find('.PopupSelectBox').hide();
	$('.flexicats').find('.clear').hide();
	$('.flexicats').find('.extra').hide();	
	$('.flexicats ').find('.PopupSelectBox:lt(1)').show();
	$('.flexicats').find('#add-cat-0:lt(1)').show();
	$('.flexicats').find('.clear-cat-0').show();
	
	$('.extra').click(function(){
		linkId = $(this).attr('id');
		
		// if user click add another dropdown
		if(linkId.substr(0,3)=='add'){
			$(this).hide();
			$(this).prev().show();
			//add category
			var counter = 0;
			var pass = 0;
			var last = 0;
			for(i=0;i<=5;i++){
				if(!$('#Container_cat-' + i).is(':visible')){
					if(!pass){
						$('#Container_cat-' + i).show();
						$('#remove-cat-' + i).show();
						$('.clear-cat-' + i).show();
						pass = i + 1;
					}
					
				} else {
					counter++;
					last = i;
				}
			}
			
			if(counter < 5){
				//show add another link to the last category dropdown
				last = (last > (pass - 1)) ? last : pass-1;
				$('#add-cat-' + last).show();
			};
			
		} else {
			//remove category
			targetIndex = linkId.substr(11,12);
			$(this).hide();
			$(this).next().hide();
			$('#Container_cat-' + targetIndex).hide();
			$('.clear-cat-' + targetIndex).hide();
			
			var counter = 0;
			var last = 0;
			for(i=0;i<=5;i++){
				if($('#Container_cat-' + i).is(':visible')){
					counter++;
					last = i;
				}
			}
			
			if(counter < 6){
				$('#add-cat-' + last).show();
			} 
			
			if(counter == 1){
				$('#remove-cat-' + last).hide();
			}
			
			
		};
		return false;
	});
	
	
	// answersTable clickable rows
	$('#answersTable .row').click(function(){

	});
	
	
		
});

function closeTools(){
	$('#searchPopup').hide();
	
	if ($('#loginPopup').length > 0)
		$('#loginPopup').hide();
	
	else
		$('#logoutPopup').hide();
}

function validEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email
   if(reg.test(address) == false) {
      return false;
   } else {
	   return true;
   }
}
