/**
 **/

(function($){
var $$;

$$ = $.fn.search = function($options) {};

/**
 *

 *
 *
**/

$$.sendForm = function(tab,contentId,additionalParameter){
   var hash = '?';
	 var value = '';
	 var overallError = false;

		if(typeof contentId!='undefined'){
			var content = contentId;
		}
		else{
			var content = 'tab_content_'+tab;
		}
		
		$('div#'+content+' :input').each(function(key,obj){
				if(this.name != ''){

				var error = errors[this.name];

				if(error!==true && this.type!='hidden' && overallError === false){
					//jesli wyszukiwany jest region, vartość pola może być jakąkolwiek liczba PRÓCZ -1
					if(new RegExp('region').test(this.name) && $(obj).val()!=-1){
						value = $(obj).val();
						}
					//jesli wyszukiwana jest dziedzina zespołu, vartość pola może być jakąkolwiek liczba PRÓCZ 0
					else if(new RegExp('specialization').test(this.name) && $(obj).val()!=0){
						value = $(obj).val();
					}
					//w każdym innym przypadku, wartośc pola nie może się równac ani 0, ani -1
					else{
						if($(obj).val() != fieldsDefaults[$(obj).attr('id')]){
							if($(obj).attr('name').substr(0,3)=='age'){
								value = Number($(obj).val())+9;
							}
							else{
								if($(obj).attr('type')=='select-one' && $(obj).val()!=0 && $(obj).val()!=-1){
									value = $(obj).val();
								}
								else if($(obj).attr('type')=='text'){
									value = $(obj).val();
								}
							}
						}
					}

					if(value!='' && value!=null){
						hash+=$(obj).attr('name')+'='+value+'&';
						value = '';
					}
				}
			}
		});
		
		if(hash.substr(hash.length-1,hash.length)){
     hash = hash.substr(0,hash.length-1);
	 	}
	 	if(typeof additionalParameter!='undefined'){
	 		hash += additionalParameter
		}
		window.location.href = url_search+hash;
   
};

/**
 *
 * @name jQuery.comments
 *
 * @desc The global galleria object holds four constant variables and four public methods:
 *       $.comments.activate(_src) = displays an image from _src in the galleria container.
  *
**/

$.extend({search : {
        send : function(current_tab,content_id,s_additional){
            $$.sendForm(current_tab,content_id,s_additional);
        }
    }
});

})(jQuery);

