/* Author: 

*/


$("select#filter-product").jgdDropdown({
    clsLIExpand: false,
    clsLISelected: 'hide_selected',
    initTitle: 'PRODUCTS',
    callback: function (obj, val) {
        location.href = val;
    }
});

$("select#filter-supplier").jgdDropdown({
    clsLIExpand: false,
    clsLISelected: 'hide_selected',
    initTitle: 'SUPPLIERS',
    callback: function(obj, val) {
        location.href = val;	
    }
});

$("select#filter-applications").jgdDropdown({
    clsLIExpand: false,
//    clsLISelected: 'hide_selected',
    initTitle: 'APPLICATIONS',
    callback: function(obj, val) {
        location.href = val;	
    }
});



function showYouTube(container, videoID, width, height) {
    flashembed(container, { src: 'http://www.youtube.com/v/' + videoID + '?enablejsapi=1&playerapiid=ytplayer', wmode: 'opaque', width: width, height: height }, { allowScriptAccess: "always" });
}

function page(listContainer, itemWidth, pageSize, el, pageInc) {
    $(el).unbind('click');
    // template only allows paging to the right
    //var pageInc = 1;
    var currentPage = $('ul', listContainer).data().CurrentPage;
    var total = $('ul li', listContainer).size();

    var totalPages = (total > pageSize) ? Math.ceil(total / pageSize) : 1;

//    if ((currentPage + pageInc) < 1 || totalPages == 1) {
//        $(el).click(function () {
//            page(listContainer, itemWidth, pageSize, this, pageInc);
//        });
//        return; 
//    }

    // if on the last page, need to scroll back to page 1 ...
    var pageMove = pageInc;
    if ((currentPage + pageInc) > totalPages) { pageMove = -(totalPages - 1); }

    // if on the first page, need to scroll back last page ...
    if ((currentPage + pageInc) < 1) {  pageMove = (totalPages - 1); }

    currentPage += pageMove;

    $('ul', listContainer).data({ CurrentPage: currentPage });

    var pos = ((itemWidth) * pageSize) * (pageMove > 0 ? pageMove : -pageMove);
    //    if (callback) {
    //        $('.clipper ul').animate({ left: (page > 0 ? '-' : '+') + '=' + pos + 'px' }, callback());
    //    }
    //    else {
    $('ul', listContainer).animate(
        {
            left: (pageMove > 0 ? '-' : '+') + '=' + pos + 'px'
        },
        function () {
            $(el).click(function () {
                page(listContainer, itemWidth, pageSize, this, pageInc);
            });
        }
    );
    //    }
}


function pageVertical(listContainer, itemHeight, pageSize, el, pageInc) {
    $(el).unbind('click');
    // template only allows paging to the right
    //var pageInc = 1;
    var currentPage = $('ul', listContainer).data().CurrentPage;
    var total = $('ul li', listContainer).size();

    var totalPages = (total > pageSize) ? Math.ceil(total / pageSize) : 1;

    // if on the last page, need to scroll back to page 1 ...
    var pageMove = pageInc;
    if ((currentPage + pageInc) > totalPages) { pageMove = -(totalPages - 1); }

    // if on the first page, need to scroll back last page ...
    if ((currentPage + pageInc) < 1) { pageMove = (totalPages - 1); }

    currentPage += pageMove;

    $('ul', listContainer).data({ CurrentPage: currentPage });

    var pos = ((itemHeight) * pageSize) * (pageMove > 0 ? pageMove : -pageMove);
    
    $('ul', listContainer).animate(
        {
            top: (pageMove > 0 ? '-' : '+') + '=' + pos + 'px'
        },
        function () {
            $(el).click(function () {
                pageVertical(listContainer, itemHeight, pageSize, this, pageInc);
            });
        }
    );
    //    }
}












