var headline_count;
var headline_interval;
var current_headline;
var old_headline;
var init_height;
var job_id;

function headline_rotate(){
	current_headline = (old_headline + 1) % headline_count;
	$("div.headline:eq(" + old_headline + ")").animate({top: -205},"slow",function(){
		$(this).css("top","210px");
	});
	$("div.headline:eq(" + current_headline + ")").animate({top: 5},"slow");
	old_headline = current_headline;
}

function getEmail(address){
	var encryptedemail_id14='';
	switch(address){
		case 'info':
			var emailriddlerarray=[105,110,102,111,64,97,115,115,117,114,101,100,114,101,99,114,117,105,116,46,99,111,109];
			break;
			
		default:
			return 'email address not found';
	}
	for(var i=0; i<emailriddlerarray.length; i++){
		encryptedemail_id14 += String.fromCharCode(emailriddlerarray[i]);
	}
	return '<a href="mailto:'+encryptedemail_id14+'">'+encryptedemail_id14+'</a>';
}

$(document).ready(function() {
	$("#left_menu").css({"display":"none"});
  	$("div#search_hidden_form").css('display','none');
	$("div#search_hidden_text").css('display','block');

	$("div.link-button").mouseover(function(){
	 	$image = $(this).attr('id');
	 	$("div.home-image").css({"display":"none"});
		$("div."+$image).css({"display":"block"});
	
	});
	
	headline_count = $("div.headline").size();
	$("div.headline:eq(" + current_headline + ")").css("top","5px");
	
	headline_interval = setInterval(headline_rotate,5000);
	$("#scrollup").hover(function(){
		clearInterval(headline_interval);
	}, function(){
		headline_interval = setInterval(headline_rotate,5000);
		headline_rotate();
	});
	
	$(".js_remove_href").removeAttr('href');
	
	$(".job_search_left").css('display','block');

	$("a.candidate").css({"background-position":"0 0"});
	$("a.employer").css({"background-position":"0 bottom"});
	
	$("a.short_list_item").click(function(e){
		e.preventDefault();
		job_id = $(this).prev().val();
		job_id = job_id * 1;
		var job = $(this).parent().parent().parent();
		$(job).remove();
		removeCookie(job_id);			
	});
	   
	$("a.add_short_list").click(function(e){
		e.preventDefault();
		job_id = $(this).prev().val();
		job_id = job_id * 1;
		if(available(job_id) === false){
			var job = $(this).parent().parent().parent();
			var options = { to: '#short_list_title', className: 'ui-effects-transfer' }; 
			$(job).hide("transfer",options,500,callback);
		}else{
			$(this).parent().append($("#list_message"));
		 	$("#list_message").fadeIn('slow').fadeOut('slow');
		}	
	});   
	
	function callback(){
		var count =	$('#job_count').html();
		count = count * 1;
		count ++;
		$('#job_count').html(count);

	//write cookie here
		 var short_list = readCookie("short_list");
		 if(short_list === null){
			short_list = "";
		}
		short_list = short_list+job_id+',';
		createCookie("short_list",short_list,30);
	}
	
	function createCookie(name,value,days) {
		var expires = '';
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function removeCookie(id){
		var count =	$('#job_count').html();
		count = count * 1;
		count --;
	  	$('#job_count').html(count);
		 var short_list = readCookie("short_list");
		 var new_list = "";
		 if(short_list !== null){
		 
			var ca = short_list.split(',');
			for(var i=0;i < ca.length;i++) {
				if(id != ca[i] && ca[i]!=''){
					new_list = new_list+id+',';
				}
			} 
			
			createCookie("short_list",new_list,30);	 
		  }
	}
	
	function available(val){
		 var short_list = readCookie("short_list");
		 if(short_list === null){
			short_list = "";
		}
		var ca = short_list.split(',');
			for(var i=0;i < ca.length;i++) {
				if(val == ca[i]){
					return true;
				}
			}
		return false;	
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while(c.charAt(0) == ' '){
				c = c.substring(1,c.length);
			}
			if(c.indexOf(nameEQ) === 0){
				return c.substring(nameEQ.length,c.length);
			}
		}
		return null;
	}
	

	$("#clear_list").click(function(e){
		createCookie("short_list","",-1);
	});

		   
	$("a.left_main").click(function(){
		$(this).next().slideToggle(500);
	    return false;
	});
	
	
	$("div.text_heading").click(function(){
		$(this).next().slideToggle(1300);
	});
	
	$("a.close_result").click(function(e){
		var link = $(this);
 		var parent = $(this).parent().next();
 		$(parent).slideToggle("1000",function(){
 			if($(link).text()=="Collapse"){
				$(link).text("Expand");
			}else{
				$(link).text("Collapse");
			}
		});
 		e.preventDefault();	
	}); 
   $("a.send").click(function(e){
		var result = $(this).parent().parent();
		$(result).fadeOut(500,function(){
		$("#search_result").append(result);
		$(result).fadeIn();
 			
 		});
		e.preventDefault();
	}); 
 
   //popup code

	$("div.searchcontent").css({"display":"none"});
	$("#job_search").append($("#popupcontent"));
    var timeout ;
	var width = '293px';
    if(	navigator.appName =="Microsoft Internet Explorer"){
        width = '320px';
	}
	
	
	$("div.searchlabel").hover(function(){
		 clearTimeout(timeout);
		 var current = $("#selected").val();
		
		 var content = $(this).next().find('fieldset');
		 if(content != null){
			 if(current!='0')
		     {
		     	$("#"+current).next().empty();
		   		$("#"+current).next().append($("#popuptext").find("fieldset"));
		   		  content = $(this).next().find('fieldset');
		   	     $("#popuptext").append(content);
			 }else{
			 	$("#popuptext").append(content);
			 	$("#popupcontent").animate({"width":width},"fast");
				
			}
			  $("#selected").val($(this).attr('id'));
		 }
	});
	
	$("#job_search").mouseleave(function(){
		timeout = setTimeout(function(){
			var current = $("#selected").val();
		    if(current!='0')
		     {
		     	$("#closepopup").trigger("click");
			}	
		},1000);
	});
	
	function closePopup(){
	 	var current = $("#selected").val();
	    if(current!='0')
	     {
	     	$("#closepopup").trigger("click");
		}
	}
	
	$("#closepopup").click(function(e){
		e.preventDefault();
		var current = $("#selected").val();
		$("#popupcontent").animate({"width":"0px"},"fast",function(){
			$("#popupcontent").css({"display":"none"});
			$("#"+current).next().empty();
	   		$("#"+current).next().append($("#popuptext").find("fieldset"));
	   	  });
		$("#selected").val('0');
	});	



	$("#search").click(function(e){
	//	 e.preventDefault();
		 var current = $("#selected").val();
		    if(current!='0')
		     {
		     	$("#closepopup").trigger("click");
			}
		 var searchData = $("#job_search").serialize();
		 $.ajax({
			   type: "POST",
	 		   url: "/search/ajax",
	   		   data: searchData,
	   		   success: function(msg){
	     			$("#main").html(msg);
	   			}
		});
	});
	
	$("a.more").click(function(e){
		e.preventDefault();
	   	job_id = $(this).prev().prev().prev().val();
		job_id = job_id * 1;
	 	$("#dialog").html("<img src='/images/loading.gif'>");
		$.ui.dialog.defaults.bgiframe = true;
		$("#dialog").dialog();
		$("#dialog").dialog('open');
		$('#dialog').load('/search/job_info/' + job_id);
//		$.ajax({
//			   type: "GET",
//	 		   url: "/search/job_info/"+job_id,
//	   		   success: function(msg){
//	     			$("#dialog").html(msg);
//	     	   }
//		});
	}); 
	
	
 	$('#job_rotate').innerfade({   
        speed: 'fast',   
        timeout:4000,   
        type: 'sequence',   
        containerheight: '150px'  
    }); 
	$('#featured_clients').innerfade({   
        speed: 'fast',   
        timeout:4500,   
        type: 'sequence',   
        containerheight: '150px'  
    }); 
    $('#newsfeed').innerfade({   
        speed: 'fast',   
        timeout:4500,   
        type: 'sequence',   
        containerheight: '150px'  
    });  
    
    
    $("#sitemenu #submenu ul li a").mouseover(function(){
    	var text = $(this).next().text();
       	if($.trim(text).length!=0){
			$(this).next().css({"display":"block"});
		}
	});
	
	$("#sitemenu #submenu ul li").mouseleave(function(){
		var text = $(this).find("div").text();
       	if($.trim(text).length!=0){
			$(this).find("div").css({"display":"none"});
		}
	});
	
	$("div.menu_toggle").mouseover(function(){
		$(this).prev().css({"background-position":"0 0"});
	});
	$("div.menu_toggle").mouseleave(function(){
		$(this).prev().removeAttr("style");
	});
});

