$.fn.infiniteCarousel = function ( fstr ) { function repeat(str, num) { return new Array( num + 1 ).join( str ); } return this.each(function () { //Netejo si ja s'havia cridat el carrousel $('.empty', this).remove(); $('.first', this).remove(); $('.last', this).remove(); $('.shown', this).removeClass('shown'); $('.notshown', this).removeClass('notshown'); //Volem filtrar if ( fstr != "" ) { $('.infiniteCarousel div ul li').each ( function ( o, elem ) { var rg = new RegExp ( fstr, 'i' ); var c = new String ($(this).find(".textpart").html()); if ( c.search (rg) != -1 ) { $(this).addClass('shown'); } else { $(this).addClass('notshown'); } }); } else { $('.thumbit', this).addClass('shown'); } $('.notshown', this).hide(); $('.shown', this).show(); $wrapper = $('> div', this).css('overflow', 'hidden'), $slider = $wrapper.find('> ul'), $items = $slider.find('> li').filter('.shown'), $single = $items.filter(':first'), singleWidth = $single.outerWidth(), visible = Math.round($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border currentPage = 1, pages = Math.ceil($items.length / visible); if (($items.length % visible) != 0) { $slider.append(repeat('
', visible - ($items.length % visible))); $items = $slider.find('> li').filter('.shown'); } $items.filter(':first').before($items.slice(- visible).clone().addClass('first')); $items.filter(':last').after($items.slice(0, visible).clone().addClass('last')); $items = $slider.find('> li').filter('.shown'); // reselect no fa res $wrapper.scrollLeft(singleWidth * visible); function gotoPage(page) { var dir = page < currentPage ? -1 : 1, n = Math.abs(currentPage - page), left = singleWidth * dir * visible * n; $wrapper.filter(':not(:animated)').animate({ scrollLeft : '+=' + left }, 500, function () { //alert ( "page " +page + " pages " + pages ); if (page == 0) { $wrapper.scrollLeft(singleWidth * visible * pages); page = pages; } else if (page > pages) { $wrapper.scrollLeft(singleWidth * visible); // reset back to start position page = 1; } currentPage = page; //$(".textpart").css('background-color','white'); firstvit = visible * (currentPage-1); $(".selectedit").removeClass('selectedit'); objit = $items.filter(':not(.first)').filter(':nth('+firstvit+')'); objit.addClass('selectedit'); select_model (objit.attr("id")); }); return false; } $wrapper.after('<>'); $(".selectedit").removeClass('selectedit'); objit = $items.filter(':not(.first)').filter(':nth('+0+')'); objit.addClass('selectedit'); select_model (objit.attr("id")); // 5. Bind to the forward and back buttons $('a.back', this).click(function () { return gotoPage(currentPage - 1); }); $('a.forward', this).click(function () { return gotoPage(currentPage + 1); }); // create a public interface to move to a specific page $(this).bind('goto', function (event, page) { gotoPage(page); }); /* $(this).bind('filter', function (event, fstr) { alert ("filter "+$items.length); });*/ }); }; function select_model (modelid ) { var p= (modelid).replace("Small_","") ; var q= (modelid).replace(/_/g,"-").replace("Small_",""); $("#featmodel").css('background-image','url(/image/models/'+p+'.jpg)'); $("#subform").attr ('action','http://www.blackberry-unlock.com/'+q+'-sim-unlock-code/'); $("#phonemodel").html(modelid.replace(/_/g," ").replace(/Small/,"")); } $(document).ready(function () { $("#featmodel").css('background-image','url(/image/models/)'); $("#ctsearch").val(""); $('.infiniteCarousel').infiniteCarousel(""); $('.jsfallback').remove(); $(".thumbit").click(function(event, ui){ select_model (this.id); $(".selectedit").removeClass('selectedit'); $(this).addClass('selectedit'); //$(this).filter().$("#"+this.id).addClass('selectedit'); }); $("#btnsearch").click(function(event, ui){ //alert (this.value); $('.infiniteCarousel').infiniteCarousel($("#ctsearch").val() ); }); $("#ctsearch").keyup(function(event, ui){ //alert (this.value); $('.infiniteCarousel').infiniteCarousel($("#ctsearch").val() ); }); $("#btncancel").click(function(event, ui){ //alert (this.value); $("#ctsearch").val(""); $('.infiniteCarousel').infiniteCarousel("" ); }); });