﻿var xmlUrl = "http://www.pickpackpont.hu/stores/storelist.xml";
var selectedShopId = 0;

$(document).ready(function () {
    //$('#content').empty();
    //Sys.Net.WebServiceProxy.invoke(xmlUrl, null, true, null, onComplete);

});


function onComplete(results) {
    LoadData(results[0].d);
}


function createXmlDOMObject(xml) {
    var xmlDoc = null;

    if (!window.DOMParser) {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(xml);
    }
    else {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(xml, "text/xml");
    }

    return xmlDoc;
}


function LoadData(xml) {
	a = "in";
	var c=0;
    var xmlDoc = createXmlDOMObject(xml);
    var out = "";
    var megye = "";
    var varos = "";
    var bolt = "";
    $(xmlDoc).find('county').each(function (index, item) {
    	insertOption($(this).find('name').text(),$(this).find('name').text(),"psel1");
    	
    	megye = $(this).find('name').text();
    	
    	/*    	
        var content = "<div><span id='county" + index + "' class='extendercounty'>+</span></div><div class='extendercountylabel'>";        
        content += name + "</div><div class='clear' ></div>";
        */
        var name = $(this).find('name').text();
        $(this).find('location').each(function (index2, item2) {
        var locationname = $(this).find('locationname').text();
        
        varos = $(this).find('locationname').text();
        /*
            content += "<div><span id='" + locationname.replace(' ', '').replace('.', '') + "' class='extenderlocation nodisplay " + name.replace(' ', '') + "' onclick='javascript: SetLocation(this)'>+</span></div><div class='extenderlocationlabel nodisplay " + name.replace(' ', '') + "' id='name" + locationname + "'>" + locationname + "</div><div class='clear'></div>"
            content += "<div class='extenderstore nodisplay " + locationname.replace(' ', '').replace('.', '') + "'  id ='store" + locationname.replace(' ', '').replace('.', '') + "'>";
        */    
            
        $(this).find('store').each(function (index3, item3) {
                var title = $(this).find('title').text();
                var address = $(this).find('address').text();
                var type = $(this).find('type').text();
                var id = $(this).find('id').text();
                
                content = "";
                /*
                content += "<div class='extenderstoreitem' id='" + id + "' onclick='javascript: SetSelection(this)'>"
                content += title + "<br />";
                content += address + " <br /> Boltkód: " + id + "</div><div class='clear' ></div>";
                */
                
                bolt = $(this).find('title').text();
                
                out += megye + " - " + varos + " - " + bolt + "\n";
                tomb[c] = megye + ";" + varos + ";" + bolt;
                c++;
                
        });
        
        /*
        	content += "</div><div class='clear' ></div>"
        */
        		
        });
        $('#content').append($(content));
        $("#county" + index).bind('click', function (event) {
            var actval = ($(this).text());
            if (actval == "+") {
                $.each($('.' + name.replace(' ', '')), function (key, option) {
                    $(this).removeClass("nodisplay");
                });
                $(this).text("-");
            } else {
                $.each($('.' + name.replace(' ', '')), function (key, option) {
                    $(this).addClass("nodisplay");
                    HideItem($(this));
                });
                $(this).text("+");
            }
        });

    });
    //alert(tomb.valueOf());
}

function SetSelection(item) {
    selectedShopId = $(item).attr("id");
    //alert(selectedShopId);
};

function SetLocation(item) {
  var id = $(item).attr("id").replace(' ', '').replace('.', '');
  if (id.substr(0, 4) != "name") {
      var actval = $('#' + id.replace(' ', '')).text();
      if (actval == "+") {
          $.each($('.' + id), function (key, option) {
              $(this).removeClass("nodisplay");
          });
          $('#' + id).text("-");
      } else {
          $.each($('.' + id), function (key, option) {
              $(this).addClass("nodisplay");
          });
          $('#' + id).text("+");
      }
  }
  //alert(id);
};

function HideItem(item) {
    var id = $(item).attr("id").replace(' ', '').replace('.', '');
    if (id.substr(0, 4) != "name") {
        $.each($('.' + id), function (key, option) {
            $(this).addClass("nodisplay");
        });
        $('#' + id).text("+");
    }
}



