var features = { feature0 : ['boost-speed','disk-defrag'], feature1 : ['boost-speed','registry-defrag'], feature2 : ['boost-speed','disk-defrag','registry-defrag','registry-cleaner'], feature3 : ['boost-speed','registry-cleaner'], feature4 : ['boost-speed','disk-defrag'], feature5 : ['boost-speed'], feature6 : ['emergency-recovery'], feature7 : ['boost-speed','duplicate-file-finder'], feature8 : ['boost-speed','task-manager'], feature9 : ['boost-speed','system-information'], feature10 : ['boost-speed','emergency-recovery'], feature11 : ['task-manager'], feature12 : ['boost-speed'], feature13 : ['antivirus']}; var products={ 'antivirus':{ name:'Element Antivirus 2010', url:'/en/software/antivirus'}, 'boost-speed':{ name:'Element BoostSpeed', url:'/en/software/boost-speed'}, 'disk-defrag':{ name:'Element Disk Defrag', url:'/en/software/disk-defrag'}, 'disk-defrag-screen-saver':{ name:'Element Disk Defrag Screen Saver', url:'/en/software/disk-defrag-screen-saver'}, 'duplicate-file-finder':{ name:'Element Duplicate File Finder', url:'/en/software/duplicate-file-finder'}, 'emergency-recovery':{ name:'Element Emergency Recovery', url:'/en/software/emergency-recovery'}, 'registry-cleaner':{ name:'Element Registry Cleaner', url:'/en/software/registry-cleaner'}, 'registry-defrag':{ name:'Element Registry Defrag', url:'/en/software/registry-defrag'}, 'system-information':{ name:'Element System Information', url:'/en/software/system-information'}, 'task-manager':{ name:'Element Task Manager', url:'http://www.fileinspect.com/task-manager/'}}; var suggestion_title = 'Suggestions:'; var empty_check ='Please select one or more checkboxes above.'; var all_features = {}; var checked_features= {}; $(document).ready(function(){ // setup html selector_content = "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "Element Web Essentials" + "

Which features do you want to use?

" + "" + "
" + "
" + "Close" + "
" + "
"; $('body').append(selector_content); if ($.fn.pngFix) { $('#selector-win').pngFix(); } // setup window resize and scrolls $(window).bind("resize scroll", PS_UpdateOnScroll); // setup "close" button $('#selector-win-close, #selector-win a.close').click(function(){ PS_Hide(); }); // setup Product Selector checkboxes $('#selector-win ul.product-selector input').click(function(){ PS_Recalculate(); }); }); /** * Recalculate checkboxes and update "suggestion" div * * @return void */ function PS_Recalculate() { // get selected checkboxes for each product checked_features= PS_SumFeatures( '#selector-win ul.product-selector input:checked' ); // if checkbox checked if ($('#selector-win ul.product-selector input:checked').length == 0) { $('#selector-win #suggestions').html(empty_check); return; } // get products for _all_ checked checkboxes // i mean checked_products - products array, wich linked with each checked checkboxes suggest_products = PS_Intersect( '#selector-win ul.product-selector input:checked' ); if ( suggest_products.length == 0 || suggest_products[0].length == 0) { suggest_products = PS_GetTop( '#selector-win ul.product-selector input:checked' ); } // update '#suggestion' div PS_PrintFeaturedProducts('#selector-win #suggestions', products, suggest_products ); } /** * Returns count features for every products * @example * var products = PS_SumFeatures('#selector-win ul.product-selector input:checked') ); * // products now {'boost-speed':2,'disk-defrag':1,'registry-defrag':1} * * @param string emelent * @return */ function PS_SumFeatures( element ) { var result = {} $(element).each(function() { // features for product product_features = features[$(this).attr('id')]; for (i in product_features) { product_id = product_features[i]; if (result[product_id]) result[product_id]++; else result[product_id]=1; } }); return result; } /** * Returns product list, with all checked features * * @example * products = PS_Intersect('#selector-win ul.product-selector input:checked'); // "elm1" alterted * * @param array sets * @return array */ function PS_Intersect( element ) { result = []; featured_products=PS_SumFeatures(element); for (i in featured_products) { if ( featured_products[i]==$(element).length ) result.push(i); } result = new Array(result); return result; } /** * @return array */ function PS_GetTop( element ) { // @example result = [[boost-speed,disk-defrag],[antivirus]] result= []; // @example inresult = {'boost-speed':1,'disk-defrag':1} inresult = {}; $(element).each(function() { // product list with checked feature product_features = features[$(this).attr('id')]; // sort array product_features on checked_features, then on all_features // if used SQL, then this code equeal query // SELECT `product_id` FROM `product_features` ORDER BY `checked_features` DESC,`all_features` DESC product_features.sort( PS_Sort ); // get most "popular" product. product_id = product_features[0]; if ( !inresult[product_id] ) { inresult[product_id] = 1; result.push([product_id]); } }); return result; } /** * Sort array product_features pushing up products with the largest fields `checked_features` and `all_features` * * @return array */ function PS_Sort(a,b) { if ( checked_features[a] > checked_features[b] ) { return -1; } else if ( checked_features[a] < checked_features[b] ) { return 1; } else if ( all_features[a] > all_features[b] ) { return -1; } else if ( all_features[a] < all_features[b] ) { return 1; } return 0; } /** * Print product list * * @example * PS_PrintFeaturedProducts( * '', * {'boost-speed':'Element BoostSpeed', * 'disk-defrag':'Element DiskDefrag', * 'reg-cleaner':'Element Registry Cleaner' * }, * ['boost-speed','reg-cleaner'] * ); * * * @param object products - all products * @param array featured - array of need products * @return array of string */ function PS_PrintFeaturedProducts( element, products, featured ) { var result_and=new Array(); for (i in featured ) { result_or = []; for (id in featured[i]) { featured_product = products[featured[i][id]]; if ( featured_product && featured_product.name ) result_or.push("" + featured_product.name + ""); } result_and.push(result_or.join(" or ")); } $(element).html( ""+suggestion_title+"
" + result_and.join(" and ") ); } /** * On browser window scroll or resize, update "Product Selector" window position * * @return void */ function PS_UpdateOnScroll() { pos_left = $(window).width()/2 - ( $('#selector-win').width() /2); pos_top = $(document).scrollTop() + ( $(window).height()/2 - $('#selector-win').height()/2 ); $('#selector-win').css('left',pos_left + 'px'); $('#selector-win').css('top', pos_top + 'px'); } /** * Show Product Selector window * * @return void */ function PS_Show() { selector_width = $('#selector-win').width() + Number($('#selector-win').css('padding-right').replace(/px/,'')) + Number($('#selector-win').css('padding-left').replace(/px/,'')); pos_left = $(window).width()/2 - ( selector_width /2); pos_top = $(document).scrollTop() + ( $(window).height()/2 - $('#selector-win').height()/2 ); $('#selector-win').css('top', pos_top + 'px'); $('#selector-win').css('left',pos_left + 'px'); $('#selector-win').show(); $('#dark-shadow').show(); $('select').css('visibility','hidden'); all_features = PS_SumFeatures( '#selector-win ul.product-selector input' ); PS_Recalculate(); } /** * Hide Product Selector window */ function PS_Hide() { $('#selector-win').hide(); $('#dark-shadow').hide(); $('select').css('visibility','visible'); }