﻿$(function () {
    $("#LoginDrop").click(function () {
        if ($("#LoginDropdown").css("marginTop") == "169px") {
            $("#LoginDropdown").animate({marginTop: "0px"}, 500);
        } else {
            $("#LoginDropdown").animate({marginTop: "169px"}, 500);
        }
    });

    $(".switch").click(function(ev) {
        ev.preventDefault();
        $.get(
            $(this).attr("href"),
            function (data) {
                $("#schedule").empty();
                $(innerShiv(data), false).appendTo('#schedule');
                percent = parseFloat($("#progressbar").attr("class"));
                $("#progressbar").progressbar({value: percent});
            }
        );
    });

});



function highlightLink(pagename) {
    $(function () {
        $("#"+pagename+"link").css({
            'background-color': '#cee2f7',
            'color': '#317fd3',
            'border-radius': '5px',
            '-moz-border-radius': '5px',
            '-webkit-border-radius': '5px'
        });
    });
}

function hideLanguage(cultureString) {
    $(function () {
        if (cultureString == "en-US") {
            $("#en").hide();
        }else{
            $("#es").hide();
        };
    });
}

function initReadingContent(pagename) {
    highlightLink(pagename);
    $(function () {
        $(".msg").hide();
        $("#IntroMsg").show();
        $("h1").click(function () {
            $(this).parent().children(".msg").slideToggle();
            if ($(this).attr("class") == "last") {
                if ($(this).css("border-bottom-right-radius") == "0px 0px") {
                    $(this).delay(400).animate({
                        'border-bottom-right-radius': '5px',
                        'border-bottom-left-radius': '5px',
                        '-moz-border-radius-bottomright': '5px',
                        '-moz-border-radius-bottomleft': '5px',
                        '-webkit-border-bottom-right-radius': '5px',
                        '-webkit-border-bottom-left-radius': '5px'
                    }, 0);
                } else {
                    $(this).css({
                        'border-radius': '0px',
                        '-moz-border-radius': '0px',
                        '-webkit-border-radius': '0px',
                    });
                }
            }
        });

    });
}

function initModifyBox(action, title, cancel, create, errorScheduleName, errorEndBook, errorStartInvalid, errorEndInvalid, errorStartBeforeToday, errorEndBeforeToday, errorEndBeforeStart, errorMax10, errorNoDays, culture){
    $("#DialogBox").load(
        action,
        function(){
            var dialog_buttons = {};
            dialog_buttons[title] = function(){ 
                                        var errormsg = modifysched(create, errorScheduleName, errorEndBook, errorStartInvalid, errorEndInvalid, errorStartBeforeToday, errorEndBeforeToday, errorEndBeforeStart, errorMax10, errorNoDays);
                                        if (errormsg == "") {
                                            if(create){
                                                $("#CreateForm").submit();
                                            }else{
                                                $("#EditForm").submit();
                                            }
                                            $(this).dialog("destroy");
                                        }
                                        $(".validateTips").html(errormsg).addClass("ui-state-highlight");
                                    }
            dialog_buttons[cancel] = function(){$(this).dialog("destroy");}
            $("#DialogBox").attr("title", title);
            $("#DialogBox").dialog({
                width: 500,
                modal: true,
                buttons: dialog_buttons,
                close: function(){$(this).dialog("destroy");}
            });
	        $("#checkboxes").buttonset();
            if(create){
	            $("#EndBookID option[value='66']").attr("selected", "selected");
                $("#StartDate").datepicker({
	                changeMonth: true,
	                changeYear: true,
	                minDate: 0,
	                maxDate: "+10Y",
                    //altFormat: _lang == 'es' ? 'dd/mm/yy' : 'mm/dd/yy', 
                    dateFormat: _lang == 'es' ? 'dd/mm/yy' : 'mm/dd/yy'//, 
                    //altField: '#StartDate'
	            });
	            $("#EndDate").datepicker({
	                changeMonth: true,
	                changeYear: true,
	                minDate: 0,
	                maxDate: "+10Y",
                    //altFormat: _lang == 'es' ? 'dd/mm/yy' : 'mm/dd/yy', 
                    dateFormat: _lang == 'es' ? 'dd/mm/yy' : 'mm/dd/yy'//, 
                    //altField: '#EndDate'
	            });
            }else{
                var sid="#StartBookID option[value='"+$("#StartID").val()+"']";
                var eid="#EndBookID option[value='"+$("#EndID").val()+"']";
                var sdate = $("#StartDate").val().split('/');
            	$("#EndDate").datepicker({
	                changeMonth: true,
	                changeYear: true,
                    minDate: _lang == "es" ? new Date(sdate[2], sdate[1], sdate[0] * 1 + 1) : new Date(sdate[2], sdate[0], sdate[1] * 1 + 1),
                    maxDate: _lang == "es" ? new Date(sdate[2] * 1 + 10, sdate[1], sdate[0]) : new Date(sdate[2] * 1 + 10, sdate[0], sdate[1]),
                    //altFormat: _lang == 'es' ? 'dd/mm/yy' : 'mm/dd/yy', 
                    dateFormat: _lang == 'es' ? 'dd/mm/yy' : 'mm/dd/yy'//, 
                    //altField: '#EndDate'
    	        });
                $(sid).attr("selected", "selected");
                $(eid).attr("selected", "selected");
            }
        }  
    );
}

function initDeleteBox(action, title, cancel){
    $("#DialogBox").load(
        action,
        function(){
            var dialog_buttons = {};
            dialog_buttons[title] = function(){$("#DeleteForm").submit();}
            dialog_buttons[cancel] = function(){$(this).dialog("destroy");}
            $("#DialogBox").attr("title", title);
            $("#DialogBox").dialog({
                width: 500,
                modal: true,
                buttons: dialog_buttons,
                close: function(){$(this).dialog("destroy");}
            });
            $('.ui-dialog :button').blur();
        }
    );
}

function initRegister(errorUsername, errorUsernameInvalid, errorEmail, errorPassword, errorConfirm, errorFirst, errorLast, errorZip){
    $(function () {
        $("#specify").hide();
        $(".additional").hide();
        $("#registerRef").change(function () {
            if ($(this).val() == "other")
                $("#specify").show();
            else
                $("#specify").hide();
        });

        register(errorUsername, errorUsernameInvalid, errorEmail, errorPassword, errorConfirm, errorFirst, errorLast, errorZip);
    });
}
