var d 		= new Date();
var nd 		= new Date(d.getFullYear(),d.getMonth(),d.getDate()+1,d.getHours()-1,d.getMinutes(),d.getSeconds());	
 $(function(){	  
	 $('#daterange').daterangepicker({arrows: false,presets:{		
			specificDate: 'Specific Date', 
			dateRange: 'Date Range'
		},presetRanges: [
			{text: 'Today', dateStart: 'today', dateEnd: 'today' },
			{text: 'Last 7 days', dateStart: 'today-7days', dateEnd: 'today' },
			{text: 'Month to date', dateStart: function(){ return Date.parse('today').moveToFirstDayOfMonth();  }, dateEnd: 'today' },
			{text: 'Previous Month', dateStart: function(){ return Date.parse('1 month ago').moveToFirstDayOfMonth();  }, dateEnd: function(){ return Date.parse('1 month ago').moveToLastDayOfMonth();  } }

		],latestDate:nd,dateFormat:datePickerDateFormat,placeHolder:'dateOverView',rangeDays:90,rangeSplitter:' - ',datepickerOptions: {maxDate:nd}});
	});
	
$('.ui-daterangepicker-dateRange').addClass('ui-state-active');	

$(document).ready(function(e) {

	$('.main_menu').find("a[rel='affDashboard']").addClass('active');
	
	$("#replotGraph").click(function(){
		var data=new Object();var arr=new Array();
		data.dateRange=$("#daterange").val();
		$(".labels").each(function(){
			if(this.checked)
				arr.push(this.value);
		});
		data.fields=JSON.stringify(arr);
		$.ajax({
		   type: "POST",
		   url: GLOBAL_BASE_PATH+"/index/ajaxGraph/",
		   data: data,
		   beforeSend : function ()
				{
				$('#affiliateHolder').block(
					{ 
					message: $('#blockUILoader'),
					css: { 
						padding:        0, 
						margin:         0, 
						width:          '30%', 
						top:            '40%', 
						left:           '35%', 
						textAlign:      'center', 
						color:          '#000', 
						border:         '0', 
						backgroundColor:'none', 
						cursor:         'wait' 
						},
					overlayCSS: 
						{ 
						backgroundColor: '#000', 
						opacity:         0.6 
						}											   
					});								
				},
		   success: function(msg){
		      $("#affiliateHolder").empty();
			  $("#affiliateHolder").html(msg);
			  $('#affiliateHolder').unblock({ message: null });
			  $(".show-box").slideUp();
			  $(".settings").toggleClass("menu-open");
		   }
		 });
		if(!$(".labels:checked").length)
		 	$(".graphTotalHolderUl").html('');

	});
	$("#widgetSelect").change(function(){
		var data=new Object();
		data.value=this.value;
		$.ajax({
		   type: "POST",
		   url: GLOBAL_BASE_PATH+"/index/ajaxDemoGraph/",
		   data: data,
		   success: function(msg){
		      if($.trim(msg)!='' && msg.indexOf('<tbody>')>0){
			  $("#tableSort-demo").html(msg);
			   	$("#tableSort-demo").show();
			  	$("#demo").hide();
			  }
			  else{
			   $("#demo").show();
			   $("#tableSort-demo").hide();
			   }
		   }
		 });
	});
	
	
});