// $Id$

function compareIds () {
  this.ids = new Array();
  this.max = 10;

  this.addId = compareIdsAddId;
  this.removeId = compareIdsRemoveId;
  this.existsId = compareIdsExistsId;
  this.setFromStr = compareIdsSetFromStr;
  this.getIds = compareIdsGetIds;
  this.toString = compareIdsToString;
  this.removeAll = compareIdsRemoveAll;
}

function compareIdsAddId (id,name) {
  if (this.ids.length < this.max) {
  	this.ids.push(new compareId(id,name));
    return true;
  }
  return false;
}

function compareIdsRemoveId (id) {
  for (x in this.ids) {
    if (this.ids[x].id == id) {
      this.ids.splice(x,1);
    }
  }
}

function compareIdsRemoveAll () {
  this.ids.splice(0,this.ids.length);
}

function compareIdsExistsId (id) {
  for (x in this.ids) {
    if (this.ids[x].id == id) {
      return true;
    }
  }
  return false;
}

function compareIdsSetFromStr (str) {
  var str_split = str.split(';');
  for (x in str_split) {
	  this.addId((str_split[x].split('::'))[0],(str_split[x].split('::'))[1]);
  }
}

function compareIdsGetIds () {
  var ids = new Array();
  for (x in this.ids) {
	  ids.push(this.ids[x].id);
  }
  return ids;
}

function compareIdsToString () {
  var str = new Array();
  for (x in this.ids) {
	  str.push(this.ids[x].id + '::' + this.ids[x].name);
  }
  return str.join(';');
}

function compareId (id,name) {
  this.id = id;
  this.name = name;
}


$(document).ready(function(){
  $("#panel_compare").draggable();

  $("#block_compare .head").mouseover(function() {
    $("#block_compare #tip").show();
  }).mouseout(function() {
    $("#block_compare #tip").hide();
  });

  var compare_str = $.cookies.get('devices_compare');
  var compare_ids = new compareIds();
  if (compare_str) { 
    if (compare_str != 'undefined') {
      compare_ids.setFromStr(compare_str);
      devices_compare_check(compare_ids);
    }
  }
  devices_compare_panel(compare_ids);

  $("#block_compare #clear").click(function () {
    compare_ids.removeAll();
    devices_compare_cookies(compare_ids);
    devices_compare_check(compare_ids);
    devices_compare_panel(compare_ids);
  });

  // Comparision checks and list
  $("input.compare").bind("change click keypress", function () {
    if ($(this).attr('checked')) {
      if (!compare_ids.existsId($(this).val())) {
        if (!compare_ids.addId($(this).val(), $(this).attr('title'))) {
            alert("Можно добавить не более " + compare_ids.max + " устройств!");
            $(this).attr("checked","");
          }
        }
    } else {
      compare_ids.removeId($(this).val());
    }
    devices_compare_cookies(compare_ids);
    devices_compare_check(compare_ids);
    devices_compare_panel(compare_ids);
  });
});

// Turns checkboxes to selected state according to the list
function devices_compare_check(compare_ids) {
  $('input.compare').attr('checked','');
  $.each(compare_ids.getIds(), function () {
    $('#compare'+this).attr('checked','checked');
  });
}

// Builds the compare panel
function devices_compare_panel(compare_ids) {
  $("#block_compare #list").empty();    
  var item = $('#block_compare #source_item');
  $.each(compare_ids.ids, function () {
    var item_new = item.clone();
    item_new.attr("id","compare" + this.id);
    item_new.attr("class","item");
    var link = item_new.find("a");
    link.attr("href", link.attr("href") + this.id);
    link.text(this.name);
    item_new.appendTo("#block_compare #list");

    item_new.find(".remove").click(function () {
      compare_ids.removeId($(this).parent().attr("id").replace(/compare/,""));
      devices_compare_cookies(compare_ids);
      devices_compare_check(compare_ids);
      devices_compare_panel(compare_ids);
    });


  });

  if (compare_ids.getIds().length) {
    $('#block_compare #list').show();
    $('#block_compare #compare').attr('href', $('#block_compare #source_compare').attr('href') + compare_ids.getIds().join(","));
    $('#block_compare #actions').show();
    $('#block_compare #empty').hide();
  } else {
    $('#block_compare #compare').attr('href', '');
    $('#block_compare #actions').hide();
    $('#block_compare #list').hide();
    $('#block_compare #empty').show();
  }
}

function devices_compare_cookies(compare_ids) {
  // Update cookie or clean it
  if (compare_ids.getIds().length) {
    $.cookies.set('devices_compare', compare_ids.toString());
  } else {
    $.cookies.del('devices_compare');
  }
}



