// JavaScript Document
var debug=function(s){if(window.console)console.log(s);};
(function($) {
		  
	//error handler for ajax requests
	$.ajaxSetup({
		error:function(x,e){
			var message;
			$('.loader','#cv_form').removeClass('loader');
			if(x.status==0){
				message=(no_ajax);
				message2=('You seem to be offline!! Check your Network');
			}else if(x.status==404){
				message=(no_ajax);
				message2=('Requested URL not found.');
			}else if(x.status==500){
				message=(no_ajax);
				message2=('Internel Server Error.');
			}else if(e=='parsererror'){
				message=(no_ajax);
				message2=('Error.\nParsing JSON Request failed.');
			}else if(e=='timeout'){
				message=(no_ajax);
				message2=('Request Time out.');
			}else {
				message=(no_ajax);
				message2=('Unknow Error.\n'+x.responseText);
				
			}
			alert(message);
			debug(message2);
		}
	});
})(jQuery);

(function($) {
  $.fn.equalizeCols = function(children){
    var child = Array(0);
    if (children) child = children.split(",");
    var maxH = 0;
    this.each(
      function(i) 
      {
        if (this.offsetHeight>maxH) maxH = this.offsetHeight;
		
      }
    ).css("height", "auto").each(
      function(i)
      {
        var gap = maxH-this.offsetHeight;
        if (gap > 0)
        {
          t = document.createElement('div');
          $(t).attr("class","fill").css("height",gap+"px");
          if (child.length > i)
          {
            $(this).find(child[i]).children(':last-child').after(t);
          } 
          else 
          {
            $(this).children(':last-child').after(t);
          }
        }
      }  
    );
    
  }
})(jQuery);
function cv_rows(){
	$("tr td .cv_search-box-wrap-s:eq(0), tr td .cv_search-box-wrap-s:eq(1)").equalizeCols();}

function changeclass(objid,newclass)
{
	document.getElementById(objid).className=newclass;
}




//Tabs
function tabs()
{
	$('#tabs li').click(function(){
				
				//remove the active class from the other tabs
				$('#tabs li a').filter('.active').removeClass('active');
				$(this).find('a').addClass('active');
				
				//hide all contents
				$(".tab-content").addClass('hidden').hide();
				
				//get the index of the clicked tab
				var i=$('#tabs li').index(this);
				
				//show the content DIV according to the index
				$('.tab-content:eq('+i+')').removeClass('hidden').fadeIn('slow');
				
				//disable the hyperlink
				return false;
			 })
			
			.children('a').hover(
				function(){
					if(!$(this).hasClass('active'))//check if this tab is active..
						$(this).addClass('hover');}//if not..add hover class
					
				,function(){$(this).removeClass('hover');}
				);
}

function rollup(){
	//$('.rollup p').hide();
			$('.rollup h3').click(function() {$(this).toggleClass('rolledup').next().slideToggle(300);;})
			
							.hover(
									function(){
										
											$(this).addClass('hover');},
									function(){
										$(this).removeClass('hover');})
}
function menu(){
	 //MENU
			$('#menu ul').hide();
			
			if(typeof on !='undefined'){
				
				$('#'+on).addClass('hover');
			}
			$('#menu').hover(function(){
									  $('#slideshow').css('zIndex',-1);
									   
		
									  }
									  ,function(){
										  $('#slideshow').css('zIndex','0');
										 	
										  });
			 $('#menu li').hover(
								
							  function() {
								 	id=$(this).find('a:first-child').attr('id');
									if (!window.XMLHttpRequest&&(id=='jobseekers'||id=='corporate')) 
										$('.ie6 select','.paddingleft26').css('visibility','hidden');
								   $(this).find('ul:first').show();
								   $(this).find('a:first').addClass('hover');
								 
							  },
							  
							function() {
								id=$(this).find('a:first-child').attr('id');
								if (!window.XMLHttpRequest&&(id=='jobseekers'||id=='corporate')) 
										$('.ie6 select','.paddingleft26').css('visibility','visible');
										
								   $(this).find('ul:first').hide();
								   $(this).find('a:first').removeClass('hover');
								   if(typeof on !='undefined')
								   	$('#'+on).addClass('hover');
							  }
												
							
							).click(function() {
								   $(this).find('ul:first').toggle();
								  
			 });
	
}

