var totalPage = '';
var currentPage = 1;
var queryString = '';
var sorting = '';
var sort_by = '';
var sorting_indicator = 0;
var fieldId = '';
var sortQuery = '';
/**************************************************
             * method to load products
             **************************************************/

function loadProducts(currentPageNo) {
    jQuery('.loading').fadeIn(900,0);
    jQuery('.loading').html('<img src="images/bigLoader.gif" />');
    jQuery.ajax({
        type: "POST",
        url: "getProductList.php",
        data: "pagetype="+pageType+"&task=loadPage&page="+currentPageNo+"&"+queryString+"&sortQuery="+sortQuery,
        success: function(data){
            jQuery('.loading').fadeOut('slow');
            if(data == 'noresult') {
                jQuery('.product_table').html('<div class="noresult">the search returned 0 result(s)</div>');
                jQuery('#paginationContainer').hide();
            } else {
                
                jQuery('#pgNo').html(currentPageNo);
                jQuery('#pageTotal').html(totalPage);
                jQuery('#paginationContainer').show();
                getPageDropdown();
                jQuery('.product_table').html(data);
                jQuery('#osd_part_number').click(function(){
                    fieldId = jQuery(this).attr('id');
                    sortQuery = getSortQuery(fieldId);
                    loadProducts(currentPage);
                });
                jQuery('#diagonal_size').click(function(){
                    fieldId = jQuery(this).attr('id');
                    sortQuery = getSortQuery(fieldId);
                    loadProducts(currentPage);
                });
                jQuery('#outline_dim_h').click(function(){
                    fieldId = jQuery(this).attr('id');
                    sortQuery = getSortQuery(fieldId);
                    loadProducts(currentPage);
                });
				jQuery('#outline_dim_v').click(function(){
                    fieldId = jQuery(this).attr('id');
                    sortQuery = getSortQuery(fieldId);
                    loadProducts(currentPage);
                });
				jQuery('#outline_dim_d').click(function(){
                    fieldId = jQuery(this).attr('id');
                    sortQuery = getSortQuery(fieldId);
                    loadProducts(currentPage);
                });
				 
				 jQuery('#resolutiono_v').click(function(){
                    fieldId = jQuery(this).attr('id');
                    sortQuery = getSortQuery(fieldId);
                    loadProducts(currentPage);
                });
 
                jQuery('#resolution_h').click(function(){
                    fieldId = jQuery(this).attr('id');
                    sortQuery = getSortQuery(fieldId);
                    loadProducts(currentPage);
                });
                jQuery('#color').click(function(){
                    fieldId = jQuery(this).attr('id');
                    sortQuery = getSortQuery(fieldId);
                    loadProducts(currentPage);
                });
                jQuery('#brightness').click(function(){
                    fieldId = jQuery(this).attr('id');
                    sortQuery = getSortQuery(fieldId);
                    loadProducts(currentPage);
                });
            }

        }
    });
}
/*************************************************/


/************************************************
             * method to get total number of pages
             ************************************************/
function getTotalPageNo() {
    jQuery.ajax({
        type: "POST",
        url: "getProductList.php",
        data: "pagetype="+pageType+"&task=getPageTotal&"+queryString,
        success: function(data){
            totalPage = data;
        }
    });
}
/**************************************************/

/*************************************************
             * method to populate page dropdown
             **************************************************/
function getPageDropdown() {
    jQuery.ajax({
        type: "POST",
        url: "getPageList.php",
        data: "totalpage="+totalPage,
        success: function(data){
            jQuery('#pageNoList').html(data);
        }
    });
}
/**************************************************/

function getSortQuery(id) {
    sort_by = "order by "+id;
    if(sorting_indicator == 0) {
        sorting = "ASC";
        sorting_indicator++;
    } else if(sorting_indicator == 1){
        sorting = "DESC";
        sorting_indicator++;
    } else if(sorting_indicator == 2) {
        sorting = "";
        sorting_indicator = 0;
        sort_by = "";
    }
    return sort_by+' '+sorting;
}

