
Event.onDOMReady(function() {
	new DropDownSelector($('support-devices-devicetype'), $('support-devices-manufacturer'), $('support-devices-software'), $('support-devices-submit'), null, null);
});




// From old StoreSelector JS
DropDownSelector = Class.create();
DropDownSelector.prototype = {	

	deviceDictionary: null,
	
	deviceTypeEl: [],
	manufacturerEl: [],
	softwareEl: [],
	submitButton: false,
	cookiedDevicetype: false,
	cookiedManufacturer: false,
	cookiedSoftware: false,
	activateButton: false,
	
	selectedDeviceTypes: false,
	selectedManufacturers: false,
	selectedSoftwares: false,
	
	deviceTypeArr: [],
	manufacturerArr: [],
	softwareArr: [],
	
	isPHP: false,
	
	// linkType options: genius, procare, business, personal, promo, workshop, and store.
	initialize: function(deviceTypeEl, manufacturerEl, softwareEl, submitButton) {
		var loc = window.location.href;
		if(loc.match(/\.php/)) this.isPHP = true;
		
        //Backward compatibility and default check:
        this.phonePropertyName = (typeof phonePropertyName === "string") ? phonePropertyName : "phone";
		
		this.deviceDictionary = compatibilityMatrix;
		
		this.deviceTypeEl = deviceTypeEl;
		this.manufacturerEl = manufacturerEl;
		this.softwareEl = softwareEl;
		this.submitButton = submitButton;
		
		// if(Cookie.get('support_devices_devicetype') && this.submitButton) this.cookiedDevicetype = Cookie.get('support_devices_devicetype');
		// if(Cookie.get('support_devices_manufacturer') && this.submitButton) this.cookiedManufacturer = Cookie.get('support_devices_manufacturer');
		// if(Cookie.get('support_devices_software') && this.submitButton) this.cookiedSoftware = Cookie.get('support_devices_software');
		
		this.populateDevicesWithin();
		
		Event.observe(this.deviceTypeEl, 'change', this.selectDeviceType.bindAsEventListener(this));
		Event.observe(this.manufacturerEl, 'change', this.selectManufacturer.bindAsEventListener(this));
		Event.observe(this.softwareEl, 'change', this.selectSoftware.bindAsEventListener(this));
		
	},
	
	selectDeviceType: function(event) {
		var selectedIndex = this.deviceTypeEl.selectedIndex;
		var selectedDeviceType = this.deviceTypeEl.getElementsByTagName('option')[selectedIndex];
		
		if(selectedDeviceType.value !== 'any' && selectedDeviceType.value !== '') {
			this.selectedDeviceTypes = this.deviceDictionary.deviceType[selectedDeviceType.value];
			
			this.showManufacturers();
			this.showSoftwares();
		} else if(selectedDeviceType.value === 'any') {
			this.selectedDeviceTypes = {
				Manufacturers: [],
				SoftwareVersions: []
			}
			
			for(var manufacturer in this.deviceDictionary.Manufacturer) {
				this.selectedDeviceTypes.Manufacturers.push(manufacturer);
			}
			
			for(var software in this.deviceDictionary.SoftwareVersion) {
				this.selectedDeviceTypes.SoftwareVersions.push(software);
			}
			
			this.showManufacturers();
			this.showSoftwares();
		}
	},
	
	selectManufacturer: function(event) {
		var selectedIndex = this.manufacturerEl.selectedIndex;
		var selectedManufacturer = this.manufacturerEl.getElementsByTagName('option')[selectedIndex];
		
		if(selectedManufacturer.value !== 'any' && selectedManufacturer.value !== '') {
			this.selectedManufacturers = this.deviceDictionary.Manufacturer[selectedManufacturer.value];
			
			this.showDeviceTypes();
			this.showSoftwares();			
		} else if(selectedManufacturer.value === 'any') {
			this.selectedManufacturers = {
				deviceTypes: [],
				SoftwareVersions: []
			}
			
			for(var deviceType in this.deviceDictionary.deviceType) {
				this.selectedManufacturers.deviceTypes.push(deviceType);
			}
			
			for(var software in this.deviceDictionary.SoftwareVersion) {
				this.selectedManufacturers.SoftwareVersions.push(software);
			}
			
			this.showDeviceTypes();
			this.showSoftwares();
		}
	},
	
	selectSoftware: function(event) {
		var selectedIndex = this.softwareEl.selectedIndex;
		var selectedSoftware = this.softwareEl.getElementsByTagName('option')[selectedIndex];
		
		if(selectedSoftware.value !== 'any' && selectedSoftware.value !== '') {
			this.selectedSoftwares = this.deviceDictionary.SoftwareVersion[selectedSoftware.value];

			this.showDeviceTypes();
			this.showManufacturers();
		} else if(selectedSoftware.value === 'any') {
			this.selectedSoftwares = {
				deviceTypes: [],
				Manufacturers: []
			}
			
			for(var deviceType in this.deviceDictionary.deviceType) {
				this.selectedSoftwares.deviceTypes.push(deviceType);
			}
			
			for(var manufacturer in this.deviceDictionary.Manufacturer) {
				this.selectedSoftwares.Manufacturers.push(manufacturer);
			}
			
			this.showDeviceTypes();
			this.showManufacturers();
		}
	},
	
	populateDeviceTypes: function(deviceTypes) {
		if(this.isPHP) {
			this.deviceTypeArr = Element.childElements(this.deviceTypeEl);
			return;
		}
		
		this.deviceTypeEl.innerHTML = '';
		
		var deviceTypeFirst = this.createOption('', 'デバイスの種類', false, ''),
			deviceTypeDash = this.createOption('', '---------------', false, ''),
			deviceTypeAny = this.createOption('any', 'すべて', false, '');
		
		this.deviceTypeEl.appendChild(deviceTypeFirst);
		this.deviceTypeEl.appendChild(deviceTypeDash);
		this.deviceTypeEl.appendChild(deviceTypeAny);
		
		for (var deviceType in deviceTypes) {
			var selected = (deviceType == this.cookiedDevicetype) ? true : false;
			
			var deviceTypeOption = this.createOption(deviceType, deviceType, selected, '');
			this.deviceTypeEl.appendChild(deviceTypeOption);
			this.deviceTypeArr.push(deviceTypeOption);
		}
	},
	
	populateManufacturers: function(manufacturers) {
		if(this.isPHP) {
			this.manufacturerArr = Element.childElements(this.manufacturerEl);
			return;
		}
		
		this.manufacturerEl.innerHTML = '';
		
		var manufacturerFirst = this.createOption('', 'メーカー', false, ''),
			manufacturerDash = this.createOption('', '---------------', false, ''),
			manufacturerAny = this.createOption('any', 'すべて', false, '');
		
		this.manufacturerEl.appendChild(manufacturerFirst);
		this.manufacturerEl.appendChild(manufacturerDash);
		this.manufacturerEl.appendChild(manufacturerAny);
		
		for (var manufacturer in manufacturers) {
			var selected = (manufacturer == this.cookiedManufacturer) ? true : false;
			
			var manufacturerOption = this.createOption(manufacturer, manufacturer, selected, '');
			this.manufacturerEl.appendChild(manufacturerOption);
			this.manufacturerArr.push(manufacturerOption);
		}
	},
	
	populateSoftware: function(softwares) {
		if(this.isPHP) {
			this.softwareArr = Element.childElements(this.softwareEl);
			return;
		}
		
		this.softwareEl.innerHTML = '';
		
		var softwareFirst = this.createOption('', 'ソフトウェアのバージョン', false, ''),
			softwareDash = this.createOption('', '---------------', false, ''),
			softwareAny = this.createOption('any', 'すべて', false, '');
		
		this.softwareEl.appendChild(softwareFirst);
		this.softwareEl.appendChild(softwareDash);
		this.softwareEl.appendChild(softwareAny);
		
		for (var software in softwares) {
			var selected = (software == this.cookiedSoftware) ? true : false;
			
			var softwareOption = this.createOption(software, software, selected, '');
			this.softwareEl.appendChild(softwareOption);
			this.softwareArr.push(softwareOption);
		}
	},
	
	populateDevicesWithin: function() {
		this.populateDeviceTypes(this.deviceDictionary.deviceType);
		this.populateManufacturers(this.deviceDictionary.Manufacturer);
		this.populateSoftware(this.deviceDictionary.SoftwareVersion);

		if(this.isPHP) {
			this.selectDeviceType();
			this.selectManufacturer();
			this.selectSoftware();
		}
	},
	
	enableOptionsForOptionsAndAvailable: function(options, available) {
		options.each(function(option, index) {
			if(available.indexOf(option.value) == -1 && !(this.isPHP && index < 3)) {
				option.disabled = true;
				if(!Element.hasClassName(option, 'disabled')) Element.addClassName(option, 'disabled');
				if(option.selected === true) option.parentNode[0].selected = true;
			} else {
				option.disabled = false;
				if(Element.hasClassName(option, 'disabled')) Element.removeClassName(option, 'disabled');
			}
		}.bind(this));
	},
	
	showDeviceTypes: function() {
		var availableDeviceTypes = [];
		
		if(this.selectedManufacturers !== false && this.selectedSoftwares === false) {
			availableDeviceTypes = this.selectedManufacturers.deviceTypes;
		} else if(this.selectedManufacturers !== false && this.selectedSoftwares !== false) {
			availableDeviceTypes = (this.selectedManufacturers.deviceTypes.length > this.selectedSoftwares.deviceTypes.length) ?
				this.removeOptionsFromLargerArrayForSmallerArray(this.selectedManufacturers.deviceTypes, this.selectedSoftwares.deviceTypes) :
				this.removeOptionsFromLargerArrayForSmallerArray(this.selectedSoftwares.deviceTypes, this.selectedManufacturers.deviceTypes);
		} else if(this.selectedManufacturers === false && this.selectedSoftwares !== false) {
			availableDeviceTypes = this.selectedSoftwares.deviceTypes;
		}
		
		this.enableOptionsForOptionsAndAvailable(this.deviceTypeArr, availableDeviceTypes);

	},
	
	showManufacturers: function() {
		var availableManufacturers = [];
		
		if(this.selectedDeviceTypes !== false && this.selectedSoftwares === false) {
			availableManufacturers = this.selectedDeviceTypes.Manufacturers;
		} else if(this.selectedDeviceTypes !== false && this.selectedSoftwares !== false) {
			availableManufacturers = (this.selectedDeviceTypes.Manufacturers.length > this.selectedSoftwares.Manufacturers.length) ?
				this.removeOptionsFromLargerArrayForSmallerArray(this.selectedDeviceTypes.Manufacturers, this.selectedSoftwares.Manufacturers) :
				this.removeOptionsFromLargerArrayForSmallerArray(this.selectedSoftwares.Manufacturers, this.selectedDeviceTypes.Manufacturers);

		} else if(this.selectedDeviceTypes === false && this.selectedSoftwares !== false) {
			availableManufacturers = this.selectedSoftwares.Manufacturers;
		}
		
		this.enableOptionsForOptionsAndAvailable(this.manufacturerArr, availableManufacturers);

	},
	
	showSoftwares: function() {
		var availableSoftwares = [];
		
		if(this.selectedDeviceTypes !== false && this.selectedManufacturers === false) {
			availableSoftwares = this.selectedDeviceTypes.SoftwareVersions;
		} else if(this.selectedDeviceTypes !== false && this.selectedManufacturers !== false) {
			availableSoftwares = (this.selectedDeviceTypes.SoftwareVersions.length > this.selectedManufacturers.SoftwareVersions.length) ?
				this.removeOptionsFromLargerArrayForSmallerArray(this.selectedDeviceTypes.SoftwareVersions, this.selectedManufacturers.SoftwareVersions) :
				this.removeOptionsFromLargerArrayForSmallerArray(this.selectedManufacturers.SoftwareVersions, this.selectedDeviceTypes.SoftwareVersions);
		} else if(this.selectedDeviceTypes === false && this.selectedManufacturers !== false) {
			availableSoftwares = this.selectedManufacturers.SoftwareVersions;
		}
		
		this.enableOptionsForOptionsAndAvailable(this.softwareArr, availableSoftwares);

	},
	
	removeOptionsFromLargerArrayForSmallerArray: function(largerArr, smallerArr) {
		var returnArr = smallerArr;
		
		largerArr.each(function(option) {
			if(returnArr.indexOf(option) == -1) {
				returnArr.slice(option);
			}
		});
		
		return returnArr;
	},

	createOption: function(value, name, selected, storeCode) {
		var option = $(document.createElement('option'));
		if(storeCode != '') option.setAttribute('name', storeCode);
		option.setAttribute('value', value);
		if(selected) option.setAttribute('selected', 'selected');
		option.appendChild(document.createTextNode(name));

		return option;
	}
	
};

// Cookie
var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
	  path = '; path=/';
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire + path);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};

