﻿$(document).ready(function () {

    $("#tabs").tabs({
        //select: function (event, ui) {alert('select') },
        //load: function(event, ui) { alert('load') },
        show: function (event, ui) { $('#stat_submit_filter').click(); }
        //, enable: function (event, ui) { alert('enable') }
    });
    

    var positionToShow = $('#stattistic_position_of_tab_element_to_show_on_start').val();
    if (positionToShow == null) {
        $('div.statistic_tabs ul li:hidden').first().show();
    }
    $("#tabs").tabs("select", positionToShow);
    //showStatisticChart('#HiddenFieldForChartGuid');



});


function statEditSelectedTab(tab, tab_id, tab_name) {
    if ($(tab).parent().hasClass('ui-tabs-selected')) {
        var href = $('#stat_tab_edit_button').attr('href');
        href = href.substring(0, href.indexOf("&tab_id="));
        href = href + "&tab_id=" + tab_id + "&tab_name=" + tab_name;
        $('#stat_tab_edit_button').attr('href', href);
        $('#stat_tab_edit_button').click();
    }
}

function statEditSelectedOption(option_id, tab_id) {
        var href = $('#stat_option_edit_button').attr('href');
        href = href.substring(0, href.indexOf("&tab_id="));
        href = href + "&tab_id=" + tab_id + "&option_id=" + option_id;
        $('#stat_option_edit_button').attr('href', href);
        $('#stat_option_edit_button').click();
}


function hideStattisticCheckboxes() {
    var sum = "";
    $("div.stat_options:visible input[name=all_options]:checked").each(function () {
        //add only if the value is number
        if (!isNaN(this.value) && this.value.length != 0) {
            sum += this.value + ",";
        }
    });
    $('#hidden_for_statistic_options').val(sum);
}
function statGraphPrepareSubmit() {
    hideStattisticCheckboxes();
    $('#stat_graph_container_for_filter_option span.error').hide();
    var GroupBy = $('#stat_graph_container_for_filter_option select[name=group_by]').val();

    var From = $('#stat_graph_container_for_filter_option input[name=date_from]').val();
    var To = $('#stat_graph_container_for_filter_option input[name=date_to]').val();
    From = parseToDate(From);
    To = parseToDate(To);
    var daydifference = dayDiff(To, From);
    if (daydifference < 0)
        daydifference = daydifference * (-1);
    if (GroupBy == 1) {
        if (daydifference > 90) {
            $('#stat_graph_container_for_filter_option span.error').show();
            return false;
        }
    }

    if (GroupBy == 2) {
        if (daydifference / 30 > 90) {
            $('#stat_graph_container_for_filter_option span.error').show();
            return false;
        }
    }
    $('#stst_form_with_table_to_submit').submit();



}




function showStatisticChart(guid_field_ident) {
    var params = { wmode: "opaque" };
    var attributes = {};
    swfobject.embedSWF("/Content/swf/open-flash-chart.swf", "StattisticChart", "766", "300",
	  "9.0.0", "expressInstall.swf",
	  { "data-file": "/no/Chart/Statistic?ident=" + $(guid_field_ident).val() ,"loading":""}
	  , params, attributes
	  );
}

//Показать персональную вкладку пользователя(Для селекта).
function showUserStatisticTab(stat_tab_to_show, select_container, pos) {
    $('.stat_tabs_li_hidden').hide();
    $(stat_tab_to_show).show();
    $(select_container).hide();
    $("#tabs").tabs("select", pos)
}


