﻿
jQuery(document).ready(
  function() {
    jQuery(".pdt-wrapper").each(attachEventsForTabs);
    autoScroller('marquee', 1);
    if(typeof(regionDataPath) != "undefined") initRegionUI();
  }
);

function attachEventsForTabs(index, tabWrapper){
  var tabs = jQuery(".pdt-menu li", tabWrapper);
  tabs.each(function(tabIndex,tabItem) {
      jQuery(tabItem).click( function(event) {
          event.preventDefault();
          var hallId = tabIndex + 1;
          window.navigate("ProdList.aspx?hall=" + hallId);
        }
      );
      jQuery(tabItem).mouseover( function(event) {
          event.preventDefault();
          jQuery(tabs).each( function(idx, target){
              if( idx == tabIndex )
                jQuery(target).addClass("mark");
              else 
                jQuery(target).removeClass("mark");
            }
          );
          jQuery(tabWrapper).find(".pdt-itembg").each( 
            function(idx, target) {
              if(idx == tabIndex)
                jQuery(target).removeClass("hidden");
              else
                jQuery(target).addClass("hidden");
            }
          );
        } 
      );
    }
  );
}

function initRegionUI() {
  var controllers = {"region": $("selRegion"), "district": $("selDistrict"), "zip": $("txtZip"), "preset": regionPreset };
  jQuery(controllers["region"]).change(function() { updateDistricts(controllers);});
  jQuery(controllers["region"]).keyup(function() { updateDistricts(controllers);});
  jQuery(controllers["district"]).change(function() { updateZIP(controllers);});
  jQuery(controllers["district"]).keyup(function() { updateZIP(controllers);});
  updateRegion(controllers);
}

function updateRegion(controllers) {
  var regionInfo = {};
  jQuery.get( regionDataPath + "UyA.xml", null, function(data) {
    regionInfo = xml2json.parser(data);
    var list = regionInfo['root']['a']['anytype'];
    var selection = controllers["region"];
    while( selection.options.length > 0 ) removeOptionLast( selection );
    appendOptionLast( selection, {"text":"--請選縣市--","value":""} );
    var selectedIndex = 0;
    for(var i=0; i<list.length; i++) {
      if( controllers["preset"]["region"] == list[i].value ) selectedIndex = i + 1;
      appendOptionLast( selection, list[i] );
    }
    selection.selectedIndex = selectedIndex;
    updateDistricts(controllers);
  }, "text");
}

function updateDistricts(controllers) {
  var region = controllers["region"].value;
  var selection = controllers["district"];
  while( selection.options.length > 0 ) removeOptionLast( selection );
  appendOptionLast( selection, {"text":"--請選鄉鎮區--","value":":"} );
  if( region != "" ){
    jQuery.get( regionDataPath + "UyA_" + region + ".xml", null, function(data) {
      districtInfo = xml2json.parser(data);
      var list = districtInfo['root']['a']['anytype'];
      var selectedIndex = 0;
      for(var i=0; i<list.length; i++) {
        if( controllers["preset"]["district"] == list[i].value ) selectedIndex = i+1;
        list[i].value += (":" + list[i].more);
        appendOptionLast( selection, list[i] );
      }
      selection.selectedIndex = selectedIndex;
      updateZIP(controllers);
    }, "text");
  } else {
    updateZIP(controllers);
  }
}

function updateZIP( controllers ){
  var txtInput = controllers["zip"];
  var info = controllers["district"].value;
  var parts = info.split(":");
  txtInput.value = parts[1];
}

function appendOptionLast( selection, item )
{
  var elOptNew = document.createElement('option');
  elOptNew.text = item.text;
  elOptNew.value = item.value;
  try {
    selection.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    selection.add(elOptNew); // IE only
  }
}

function removeOptionLast( selection )
{
  if (selection.length > 0) selection.remove(selection.length - 1);
}

function disableAllInputs(disableFlag)
{
  if(disableFlag==true) {
    jQuery("input").each( 
      function(idx, target) {
        alert(jQuery(target).outerHTML);
        jQuery(target).attr({'readonly':''}); 
      }
    );
  } else {
    alert('ss');
  }
}