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),
|
|
|
|
IMPRESS_HOST = PREVIEW_SWF.getAttribute('data-impress-host'),
|
2010-05-20 18:16:35 -07:00
|
|
|
speciesList = $('#item-preview a'),
|
|
|
|
MainWardrobe;
|
|
|
|
|
|
|
|
if(console === undefined || console.log === undefined) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
String.prototype.article = function () {
|
|
|
|
return 'aeiou'.indexOf(this.charAt(0).toLowerCase()) == -1 ? 'a' : 'an'
|
|
|
|
}
|
|
|
|
|
2010-05-16 17:45:30 -07:00
|
|
|
function impressUrl(path) {
|
|
|
|
return 'http://' + IMPRESS_HOST + path;
|
|
|
|
}
|
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
function LoadError(base_msg) {
|
|
|
|
this.render = function (args) {
|
|
|
|
var msg = base_msg, token, article_token;
|
|
|
|
for(var i in args) {
|
|
|
|
token = "$" + i;
|
|
|
|
article_token = token + "_article";
|
|
|
|
if(msg.indexOf(article_token) != -1) {
|
|
|
|
msg = msg.replace(article_token, args[i].article());
|
|
|
|
}
|
|
|
|
msg = msg.replace(token, args[i]);
|
|
|
|
}
|
|
|
|
return "Whoops - we've never seen " + msg + " before! If you have, please " +
|
|
|
|
"<a href='http://" + IMPRESS_HOST + "'>submit that pet's name</a> as soon as you " +
|
|
|
|
"get the chance! Thanks!";
|
|
|
|
}
|
2010-05-16 13:37:55 -07:00
|
|
|
}
|
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
function PetType() {
|
|
|
|
var pet_type = this;
|
|
|
|
|
|
|
|
this.activated = true;
|
|
|
|
|
|
|
|
this.deactivate = function (error, args) {
|
|
|
|
var msg;
|
|
|
|
this.activated = false;
|
|
|
|
if(typeof args == 'undefined') args = {};
|
|
|
|
args.color = this.color_name.capitalize();
|
|
|
|
args.species = this.species_name.capitalize();
|
|
|
|
this.deactivation_msg = error.render(args);
|
|
|
|
if(this == PetType.current) showDeactivationMsg();
|
|
|
|
var img = this.link.children('img').get(0);
|
|
|
|
this.link.addClass('deactivated');
|
|
|
|
img.src = img.src.replace('/1/', '/2/');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.load = function () {
|
|
|
|
var url = '/species/' + this.species_id + '/color/' + this.color_id + '/pet_type.json',
|
|
|
|
pet_type = this;
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
dataType: 'json',
|
|
|
|
success: function (data) {
|
|
|
|
pet_type.id = data.id;
|
|
|
|
pet_type.body_id = data.body_id;
|
|
|
|
Item.current.load(pet_type);
|
|
|
|
$.getJSON('/pet_types/' + data.id + '/swf_assets.json', function (assets) {
|
|
|
|
pet_type.assets = assets;
|
|
|
|
Preview.update();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
pet_type.deactivate(PetType.LOAD_ERROR);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setAsCurrent = function () {
|
|
|
|
PetType.current = this;
|
|
|
|
speciesList.filter('.current').removeClass('current');
|
|
|
|
this.link.addClass('current');
|
|
|
|
if(this.activated) {
|
|
|
|
Preview.enable();
|
|
|
|
this.load();
|
|
|
|
} else {
|
|
|
|
showDeactivationMsg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showDeactivationMsg() {
|
|
|
|
Preview.disable(pet_type.deactivation_msg);
|
|
|
|
}
|
2010-05-16 13:37:55 -07:00
|
|
|
}
|
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
PetType.all = [];
|
|
|
|
|
|
|
|
PetType.LOAD_ERROR = new LoadError("$color_article $color $species");
|
|
|
|
|
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();
|
|
|
|
pet_type.color_id = link.attr('data-color-id');
|
2010-05-31 12:45:03 -07:00
|
|
|
pet_type.color_name = link.attr('data-color-name');
|
2010-05-16 13:37:55 -07:00
|
|
|
pet_type.species_id = link.attr('data-species-id');
|
2010-05-31 12:45:03 -07:00
|
|
|
pet_type.species_name = link.attr('data-species-name');
|
2010-05-16 13:37:55 -07:00
|
|
|
pet_type.link = link;
|
2010-05-31 12:45:03 -07:00
|
|
|
PetType.all.push(pet_type);
|
2010-05-16 13:37:55 -07:00
|
|
|
return pet_type;
|
|
|
|
}
|
2010-05-16 12:01:38 -07:00
|
|
|
|
2010-05-20 18:55:09 -07:00
|
|
|
function Item() {
|
2010-05-31 12:45:03 -07:00
|
|
|
this.load = function (pet_type) {
|
|
|
|
var url = '/' + this.id + '/swf_assets.json?body_id=' + pet_type.body_id,
|
2010-05-20 18:55:09 -07:00
|
|
|
item = this;
|
|
|
|
$.getJSON(url, function (data) {
|
2010-05-31 12:45:03 -07:00
|
|
|
if(data.length) {
|
|
|
|
item.assets = data;
|
|
|
|
Preview.update();
|
|
|
|
} else {
|
|
|
|
pet_type.deactivate(Item.LOAD_ERROR, {
|
|
|
|
item: item.name
|
|
|
|
});
|
|
|
|
}
|
2010-05-20 18:55:09 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setAsCurrent = function () {
|
|
|
|
Item.current = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
Item.LOAD_ERROR = new LoadError("$species_article $species wear a $item");
|
|
|
|
|
2010-05-20 18:55:09 -07:00
|
|
|
Item.createFromLocation = function () {
|
|
|
|
var item = new Item();
|
|
|
|
item.id = parseInt(document.location.pathname.substr(1));
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2010-05-20 18:16:35 -07:00
|
|
|
Preview = new function Preview() {
|
|
|
|
var assets = [], swf_id, swf, updateWhenFlashReady = false;
|
|
|
|
|
|
|
|
this.setFlashIsReady = function () {
|
|
|
|
swf = document.getElementById(swf_id);
|
|
|
|
if(updateWhenFlashReady) this.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.update = function (assets) {
|
2010-05-20 18:55:09 -07:00
|
|
|
var assets = [];
|
2010-05-20 18:16:35 -07:00
|
|
|
if(swf) {
|
2010-05-20 18:55:09 -07:00
|
|
|
$.each([PetType, Item], function () {
|
|
|
|
if(this.current.assets) assets = assets.concat(this.current.assets);
|
|
|
|
});
|
|
|
|
assets = $.each(assets, function () {
|
2010-05-20 18:16:35 -07:00
|
|
|
this.local_path = this.local_url;
|
|
|
|
});
|
|
|
|
swf.setAssets(assets);
|
|
|
|
} else {
|
|
|
|
updateWhenFlashReady = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.embed = function (id) {
|
|
|
|
swf_id = id;
|
|
|
|
swfobject.embedSWF(
|
|
|
|
impressUrl('/assets/swf/preview.swf'), // URL
|
|
|
|
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
|
|
|
|
impressUrl('/assets/js/swfobject/expressInstall.swf'), // express install URL
|
|
|
|
{'swf_assets_path': impressUrl('/assets')}, // flashvars
|
|
|
|
{'wmode': 'transparent', 'allowscriptaccess': 'always'} // params
|
|
|
|
);
|
|
|
|
}
|
2010-05-31 12:45:03 -07:00
|
|
|
|
|
|
|
this.disable = function (msg) {
|
|
|
|
$('#' + swf_id).hide();
|
|
|
|
$('#item-preview-error').html(msg).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
PetType.createFromLink(speciesList.eq(Math.floor(Math.random()*speciesList.length))).setAsCurrent();
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-05-31 12:45:03 -07:00
|
|
|
speciesList.each(function () {
|
|
|
|
var pet_type = PetType.createFromLink($(this));
|
|
|
|
$(this).click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
pet_type.setAsCurrent();
|
|
|
|
});
|
2010-05-16 13:37:55 -07:00
|
|
|
});
|
2010-05-20 18:16:35 -07:00
|
|
|
|
|
|
|
MainWardrobe = { View: { Outfit: Preview } };
|
2010-05-31 12:45:03 -07:00
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
$.each(PetType.all, function () {
|
|
|
|
this.load();
|
|
|
|
});
|
|
|
|
}, 5000);
|