﻿
function array_unique(array){
    var r = new Array();
	o:for(var i = 0, n = array.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==array[i])
			{
				continue o;
			}
		}
		r[r.length] = array[i];
	}
	return r;
}

function array_search(array, elem){
	for(var i = 0; i < array.length; i++)
	{
        if(array[i] == elem) return i;
	}
	return -1;
}



function PRICE_RANGES(args)
{
    if(args){this.args=args;}
    this.set_default_args();
}

PRICE_RANGES.prototype.init_complete = false;
PRICE_RANGES.prototype.price_section_pairs = [];
PRICE_RANGES.prototype.range_to_sections_map = [];
PRICE_RANGES.prototype.sections_colors_map = new Object();

//ELI default parameters
PRICE_RANGES.prototype.args={
    id:null,
    colors:[0xffAC64DB, 0xff9C38DB, 0xff7002B6, 0xff612389, 0xff490176],
    grey_color:0xff868686
}

PRICE_RANGES.prototype.set_default_args=function()
{       
    if(!this.args.colors) {
        this.args.colors=[0xffAC64DB, 0xff9C38DB, 0xff7002B6, 0xff612389, 0xff490176]; 
    }
    if(!this.args.grey_color) {
        this.args.grey_color=0xff868686; 
    }
};


PRICE_RANGES.prototype.init_price_ranges=function(prices, sections, min, max)
{
    //set price ranges
    if(!this.init_complete)
    {
        var split_prices_results = prices.split(';');
        split_prices_results.pop(); //empty string
        var split_sections_results = sections.split(';');
        split_sections_results.pop(); //empty string
        //var price_section_pairs = [];
        for(var i = 0; i < split_prices_results.length; i++)
        {
            this.price_section_pairs.push(new KeyValuePair(split_prices_results[i], split_sections_results[i]));
            this.sections_colors_map[split_sections_results[i]] = this.args.grey_color;
        }
        this.apply_price_ranges(this.price_section_pairs, min, max, this.args.id);
        this.init_complete = true;
    }
}
    
    
PRICE_RANGES.prototype.compute_num_of_points=function(data_length)
{
    if(data_length > 30) {
        return 4;
    }
    else if(data_length > 15) {
        return 3;
    }    
    else if(data_length > 10) {
        return 2;
    }    
    else
        return 1;        
}
    
PRICE_RANGES.prototype.apply_price_ranges=function(price_section_pairs, min, max, id)
{
    var points = this.get_dividing_points(price_section_pairs, this.compute_num_of_points(price_section_pairs.length), min, max); 
    var html = "";
    //var html = createButtonHTML("Under $" + points[0]);
    for(var i = 0; i < points.length - 1; i++)
    {
        html += this.createRangeButtonHTML("$" + points[i] + " - $" + points[i+1], points[i], points[i+1], i, i + 1);
    }
    //html += createButtonHTML("Above $" + points[points.length - 1]);
    if(points.length == 2) html = ""; //special case only one price range
    html += this.createRangeButtonHTML("All", points[0], points[points.length - 1], 0, points.length - 1);
    $(id).innerHTML = html + $(id).innerHTML; 
}
    
PRICE_RANGES.prototype.createRangeButtonHTML=function(label, min, max, range_start, range_end)
{
    //return '<a class="button" href="javascript:void(0);" onclick="priceRangeButtonClick(' + min + ',' + max + ')"><span>' + label + '</span></a>';
    var color = null;
    if(range_end - range_start != 1) {
        color = "000000";
    } else {
        color = this.args.colors[range_start].toString(16).substr(2);
    }       
    return '<input class="button_pr" style="background-color:#' + color + '" type="submit" onclick="framework.price_ranges.priceRangeButtonClick(' + min + ',' + max + ',' + range_start + ',' + range_end + ')" value="' + label + '"/>';
}
    
    
    
    
PRICE_RANGES.prototype.priceRangeButtonClick=function(min,max,range_start,range_end)
{
    $('input_min').value = min;
    $('input_max').value = max;
    //framework.zoomer.reset_section_highlights();
    //TODO: disable apply_to_zoomer
    framework.filter.reset_selected_sections();
    if(range_end - range_start == 1) {
         framework.filter.apply({params:{color:this.args.colors[range_start]}});
    }
    else {
        framework.filter.apply();
    }    
}
    
function sortNumber(a,b)
{
    return a._strKey - b._strKey;
}

PRICE_RANGES.prototype.get_dividing_points=function(price_section_pairs, num_of_points, min, max)
{
    var result = [];
    price_section_pairs.sort(sortNumber);
    result.push(min);
    
    var index = 0;
    var old_index = 0;
    for(var i = 1; i < num_of_points; i++)
    {
        index = Math.round((price_section_pairs.length * i)/num_of_points);
        result.push(price_section_pairs[index]._strKey);
        this.add_range(price_section_pairs, this.range_to_sections_map, old_index, index);
        old_index = index;
    }
    result.push(max);
    this.add_range(price_section_pairs, this.range_to_sections_map, old_index, price_section_pairs.length); 
    return result;   
}