function focusblur(){
	 //focus and blur	
			$('input[@title]').focus(function(){
								
									if($(this).attr('id')=='key')
										$(this).addClass('wordsearchon').removeClass('wordsearch');;
										
									if($(this).attr('value')==$(this).attr('title'))
								 	$(this).attr('value','');
								 });
			
			$('input[@title]').blur(function(){
								if($(this).attr('id')=='key')
										$(this).removeClass('wordsearchon').addClass('wordsearch');;
								  if($(this).attr('value')==''&&$(this).attr('id')!='key')
								 	$(this).attr('value',$(this).attr('title'));
								 });
}

function oddeven(){
	
			 //odd even bold
			/*$('#cv table tr:odd').addClass('bgcolor');
			$('#cv2 table tr:odd').addClass('bgcolor');
			$('#cv2 table tr:odd').find('.jobcompanystyle').css('color','#353994');
			$('#cvbox table tr:last').find('.whitebg').css('border','none');*/
			
			$('#cvbox.oddeven .cv_search-box-wrap:odd').addClass('bgcolor');
}

function help(){
////////help
	$('.help-icon').click(function(){
				 if($(this).find('.x').length)
				 $('.help-text').hide();
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
					$('#cv_form select').addClass('v-hidden');
					$('.buttons-wrap').css('z-index','-100');
					
				}
				if($(this).parent().hasClass('title-li'))
					$('#cv_form .help-icon').not(this).parent().css({position:'relative','z-index':'10'});
				else
					$('#cv_form .help-icon').not(this).parent().parent().css({position:'relative','z-index':'10'});
					
					 
				
				  
		if($(this).find('.x').length)
					  {
							$(this).parent().parent().css({position:'relative','z-index':'90'});
					  		$(this).parent().addClass('top').find('.help-text').show();
					  }
					 else
						$(this).parent().toggleClass('top').find('.help-text').toggle();
					 		 
		 }
				
					 );	
	$('.help-icon .x').click(function(){
					  if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)
							{$('#cv_form select').removeClass('v-hidden');
							$('.buttons-wrap').css('z-index','0');}
									   $('.help-icon').not(this).show();
				 $icon=$(this).parent().parent();
				 $('#cv_form .help-icon .x').not(this).parent().parent().parent().parent().css({position:'relative','z-index':'10'});
				   $icon.parent().removeClass('top').find('.help-text').hide();
					return false;
					   }
				
					 );	
}
function multiple(){
	//MULTIPLE SELECTS
			$('.add-multiple').click(function() {
				$(this).parent().parent().children('.mselect:first').addClass('current-mselect');
				
				$(this).parent().parent().children('.mselect_all:first').children('option:selected').remove().appendTo('.current-mselect');
				
				$(this).parent().parent().children('.mselect:first').removeClass('current-mselect');
				return false;
				 });  
			
				$('.remove-multiple').click(function() { 
				$(this).parent().parent().children('.mselect_all:first').addClass('current-mselect');
				$(this).parent().parent().children('.mselect:first').children('option:selected').remove().appendTo('.current-mselect'); 
				$(this).parent().parent().children('.mselect_all:first').removeClass('current-mselect');
				
				return false;
				});

	$('.add-all').click(function() {
		$(this).parent().parent().children('.mselect:first').addClass('current-mselect');
		$(this).parent().parent().children('.mselect_all:first').children('option').remove().appendTo('.current-mselect');
		$(this).parent().parent().children('.mselect:first').removeClass('current-mselect');
		//$(this).parent().parent().find('.add-multiple').click();
		return false;
	});
	
	$('.remove-all').click(function() {
		$(this).parent().parent().children('.mselect_all:first').addClass('current-mselect');
		$(this).parent().parent().find('.mselect option').remove().appendTo('.current-mselect'); 
		$(this).parent().parent().children('.mselect_all:first').removeClass('current-mselect');
		//$(this).parent().parent().find('.remove-multiple').click();
		return false;
	});
	//form submit
	$('form[name=job_search]').submit(
				function(){
					
					$(this).find('.mselect option').each(function(i) {  
					$(this).attr("selected", "selected");  
					})
				}
	);
	
	
}
function printButton(){
	
	$('#print_button').click(function(){
	var location=String (window.location);
	
	if(location.match('cv-details.php'))
		window.print();
	else
	$.jPrintArea('#toprint');
	return false;
	});
}

