﻿// namespace
window.LOV = window.LOV || {};
LOV.Sony = LOV.Sony || {};
LOV.Sony.Business = LOV.Sony.Business || {};

// shortcut
var sonyB = LOV.Sony.Business;

sonyB.sfir = sonyB.sfir || {};
sonyB.Purchase = sonyB.Purchase || {};

sonyB.sfir.init = function() {
	if (typeof sIFR == 'function') {
		sIFR.replaceElement(named({sSelector:"#headerCategory h2 span", sFlashSrc:"../swf/h2.swf", sWmode:"transparent", sColor:"#333333"}));
	}
};
$().ready(sonyB.sfir.init);

sonyB.Purchase.init = function() {
	$('#ci').cascade('#es', {
		ajax: {url: '/ajax/purchaseCity.aspx'},
		template: sonyB.Purchase.commonTemplate,
		match: sonyB.Purchase.commonMatch
	})
	.bind('loaded.cascade', sonyB.Purchase.cascadeLoaded);

	$('#ca').cascade('#li', {
		ajax: {url: '/ajax/purchaseProductLine.aspx'},
		template: sonyB.Purchase.commonTemplate,
		match: sonyB.Purchase.commonMatch
	})
	.bind('loaded.cascade', sonyB.Purchase.cascadeLoaded);

	$('#pr').cascade('#ca', {
		ajax: {url: '/ajax/purchaseModel.aspx'},
		template: sonyB.Purchase.commonTemplate,
		match: sonyB.Purchase.commonMatch
	})
	.bind('loaded.cascade', sonyB.Purchase.cascadeLoaded);
};
$().ready(sonyB.Purchase.init);

sonyB.Purchase.commonTemplate = function(item) {
	return '<option value="' + item.Value + '">' + item.Text + '</option>';
};

sonyB.Purchase.commonMatch = function(selectedValue) {
	return this.When == selectedValue;
};

sonyB.Purchase.cascadeLoaded = function(e, target) {
	if (this.length) {
		var valueStr = (this.id == 'pr') ? 'Todos' : 'Todas';
		$(this).prepend('<option value="" selected="true">' + valueStr + '</option>');
		$(this).find('option:first')[0].selected = true;
		$(this).removeAttr('disabled');
	} else {
		$(this).attr('disabled', 'disabled');
	}
};