//calculate weeknumer from date
function getWeekNumber(myDate) {
    myDate = new Date(myDate.getTime());
    myDate.setDate(myDate.getDate() + 4 - (myDate.getDay() || 7));
    var myTime = myDate.getTime();
    myDate.setMonth(0);
    myDate.setDate(1);
    var myWeek = Math.floor(Math.round((myTime - myDate) / 86400000) / 7) + 1;
    if(myWeek < 10) return "0" + myWeek;
    return myWeek;
}
//onchange function for datepicker
function updateSystemDate() {
    var newSystemTimeValue;
    //get the date from the datepicker
    var datepickerValue = $("#time_form #time_start_datepicker").datepicker("getDate");
    //get the time unit (dagen/weken/maanden)
    var unitType = $("#time_form .time_unit input:checked").val();
    //format time to system reqs
    if(unitType == "maanden") {
        newSystemTimeValue = $.datepicker.formatDate("yymm",datepickerValue);
    } else if(unitType == "weken") {
        newSystemTimeValue = $.datepicker.formatDate("yy",datepickerValue) + "w" + getWeekNumber(datepickerValue);
    } else {
        newSystemTimeValue = $.datepicker.formatDate("yymmdd",datepickerValue);
    }
    $("#time_form #time_start").val(newSystemTimeValue);
    $("#time_form").submit();
}



$('#content').ready(function() {
    $('#login').flyOut();
    /*/ some stuff we'd best do first /*/

    //hide big ugly tables
    $('#content table.replace').css("display","none");
    //slideshow home
    $('#slides').tabbedCarousel();
    //tooltips in forms
    $('#content').tooltips();

    /*/ analysis form general /*/

    //make tabs
    $("#issueGroups.tab").tabs();
    $("#issueGroups.tab2").tabs({selected:1});
    $("#sourceGroups.tab").tabs();
    $("#sourceGroups.tab2").tabs({selected:1});
    $("#issues.tab").tabs();
    $("#issues.tab2").tabs({selected:1});

    //make accordion
    $("#content .jq_accordion.option").accordion({ collapsible: true, autoHeight: false, active: 0 });
    $("#content .jq_accordion.option1").accordion({ collapsible: true, autoHeight: false, active: 1 });
    $("#content .jq_accordion.option2").accordion({ collapsible: true, autoHeight: false, active: 2 });
    $("#content .jq_accordion.option3").accordion({ collapsible: true, autoHeight: false, active: 3 });

    //make nice buttons from radiobuttons
    $("#content .jq_radio").buttonset();

    /*/ build date selection form /*/

    //remove the submit button
    $("#time_form button").css("display","none");
    //hide system date start input
    $("#time_start").css("visibility","hidden");
    $("#time_start").css("float","right");
    $("#time_start").css("width","10px");
    //instantiate datepicker
    $("#time_form #time_start_datepicker").datepicker({ showWeek: true, firstDay: 1, dateFormat: "yymmdd" });
    //add changelistener to datepicker
    $("#time_form #time_start_datepicker").change( updateSystemDate );
    //add wrapper around datepicker for styling scope
    $("#ui-datepicker-div").wrap('<div class="jq_style" />');
    //add changelistener to unit buttons
    $("#time_form .time_unit input").change( updateSystemDate );
    //add changelistener to interval buttons
    $("#time_form .time_interval input").change(function() {
        $("#time_form").submit();
    });
    
    /*/ build display selection form /*/

    //remove the submit button
    $("#display_form button").css("display","none");
    //add changelistener to display buttons
    $("#display_form .display_type input").change(function() {
        $("#display_form").submit();
    });

    /*/ build issue selection forms /*/

    //remove the submit buttons
    $("#my_issues_form button").css("display","none");
    $("#default_issues_form button").css("display","none");
    //add changelistener to issue buttons
    $("#my_issues_form input").change(function() {
        $("#my_issues_form").submit();
    });
    $("#default_issues_form input").change(function() {
        $("#default_issues_form").submit();
    });

    /*/ build issuegroup selection forms /*/

    //remove the submit buttons
    $("#my_issue_groups_form button").css("display","none");
    $("#default_issue_groups_form button").css("display","none");
    //add changelistener to issuegroups buttons
    $("#my_issue_groups_form input").change(function() {
        $("#my_issue_groups_form").submit();
    });
    $("#default_issue_groups_form input").change(function() {
        $("#default_issue_groups_form").submit();
    });

    /*/ build sourcegroup selection forms /*/

    //remove the submit buttons
    $("#my_source_groups_form button").css("display","none");
    $("#default_source_groups_form button").css("display","none");
    //add changelistener to sourcegroups buttons
    $("#my_source_groups_form input").change(function() {
        $("#my_source_groups_form").submit();
    });
    $("#default_source_groups_form input").change(function() {
        $("#default_source_groups_form").submit();
    });

    /*/ generate graphs /*/
    var colorArray = ['#f84048','#3399ee','#b0d022','#e82890','#28d8d0','#f8d038','#cc33ee','#50dd20','#f08833','#ca343b','#2a7ec1','#91ab17','#bd2175','#1faeac','#c9ad2c','#a828bf','#3db326','#c6832b','#f9747b','#6cb6f1','#c7de5b','#ed64ae','#62e0df','#f8df6e','#db6aef','#7ce568','#f6ba6d'];
    //in main column
    $('#content table.pie.500').visualize({type: 'pie', height: '300px', width: '500px', colors: colorArray });
    $('#content table.bar.500').visualize({type: 'bar', height: '300px', width: '500px', colors: colorArray });
    $('#content table.area.500').visualize({type: 'area', height: '300px', width: '500px', colors: colorArray });
    $('#content table.line.500').visualize({type: 'line', height: '300px', width: '500px', colors: colorArray });

    //in right column
    $('#content table.pie.240').visualize({type: 'pie', height: '160px', width: '240px', colors: colorArray });
    $('#content table.bar.240').visualize({type: 'bar', height: '160px', width: '240px', colors: colorArray });
    $('#content table.area.240').visualize({type: 'area', height: '160px', width: '240px', colors: colorArray });
    $('#content table.line.240').visualize({type: 'line', height: '160px', width: '240px', colors: colorArray });

    //fix height
    $(".visualize").css("height","");

    //add close button to messages
    $('.closable').closeMessages();

    //add logo, or don't
    //^ AssetProxy(makeso/js/personal-only-js);
    //^ AssetProxy(makeso/js/commercial-only-js);
    //^ AssetProxy(makeso/js/professional-only-js);

});