function stat_change_graph_subtype(select) {

    //Очистить все.
    $('div#popup_stat_for_subtype_selects div').hide();
    $('div#popup_stat_for_subtype_selects div select').attr("name", "");

    //Присвоить только одному.
    $('div#popup_stat_for_subtype_selects div#popup_stat_for_subtype_' + $(select).val() + '').show();
    $('div#popup_stat_for_subtype_selects div#popup_stat_for_subtype_' + $(select).val() + ' select').attr("name", "subtype_id");

    //все селекты видимые 
    $('div#popup_stat_main select[name=partner_id]').removeAttr("disabled");
    $('div#popup_stat_main select[name=service_code_id]').removeAttr("disabled");
    $('div#popup_stat_main select[name=app_id]').removeAttr("disabled");
    $('div#popup_stat_main select[name=market_id]').removeAttr("disabled");
    $('div#popup_stat_main select[name=ref_camp_id]').removeAttr("disabled");

    //Биллинг()
    if ($(select).val() == 2) {
        $('div#popup_stat_main select[name=partner_id]').attr("disabled", true);
    }
    if ($(select).val() == 1) {
        $('div#popup_stat_main select[name=partner_id]').attr("disabled", true);
        $('div#popup_stat_main select[name=service_code_id]').attr("disabled", true);
    }
    if ($(select).val() == 3) {
        $('div#popup_stat_main select[name=partner_id]').attr("disabled", true);
        $('div#popup_stat_main select[name=service_code_id]').attr("disabled", true);
        $('div#popup_stat_main select[name=app_id]').attr("disabled", true);
        $('div#popup_stat_main select[name=market_id]').attr("disabled", true);
        $('div#popup_stat_main select[name=ref_camp_id]').attr("disabled", true);
    }
    stat_change_filters_visibility();
}

function stat_subtype_name_fix(app_id) {
    $('div#popup_stat_for_subtype_selects div select').attr("name", "");
    $('div#popup_stat_for_subtype_selects div:visible select').attr("name", "subtype_id");
}


function stat_get_app_service_code(app_id) {
    var subtype = ($('div#popup_stat_main div#popup_stat_for_subtype_2 select[name=subtype_id]').val().split(',')[0]);
    if ($('div#popup_stat_main select[name=type_id]').val() == "2") {
        if (subtype == 1) {
            $.ajaxSetup({ jsonp: null, jsonpCallback: null });
            $('div#popup_stat_main select[name=service_code_id]').attr("disabled", true);
            $.getJSON("/PopUp/StatisticGetAppServCode", { app_id: $(app_id).val() }, stat_apply_app_service_code);
        }
    }
}

function stat_apply_app_service_code(str) {
        var ids = str["ids"].split('&');
        var names = str["names"].split('&');

        $('div#popup_stat_main select[name=service_code_id]').find('option').remove();
        for (var i = 0; i < ids.length; i++) {
            $('select[name=service_code_id]').append($("<option></option>").attr("value", ids[i]).text(names[i]));
        }
        $('div#popup_stat_main select[name=service_code_id]').removeAttr("disabled");
    }

    function stat_change_filters_visibility() {

        var subtype = ($('div#popup_stat_main div:visible select[name=subtype_id]').val());
        var type = ($('div#popup_stat_main select[name=type_id]').val());
        subtype = subtype.split(',');

        $('div#container_for_info_blocks div').hide();
        $('div#container_for_info_blocks div#info_blok_' + type + '_' + subtype).show();

        if (subtype[1] != "True")
            $('div#popup_stat_main select[name=market_id]').attr("disabled", true);
        else
            $('div#popup_stat_main select[name=market_id]').removeAttr("disabled");
        if (subtype[2] != "True")
            $('div#popup_stat_main select[name=ref_camp_id]').attr("disabled", true);
        else
            $('div#popup_stat_main select[name=ref_camp_id]').removeAttr("disabled");
        if (subtype[3] != "True")
            $('div#popup_stat_main select[name=app_id]').attr("disabled", true);
        else
            $('div#popup_stat_main select[name=app_id]').removeAttr("disabled");
        if (subtype[4] != "True")
            $('div#popup_stat_main select[name=service_code_id]').attr("disabled", true);
        else
            $('div#popup_stat_main select[name=service_code_id]').removeAttr("disabled");
        if (subtype[5] != "True")
            $('div#popup_stat_main select[name=partner_id]').attr("disabled", true);
        else
            $('div#popup_stat_main select[name=partner_id]').removeAttr("disabled");

        //alert($('div#popup_stat_main select[name=partner_id]').attr("disabled"));
}


function stat_change_table_visibility() {
    $('table.info').toggle();

}
  



		
		
		


