// JavaScript Document

jQuery.noConflict();
jQuery(document).ready(function(){

    format_table();	
    show_hint();

    var year = jQuery("select[name='year_selection']").val();
    var month = jQuery("select[name='month_selection']").val();;
    jQuery("select[name='year_selection']").bind('change', function() {
            var opts = {};
            var f = jQuery(this).attr('class');
            year = jQuery(this).val();
            opts.f = f; opts.y = year;
            if(f == 'dey') {
                opts.m = jQuery("select[name='month_selection']").val();
            }

            get_data(opts);
    });

    jQuery("select[name='month_selection']").bind('change', function() {
            var opts = {};
            var f = jQuery(this).attr('class');
            month = jQuery(this).val();
            opts.f = f; opts.y = year, opts.m = month;

            get_data(opts);
    });

    jQuery('#payoutBtn').click(function() {
    
        fields = {}; fields.a = 'payout';
        jQuery('form#withdraw input, select').each(function() {
            fields[this.name] = jQuery(this).val();
        });

        jQuery.post('/earnings.php', fields, function(resp) {
            if(resp != 'Payout successful! One of our admins will approve your request soon enough. Thanks for using Linkbee.') {
                jQuery('div#msg').html(resp); 
            }
            else {
                alert(resp);
                jQuery('.cntTabs').tabs("load", 4);
            }
        });

        return false;
    });

    jQuery('#payoutBtn_v2').click(function() {
    
        fields = {}; fields.a = 'payout_v2';
        jQuery('form#withdraw_v2 input, select').each(function() {
            fields[this.name] = jQuery(this).val();
        });

        jQuery.post('/earnings_v2.php', fields, function(resp) {
            if(resp != 'Payout successful! One of our admins will approve your request soon enough. Thanks for using Linkbee.') {
                jQuery('div#msg').html(resp); 
            }
            else {
                alert(resp);
                jQuery('.cntTabs').tabs("load", 5);
            }
        });

        return false;
    });
});

function get_data(opts) {

    jQuery.get('/earning/common.php', opts, function(resp) {
        data = eval('(' + resp + ')');
        jQuery('div.tblContainer').html(data.t);
        jQuery('div.tblGraph').html(data.r);
        format_table();
    });
} 

function format_table() {
    // intiate tabs
	jQuery('.cntTabs').tabs();
	
	
	// table formating
	jQuery('.dataTbl td:last-child, .dataTbl th:last-child').css('border-right','none');
	
	jQuery('.tblTrDef tr:nth-child(2n)').addClass('trOdd');
	
	// show data details in table
	jQuery('.tblTrExt tr:odd').addClass('odd');
	jQuery('.tblTrExt tr:not(.odd)').hide();
	jQuery('.tblTrExt tr:first-child').show();
	jQuery('.tblTrExt tr:odd td:first-child').addClass('tdExt');
	jQuery('.tblTrExt tr:nth-child(4n+1) td').addClass('td1');
	jQuery('.tblTrExt tr:nth-child(4n+4) td').addClass('td4');
	
	jQuery('.tblTrExt tr.odd').click(function(){
			jQuery(this).next('tr').toggle();
			jQuery(this).find('td:first-child').toggleClass('tdExtUp');
	});
	
	// checkbox to show data with detals
	jQuery('#chkboxShowDataDetails').click(function(){
			if(jQuery(this).is(':checked')){																 
				jQuery('.tblTrExt tr.odd').next('tr').show();
				jQuery('.tblTrExt tr.odd').find('td:first-child').addClass('tdExtUp');		
			}
			else {
				jQuery('.tblTrExt tr.odd').next('tr').hide();
				jQuery('.tblTrExt tr.odd').find('td:first-child').removeClass('tdExtUp');	
			}
	});
	
	
	// intiate Payouts by Country table
	jQuery("#tblRatesByCountry").tablesorter(
    {
       headers:
       {  
         0 : { sorter: "text"  },
         1 : { sorter: "currency"  },
         2 : { sorter: "currency"  },
         3 : { sorter: "currency"  },
				 4 : { sorter: "currency"  },
				 5 : { sorter: "currency"  }
       }, 
       // widthFixed: true,
       widgets: ['zebra']
    }); 
	
	// checkbox to show all countries for Earnings
	jQuery('#chkboxShowAllCountries').click(function(){
			if(jQuery(this).is(':checked')){																 
				jQuery('#tblRatesByCountry tbody').find('tr').each(function(){
					if (jQuery(this).find('td:nth-child(3n)').text()=='' && jQuery(this).find('td:nth-child(5n)').text()==''){
						jQuery(this).hide();
					}
				});
			}
			else {
				jQuery('#tblRatesByCountry tr').show();
			}

			var sorting = [[0,0]]; 
			jQuery("#tblRatesByCountry").trigger("sorton",[sorting]); 
	});
	
	
	// intiate Clicks by Country table in Stats
	jQuery("#tblStatsByCountry").tablesorter(
    {
       headers:
       {  
         0 : { sorter: "text"  },
         1 : { sorter: "currency"  },
         2 : { sorter: "currency"  }
       }, 
       // widthFixed: true,
       widgets: ['zebra']
   }); 
	// checkbox to show all countries for Earnings
	jQuery('#chkboxStatsShowAllCountries').click(function(){
			if(jQuery(this).is(':checked')){																 
				jQuery('#tblStatsByCountry tbody').find('tr').each(function(){
					if (jQuery(this).find('td:nth-child(2n)').text()=='' && jQuery(this).find('td:nth-child(3n)').text()==''){
						jQuery(this).hide();
					}
				});
			}
			else {
				jQuery('#tblStatsByCountry tr').show();
			}
			var sorting = [[0,0]]; 
			jQuery("#tblStatsByCountry").trigger("sorton",[sorting]); 
	});

}

function show_hint() {
    jQuery('form#withdraw input').focus(function() {
        jQuery(this).next().slideDown();
    });

    jQuery('form#withdraw input').blur(function() {
        jQuery(this).next().hide();
    });
}

