2010-09-08 19:49:39 -07:00
|
|
|
// FIXME: pick a consistent javascript style! underscores for vars or camelCase?
|
|
|
|
|
2010-05-16 13:37:55 -07:00
|
|
|
var PREVIEW_SWF_ID = 'item-preview-swf',
|
2010-05-16 17:45:30 -07:00
|
|
|
PREVIEW_SWF = document.getElementById(PREVIEW_SWF_ID),
|
2013-12-05 13:22:43 -08:00
|
|
|
speciesEls,
|
|
|
|
petTypeEls,
|
2010-09-08 19:49:39 -07:00
|
|
|
customize_more_el = $('#customize-more'),
|
2010-05-20 18:16:35 -07:00
|
|
|
MainWardrobe;
|
|
|
|
|
2010-06-08 15:55:13 -07:00
|
|
|
if(typeof console == 'undefined' || typeof console.log == 'undefined') {
|
2010-05-20 18:16:35 -07:00
|
|
|
function log() {}
|
|
|
|
} else {
|
|
|
|
log = $.proxy(console, 'log');
|
|
|
|
}
|
2010-05-16 13:37:55 -07:00
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
String.prototype.capitalize = function () {
|
|
|
|
return this.charAt(0).toUpperCase() + this.substr(1);
|
|
|
|
}
|
|
|
|
|
2010-12-06 15:50:13 -08:00
|
|
|
function impressUrl(path) {
|
|
|
|
return 'http://' + IMPRESS_HOST + path;
|
|
|
|
}
|
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
function PetType() {
|
2010-06-04 18:09:19 -07:00
|
|
|
var pet_type = this, loaded_data = false, loaded_assets = false;
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
this.activated = true;
|
2010-06-22 09:42:25 -07:00
|
|
|
this.assets = [];
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2012-12-30 11:15:55 -08:00
|
|
|
this.deactivate = function () {
|
2010-05-31 12:45:03 -07:00
|
|
|
var msg;
|
|
|
|
this.activated = false;
|
2012-12-30 11:15:55 -08:00
|
|
|
this.deactivation_msg = $('#swf-assets-not-found-template').tmpl({
|
|
|
|
color_name: this.color_name.capitalize(),
|
|
|
|
species_name: this.species_name.capitalize()
|
|
|
|
});
|
2010-05-31 12:45:03 -07:00
|
|
|
if(this == PetType.current) showDeactivationMsg();
|
|
|
|
var img = this.link.children('img').get(0);
|
|
|
|
this.link.addClass('deactivated');
|
|
|
|
img.src = img.src.replace('/1/', '/2/');
|
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
this.load = function () {
|
2010-06-07 13:08:53 -07:00
|
|
|
Item.current.load(this);
|
|
|
|
loadAssets();
|
2010-05-31 12:45:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setAsCurrent = function () {
|
|
|
|
PetType.current = this;
|
2013-12-05 13:22:43 -08:00
|
|
|
petTypeEls.filter('.current').removeClass('current');
|
2010-05-31 12:45:03 -07:00
|
|
|
this.link.addClass('current');
|
2010-09-08 19:49:39 -07:00
|
|
|
customize_more_el.attr('href',
|
2011-07-11 07:58:30 -07:00
|
|
|
'http://impress.openneo.net/wardrobe?species=' + this.species_id +
|
2010-09-08 19:49:39 -07:00
|
|
|
'&color=' + this.color_id + '&objects[]=' + Item.current.id);
|
2010-05-31 12:45:03 -07:00
|
|
|
if(this.activated) {
|
2010-06-08 15:59:18 -07:00
|
|
|
Preview.enable();
|
2010-05-31 12:45:03 -07:00
|
|
|
this.load();
|
|
|
|
} else {
|
|
|
|
showDeactivationMsg();
|
|
|
|
}
|
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-06-07 13:33:43 -07:00
|
|
|
this.onUpdate = function () {
|
|
|
|
if(pet_type == PetType.current) Preview.update()
|
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-06-04 18:09:19 -07:00
|
|
|
function loadAssets() {
|
2010-06-07 13:33:43 -07:00
|
|
|
if(loaded_assets) {
|
|
|
|
pet_type.onUpdate();
|
|
|
|
} else {
|
2010-06-04 18:09:19 -07:00
|
|
|
$.getJSON('/pet_types/' + pet_type.id + '/swf_assets.json', function (assets) {
|
|
|
|
pet_type.assets = assets;
|
|
|
|
loaded_assets = true;
|
2010-06-07 13:33:43 -07:00
|
|
|
pet_type.onUpdate();
|
2010-06-04 18:09:19 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
function showDeactivationMsg() {
|
|
|
|
Preview.disable(pet_type.deactivation_msg);
|
|
|
|
}
|
2010-05-16 13:37:55 -07:00
|
|
|
}
|
|
|
|
|
2011-07-11 07:58:30 -07:00
|
|
|
PetType.all = {};
|
2010-06-07 13:08:53 -07:00
|
|
|
PetType.DASH_REGEX = /-/g;
|
2010-05-31 12:45:03 -07:00
|
|
|
|
2010-05-20 18:55:09 -07:00
|
|
|
PetType.createFromLink = function (link) {
|
2010-05-16 13:37:55 -07:00
|
|
|
var pet_type = new PetType();
|
2010-06-07 13:08:53 -07:00
|
|
|
$.each(link.get(0).attributes, function () {
|
|
|
|
if(this.name.substr(0, 5) == 'data-') {
|
|
|
|
pet_type[this.name.substr(5).replace(PetType.DASH_REGEX, '_')] = this.value;
|
|
|
|
}
|
|
|
|
});
|
2010-05-16 13:37:55 -07:00
|
|
|
pet_type.link = link;
|
2011-07-11 07:58:30 -07:00
|
|
|
PetType.all[pet_type.id] = pet_type;
|
2010-05-16 13:37:55 -07:00
|
|
|
return pet_type;
|
|
|
|
}
|
2010-05-16 12:01:38 -07:00
|
|
|
|
2010-06-07 13:33:43 -07:00
|
|
|
function Item(id) {
|
2010-06-04 18:09:19 -07:00
|
|
|
this.assets_by_body_id = {};
|
2010-09-08 19:49:39 -07:00
|
|
|
this.id = id;
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
this.load = function (pet_type) {
|
2010-06-08 15:35:39 -07:00
|
|
|
var url = '/items/' + id + '/bodies/' + pet_type.body_id + '/swf_assets.json',
|
2010-05-20 18:55:09 -07:00
|
|
|
item = this;
|
2010-06-04 18:09:19 -07:00
|
|
|
if(this.getAssetsForPetType(pet_type).length) {
|
2010-06-07 13:33:43 -07:00
|
|
|
pet_type.onUpdate();
|
2010-06-04 18:09:19 -07:00
|
|
|
} else {
|
|
|
|
$.getJSON(url, function (data) {
|
2010-06-07 13:33:43 -07:00
|
|
|
item.setAssetsForPetType(data, pet_type);
|
2010-06-04 18:09:19 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-06-08 15:26:42 -07:00
|
|
|
this.loadAllStandard = function () {
|
|
|
|
var item = this;
|
2010-06-08 15:35:39 -07:00
|
|
|
$.getJSON('/items/' + id + '/swf_assets.json', function (assets_by_body_id) {
|
2010-06-08 15:26:42 -07:00
|
|
|
$.each(assets_by_body_id, function (i) {
|
|
|
|
item.assets_by_body_id[parseInt(i)] = this;
|
|
|
|
});
|
|
|
|
$.each(PetType.all, function () {
|
|
|
|
if(item.getAssetsForPetType(this).length == 0) {
|
2012-12-30 11:15:55 -08:00
|
|
|
this.deactivate();
|
2010-06-08 15:26:42 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-06-04 18:09:19 -07:00
|
|
|
this.getAssetsForPetType = function (pet_type) {
|
2010-06-08 07:58:13 -07:00
|
|
|
return this.assets_by_body_id[pet_type.body_id] || this.assets_by_body_id[0] || [];
|
2010-05-20 18:55:09 -07:00
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-05-20 18:55:09 -07:00
|
|
|
this.setAsCurrent = function () {
|
|
|
|
Item.current = this;
|
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-06-07 13:33:43 -07:00
|
|
|
this.setAssetsForPetType = function (assets, pet_type) {
|
|
|
|
if(assets.length) {
|
|
|
|
this.assets_by_body_id[pet_type.body_id] = assets;
|
|
|
|
pet_type.onUpdate();
|
|
|
|
} else {
|
2012-12-30 11:15:55 -08:00
|
|
|
pet_type.deactivate();
|
2010-06-07 13:33:43 -07:00
|
|
|
}
|
|
|
|
}
|
2010-05-20 18:55:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Item.createFromLocation = function () {
|
2010-11-06 10:07:12 -07:00
|
|
|
var item = new Item(parseInt(document.location.pathname.substr(7), 10)),
|
2010-06-07 16:50:49 -07:00
|
|
|
z = CURRENT_ITEM_ZONES_RESTRICT, zl = z.length;
|
|
|
|
item.restricted_zones = [];
|
|
|
|
for(i = 0; i < zl; i++) {
|
|
|
|
if(z.charAt(i) == '1') {
|
|
|
|
item.restricted_zones.push(i + 1);
|
|
|
|
}
|
|
|
|
}
|
2010-05-20 18:55:09 -07:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2010-05-20 18:16:35 -07:00
|
|
|
Preview = new function Preview() {
|
2010-06-30 11:16:16 -07:00
|
|
|
var preview = this, swf_id, swf, update_when_swf_ready = false;
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-06-22 09:42:25 -07:00
|
|
|
window.previewSWFIsReady = function () {
|
2010-06-30 11:16:16 -07:00
|
|
|
log('preview SWF is ready');
|
2010-05-20 18:16:35 -07:00
|
|
|
swf = document.getElementById(swf_id);
|
2010-06-30 11:16:16 -07:00
|
|
|
if(update_when_swf_ready) preview.update();
|
2010-05-20 18:16:35 -07:00
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-05-20 18:16:35 -07:00
|
|
|
this.update = function (assets) {
|
2010-06-22 09:42:25 -07:00
|
|
|
var assets;
|
2012-12-30 11:15:55 -08:00
|
|
|
if(swf && typeof swf.setAssets == 'function') {
|
2010-06-30 11:16:16 -07:00
|
|
|
log('now doing update');
|
2010-06-22 09:42:25 -07:00
|
|
|
assets = PetType.current.assets.concat(
|
|
|
|
Item.current.getAssetsForPetType(PetType.current)
|
|
|
|
);
|
2010-06-07 16:50:49 -07:00
|
|
|
assets = $.grep(assets, function (asset) {
|
|
|
|
var visible = $.inArray(asset.zone_id, Item.current.restricted_zones) == -1;
|
|
|
|
if(visible) asset.local_path = asset.local_url;
|
|
|
|
return visible;
|
2010-05-20 18:16:35 -07:00
|
|
|
});
|
|
|
|
swf.setAssets(assets);
|
|
|
|
} else {
|
2010-06-30 11:16:16 -07:00
|
|
|
log('putting off update');
|
|
|
|
update_when_swf_ready = true;
|
2010-05-20 18:16:35 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-05-20 18:16:35 -07:00
|
|
|
this.embed = function (id) {
|
|
|
|
swf_id = id;
|
|
|
|
swfobject.embedSWF(
|
2010-10-09 07:53:58 -07:00
|
|
|
'/swfs/preview.swf?v=2', // URL
|
2010-05-20 18:16:35 -07:00
|
|
|
id, // ID
|
2010-05-20 19:05:13 -07:00
|
|
|
'100%', // width
|
|
|
|
'100%', // height
|
2010-05-20 18:16:35 -07:00
|
|
|
'9', // required version
|
2010-12-06 15:50:13 -08:00
|
|
|
impressUrl('/assets/js/swfobject/expressInstall.swf'), // express install URL
|
2010-10-09 07:53:58 -07:00
|
|
|
{}, // flashvars
|
2010-05-20 18:16:35 -07:00
|
|
|
{'wmode': 'transparent', 'allowscriptaccess': 'always'} // params
|
|
|
|
);
|
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2012-12-30 11:15:55 -08:00
|
|
|
this.disable = function (errorMessage) {
|
2010-05-31 12:45:03 -07:00
|
|
|
$('#' + swf_id).hide();
|
2012-12-30 11:15:55 -08:00
|
|
|
$('#item-preview-error').empty().append(errorMessage).show();
|
2010-05-31 12:45:03 -07:00
|
|
|
}
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
this.enable = function () {
|
|
|
|
$('#item-preview-error').hide();
|
|
|
|
$('#' + swf_id).show();
|
|
|
|
}
|
2010-05-20 18:16:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Preview.embed(PREVIEW_SWF_ID);
|
2010-05-16 13:37:55 -07:00
|
|
|
|
2010-05-20 18:55:09 -07:00
|
|
|
Item.createFromLocation().setAsCurrent();
|
2010-05-31 12:45:03 -07:00
|
|
|
Item.current.name = $('#item-name').text();
|
2010-05-16 13:37:55 -07:00
|
|
|
|
2013-12-05 13:22:43 -08:00
|
|
|
// Choose only supported species, and remove the unsupported.
|
|
|
|
var supportedSpeciesIds = $('#item-preview-species').attr('data-supported-species-ids').split(',');
|
|
|
|
var supportedSpeciesIdPresenceMap = {};
|
|
|
|
for(var i = 0; i < supportedSpeciesIds.length; i++) {
|
|
|
|
supportedSpeciesIdPresenceMap[supportedSpeciesIds[i]] = true;
|
|
|
|
}
|
|
|
|
speciesEls = $('#item-preview-species > li').filter(function() {
|
|
|
|
var supported = supportedSpeciesIdPresenceMap[this.getAttribute('data-id')];
|
|
|
|
if(!supported) this.parentNode.removeChild(this);
|
|
|
|
return supported;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Choose random pet type for each species.
|
|
|
|
speciesEls.each(function() {
|
|
|
|
var speciesPetTypeEls = $(this).find('.pet-type');
|
|
|
|
var chosen = speciesPetTypeEls.eq(Math.floor(Math.random()*speciesPetTypeEls.length));
|
|
|
|
speciesPetTypeEls.not(chosen).remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
petTypeEls = speciesEls.find('.pet-type');
|
|
|
|
|
|
|
|
// Choose random starting pet type
|
|
|
|
PetType.createFromLink(petTypeEls.eq(Math.floor(Math.random()*petTypeEls.length))).setAsCurrent();
|
2010-06-07 13:08:53 -07:00
|
|
|
|
2013-12-05 13:22:43 -08:00
|
|
|
// Setup pet type click behavior
|
|
|
|
petTypeEls.each(function () {
|
2010-06-30 11:16:16 -07:00
|
|
|
var el = $(this);
|
2011-07-11 07:58:30 -07:00
|
|
|
PetType.createFromLink(el);
|
2013-12-05 13:22:43 -08:00
|
|
|
}).click(function (e) {
|
2011-07-11 07:58:30 -07:00
|
|
|
PetType.all[$(this).data('id')].setAsCurrent();
|
2010-05-16 13:37:55 -07:00
|
|
|
});
|
2010-05-20 18:16:35 -07:00
|
|
|
|
2013-12-05 13:22:43 -08:00
|
|
|
// Load the other pet type data in 5 seconds, to save database effort in case
|
|
|
|
// the user decides to bounce.
|
2010-06-08 15:26:42 -07:00
|
|
|
setTimeout($.proxy(Item.current, 'loadAllStandard'), 5000);
|
2010-06-07 13:33:43 -07:00
|
|
|
|
2010-06-30 11:27:06 -07:00
|
|
|
window.MainWardrobe = {View: {Outfit: {setFlashIsReady: previewSWFIsReady}}}
|
|
|
|
|
2010-06-22 09:42:25 -07:00
|
|
|
var SWFLog = $.noop;
|
2011-07-11 07:58:30 -07:00
|
|
|
|
2011-07-30 21:19:28 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Trade hangers
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(document.body).addClass('js');
|
|
|
|
|
|
|
|
$('#trade-hangers p').wrapInner('<div/>').each(function () {
|
|
|
|
var el = $(this);
|
|
|
|
if(el.height() < el.children().height()) {
|
|
|
|
el.addClass('overflows');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#trade-hangers .toggle').click(function () {
|
|
|
|
$(this).closest('p').toggleClass('showing-more');
|
|
|
|
});
|
|
|
|
|