/*
v1.01 Added specials
v1.02 Properly handled querystring for return to stock page, added viewport autoheight depends on comments,remove features field if has no value, efficiency ratings added, data access security, preload images, added thumbnails
v1.03 datasource fetched from jquery.dealerbase.co.nz or jquery service, optional not to provide
*/
(function ($) {
$.fn.stocklist = function (options) {
var defaults = $.extend({
ver: 1.03,
branchId: 0,
datasource: null,
pagetype: 0, /*0 - stocklist, 1 - details, 2- compare*/
detailspage: 'http:\/\/' + document.domain + '\/details.html',
stockpage: 'http:\/\/' + document.domain + '\/vehicle.html',
viewtype: 0, /*0 - galleryview, 1 - listview*/
marker: null,
loancalculator: false,
loanparam: null,
financethisvehicle: false,
playvideo: false,
maxgalleryview: 12,
maxlistview: 5,
mediapath: 'https://trademe.tmcdn.co.nz/photoserver',
mediapathautobase: 'http://media.autobase.co.nz',
jsonpath: 'http://jquery.dealerbase.co.nz',
scriptautoload: true,
css: null,
branches: null,
queryvehicleid: 'id',
gstincluded: true
}, options);
var loan =
{
"PPSR": 10,
"rate": 15.95,
"processfee": 325,
"amount": 0,
"deposit": 0,
"depositrate": "0.10",
"year": 0,
"years": 0,
"months": 24,
"GETPVIFA": function (rate, years, period) {
var monthly_rate = rate / period;
var factor = 0;
var base_rate = 1 + monthly_rate;
var denominator = base_rate;
for (var i = 0; i < (years * period); i++) {
factor += (1 / denominator);
denominator *= base_rate;
}
return factor;
}
};
var fuelefficiency = {
"rating": null,
"value": null,
"cost": null,
"message": null,
"toString": function () {
var effinfo = [];
if (this.rating) {
effinfo.push("This vehicle has a Fuel Economy Star Rating of " + this.rating + " out of 6.");
}
if (this.value && parseFloat(this.value) > 0) {
effinfo.push("It consumes " + this.value + "L litres per 100km travelled.");
}
if (this.cost) {
effinfo.push("The Estimated Annual Cost of running this vehicle is " + this.cost + ".");
}
if (this.message) {
effinfo.push(this.message);
}
//blank fillup
if (parseFloat(this.value) == 0) {
effinfo.push("
");
}
if (effinfo.length > 0) {
return effinfo.join(" ");
}
return null;
},
"tooltip": function () {
var tooltip = trademe.structure.create("div", "fuel-economy-tooltip", "ui-curve-med ui-shadow-large");
if (this.toString()) {
tooltip
.append(trademe.structure.create("div", null, "arrow-left-border"))
.append(trademe.structure.create("div", null, "arrow-left"))
.append(trademe.structure.create("div", null, "fuel-rating-medium fuel-rating-medium-" + this.rating.replace(".", "_")))
.append(trademe.structure.create("h5", null, null).html("Vehicle Fuel Economy Labelling"))
.append(trademe.structure.create("p", null, null).html(this.toString()))
.append(trademe.structure.create("p", null, "disclaimer").html("Disclaimer: The Fuel Saver Information displayed here is provided for comparative purposes. Your actual cost per year and fuel consumption will vary from that shown, depending on factors such as vehicle condition and any vehicle modifications, driving style, traffic conditions, distance travelled and fuel price variations. You should not expect to meet exactly what this information says. Visit www.fuelsaver.govt.nz to find out more about this information and fuel saver labels, and how to improve fuel economy."))
.css("display", "none");
if ($("#fuel-economy-tooltip").length > 0) {
$("#fuel-economy-tooltip").remove();
}
$("body").append(tooltip);
var tooltipfunc = function () {
//do tooltip
var _pt = window.setInterval(function () {
//if (listing.listingdatafetched != 'undefined' && listing.listingdatafetched == true) {
if (defaults.datasource) {
$("." + trademe.cssclass.fueleconomyhelp).TradeMeMotorsTooltip({ tooltipid: "fuel-economy-tooltip" });
window.clearInterval(_pt);
}
//}
}, 100);
};
//load tooltip script
if (defaults.scriptautoload) {
$.getScript(defaults.jsonpath + "\/trademe.motors.tooltip.js",
function (data, textStatus, jqxhr) {
tooltipfunc();
});
} else {
tooltipfunc();
}
return true;
}
return null;
}
};
var carousel = {
"create": function () {
if ($("#carousel").length > 0) {
$("#carousel").remove();
}
var c = trademe.structure.create("div", "carousel", null);
trademe.structure.target.after(c);
var carouselfunc = function () {
//populate data
var _populate = window.setInterval(function () {
//if (listing.listingdatafetched != 'undefined' && listing.listingdatafetched == true) {
if (defaults.datasource) {
$("#carousel").TradeMeMotorsCarousel({ datasource: defaults.datasource, carouseltype: 0 });
$("#carousel").appendTo("#details-viewport .details-form");
window.clearInterval(_populate);
}
//}
}, 100);
};
//load carousel script
if (defaults.scriptautoload) {
$.getScript(defaults.jsonpath + "\/trademe.motors.carousel.js",
function (data, textStatus, jqxhr) {
carouselfunc();
});
} else {
carouselfunc();
}
}
};
var listing = {
minpage: 0, maxpage: 0, totalpage: 0, currentpage: 0, currentmakeId: 0, currentsortby: 7, currentviewtype: 0, populatecallback: null,
init: function () {
if (!listing.hasViewport()) {
return;
}
if (!trademe.structure.target.hasClass(trademe.cssclass.viewportcontainer)) {
trademe.structure.target.addClass(trademe.cssclass.viewportcontainer);
}
//load data
listing.loaddata();
//do load cookie 1st
listing.loadcookie();
if (!listing.hasHeader()) {
listing.createHeader();
trademe.structure.fetchlistingextra();
listing.addFilterOption();
}
//populate data
var _populate = window.setInterval(function () {
if (listing.listingdatafetched != 'undefined' && listing.listingdatafetched == true && typeof trademe.selection.fetched != 'undefined' && trademe.selection["fetched"] == true) {
if (listing.populatecallback) {
listing.populatecallback();
window.clearInterval(_populate);
}
if (!defaults.scriptautoload) {
listing.populatecallback();
window.clearInterval(_populate);
}
}
}, 100);
if (!listing.hasFooter()) {
listing.createFooter();
}
listing.createContainer();
},
hasViewport: function () {
if (defaults.pagetype == trademe.enume.pagetype.stock) {
return (trademe.structure.target.attr("id") == trademe.id.viewport);
}
return false;
},
hasHeader: function () {
return (trademe.structure.target.find("." + trademe.cssclass.header).length > 0);
},
hasFooter: function () {
return (trademe.structure.target.find("." + trademe.cssclass.footer).length > 0);
},
loadcookie: function () {
//can be optimized, todo next
var cookienames = [{ listingobj: "currentpage", name: "currentpage" }, { listingobj: "currentmakeId", name: "ManufacturerId" }, { listingobj: "currentsortby", name: "SortBy" }, { listingobj: "currentviewtype", name: "viewtype" }, { listingobj: "branchId", name: "branchId"}];
$.each(cookienames, function (i, v) {
if (cookie(v.name)) {
listing[v.listingobj] = $.IsNumber(cookie(v.name)) ? parseInt(cookie(v.name)) : cookie(v.name);
}
});
},
createHeader: function () {
var header = trademe.structure.create("div", null, trademe.cssclass.header);
var galleryview = trademe.structure.create("a", null, trademe.cssclass.galleryview).attr("href", "");
var listview = trademe.structure.create("a", null, trademe.cssclass.listview).attr("href", "");
var label = trademe.structure.create("span", trademe.id.totallisting, null);
var makelabel = trademe.structure.create("span", trademe.id.makelabel, null).html("Make");
var sortbylabel = trademe.structure.create("span", trademe.id.sortbylabel, null).html("Sort By");
var makeselect = trademe.structure.create("select", trademe.id.makeoption, null);
var sortbyselect = trademe.structure.create("select", trademe.id.sortoption, null);
trademe.structure.target
.append(header
.append(galleryview)
.append(listview)
.append(label)
.append(sortbyselect)
.append(sortbylabel)
.append(makeselect)
.append(makelabel)
);
galleryview.bind("click", trademe.event.galleryviewclicked);
listview.bind("click", trademe.event.listviewclicked);
$("#" + trademe.id.makeoption).bind("change", trademe.event.makeoptionclicked);
$("#" + trademe.id.sortoption).bind("change", trademe.event.sortbyclicked);
},
createFooter: function () {
var footer = trademe.structure.create("div", null, trademe.cssclass.footer);
var poweredby = trademe.structure.create("div", null, trademe.cssclass.poweredby).html("Powered by ").append($("").attr("href", "http://www.trademe.co.nz/motors").attr("target", "_new").html("TradeMe Motors"));
trademe.structure.target
.append(footer.append(poweredby));
var f = window.setTimeout(function () {
footer.css({ marginTop: parseInt(trademe.structure.target.css("height").replace("px", "")), width: trademe.structure.target.width() });
window.clearTimeout(f);
}, 100);
},
createContainer: function () {
var container = trademe.structure.create("div", trademe.id.itemcontainerlist, trademe.cssclass.itemcontainerlist);
if (trademe.structure.target.find("." + trademe.cssclass.itemcontainerlist).length == 0) {
trademe.structure.target.append(container);
}
},
arrangeGalleryView: function () {
if ($(".item-container").length == 0) return;
var parentWidth = $(".gallery-view").innerWidth();
var item_per_row = Math.floor(parentWidth / $(".item-container").innerWidth());
//var marginLeft = parseInt($(".item-container").css("marginLeft").replace("px", ""));
var marginRight = parseInt($(".item-container").css("marginRight").replace("px", ""));
var marginTop = parseInt($(".item-container").css("marginTop").replace("px", ""));
//set position of each item-container
var item_index = 1;
var item_left = 0;
var item_top = 0;
$.each($(".gallery-view .item-container"), function (i, v) {
if (item_index > item_per_row) {
item_index = 1;
item_left = 0;
item_top += $(".item-container").outerHeight() + marginTop;
}
$(this).css({ 'left': item_left, 'top': item_top });
item_left = ($(".item-container").outerWidth() + marginRight) * item_index;
$(this).data("imageindex", 0);
//$(this).prepend("