var pageLimit=20;
var pageStart=0;
var popup_top,popup_left,popup_height,popup_width = 0;

function di(refer){
	return document.getElementById(refer);
}

function goTo(i){
  pageStart=i;
  writeTOC();
}

function removeAllChildNodes(target_element, length_node){
	if(target_element.hasChildNodes()){
		while(target_element.childNodes.length >= length_node){
			target_element.removeChild(target_element.firstChild);
		}
	}
}


function Press(market, pdfname, title, date){
	this.market = market;
	this.pdfname = pdfname;
	this.title = title;
	this.date = date;
	
	this.checkValues = function(valueArray){
		var mtype = valueArray[0];
		var from_month = valueArray[1];
		var from_year = valueArray[2];
		var to_month = valueArray[3];
		var to_year = valueArray[4];
		var from_date = new Date();
			from_date.setFullYear(from_year,from_month,1);
		var to_date = new Date();
		if(to_month==11){
			to_date.setFullYear((1*to_year+1),1,1);
		}else{
			to_date.setFullYear(to_year,(1*to_month+1),1);
		}
		to_date.setDate(to_date.getDate()-1);
		
		if( (mtype=='all_type' || mtype==this.market || this.market=='')
			&& (this.date>=from_date && this.date<=to_date)
		   ){
		   	return true;
		}else{
			return false;
		}
	}

	this.myDisplay = function(){
		var str = '<b>'+((this.date.getMonth()+1)<10 ? '0':'')+(this.date.getMonth()+1)+'/'+(this.date.getDate()<10 ? '0':'')+ this.date.getDate()+'/'+this.date.getFullYear()+'</b> - ';
		str += '<span class="user_link">'+this.title+'</span>';
		return str;
	}
}


function resetLists(){
  var today = new Date();
  document.forms["market"].elements["market_select"].selectedIndex = 0;
  document.forms["from_month"].elements["from_month_select"].selectedIndex = 0;
  document.forms["from_year"].elements["from_year_select"].selectedIndex = 0;
  document.forms["to_month"].elements["to_month_select"].selectedIndex = today.getMonth();
  document.forms["to_year"].elements["to_year_select"].selectedIndex = (document.forms["to_year"].elements["to_year_select"].length - 1);
  removeAllChildNodes(di('container_toc'),1);
  update_TOC();
}


function sort_pr(a,b){
  if(a.date>b.date){
    return -1;
  }else if(a.date==b.date){
    return 0;
  }else{
    return 1;
  }
}


function update_TOC(){
  disp_array = [];
  var cur_vals = new Array();
  	cur_vals.push(di('market_select').options[di('market_select').selectedIndex].value);
  	cur_vals.push(di('from_month_select').options[di('from_month_select').selectedIndex].value);
  	cur_vals.push(di('from_year_select').options[di('from_year_select').selectedIndex].value);
  	cur_vals.push(di('to_month_select').options[di('to_month_select').selectedIndex].value);
  	cur_vals.push(di('to_year_select').options[di('to_year_select').selectedIndex].value);

  	
  for(i=0; i<master_list.length; i++){
  	if(master_list[i].checkValues(cur_vals)){
    	disp_array.push(master_list[i]);
    }
  }
  writeTOC();
}


function writeTOC(){
  disp_array.sort(sort_pr);
  removeAllChildNodes(di('container_toc'),1);
  var parse_list = new Array();
  var parent_div = document.createElement('div');
  parent_div.className = 'principal_div';

  var endDisp = Math.min(disp_array.length,pageStart+pageLimit);
  for(i=pageStart; i<endDisp; i++){
    var temp_div = document.createElement('a');
    temp_div.className='inner_content';
    temp_div.isIn=false;
    temp_div.target = 'press';
    temp_div.href='/press_release/'+disp_array[i].pdfname+'#page=1';
    temp_div.style.display='block';

    temp_div.onmouseover = function() {
      if(!temp_div.isIn){
        this.isIn=true;
        this.className='inner_content_sel';
      }
    }
    temp_div.onmouseout = function(e) {
      this.isIn=false;
      this.className='inner_content';
    };
    
    temp_div.innerHTML = disp_array[i].myDisplay();
    parent_div.appendChild(temp_div);  
  }


    var head_word = 'Press Releases for ';
    if(di('market_select').selectedIndex==0){
    	head_word += di('market_select').options[di('market_select').selectedIndex].text;
    }else{
    	head_word += di('market_select').options[di('market_select').selectedIndex].text + ' Market';
    }
    	head_word += ' from ' + di('from_month_select').options[di('from_month_select').selectedIndex].text;
    	head_word += ' ' + di('from_year_select').options[di('from_year_select').selectedIndex].text;
    	head_word += ' to ' + di('to_month_select').options[di('to_month_select').selectedIndex].text;
    	head_word += ' '+ di('to_year_select').options[di('to_year_select').selectedIndex].text;
    	
	if(disp_array.length==0){
		parent_div.innerHTML = head_word+'</div><div class="inner_content"></br>None</div>';
	}else{
	  var head_title = document.createElement('span');
      	var searchBox = document.createElement('div');
      	searchBox.className='search_box';
      	var endDisp = Math.min(disp_array.length,pageStart+pageLimit);
      	var searchBoxExtra = '';
      	var searchBoxExtraEnd = '';
	  if(disp_array.length>pageLimit){
		  searchBoxExtra='<div style="margin-top: 10px">';
		  searchBoxExtraEnd='</div>';
		  var navHTML = '<div class="result_right">';
		  if(pageStart==0){
			navHTML+='<span style="color: #666666">|&lt;&lt; '+navText[0]+'</span> - <span style="color: #666666">&lt; '+navText[1]+'</span>';
		  }else{
			navHTML+='<a href="javascript: goTo(0)">|&lt;&lt; '+navText[0]+'</a> - <a href="javascript: goTo('+(pageStart-pageLimit)+')">&lt; '+navText[1]+'</a>';
		  }
		  var max=(pageLimit*Math.floor((disp_array.length-1)/pageLimit));
		  if(pageStart==max){
			navHTML+=' - <span style="color: #666666">'+navText[2]+' &gt;</span> - <span style="color: #666666">'+navText[3]+' &gt;&gt;|</span>';
		  }else{
			navHTML+=' - <a href="javascript: goTo('+(pageStart+pageLimit)+')">'+navText[2]+' &gt;</a> - <a href="javascript: goTo('+max+')">'+navText[3]+' &gt;&gt;|</a>';
		  }
		  searchBox.innerHTML += navHTML+'</div>';
 	  }
      	searchBox.innerHTML += '<div class="result">Results <b>'+(pageStart+1)+'</b> - <b>'+endDisp+'</b> of <b>'+disp_array.length+'</b></div>'; 	  
	head_title.innerHTML = '<div class="head_word">'+head_word + searchBoxExtra + searchBox.innerHTML + searchBoxExtraEnd+'</div>';
	parent_div.insertBefore(head_title,parent_div.firstChild);
	parent_div.appendChild(searchBox);
  }	
  di('container_toc').appendChild(parent_div);
}