$(document).ready(function (){
		$('form[id*="form_box"]').keyup(function(event){
			if(event.keyCode == 13 || event.which == 13){
				usersContainer='advForm';
				sendSearchForm();
				return false;
			}
		});

		$('form[id*="form_result"]').keyup(function(event){
			if(event.keyCode == 13 || event.which == 13){
				usersContainer='advForm';
				sendSearchForm();
			}
		}).submit(function(){return false;});

	$('#olbs').keyup(function(event){
		if(event.keyCode == 13 || event.which == 13){
				usersContainer='olbs';
				sendSearchForm();
				return false;
			}
		});

   var oFields = {
    users: {
      users: {
                person_firstname : true, person_surname : true, person_login : true, person_gender : true, person_age_from : true, person_age_to : true, person_region : true,
                person_city : true, person_avg_rating_from : true, person_avg_rating_to : true,  person_ranking_from  : true, person_ranking_to : true,
                person_language : true, person_language_level  : true, person_field  : true, person_specialization : false
             },
  
      groups: {
                group_name : true, group_field : true, group_login : true
              },
  
      teams:  {
                team_name : true, team_alias : true, team_login : true, team_field : true,
				team_specialization : true, team_ranking_from : true, team_ranking_to : true, team_avg_rating_from : true,
				team_avg_rating_to : true
              }
     },

    works: {
      users:  {work_title        : true, work_tag   : true, work_talent  : true, work_file_type  : true, work_avg_rating_from : true, work_avg_rating_to : true, work_avg_ranking_from  : true,
               work_avg_ranking_to : true, work_where : true, work_name    : true, work_gender    : true, work_age_from       : true, work_age_to       : true, work_region          : true,
               work_city         : true},

      groups: {work_title        : true, work_tag   : true, work_talent  : false , work_file_type  : true  , work_avg_rating_from : true  , work_avg_rating_to : true  , work_avg_ranking_from  : true,
               work_avg_ranking_to : true, work_where : true, work_name    : true  , work_gender    : false , work_age_from       : false , work_age_to       : false , work_region          : false,
               work_city         : false},

      teams:  {work_title        : true, work_tag   : true, work_talent  : true, work_file_type  : true  , work_avg_rating_from : true  , work_avg_rating_to : true  , work_avg_ranking_from  : true,
               work_avg_ranking_to : true, work_where : true, work_name    : true, work_gender    : false , work_age_from       : false , work_age_to       : false , work_region          : true,
               work_city         : true}
     }
  }



   hideFields($('#work_where').val(),oFields.works);
   hideFields($('#category').val(),oFields.users);
   


  
  function hideFields(val,fields){
    switch(val){
      default:
      case '1':case 'talent':
        fields = fields.users;
      break;
      case '2':case 'group':
        fields = fields.groups;
      break;
      case '3':case 'team':
        fields = fields.teams;
      break;
    }

    
    if(typeof(fields)=='object'){
      $.each(fields,function(k,v){

        if(v===true){
          //$('#'+k).show();
          $('#'+k).parent("td").parent("tr").show();
        }
        else if(v===false){
          //$('#'+k).hide();
          $('#'+k).parent("td").parent("tr").hide();
        }

      });
    }
    
  }

//	$('#person_specialization').hide();

	$('#person_field').change(function(){
			customSelectPopulate(specs,$(this).val(),{
					firstLevelPopulate:'person_specialization',
					firstLevelPopulateDef:'SPECJALIZACJA'
				});
	});

//	$('#team_specialization').hide();

	$('#team_field').change(function(){
			customSelectPopulate(teams,$(this).val(),{
					firstLevelPopulate:'team_specialization',
					firstLevelPopulateDef:'SPECJALIZACJA'
				});
	});

//	$('#work_category').hide();
//	$('#work_specialization').hide();

	$('#work_field').change(function(){
			customSelectPopulate(multilist,$(this).val(),{
//					hiddenPopulate:'work_search_cats',

					firstLevelPopulate:'work_specialization',
					firstLevelPopulateDef:'SPECJALIZACJA',

					secondLevelPopulate:'work_category',
					secondLevelPopulateDef:'KATEGORIA'
				});
	});

	$('#work_specialization').change(function(){
			customSelectPopulate(cats,$(this).val(),{
//					hiddenPopulate:'work_search_cats',

					firstLevelPopulate:'work_category',
					firstLevelPopulateDef:'KATEGORIA'
				});
	});

	$('#blog_service').change(function(){
			customSelectPopulate(multilist,$(this).val(),{
				firstLevelPopulate:'blog_channel',
				firstLevelPopulateDef:'DZIAŁ'
			});
	});

});

function customSelectPopulate(list,val,conf){
	if(list!=''){
			var specsOptions = '';
			var catsOptions = '';
			var searchCats = new Array();
			var index = 0;

			if(typeof list[val]!='undefined'){
				$('#'+conf.firstLevelPopulate).show();
				specsOptions='<option value="0">'+conf.firstLevelPopulateDef+'</option>';
				catsOptions='<option value="0">'+conf.secondLevelPopulateDef+'</option>';
		
				$.each(list[val],function(k,v){
					index = v.id || k;
					specsOptions+='<option value="'+index+'">'+v.name+'</option>';
					if(typeof conf.secondLevelPopulate != 'undefined'){
						if(v.cats != null){
							$('#'+conf.hiddenPopulate).val('');
							$('#'+conf.secondLevelPopulate).show();
							$.each(v.cats,function(kk,vv){
								catsOptions+='<option value="'+vv.id+'">'+vv.name+'</option>';
								searchCats[searchCats.length] = vv.id;
							});
						}
						else{
							$('#'+conf.hiddenPopulate).val('');
						}
						}
				});
				
				if(typeof conf.hiddenPopulate!='undefined'){$('#'+conf.hiddenPopulate).val(searchCats.join(','));}
				$('#'+conf.firstLevelPopulate).html(specsOptions);
				if(typeof conf.secondLevelPopulate!='undefined'){$('#'+conf.secondLevelPopulate).html(catsOptions);}
			}
			else{
				if(typeof conf.hiddenPopulate!='undefined'){$('#'+conf.hiddenPopulate).val('');}
				if(typeof conf.firstLevelPopulate != 'undefined'){
					$('#'+conf.firstLevelPopulate).html('<option value="0">'+conf.firstLevelPopulateDef+'</option>');
				}
				if(typeof conf.secondLevelPopulate != 'undefined'){
					$('#'+conf.secondLevelPopulate).html('<option value="0">'+conf.secondLevelPopulateDef+'</option>');
				}
//			$('#'+conf.firstLevelPopulate).hide();
//			if(typeof conf.secondLevelPopulate!='undefined'){$('#'+conf.secondLevelPopulate).hide();}
			}
			
		}
}