PRICE_RANGES.prototype.add_range=function(price_section_pairs, range_to_sections_map, old_index, index)
{
    var map = new Object();
    for(var i = old_index; i < index; i++)
    {
        map[price_section_pairs[i]._strValue] = price_section_pairs[i]._strValue;
    }
    this.range_to_sections_map[this.range_to_sections_map.length] = map;
}

PRICE_RANGES.prototype.get_section_color=function(sec_name)
{
    for(var i = 0; i < this.range_to_sections_map.length; i++)
    {
        if(this.range_to_sections_map[i][sec_name]) return this.args.colors[i];
    }
    return null;
}


//PRICE_RANGES.prototype.show_sections=function(sections, color)
//{
//    if(this.args.debug!=null) this.args.debug.time("show_sections");
//     framework.zoomer.reset_section_highlights();   
//     //remove duplicates
//     var split_sections_results = sections.split(';');
//     if(split_sections_results[split_sections_results.length - 1] == "") {
//        split_sections_results.pop(); //empty string
//     }   
//     split_sections_results = split_sections_results.unique();

//     var gray_sections = new Array();
//     var tmp_color = color;
//     for(var i = 0; i < this.range_to_sections_map.length; i++) {
//        var colored_sections = new Array();          
//        if(color == null) {
//            tmp_color = this.args.colors[i];
//        } else {
//            tmp_color = color;
//        }  
//                     
//        for (var j in this.range_to_sections_map[i]) {
//            if(split_sections_results.search(j) != -1) {
//               var j_corrected = j; 
//               if(j == "BL") j_corrected = "GA";
//               colored_sections.push(j_corrected);           
//            } else {
//               var j_corrected = j;  
//               if(j == "BL") 
//                    j_corrected = "GA";
//                    
//               if(gray_sections.search(j_corrected) == -1)     
//                    gray_sections.push(j_corrected); 
//            }
//        }
//        if(colored_sections.length > 0) framework.zoomer.highlight_sections(colored_sections.join("$,"), tmp_color);
//     }
//     framework.zoomer.highlight_sections(gray_sections.join("$,"), this.args.grey_color);
//     if(this.args.debug!=null) this.args.debug.time("show_sections",true);
//}


PRICE_RANGES.prototype.show_sections=function(sections, color)
{   
    if(this.args.debug!=null) this.args.debug.time("show_sections");
    //remove duplicates
    var split_sections_results = sections.split(';');
    if(split_sections_results[split_sections_results.length - 1] == "") {
    split_sections_results.pop(); //empty string
    } 
    
    var diff = new Object();  
    for(var section in this.sections_colors_map) {
        var tmp_color = color;
        //find color
        if(array_search(split_sections_results, section) != -1) {
            if(color == null) {
                tmp_color = this.get_section_color(section);
            } else {
                tmp_color = color;
            }  
        } else {
            tmp_color = this.args.grey_color;
        }
        
        if(this.sections_colors_map[section] != tmp_color) {
            this.sections_colors_map[section] = tmp_color;
            //handle GA and BL separatly
            if(section == "BL" || section == "GA") {
                if(tmp_color == this.args.grey_color)
                    framework.zoomer.reset_section_highlight("GA");
                else
                    framework.zoomer.highlight_section("GA", tmp_color); //zoomer know only about GA
            }

            if(diff[tmp_color])
                diff[tmp_color] += "$," + section;
            else
                diff[tmp_color] = section;    
        }
    }
    //apply new colors
    for(var tmp_color in diff) {
        framework.zoomer.highlight_sections(diff[tmp_color], tmp_color);
    }
    
    if(this.args.debug!=null) this.args.debug.time("show_sections", true);
}




//KeyValuePair function
function KeyValuePair()
{

  //Assigning the Key from the arguments
  this._strKey = (arguments.length == 2) ? arguments[0]: null;
  //Assigning the Key from the arguments
  this._strValue = (arguments.length == 2) ? arguments[1]: null;
 
  // if parameter is defined, the key is set.
  // if parameter is not defined, the key is retrieved.
  this.Key = function()
  {
    if (arguments.length == 1)
      this._strKey = arguments[0];
    else
      return this._strKey;
  }
  // if parameter is defined, the value is set.
  // if parameter is not defined, the value is retrieved.
  this.Value = function()
  {
    if (arguments.length == 1)
      this._strValue = arguments[0];
    else
      return this._strValue;
  }
  // Params: (KeyValuePair objkvp)
  // Return: boolean
  this.Equals = function(oKV)
  {
    try
    {
      if (oKV.GetType() == this.GetType())
      {
        if (this._strKey == oKV.Key() && this._strValue == oKV.Value())
        {
          return true;
        }
      }
    }
    catch (e)
    {
      throw "(Exception: kvp_e0) Invalid parameter type";
    }
    return false;
  }
  // Return: string
  this.GetType = function()
  {
    return "Library.KeyValuePair";
  }
  // Return: string (delimits the key and value if provided)
  this.ToString = function()
  {
    var strDelimiter = (arguments.length == 1) ? arguments[0]: "";
    return this._strKey + strDelimiter + this._strValue;
  }
}