jQuery(document).ready(function(){
    jQuery(function() {
        getTotalPageNo();
        loadProducts(currentPage);
    });

    /*************************************
                 * jQuery for pagination - start
                 ************************************/

    jQuery('#next').click(function(){
        if(currentPage != totalPage) {
            currentPage = currentPage + 1;
            loadProducts(currentPage);
        }
        else {
            alert("You are on the last page!");
        }
    });
    jQuery('#prev').click(function(){
        if(currentPage != 1) {
            currentPage = currentPage - 1;
            loadProducts(currentPage);
        }
        else {
            alert("You are on the first page!");
        }
    });
    jQuery('#first').click(function(){
        if(currentPage != 1) {
            currentPage = 1;
            loadProducts(currentPage);
        }
        else {
            alert("You are on the first page!");
        }
    });
    jQuery('#last').click(function(){
        if(currentPage != totalPage) {
            currentPage = parseInt(totalPage);
            loadProducts(currentPage);
        }
        else {
            alert("You are on the last page!");
        }
    });
    jQuery('#goto').click(function(){
        var pgno = document.getElementById('pageno').value;
        if(currentPage != pgno) {
            currentPage = parseInt(pgno);
            loadProducts(currentPage);
        }
        else {
            alert("You are viewing page "+pgno);
        }
    });
    /*
                 * jQuery for pagination - end
                 */

    jQuery('input:radio[name=category]').bind('click',function(){
        //alert(jQuery('input:radio[name=category]:checked').val());
        jQuery('#categoryValue').html(jQuery('input:radio[name=category]:checked').val());
        jQuery('#catagoryFilter').show();
        jQuery('#search').trigger('click');
    });
    jQuery('input:radio[name=color]').bind('click',function(){
        jQuery('#colorValue').html(jQuery('input:radio[name=color]:checked').val());
        jQuery('#colorFilter').show();
        jQuery('#search').trigger('click');
    //alert(jQuery('input:radio[name=color]:checked').val());
    });
    jQuery('input:radio[name=brightness]').bind('click',function(){
        //alert(jQuery('input:radio[name=brightness]:checked').val());
        jQuery('#brightnessValue').html(jQuery('input:radio[name=brightness]:checked').val());
        jQuery('#brightnessFilter').show();
        jQuery('#search').trigger('click');
    });
    jQuery('input:radio[name=interface]').bind('click',function(){
        //alert(jQuery('input:radio[name=interface]:checked').val());
        jQuery('#interfaceValue').html(jQuery('input:radio[name=interface]:checked').val());
        jQuery('#interfaceFilter').show();
        jQuery('#search').trigger('click');
    });
    jQuery('input:radio[name=diagonal]').bind('click',function(){
        //alert(jQuery('input:radio[name=diagonal]:checked').val());
        jQuery('#diagonalValue').html(jQuery('input:radio[name=diagonal]:checked').val());
        jQuery('#diagonalFilter').show();
        jQuery('#search').trigger('click');
    });
    jQuery('input:radio[name=orientation]').bind('click',function(){
        //alert(jQuery('input:radio[name=orientation]:checked').val());
        jQuery('#orientationValue').html(jQuery('input:radio[name=orientation]:checked').val());
        jQuery('#orientationFilter').show();
        jQuery('#search').trigger('click');
    });
    jQuery('input:radio[name=touchpanel]').bind('click',function(){
        //alert(jQuery('input:radio[name=touchpanel]:checked').val());
        jQuery('#touchpanelValue').html(jQuery('input:radio[name=touchpanel]:checked').val());
        jQuery('#touchpanelFilter').show();
        jQuery('#search').trigger('click');
    });
    jQuery('input:radio[name=backlight]').bind('click',function(){
        //alert(jQuery('input:radio[name=backlight]:checked').val());
        jQuery('#backlightValue').html(jQuery('input:radio[name=backlight]:checked').val());
        jQuery('#backlightFilter').show();
        jQuery('#search').trigger('click');
    });
    jQuery('#search').click(function(){
        queryString = jQuery('#searchForm').serialize();
        getTotalPageNo();
        if(queryString != '') {
            jQuery('.filters').fadeIn().show();
        } else {
            jQuery('.filters').fadeOut();
        }
        currentPage = 1;
        loadProducts(currentPage);
    });
    jQuery('#filter1').click(function(){
        jQuery('input:radio[name=category]').attr('checked', false);
        jQuery('#catagoryFilter').fadeOut(1000);
        jQuery('#search').trigger('click');

    });
    jQuery('#filter2').click(function(){
        jQuery('input:radio[name=color]').attr('checked', false);
        jQuery('#colorFilter').fadeOut(1000);
        jQuery('#search').trigger('click');

    });
    jQuery('#filter3').click(function(){
        jQuery('input:radio[name=brightness]').attr('checked', false);
        jQuery('#brightnessFilter').fadeOut(1000);
        jQuery('#search').trigger('click');

    });
    jQuery('#filter4').click(function(){
        jQuery('input:radio[name=interface]').attr('checked', false);
        jQuery('#interfaceFilter').fadeOut(1000);
        jQuery('#search').trigger('click');

    });
    jQuery('#filter5').click(function(){
        jQuery('input:radio[name=diagonal]').attr('checked', false);
        jQuery('#diagonalFilter').fadeOut(1000);
        jQuery('#search').trigger('click');

    });
    jQuery('#filter6').click(function(){
        jQuery('input:radio[name=orientation]').attr('checked', false);
        jQuery('#orientationFilter').fadeOut(1000);
        jQuery('#search').trigger('click');

    });
    jQuery('#filter7').click(function(){
        jQuery('input:radio[name=touchpanel]').attr('checked', false);
        jQuery('#touchpanelFilter').fadeOut(1000);
        jQuery('#search').trigger('click');

    });
    jQuery('#filter8').click(function(){
        jQuery('input:radio[name=backlight]').attr('checked', false);
        jQuery('#backlightFilter').fadeOut(1000);
        jQuery('#search').trigger('click');

    });
});



