var newsLineWidth;
var newsLineEffects;
var spanFlagLeft;
var allNews;
var newsTimer;
var activeNews = 0;

var hotJobsCount = 0;
var hotJobIndex = 0;
var hotJobIndexTemp = 0;
var hotJobContentHeight = 0;
var hotJobEffectIsBusy = false;
var cat_obj, sal_obj, loc_obj;

//======================
function clear_search(num)
{
             $('cc_category').set('html',"Select Role");
             cat_obj.selectedIndex=-1;
             $('cc_location').set('html',"Select Location");
             loc_obj.selectedIndex=-1;
             $('cc_salary').set('html',"Select Salary");
             sal_obj.selectedIndex=-1;
}
//======================
function init_search()
{
  if(category_id == 0 )
  {
  	$('cc_category').set('html',"Select Role");
  	cat_obj.selectedIndex=-1;
  }
  if(location_id == 0 )
   {
     $('cc_location').set('html',"Select Location");
     loc_obj.selectedIndex=-1;
   }
  if(salary_range_id == 0)
   {
     $('cc_salary').set('html',"Select Salary");
     sal_obj.selectedIndex=-1;
   }
}
//=====================
function set_underline(obj)
{
	$(obj).setStyle('text-decoration', 'underline');
}
//=====================
function unset_underline(obj)
{
	$(obj).setStyle('text-decoration', 'none');
}
//====================================
var nextNews = function()
{
	 if (activeNews >= allNews.length){activeNews = 0;}
     var newscontent = '<a href="'+site_url+'/news/'+allNews[activeNews].id+'"/>'+allNews[activeNews].header+'</a>';
     $('news_line_date').innerHTML = allNews[activeNews].date;

	 $('news_line_content').innerHTML = '<span id="span_flag" style="float:left"></span>';
	 spanFlagLeft = $('span_flag').getCoordinates().left;
	 $('news_line_content').innerHTML = newscontent+'<span id="span_flag" style="float:left"></span>';
	 newsLineWidth = $('span_flag').getCoordinates().left;
	 newsLineWidth = newsLineWidth - spanFlagLeft;
     $('news_line_wrapper').setStyle('width','0px');
     $('cursor').className = '';
     newsLineEffects = new Fx.Tween('news_line_wrapper', {duration: 500, transition: Fx.Transitions.linear, onComplete: function(){$('cursor').className = 'hidden';}});
     newsLineEffects.start('width', newsLineWidth+'px');
	 activeNews++;
}
//====================================
var morph = function()
{
  var morphEffect = new Fx.Tween(this, {duration: 1000, transition: Fx.Transitions.linear});
  var morphNextEffect = new Fx.Tween(this.getNext(), {duration: 1000, transition: Fx.Transitions.linear});
  if (this.getNext().style.opacity == 0)
  {
      morphEffect.start('opacity','0');
      morphNextEffect.start('opacity','1');
  }
  else
  {
      morphEffect.start('opacity','1');
      morphNextEffect.start('opacity','0');
  }
}
//====================================
/*function moveNewsLine()
{
   var newsMarginLeft = $('news_line_content').getStyle('margin-left');
   newsMarginLeft = newsMarginLeft.substring(0, newsMarginLeft.length-2);

   if (newsMarginLeft*1 < -(newsLineWidth*1))
   {
   	    $('news_line_content').setStyle('margin-left', '498px');
   	    newsMarginLeft = 498;
   }
   newsLineEffects.start('margin-left', newsMarginLeft*1-50+'px');
}*/
//====================================
function setMorphMargins()
{  $$('.morph').each( function ( obj ) {
     var nextclass = '';
     var nextobj;
     try{
     	nextobj = obj.getNext();
     	nextclass = nextobj.className;
     }catch(e){};

     if (nextclass == 'unvisible')
     {
          var h = obj.getFirst().height*1;
          nextobj.setStyle('margin-top','-'+h*1+'px');
          nextobj.setStyle('opacity','0');
          morph.periodical(10000, obj);
     }
 });
}
//====================================
function showHotJobsFromIndex()
{  if(hotJobsCount<=hotJobIndexTemp)
  {  	 index = 0;
  	 hotJobIndex = hotJobIndexTemp;
  }

  $$('.hot_job').each( function ( obj ) {
        obj.className = 'hot_job hidden';
  });

  $$('.hot_job_line').each( function ( obj ) {  	    obj.className = 'hot_job_line hidden';
  });
  $('hot_jobs_right').className = 'hidden';
  $('hot_jobs_left').className = 'hidden';

  if (hotJobsCount>hotJobIndexTemp)
  {
      $('hot_job_'+hotJobIndexTemp).className = 'hot_job';
      if (hotJobsCount>(hotJobIndexTemp*1+1))
      {
         $('hot_job_line_'+hotJobIndexTemp).className = 'hot_job_line';
         $('hot_job_'+(hotJobIndexTemp*1+1)).className = 'hot_job';
      }

      /*
      if (hotJobsCount>(hotJobIndexTemp*1+2))
      {
          $('hot_jobs_right').className = '';
      }
      if (hotJobIndexTemp>0)
      {      	 $('hot_jobs_left').className = '';
      }
      */
      hotJobIndex = hotJobIndexTemp;

      var hot_job_content_height = $('left_panel_content_hot_job').getStyle('height');
      hotJobContentHeight = hot_job_content_height.substring(0, hot_job_content_height.length-2)*1;

      //var hotJobEffects = new Fx.Tween('left_panel_content_wrapper', {duration: 300, transition: Fx.Transitions.Cubic.easeInOut, onComplete: function(){hotJobEffectIsBusy = false;$('hot_jobs_browse').setStyle('opacity','100');}});
	  //hotJobEffects.start('height', hotJobContentHeight+20+'px');

	  hotJobEffectIsBusy = false;//$('hot_jobs_browse').setStyle('opacity','100');
  }
  else
  {     hotJobIndexTemp = 0;
     showHotJobsFromIndex();
  }
}
//====================================
function hotJobRight()
{	hotJobIndexTemp = hotJobIndex + 2;
	showHotJobsFromIndex();
}
//====================================
window.addEvent('domready', function() {

//hot jobs
try{
  hotJobsCount = $('hot_jobs_count').value;
  if (hotJobsCount>0)
  {      $('hot_job_0').className = 'hot_job';
      if (hotJobsCount>1)
      {         $('hot_job_line_0').className = 'hot_job_line';
         $('hot_job_1').className = 'hot_job';
      }
      if (hotJobsCount>2)
      {          //$('hot_jobs_right').className = '';
      }
  }
  var hot_job_content_height = $('left_panel_content_hot_job').getStyle('height');
  hot_job_content_height = hot_job_content_height.substring(0, hot_job_content_height.length-2)*1;
  //$('left_panel_content_wrapper').setStyle('height', hot_job_content_height+20+'px');
  $('left_panel_content_superwrapper').setStyle('height', hot_job_content_height+95+'px');

  $('hot_jobs_right').addEvent('click', function(){
        if (hotJobEffectIsBusy) {return;}
        hotJobEffectIsBusy = true;
        //$('hot_jobs_browse').setStyle('opacity','0');
        hotJobIndexTemp = hotJobIndex + 2;
        //var hotJobEffects = new Fx.Tween('left_panel_content_wrapper', {duration: 300, transition: Fx.Transitions.Cubic.easeInOut, onComplete: showHotJobsFromIndex});
	    //hotJobEffects.start('height', '0px');
	    showHotJobsFromIndex();  });

  $('hot_jobs_left').addEvent('click', function(){
        if (hotJobEffectIsBusy) {return;}
        hotJobEffectIsBusy = true;
        //$('hot_jobs_browse').setStyle('opacity','0');
        hotJobIndexTemp = hotJobIndex - 2;
        //var hotJobEffects = new Fx.Tween('left_panel_content_wrapper', {duration: 300, transition: Fx.Transitions.Cubic.easeInOut, onComplete: showHotJobsFromIndex});
	    //hotJobEffects.start('height', '0px');
	    showHotJobsFromIndex();
  });

  hotJobRight.periodical(3000);

  $('hot_jobs_browse').addEvent('click', function(){
      $('left_panel_content_hot_job').innerHTML =
        '<form id="jhot_form" action="'+site_url+'/hot-jobs" method="post">'+
        '<input type="hidden" name="category_id" value="'+category_id+'" />'+
        '<input type="hidden" name="job_id" value="0" />'+
        '</form>';
        $('jhot_form').submit();
  });

  $$('.hot_job').each( function ( obj ) {
      obj.addEvent('click',function(){
      $('left_panel_content_hot_job').innerHTML =
        '<form id="jhot_form" action="'+site_url+'/hot-jobs" method="post">'+
        '<input type="hidden" name="category_id" value="'+category_id+'" />'+
        '<input type="hidden" name="job_id" value="'+obj.getFirst().value+'" />'+
        '</form>';
        $('jhot_form').submit();
     });
  });
}catch(e){}

  //navigation
  $$('.nav_item').addEvent('click', function(){
      window.location = site_url+'/'+this.id;
  });



  //morph pictures
  $$('.morph').each( function ( obj ) {
     var nextclass = '';
     var nextobj;
     try{
     	nextobj = obj.getNext();
     	nextclass = nextobj.className;
     }catch(e){};

     if (nextclass == 'unvisible')
     {
          nextobj.setStyle('opacity','0');
     }
 });
 setMorphMargins.delay(1000);

 //customized drop down and init search
 try{
 cat_obj=$('category').styleSelect('category');
 sal_obj=$('salary').styleSelect('salary');
 loc_obj=$('location').styleSelect('location');
 init_search();
 } catch(e){}
 //news line
 if ($('news_headers') != null)
 {
	 eval("allNews = [" + $('news_headers').value + ']');
	 if (activeNews >= allNews.length){activeNews = 0;}
	 var newscontent = '<a href="'+site_url+'/news/'+allNews[activeNews].id+'"/>'+allNews[activeNews].header+'</a>';
	 $('news_line_date').innerHTML = allNews[activeNews].date;

	 $('news_line_content').innerHTML = '<span id="span_flag" style="float:left"></span>';
	 spanFlagLeft = $('span_flag').getCoordinates().left;
	 $('news_line_content').innerHTML = newscontent+'<span id="span_flag" style="float:left"></span>';
	 newsLineWidth = $('span_flag').getCoordinates().left;
	 newsLineWidth = newsLineWidth - spanFlagLeft;
	 $('news_line_wrapper').setStyle('width','0px');
	 $('cursor').className = '';
	 newsLineEffects = new Fx.Tween('news_line_wrapper', {duration: 500, transition: Fx.Transitions.linear, onComplete: function(){$('cursor').className = 'hidden';}});
	 newsLineEffects.start('width', newsLineWidth+'px');

	 activeNews++;
	 newsTimer = nextNews.periodical(8000);

	 $('news_line_right').addEvent('click', function(){	 	    $clear(newsTimer);
	 	    nextNews();
	 	    newsTimer = nextNews.periodical(8000);	 });
	 $('news_line_left').addEvent('click', function(){
	 	    $clear(newsTimer);
	 	    activeNews--;
	 	    activeNews--;
	 	    if (activeNews<0){activeNews = allNews.length-1;}
	 	    nextNews();
	 	    newsTimer = nextNews.periodical(8000);
	 });
 }

});