Skip to content

Instantly share code, notes, and snippets.

@shanejdonnelly
Created July 6, 2012 17:32
Show Gist options
  • Save shanejdonnelly/3061504 to your computer and use it in GitHub Desktop.
Save shanejdonnelly/3061504 to your computer and use it in GitHub Desktop.
tech window for tbl
;(function($){
//here's our TechWindow constructor
$.TechWindow = function (el, options) {
this.options = options;
this.$el = $(el);
this.init();
};
//default options
$.TechWindow.prototype.defaults = {
site_url : 'http://technology.timberland.com',
lang : 'en',
image_path: '/wp-content/uploads/'
};
$.TechWindow.prototype.shop_translation = {
'en' : 'Shop Technology',
'uk' : 'Shop Technology',
'es' : 'Shop',
'it' : 'Shop',
'de' : 'Shop',
'fr' : 'Shop',
'zh' : '科技一览',
'tw' : '科技一覽',
'my' : 'BROWSE TECHNOLOGY',
'hk' : '科技一覽',
'sg' : 'BROWSE TECHNOLOGY',
'jp' : 'Shop'
};
$.TechWindow.prototype.getShopLink = function(links_array, lang){
var link = "";
var position = -1;
$(links_array).each(function(index,value){
if(value.indexOf(lang + '|') !== -1){
position = index
return false;
}
});
if(position !== -1){
link = links_array[position].split('|')
return link[1];
}
else{
return false;
}
}
$.TechWindow.prototype.createWindow = function(data){
var base = this;
//get the right shop link ready
var shop_link_array = data.post.custom_fields.shopLinks,
shop_link = base.getShopLink(shop_link_array, base.settings.lang);
var post = '<div class="tech_window_wrapper">';
post += '<h1>' + data.post.title + '</h1>';
post += '<a href="' + shop_link + '" target="_blank">'+ base.shop_translation[base.settings.lang] +'</a>';
post += '<img src="' + base.settings.site_url + base.settings.image_path + data.post.custom_fields.image + '" alt="' + data.post.title + '" />';
post += data.post.content;
post += '</div>';
//this would be better to do with v2.0, where you can use a jQuery object as the content in a fancybox
$.fancybox(post,{margin:0, padding:0});
}
$.TechWindow.prototype.init = function(){
var base = this;
base.settings = $.extend({}, base.defaults, base.options);
var post_slug = base.$el.data('post'),
json_url = base.settings.site_url + '/' + base.settings.lang + '/?json=get_post&post_slug=' + post_slug + '&custom_fields=shopLinks,image';
$.ajax({
url:json_url,
dataType: 'jsonp',
success:function(data){
//load the post image first so the fancybox sizes correctly
var $image = $('<img src="' + base.settings.site_url + base.settings.image_path + data.post.custom_fields.image + '" alt="' + data.post.title + '" />');
//check to see if image is already in cache - for IE7, make sure it fires the callback
if($image.height() > 0){
base.createWindow(data);
}else{
$image.load(function(){
base.createWindow(data);
});
}
},
error:function(){
return false;
},
});
}
//the jQuery plugin call
$.fn.techWindow = function(options) {
return this.each(function() {
new $.TechWindow(this, options);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment