$(document).ready(function() {

    var hash = window.location.hash;
    if ($(hash).length > 0 && $('[href="'+hash+'"]').length > 0) 
    {
        $('[href="'+hash+'"]').click();
    }

	$(".important_button").click(function () {
		$(this).toggleClass("important_button_active");
		$(".important_button_text").toggleClass("important_button_text_active");

		var $input = $(this).closest('.important_box').find('input');
		if($input.val() == $input.data('simple'))
		{
			$input.val($input.data('fire'));
		}
		else
		{
			$input.val($input.data('simple'));
		}
	});

	$(window).scroll(function(){
		if($(window).scrollTop() > 700)
		{
			$('.up-button').show();
		}
		else
		{
			$('.up-button').hide();
		}
	});

	$('.up-button').click(function(){
		$("html, body").animate({ scrollTop: 0 }, "slow");
	});

	$("a.toggle_bottom").click(function() {
		var a = $(this);
		$("html, body").animate({ scrollTop: $(a.attr('href')).position().top - 50 }, "slow");
		return false;
	});

	$('a#load-post').click(function() {

		var button = $(this), loading = $('.loading-post'), offset = button.data('offset'), tag = button.data('tag');

		button.hide();
		loading.show();

		$.ajax({
			method: 'POST',
			url: "/blog/post/load",
			data: {
				offset: offset,
				tag: tag
			},
			success: function(response) 
			{
				$('.loaded').append(response.posts);
				button.data('offset', response.offset);

				if(response.count <= response.offset)
				{
					button.hide();
				}
				else
				{
					button.show();
				}

				loading.hide();
			}
		});

		return false;

	});

	$.ajax({
		url: "/sessions/default/add"
	});

	setInterval(function(){
		$.ajax({
			url: "/sessions/default/update"
		});
	}, 1000 * 60 * 5);
	

	$('form#error-testing-form').on('beforeSubmit', function(e) {
	    $.ajax({
	        url: $('form#error-testing-form').attr('action'),
	        method: 'POST',
	        dataType: 'json',
	        data: {
	            message: $('[name="message"]').val()
	        },
	        success: function(response){
	            if(response.success)
	            {
	                $(".error_fedback").html('<div class="row"><div class="col-sm-8" style="margin:0 auto;float:none;"><div class="gray_box"><p>' + response.message + '</p></div></div></div>');
	            }
	        }
	    });
	}).on('submit', function(e){
	    return false;
	});
});

if($('#chart-container').length > 0)
{
	FusionCharts.ready(function () {
		var revenueChart = new FusionCharts({
		    type: 'doughnut2d',
		    renderAt: 'chart-container',
		    width: '100%',
		    height: '450',
		    dataFormat: 'json',
		    dataSource: {
		        
		        "chart": {
		            //"caption": "Expenditures Incurred in Publishing a Book",
		            "bgcolor": "FFFFFF",
		            "showvalues": "1",
		            "showpercentvalues": "1",
		            "showborder": "0",
		            "showplotborder": "0",
		            //"showlegend": "1",
		            //"legendborder": "0",
		            //"legendposition": "bottom",
		            "enablesmartlabels": "1",
		            "use3dlighting": "0",
		            "showshadow": "0",
		            //"legendbgcolor": "#CCCCCC",
		            //"legendbgalpha": "20",
		            //"legendborderalpha": "0",
		            //"legendshadow": "0",
		            //"legendnumcolumns": "3",
		            "labelFont": "RobotoBold",
		            "labelFontColor": "2d323a",
		            "labelFontSize": "14",
		            "labelFontBold": "1",
		            

		            "palettecolors": "#ff3030,#ff7711,#ffc600,#95d900,#97cbdd,#cdc9c4",
		            "showHoverEffect":"1",
		        },
		        "data": [ 
		            {
		                "label": "Переходы из поисковых сетей",
		                "value": "71.75"
		            },
		            {
		                "label": "Переходы по рекламе",
		                "value": "12.28"
		            },
		            {
		                "label": "Прямые заходы",
		                "value": "12.19"
		            },
		            {
		                "label": "Другие",
		                "value": "2.26"
		            },
		            {
		                "label": "Другие",
		                "value": "0.76"
		            },
		            {
		                "label": "Другие",
		                "value": "0.76"
		            }
		        ]
		    }
		}).render();
	});
}

if($('#chart-container-2').length > 0)
{
	FusionCharts.ready(function () {
		var revenueChart = new FusionCharts({
		    type: 'column2d',
		    renderAt: 'chart-container-2',
		    width: '100%',
		    height: '300',
		    dataFormat: 'json',
		    dataSource: {
		        "chart": {
		            //"caption": "Monthly revenue for last year",
		            //"subCaption": "Harry's SuperMart",
		            "xAxisName": "",
		            "yAxisName": "Визиты:",
		            //"numberPrefix": "",
		            "paletteColors": "#c3b0fa",
		            "bgColor": "#ffffff",
		            "borderAlpha": "20",
		            "canvasBorderAlpha": "0",
		            "usePlotGradientColor": "0",
		            "plotBorderAlpha": "10",
		            "placevaluesInside": "1",
		            "rotatevalues": "1",
		            "valueFontColor": "#ffffff",                
		            "showXAxisLine": "1",
		            "xAxisLineColor": "#999999",
		            "divlineColor": "#999999",               
		            "divLineIsDashed": "1",
		            "showAlternateHGridColor": "0",
		            "subcaptionFontBold": "0",
		            "subcaptionFontSize": "14"
		        },            
		        "data": [
		            {
		                "label": "Июн 14",
		                "value": "3000"
		            }, 
		            {
		                "label": "",
		                "value": "3400"
		            }, 
		            {
		                "label": "Авг 14",
		                "value": "7100"
		            }, 
		            {
		                "label": "",
		                "value": "9200"
		            }, 
		            {
		                "label": "Окт 14",
		                "value": "3400"
		            }, 
		            {
		                "label": "",
		                "value": "2850"
		            }, 
		            {
		                "label": "Дек 14",
		                "value": "2800"
		            }, 
		            {
		                "label": "",
		                "value": "2400"
		            }, 
		            {
		                "label": "Фев 15",
		                "value": "3800"
		            }, 
		            {
		                "label": "",
		                "value": "3900"
		            }, 
		            {
		                "label": "Апр 15",
		                "value": "3500"
		            },
		            {
		                "label": "",
		                "value": "3500"
		            },
		            {
		                "label": "Июн 15",
		                "value": "4100"
		            },
		            {
		                "label": "",
		                "value": "5900"
		            },
		            {
		                "label": "Авг 15",
		                "value": "5100"
		            },
		            {
		                "label": "",
		                "value": "4600"
		            },
		            {
		                "label": "Окт 15",
		                "value": "2800"
		            },
		            {
		                "label": "",
		                "value": "4500"
		            },
		            {
		                "label": "Дек 15",
		                "value": "5100"
		            }
		        ],
		        "trendlines": [
		            {
		                "line": [
		                    {
		                        "startvalue": "700000",
		                        "color": "#1aaf5d",
		                        "valueOnRight": "1",
		                        "displayvalue": "Monthly Target"
		                    }
		                ]
		            }
		        ]
		    }
		}).render();
	});
}