function workspace(){

			$(".ws-title").click(function(){
										  $(this).parent().find(".ws-body").slideToggle();
									  });
			
			$("#apply-job").click(function(){
										   $("#cv-list").toggleClass('hidden'); return false;});	
}
function alignanswers(){

	temp=maxWidth=0;
	$(".vote-results").find('li').each(function(){
				var span=$(this).find('.answer');
				temp=span.width();
				
					if(temp>maxWidth)
						maxWidth=temp;
																	 
			 });
	
	$(".vote-results").find('.answer').css('width',maxWidth);
	
	}

function vote(){
	
	
	$("#vote-results").click(function(){
		if(!$(this).hasClass('loader')&&!$(this).hasClass('disabled')){
		//$(this).addClass('loader');
		$phpfile='_ajax-vote-results.php';
		$v_id=$('input[name=v_id]').attr('value');
		$lang=$('input[name=lang]').attr('value');
		if(typeof results!='undefined')
				results.close();
		
		h=(screen.height);
		w=(screen.width);
		t=(h/2)-175;
		l=(w/2)-175;
		
		results=window.open($phpfile+'?popup=1&lang='+$lang+'&v_id='+$v_id,'results',"status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=0,height=350,width=350,top="+t+",left="+l);
		return false;
		$.get($phpfile,{'v_id':$v_id,'lang':$lang},function(data){
				
				
				$('#vote-area').html(data);							   
				$('#vote-results').removeClass('loader');
				$('#vote').addClass('disabled');
				alignanswers();
				return false;
		});
		}
		
	});
	
	$("#vote").click(function(){
		if(!$(this).hasClass('loader')&&!$(this).hasClass('disabled')){
		
		$phpfile='_ajax-vote.php';
		$(this).addClass('loader');
		$('#vote-results').addClass('disabled');
		$v_id=$('form[name=vote_form] input[name=v_id]').attr('value');
		
		$vote=$('form[name=vote_form] input[name=vote]:checked').attr('value');
		alreadypost=$.cookie('talent_vote');
		if(alreadypost!=$v_id)
		$.post($phpfile,{'v_id':$v_id,'vote':$vote},function(data){
				
				if(data==1){							   
				//$('#vote').removeClass('loader').addClass('disabled');
				alreadypost=$.cookie('talent_vote', $v_id,{ expires: 7});
				
				$('#vote').removeClass('loader');
				$('#vote-results').removeClass('disabled').click();
				}
				return false;
		});
		else{
			alert(alreadyvote);
			$('#vote').removeClass('loader');
			$('#vote-results').removeClass('disabled')
		}
							}
							
		});
	
	
	
}

function newsletter(){
	$('#newsletter').click(function(e){
									
		if(!$(this).hasClass('disabled')){
		
		$phpfile='_ajax-newsletter.php';
		$e=$('input[name=newsletteremail]').attr('value');
		lang=$('input[name=lang]').attr('value');
		if(validateEmail($e)){
		$(this).addClass('disabled');
		$('#newsletterarea').addClass('loader');
		$.post($phpfile,{'newsletteremail':$e,'lang':lang},function(data){
				$('#newsletterarea').removeClass('loader');
				$('#newslettermessage').text(data);							   
				$('#newsletter').removeClass('disabled');
				return false;
				});
		}
		else
			alert('Enter a valid Email');
		}	
		//$(this).addClass('disabled');
		});
	
}
//for banners
function slideSwitch() {
	
	
       var $active = $('#slideshow a.active');
	//alert($active.attr('title'));
    if ( $active.length == 0 ) $active = $('#slideshow a:last');
	
	
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow a:first');
		
	//var i= $next.html();
	//alert(i);
	
	 //$active.removeClass('active');
 	$active.addClass('last-active');
	
   $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 3000, function() {
            $active.removeClass('active last-active');
        });
}

function slidehow(){
 var $links= $('#slideshow a');
			if ( $links.length > 1 ){
			setInterval( "slideSwitch()", 4000 );
			}//animate if more than 1	
}

function feedback(){
	
	$('#feedback-form').submit(function(){
										 
	if(!$('#feedback-form').hasClass('loader')){	
	
									 
	phpfile=$('#feedback-form').attr('action');
	
	message=$('textarea[name=message]','#feedback-form').attr('value');
	$lang=$('input[name=lang]').attr('value');
	
	if(message.length==0||message.length>500){
		alert(feedbackval); return false;}
	$('#feedback-form').addClass('loader');
	$.get(phpfile,{message:message,lang:$lang},function(data){
						
			$('#feedback-form').replaceWith(data);
				return false;	
				   });
	}
	return false;		
	});
	
}

