var PSW = function(config) {
var wizard = {
"dataurl": "/psw/psw.json",
"UIurl": "/psw/generated.html",
chain: [],
container: "#psg",
controls: {
next: "#nextstep"
},
currentStep: false,
currentScope: false,
nextStep_in_structure : false,
selectedValue: "",
status : "",
show: function() {
$A.util.maskScreen('#FFFFFF', .5, this.container);
this.$wizard.css('left',($(window).width()-myPSW.$wizard.width())/2+$(document).scrollLeft());
this.$wizard.css('top',($(window).height()-myPSW.$wizard.height())/2+$(document).scrollTop());
this.$wizard.show();
},
hide: function() {
$A.util.unmaskScreen('#FFFFFF', .5, this.container);
this.$wizard.hide();
},
findStep_in_steps: function(id) {
var findedNodes = [];
$.each(this.steps,
function(index, element) {
if (element.id == id) {
findedNodes.push(element);
}
}.createDelegate(this));
return findedNodes;
},
findStep_in_structure: function(id) {
var next_step = [];
var stepbyidSearch = function(node, value, findedNodes) {
typeof findedNodes == 'undefined' ? findedNodes = [] : false;
if (typeof node.id !== 'undefined' && node.id == value) {
findedNodes.push(node);
};
if (typeof node.step !== 'undefined') {
$.each(node.step,
function(i, node_in_array) {
if (typeof node_in_array.id !== 'undefined' && node_in_array.id == value) {
findedNodes.push(node_in_array);
return false;
};
stepbyidSearch(node_in_array, value, findedNodes);
});
};
return findedNodes;
};
var nextstepAvaliable = function(step, condition, findedNodes) {
typeof findedNodes == 'undefined' ? findedNodes = [] : false;
if (typeof step.step !== 'undefined') {
$.each(step.step,
function(i, node) {
if (typeof node.condition !== 'undefined' && node.condition == condition) {
findedNodes.push(node);
return false;
};
});
};
return findedNodes;
};
if (typeof id !== "undefined") {
next_step = this.findStep_in_steps(id);
} else {
!!this.currentScope ? false : this.currentScope=this.structure;
var current_step_in_structue = stepbyidSearch(this.currentScope, this.currentStep.id)[0];
var next_step_in_structue = nextstepAvaliable(current_step_in_structue, this.selectedValue)[0];
if ( !! next_step_in_structue) {
this.nextStep_in_structure = next_step_in_structue;
next_step = this.findStep_in_steps(next_step_in_structue.id);
$(this.container).addClass('next');
} else {
$(this.container).removeClass('next');
next_step = false;
}
}
return next_step;
},
goStep: function(step) {
typeof step === "undefined" ? step = this.currentStep : this.selectedValue="";
this.currentScope = this.nextStep_in_structure;
this.currentStep = this.findStep_in_steps(step.id)[0];
this.chain.push(this.currentStep);
this.updateNavigation();
this.handlers[this.currentStep.handler].createDelegate(this)(this.currentStep);
this.updateState();
},
goNextstep: function() {
var nextstep = this.findStep_in_structure()[0];
if ( !! nextstep) {
this.goStep(nextstep);
this.findStep_in_structure();
}
},
goStepchain: function(index) {
this.currentStep = this.chain[index];
this.nextStep_in_structure = this.chain[index].stepScope;
this.selectedValue=this.chain[index].selectedValue;
this.chain = this.chain.slice(0, index);
this.goStep();
},
updateState: function() {
if ( !! this.selectedValue) {
this.currentStep.selectedValue=this.selectedValue;
this.currentStep.stepScope=this.currentScope;
this.findStep_in_structure();
}
},
updateNavigation: function() {
$('#psg-steps ul').empty();
$('#psg-steps ul').prepend($('<li>').attr('id', 'nextstep').html('Next step'));
$.each(this.chain,
function(i, el) {
if (i != this.chain.length - 1) {
$('#psg-steps ul').append($('<li>').addClass('step').html('Step ' + (parseInt($.inArray(el, this.chain)) + 1)).click(function(e) {
this.goStepchain(i);
}.createDelegate(this)));
} else
{
$('#psg-steps ul').append($('<li>').attr('id', 'currentstep').html('Step ' + (parseInt($.inArray(this.currentStep, this.chain)) + 1)));
}
}.createDelegate(this));
},
handlers: {
single: function() {
this.$wizard.removeClass('psg-double psg-single next').addClass('psg-questions');
var isChecked = function(index) {
return !! parseInt(this.selectedValue[index]);
}.createDelegate(this);
var step = arguments[0];
var $fileds = $('<fieldset>');
$('#psg-questions #title').html(step.title);
var $fileds = $('<fieldset>');
$.each(step.param,
function(i, el) {
$.each(el,
function(i2, el2) {
var $input_element = $('<input>').attr({
'type': 'radio',
'value': '',
'name': 'single' + step.id
});
isChecked(i2) ? $input_element.attr('checked', 'checked') : false;
$fileds.append($('<label>').html(el2).prepend($input_element).hover(
function(e) {
$(e.target).addClass('hover')
},
function(e) {
$(e.target).removeClass('hover')
}
));
});
});
$('#psg-questions form').empty().append($fileds).find('label:odd').addClass('odd');
$('#psg-questions form fieldset input').click(function(e) {
e.preventDefault();
e.stopPropagation();
e.target.checked = 'checked';
$.each($('#psg-questions form fieldset input'),
function(i, el) {
$(el).removeAttr('checked');
});
$(e.target).attr('checked', 'checked');
this.selectedValue = "";
$.each($('#psg-questions form fieldset input'),
function(i, el) {
if ( !! el.checked) {
this.selectedValue = this.selectedValue + "1";
} else {
this.selectedValue = this.selectedValue + "0";
};
}.createDelegate(this));
this.updateState();
}.createDelegate(this));
$('#psg-questions form label').click(function(e) {
$(e.target).find('input').click();
});
},
multiple: function() {
this.$wizard.removeClass('psg-double psg-single next').addClass('psg-questions');
var isChecked = function(index) {
return !! parseInt(this.selectedValue[index]);
}.createDelegate(this);
var step = arguments[0];
var $fileds = $('<fieldset>');
$('#psg-questions #title').html(step.title);
var $fileds = $('<fieldset>');
$.each(step.param,
function(i, el) {
$.each(el,
function(i2, el2) {
var $input_element = $('<input>').attr({
'type': 'checkbox',
'name': 'multiple' + step.id
});
isChecked(i2) ? $input_element.attr('checked', true) : false;
$fileds.append($('<label>').html(el2).prepend($input_element));
});
});
$('#psg-questions form').empty().append($fileds).find('label:odd').addClass('odd');
$('#psg-questions form fieldset input').click(function(e) {
e.stopPropagation();
this.selectedValue = "";
$.each($('#psg form fieldset input'),
function(i, el) {
el.checked ? this.selectedValue = this.selectedValue + "1": this.selectedValue = this.selectedValue + "0";
}.createDelegate(this));
this.updateState();
}.createDelegate(this));
},
getProductsInfo: function(step) {
this.selectedValue = false;
var fillProductInfo = function(product, $product_layer) {
$product_layer.find('.psg-calculator input').remove();
$product_layer.find('h4').html(product["title"]);
$product_layer.find('.buyblock-description').html(product["description"]);
$product_layer.find('.buyblock-more .more').attr('href', product["url-info"]);
$product_layer.find('.buyblock-more .download').attr('href', product["url-trial"]);
$product_layer.find('.buyblock-image img').attr({
'src': product["box-image"],
'alt': product["title"]
});
typeof product["addons-note"] !== 'undefined' ? $product_layer.find('.addons-note').html(product["addons-note"]) : $product_layer.find('.addons-note').empty();
if (typeof product["custom-buy"] === 'undefined') {
var $input_elements = $();
var $addons_elements = $();
$input_elements = $input_elements.add($('<input>').attr({
'type': 'hidden',
'value': product['productId'],
'name': 'id'
}));
if (typeof product.options !== 'undefined') {
$.each(product.options,
function(index, option) {
$addons_elements = $addons_elements.add(
$('<li>').attr({
'id': 'addon' + option['id']
}).append(
$('<strong>').addClass('clear').append(
$('<input>').attr({
'type': 'checkbox',
'name': 'o',
'value': option['id'],
'checked': 'checked'
})
).append($('<a>').attr({
'href': option['url']
}).html(option['title']))
).append(
$('<span>').addClass('a-calculator-o-discount a-calculator-o-discount-' + option['id']),
$('<span>').addClass('a-calculator-o a-calculator-o-' + option['id'])
)
);
});
}
$product_layer.find('.psg-calculator').append($input_elements);
$product_layer.find('.addons').empty().append($addons_elements);
$product_layer.find('.buybutton').html('Buy now for <strong class="a-calculator-price"></strong><span></span>');
$A.initCalculators($product_layer.find('.psg-calculator'));
} else {
$product_layer.find('.buybutton').attr('href', product["custom-buy"]["link"]).html(product["custom-buy"]["button-title"]).append($('<span>'));
}
};
this.$wizard.find('#currentstep').html('Result');
if (step.products.length == 2) {
this.selectedValue = false;
this.$wizard.removeClass('psg-questions next psg-single').addClass('psg-double');
$.each(step.products,
function(index, product) {
var $product_layer = this.$wizard.find('#psg-double .block > div').eq(index);
fillProductInfo(product, $product_layer);
}.createDelegate(this));
}
if (step.products.length == 1) {
this.selectedValue = false;
this.$wizard.removeClass('psg-questions next psg-double').addClass('psg-single');
var product = step.products[0];
var $product_layer = this.$wizard.find('#psg-single');
fillProductInfo(product, $product_layer);
}
}
}
}
wizard = $.extend(wizard, config);
$.getJSON(wizard.dataurl,
function(data) {
wizard = $.extend(wizard, data);
$.get(wizard.UIurl,
function(data) {
wizard.status = 'ok';
$('body').append(data);
wizard.$wizard = $(wizard.container);
wizard.$wizard.find(wizard.controls.next).live("click",
function(e) {
this.goNextstep();
}.createDelegate(wizard));
wizard.$wizard.find('.close').click(function(e) {
this.hide();e.stopPropagation();
}.createDelegate(wizard));
wizard.goStep(wizard.findStep_in_structure(1)[0]);
wizard.updateState();
});
});
return wizard
};
