/*
var myArray = {													
		'checkhow':['myngle-pro','individual'],
		//'name':['a'],
		//'languageIdOnHeader':['amharic'],
		'checkwhy':[],
		'checklevel':['pre-intermediate'],	
		'checktopic':[],
		'checkfocus':[],
		'langspeak':['albanian','afrikaans'],
		'checkwith':[],
		'checkfreetrial':['yes'],
		'checkthumbsup':[],
		'checkpackage':[],
		'checkprice':[],
		'checkgender':['male','female']										
	};
*/       
(function($) {
    /*load data then fill on search box*/
    function init(myArray){    
        var expandadvancedsearch = false;
    	var haslang = false;

			    
    	for (var k in myArray){					
    		var subarr = myArray[k];	
    		/* build pre-data on left search section */
    		if(k=='langspeak' && subarr[0]!=''){
    			haslang = true;					
    			jQuery.each(subarr, function(i) {						    
    			    var temp = i==0 ? '<tr height="40" class="langspeak" id="'+i+'"><td height="20" align="left" valign="middle" colspan="2"><select name="langspeak" id="'+ k + i +'" class="content_text width-153 no-padding" title="check'+ k + i +'" ></select></td></tr>' : '<tr class="langspeak" id="'+i+'"><td width="40"><input type="checkbox" id="check'+ k + i +'" name="checklangspeak" /></td><td height="20" align="left" valign="middle" colspan="2"><select name="langspeak" id="'+ k + i +'" class="content_text width-153 no-padding" title="check'+ k + i +'" ></select>&nbsp;<a href="javascript:;"><span style="font-weight: bold; color: rgb(255, 0, 0);" id="removeselect'+ i +'" >x</span></a></td></tr>';						    
    				$("table[id=chooselangspeak]").html(temp); // tr:last-child").before(temp);
    				loadLanguages(k + i, '0', langspeaktext);								
    				$('select[id=' + k + i + '] option[title='+this+']').attr('selected','selected');
    				$('input[id=check' + k + i + ']').attr('checked',true);
    				expandadvancedsearch = true;
    				$('span[id=removeselect'+i+']').click(function(){ 						
    					$('tr[class=langspeak][id='+i+']').remove();
    					handleshowaddlang();
    				});
    		    });																					
    		}
    		else if(subarr[0]!=''){ 					   
    		   //fill multi options (for topic)
    		   if(k=='checktopic'){
    			   $('input[name='+k+'][class=checkfilter]').each(function(i){	
    					if(jQuery.inArray($(this).val(),subarr)>-1){							
    						$(this).attr('checked',true);																												
    					}
    				});					
    				 /*ThoaTTK.#2778*/
    				if(subarr.length==1) $('#headertopic').val(subarr[0]);	
    				 /*ThoaTTK.#2778*/
    		   }else{ 
    			   $('select[title='+ k +'] option[title='+subarr[0]+']').attr('selected','selected');
    			   if($('select[title='+ k +']').length && $('select[title='+ k +']').val()!='' && $('select[title='+ k +']').val()!='0'){ 
    				   	$('#'+ k).attr('checked',true);
    				   	expandadvancedsearch = true;
    			   }else if($('input[name='+k+'][value=' + subarr[0] + ']').length){ 
    			   		$('input[name='+k+'][value=' + subarr[0] + ']').attr('checked',true);
    			   		expandadvancedsearch = true;
    			   }
    		   }	   
    		}							        	
    	}
    	$('#name').val(name);
        if(name!=''){                                                     
            expandadvancedsearch = true;
            $('#checkname').attr('checked',true);                    
        }else{                    
            $('#name').val(defaultname);
        }	
    	if(haslang==false){ 
    	    var temp = '<tr height="40" class="langspeak" id="0"><td width="40"><select name="langspeak" id="langspeak0" class="content_text width-153 no-padding" title="checklangspeak0" ></select></td></tr>';					
    		$("table[id=chooselangspeak]").html(temp);
			loadLanguages('langspeak0',langspeakvalue, langspeaktext);
    	}
    	if(expandadvancedsearch==true){ 
    	    $('#advancedsearch >td >img').attr('src',staticUrl + "/img/arrow_down.gif");
            $('#adsearch').show();
			$('#bfind').hide(); 
    	}				
    	//if group-lesson checked, disable others related
    	if($('#how').val()=='group-lesson'){  	
    		$('input[name=checkfreetrial]').removeAttr('checked');
    		$('input[name=checkfreetrial]').attr('disabled','disabled');		
    		if($('#why').val()!='programs'){	    
    			$('input[name=checktopic]').removeAttr('checked');
    			$('input[name=checktopic]').attr('disabled','disabled');	    				    	
    		}						
    	}
    	//if conversation is checked && Test Preparation is uncheck
    	if($('#why').val()=='conversation' && !$('#checktopicB').is(":checked")){   		   
    	    $('select[name=level] option[value=""]').attr("selected","selected");             	        
            $('select[name=level]').attr('disabled','disabled');
            $('input[name=checklevel]').removeAttr('checked');
            $('input[name=checklevel]').attr('disabled','disabled');				    		
    	}
    }
            	        	
    function buildurl(baseUrl,name){     
        /*ThoaTTK.#2778*/
        var temp = '';
        if($("#headertopic").length > 0){
            if(name=='topic' && $('#headertopic').val()!=''){ 
                temp = $('#headertopic').val();	   
        	} 
        }
    	/*ThoaTTK.#2778*/
        var item = '';    	        										
    	$('input[name=check'+name+']').each(function(){		
    		if($(this).attr('checked')){	
    		    if(name=='langspeak' && $('select[title=' + $(this).attr('id') + ']')!=''){ 	
    		        item += (item!='') ? '+' + $('select[title=' + $(this).attr('id') + '] option:selected').attr("title") : $('select[title=' + $(this).attr('id') + '] option:selected').attr("title");							       					       
    		    }else{ 					       
    		        if($('input[title=' + $(this).attr('name') + ']') && $('input[title=' + $(this).attr('name') + ']').length && $('input[title=' + $(this).attr('name') + ']').val()!=''){ 							        
    		           if(name=='name'){ 
    		               var input = $('input[title=' + $(this).attr('name') + ']').val();
    		               //alert(input);
    		               if(input!=defaultname){ 					                   
    		                   item += (item!='') ? '+' + $('input[title=' + $(this).attr('name') + ']').val() : $('input[title=' + $(this).attr('name') + ']').val();		    					       
    		               }					           
    		           }
    			       else {			          
    			       		item += (item!='') ? '+' + $('input[title=' + $(this).attr('name') + ']').val() : $('input[title=' + $(this).attr('name') + ']').val();		    					       			       		
    			       }
    			    }else if($('select[title=' + $(this).attr('name') + ']') && $('select[title=' + $(this).attr('name') + ']').length && $('select[title=' + $(this).attr('name') + ']')!=''){ 
    			       item += (item!='') ? '+' + $('select[title=' + $(this).attr('name') + ']').val() : $('select[title=' + $(this).attr('name') + ']').val();							       					       
    			    }
    			    else{
    			      if($(this).val()==temp) temp = '';
    				  item += (item!='') ? '+' + $(this).val() : $(this).val();														  
    			    }
    		    }					    					   
    		}
    	})
    //	if(name=='langspeak'){ 
    //	    name = 'speaks';
    //	}
    	if(name=='timeofday' && item!=''){ 
    		var theDate=new Date();
    		baseUrl +='/vieweroffset/'+(-theDate.getTimezoneOffset()/60);
    	}	
    	if(name=='how' && item=='' && typeof argumentArray!="undefined" && typeof argumentArray['checkhow']!="undefined"){ 
    	   item = argumentArray['checkhow'][0];
    	}
    	/*ThoaTTK.#2778*/
    	if(name=='topic' && temp!=''){ 
    	   item += (item!='') ? '+' + temp : temp;	
    	}
    	/*ThoaTTK.#2778*/
    	baseUrl += (item!='') ? '/' + name +'/' + item : '';						
    	
    	return baseUrl;
    }        	
    $(document).ready(function () {
    	$('#advancedsearch >td >img').attr('src',staticUrl + "/img/arrow_right.gif");
        $('#adsearch').hide();
		$('#bfind').show(); 	                										
    	//prefill data
    	init(myArray);		
    	onchange();							
    												
    	/*onclick add more lang*/											
    	$('#addlang').click(function(){		
    		var count = $("table[id=chooselangspeak] tr:last-child").prev().attr('id');		
    		count++;	
    		var temp = '<tr height="40" class="langspeak" id="'+count+'"><td height="20" align="left" valign="middle" colspan="2"><select name="langspeak" id="langspeak' + count + '" class="content_text width-153 no-padding" title="checklangspeak' + count + '" ></select><a href="javascript:;"><span style="font-weight: bold; color: rgb(255, 0, 0);" id="removeselect'+ count +'" >x</span></a></td></tr>'; 
			$("table[id=chooselangspeak]").html(temp); // tr:last-child").before(temp);				
    		loadLanguages('langspeak'+count, '0', langspeaktext);
    		//onChange(myArray);
    		handleshowaddlang();
    		$('span[id=removeselect'+count+']').click(function(){ 						
    			$('tr[class=langspeak][id='+count+']').remove();
    			handleshowaddlang();
    		})	
    		onchange();			
    	});	
        
    	$('#how').change(function(){	
            handleHow();
    	})
    	$('#checkhow').click(function(){ 
    	    handleHow();            	
    	})
    	$('#why').change(function(){ 
    	    handleWhy();
    	})	
    	$('#checkwhy').click(function(){ 
    	    handleWhy();
    	})
    	
    	//course's name be blank automatically for user to place input
    	$('#name').click(function(){ 
    		if($(this).val()==defaultname) $(this).val('');
    	})
    	$('#name').mouseout(function(){ 
    		if($(this).val()=='') {
    			$(this).val(defaultname);
    			$('#checkname').attr('checked',false);
    		} else if ($(this).val() != defaultname) {
    			$('#checkname').attr('checked',true);
    		}
    	})
    	//check/uncheck Test Preparation 
    	$('#checktopicB').click(function(){				    	
        	if($(this).attr('checked')){	
        		//if checked	    		
    			$('select[name=level]').removeAttr('disabled');
    			$('input[name=checklevel]').removeAttr('disabled');
    			$('input[name=checktopic]').removeAttr('disabled');				    													    			
        	}else{			    					    	
    			//if  Test Preparation && Programs in group why are unchecked && group lesson in group how is checked
        		if($('#why').val()!='programs' && $('#how').val()=='group-lesson'){				    			
    				$('input[name=checktopic]').removeAttr('checked');
        			$('input[name=checktopic]').attr('disabled','disabled');					    								    						    								    							   
        		}
        		//if  Test Preparation && Programs in group why are unchecked && conversation in group why is checked
        		if($('#why').val()=='conversation'){ 					    			
    				$('select[name=level] option[value=""]').attr("selected","selected");             	        
        	        $('select[name=level]').attr('disabled','disabled');
        	        $('input[name=checklevel]').removeAttr('checked');
    	            $('input[name=checklevel]').attr('disabled','disabled');
        		}			    					    						    						    
        	}						
    	});										
    			
    	
    	$('td[title=expand]').each(function(){
    		var id = $(this).attr('id');		
    		$(this).click(function(){		
    			if(document.getElementById(id+'_1').style.display=='none'){//$('.'+$(this).attr('id')).is(":hidden")
    				$('td[id=' + id + '] img[class=expand]').attr('src',staticUrl + "/img/course-listing-refine-search-arrow-down.gif");
    				$('.'+$(this).attr('id')).show();
    				document.getElementById(id+'_1').style.display='';
    			}else{
    				$('td[id=' + id + '] img[class=expand]').attr('src',staticUrl + "/img/course-listing-refine-search-arrow-right.gif");
    				$('.'+$(this).attr('id')).hide();
    				document.getElementById(id+'_1').style.display='none';
    			}
    		});
    		
    	});
    					
    	
    	$('td[title=Refine search]').each(function(){ 
    		$(this).click(function(){			    		       
    			window.location.href = getURL();				
    		})		
    	});	
    	
    	$('#headersearch').click(function(){ 
    	   	window.location.href = getURL();					   
    	})
    	$('#levelhelp').mouseenter(function(){
    		Tip(fluencyChartTip(8), TITLE, charttitle, STICKY, 1, CLOSEBTN, true,FADEIN,300);
    	})
    	$('#levelhelp').mouseleave(function(){
    		UnTip();
    	})	
    	/* Handle show/hide tooltip on 'thumbs up teacher' group*/
    	$('#thumbuphelp').hover(
    		function(){ 					
    			$('#titleTip').css('display','block');						
    		},
    		function(){ 							
    			$('#titleTip').css('display','none');
    		}
    	);		
    	$('#learnnowhelp').hover(
    		function(){ 					
    			$('#ttLearnnow').css('display','block');						
    		},
    		function(){ 							
    			$('#ttLearnnow').css('display','none');
    		}
    	);					
    	$('#languageIdOnHeader').change(function(){ 		      	
    		  window.location.href = getURL();			         
    	})
    	$('#level').change(function(){		 		      	
    		  window.location.href = getURL();			         
    	})
    	$('#langspeak0').change(function(){ 		      	
    		  window.location.href = getURL();			         
    	})
    	$('#recom').click(function(){
			  document.getElementById('selectOrder').value = 1;	
			  // setOrderNew();	 		      	
    		  window.location.href = getURL();
    	})	  
		$('#pri').click(function(){
			  document.getElementById('selectOrder').value = 3; 			  		 		      	
    		  //setOrderNew();	  		
    		  window.location.href = getURL();	         
    	}) 
		$('#price_course').click(function(){
			  document.getElementById('selectOrder').value = 6; 			  		 		      	
    		  //setOrderNew();	  			         
    		  window.location.href = getURL();
    	}) 
		
    	//$('#selectOrder').change(function(){ 		      	
    	//	  setOrderNew();
    	//})
    	$('a[name=perpage]').click(function(){    	
    	    //$(this).click(function(){    	       
    	       //setPerPage($(this).attr('id'));
			   document.getElementById('perpages').value = $(this).attr('id'); 
			   window.location.href = getURL();      
    	    //})  
    	})
    	$('#advancedsearch').click(function(){ 
    	    var src = $('#advancedsearch >td >img').attr('src');
            if(src==staticUrl + "/img/arrow_down.gif"){ 
            $('#advancedsearch >td >img').attr('src',staticUrl + "/img/arrow_right.gif");	                        
                $('#adsearch').hide();
				$('#bfind').show(); 		                        							
            }else{ 
            $('#advancedsearch >td >img').attr('src',staticUrl + "/img/arrow_down.gif");	                        
                $('#adsearch').show();
				$('#bfind').hide(); 		                        						
            }            	 
    	})
    	function handleHow(){ 
    	   if($('#how').val()=='group-lesson' && $('#checkhow').attr('checked')){ 
                //disable 'Test Preparation' on Motivation to study
    			$('#checktopicB').removeAttr('checked');
    		    $('#checktopicB').attr('disabled','disabled');
    		    //focuson is not programs
    		    if($('#why').val()!='programs'){ 
    		        $('input[name=checktopic]').removeAttr('checked');
    	    		$('input[name=checktopic]').attr('disabled','disabled');
    		    }
    		    $('input[name=checkfreetrial]').removeAttr('checked');
    	    	$('input[name=checkfreetrial]').attr('disabled','disabled');
            }else{
              //active group how, about, focus and Test Preparation in group why			    			
    			$('input[name=checktopic]').removeAttr('disabled');			    			
    			$('input[name=checkfreetrial]').removeAttr('disabled');			    		                          
            }
    	}
    	function handleWhy(){ 
    	    if($('#why').val()=='conversation' && $('#checkwhy').attr('checked')){ 
    	        $('select[name=level] option[value=""]').attr("selected","selected");             	        
    	        $('select[name=level]').attr('disabled','disabled');
    	        $('input[name=checklevel]').removeAttr('checked');
    	        $('input[name=checklevel]').attr('disabled','disabled');
    	        if($('#how').val()=='group-lesson'){ 
    	           $('input[name=checktopic]').removeAttr('checked');
    		       $('input[name=checktopic]').attr('disabled','disabled');	
    	        }	            	   
    	   }else{ 
    	       //active group level
    			$('select[name=level]').removeAttr('disabled');		    		
    			$('input[name=checklevel]').removeAttr('disabled');		    		
    	        $('input[name=checktopic]').removeAttr('disabled');
    	   }        
    	}
    	function onchange(){ 
    		//Checkbox be 'checked' automatically if user chooses something from the drop-down menu
    		$('select[class=content_text width-153 no-padding]').each(function(){
    			$(this).change(function(){
    				if($(this).val()!='' || $(this).val()!="0"){ 
    					$('input[id=' + $(this).attr('title') + ']').attr('checked',true);
    				}else{ 
    					$('input[id=' + $(this).attr('title') + ']').removeAttr('checked');
    				}
    			})            		
    		})
    	}
    	$("#hideNewTeacher").click(function(event){
            $(this).css('display', 'none');
            $("#expandNewTeacher").css('display', 'block');
            $("#newTeacher").css('display', 'none');
        });
        
        $("#expandNewTeacher").click(function(event){
            $(this).css('display', 'none');
            $("#hideNewTeacher").css('display', 'block');
            $("#newTeacher").css('display', 'block');
        });
    });            
    /* Handle show/hide 'add a language link'*/           
    function handleshowaddlang(){ 
    	var size = $('select[name=langspeak]').size();
    	if(size==4) {
    		$('#addlang').css('display','none');
    	}else{
    		$('#addlang').css('display','block');
    	}
    }
    function getURL() {						
    	var url = '';
    	var langteach = '';
    	var item = '';
    	var baseUrl = '';
    	if($('#languageIdOnHeader').val()>0){
    		langteach = $('#languageIdOnHeader option:selected').attr("title") + '-' + pagename;
    	}
        //if($('#checklevel').attr('checked') && $('#level').val()!=''){
		if($('#level').val()!=''){	                      
            if (langteach!='') baseUrl += '/' + $('#level').val() + '-' + langteach;	
            else baseUrl += '/' + $('#level').val() + '-language-' + pagename;
        
        }else if(langteach==''){ 
            baseUrl += '/' + 'language-' + pagename;
        }else{ 
             baseUrl += '/' + langteach;
        }         
        if($('#selectOrder').val() == 1)
        {			 
			 if($('#perpages').val() !=10)
			 	baseUrl +='/order/d/direction/desc/limit/'+$('#perpages').val();
			 else
			 	baseUrl += ''; ///order/d/direction/desc';	
		}
		if($('#selectOrder').val() == 3){			
			
			 	baseUrl +='/order/p/direction/asc/limit/'+$('#perpages').val();	
		}   
        if($('#selectOrder').val() == 6)
        {			
			 baseUrl +='/order/p/direction/desc/limit/'+$('#perpages').val();	
		}                
        if($('#langspeak0').val() != '0') {
            baseUrl += '/speaks/' + $('#langspeak0 option:selected').attr("title");
        }

        if($('#languageIdOnHeader').val() == -1) {
            baseUrl += '/offers/' + $('#languageIdOnHeader option:selected').attr("title");
        }
        if($('#package').val()!="") {
            baseUrl += '/package/' +$('#package').val();
        }
               
    	if($('#fortypercent').val()!="") {
            baseUrl += '/fortypercent/' +$('#fortypercent').val();
        }       
    	var temp = ['how','why','focus','topic','with','freetrial','fortypercent','packages','learnnow','thumbsup','langspeak','name','timeofday'];
    
    	for (var i=0;i<temp.length;i++){							
    		baseUrl = buildurl(baseUrl,temp[i]);
    	}               
        return baseUrl;
    }
    function setOrderNew()
    {
    	var orderIndex = parseInt(document.getElementById('selectOrder').value);
    	if(pagename=='teachers'){
    	    switch (orderIndex)
        	{
        		case 1:
        			order = 'd';
        			orderDirection = 'desc';
        			break;
        		case 2:
        			order = 'd';
        			orderDirection = 'asc';
        			break;
        		case 3:
        			order = 'p';
        			orderDirection = 'asc';
        			break;
        		case 4:
        			order = 'p';
        			orderDirection = 'desc';
        			break;
        		case 5:
        			order = 't';
        			orderDirection = 'desc';
        			break;
        		case 6:
        			order = 't';
        			orderDirection = 'asc';
        			break;
        		default:
        			order = 'd';
        			orderDirection = 'asc';
        			break;
        	}        	
        	window.location.href = getURL() + '/order/' + order + '/direction/' + orderDirection + '/limit/' + limit;
    	}else if(pagename=='courses'){
    	    switch (orderIndex)
        	{
        		
				case 1:
        			order = 'a';
        			orderDirection = 'desc';
        			break;
        		case 2:
        			order = 'a';
        			orderDirection = 'asc';
        			break;
				case 3:
        			order = 'r';
        			orderDirection = 'desc';
        			break;
        		case 4:
        			order = 'r';
        			orderDirection = 'asc';
        			break;
        		case 5:
        			order = 'p';
        			orderDirection = 'asc';
        			break;
        		case 6:
        			order = 'p';
        			orderDirection = 'desc';
        			break;
           		default:
        			order = 'a';
        			orderDirection = 'desc';
        			break;
        	}        	
        	window.location.href = getURL() + '/order/' + order + '/direction/' + orderDirection + '/limit/' + limit;
    	}
    	
    }
	 function setPerPage(perPage) {	 
	 	
     	window.location.href = getURL() + '/order/' + order + '/direction/' + orderDirection + '/limit/' + perPage;
    }   
})(jQuery)