function contactLink(){
	
$('#contact-link').click(function(){
								  
		$phpfile=$(this).attr('href');
		
		if(typeof form!='undefined')
				form.close();
		
		h=(screen.height);
		w=(screen.width);
		t=0;
		l=175;
		
		form=window.open($phpfile,'form',"status=0,toolbar=0,location=0,menubar=0,height=700,width=700,top="+t+",left="+l);							  
		return false;				  
  });
}

function contactTemplates(){
	text=$('textarea#message');
		$('select#template').change(function(e){
			obj=$(this);
			t=obj.find('option:selected').attr('value');
			
			if(jQuery.browser.msie )
				text.html(template[t]);
			else
				text.text(template[t]);

			/*if(t==0)
				text.attr('disabled','');
			else
				text.attr('disabled','disabled');*/
			if(t==2){$('#contact-jobseeker-form').addClass('rtl')}
			else $('#contact-jobseeker-form').removeClass('rtl');
			$('input[name=template_no]').attr('value',t);
			
			
		});
		
		if(typeof reject!='undefined'){
			dt=1;
			if(lang=='ar'){
				dt=2;
				
			}
			
		text.text(template[dt]);
		$('select#template option:eq('+dt+')').attr('selected','selected');
		$('input[name=template_no]').attr('value',dt);
		}
		$('#contact-jobseeker-form').submit(function(e){
			//text.attr('disabled','');
			valid=true;
			
			$(this).find('.req').each(function(){
				
				if($(this).attr('value')==''){
					id=$(this).attr('id');
					label=$('label[for='+id+']').text();
					
					alert(fill+' '+label);
					$(this).focus();
					valid=false;
					return valid;
				}
		   });
			
				if(valid)
				$('textarea#message').attr('disabled','');
				return valid;
			
		 });
}

function bopopup(){
	
	$("a.bopopup").click(function(){
		$phpfile=$(this).attr('href');
		h=(screen.height);
		w=(screen.width);
		t=(h/2)-175;
		l=(w/2)-175;
		results=window.open($phpfile,'rejection',"status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=0,height=650,width=650,top="+t+",left="+l);
		return false;
	 });
	
}

function clean(){
	
	if($('#cv-details').length>0){
		
		$('.ol-set:empty','#cv-details').parent().remove();
	}
}
function datePicker(){
	format='yy-mm-dd';
	$('.date-picker').datepicker({dateFormat:format});
	}
	
	function cvsearch(){
		var $select=$('select#cv-category-select','#cv-search');
		var $experience=$('li#exp-range-li','#cv-search');
		var value=$select.val();
		
		
		var exp_view=function(v){
			
			if(!v)
				$experience.hide();
			else
				$experience.show();
		}
		exp_view(value);
		$select.change(function(){		var v=$(this).val();
										exp_view(v);
								});
		
		
	}
	
	function confirmDeleteInbox(){
		
	$('a.deleteinbox','#cvbox').click(function(){
				
				if(!confirm(delete_inbox_confirm))
					return false;
				
					
				})	
	}
$(document).ready(
			function() {
			//$("#cv-details").find("span.hide-p").parent().parent().remove();
			
		
	         tabs();
			 rollup();
			 menu();
			focusblur();
			oddeven();
			help();
			cv_rows();
			multiple();
			printButton();
			workspace();
			vote();
			newsletter();
			slidehow();
			feedback();
			//spelling();
			clean();
			confirmDeleteInbox();
			 contactTemplates();
			 if(typeof BO!='undefined')
			 bopopup();
			 
			 cvsearch();
			}
		);



function log_type()
{	lang=$("input[name=lang]").attr("value");
	if(document.login_form.login_type.value==1)
		{
			document.login_form.action="employer.php?cAction=login&lang="+lang;
			//$("#newuser").attr("href","employer.php?cAction=AddForm&lang="+lang);
			//$("#newuser").text(new_user_emp);
			$("#forgotpass").attr("href","employer.php?cAction=forget&lang="+lang);}
	else
		{
			document.login_form.action="job-seeker.php?cAction=login&lang="+lang;
			//$("#newuser").attr("href","job-seeker.php?cAction=AddForm&lang="+lang);
			//$("#newuser").text(new_user_js);
			$("#forgotpass").attr("href","job-seeker.php?cAction=forget&lang="+lang);}
}

