var aCount  = 0;
var activeTab  = "";
var anum;
var mynum;
var counter;
var MLAetal;
var etaltext;
var i;
$('#TextBoxesGroup').html('');
// AUTHORBOX FUNCTION
function countAuthors(contrib)  {
    if (contrib.value  == "Au"  && aCount  - editorCount  == 7)  {
        alert( "Only 7 authors allowed");
        contrib.selectedIndex  = 1;
        return false;
    }
}
function recount()  {
    if (aCount  - editorCount  == 7  && style  == "APA")  {
        addauthortext  = apaseventext;
    } else addauthortext  = addauthortextorig;
    $('#addauthor').html("<table cellpadding='0' cellspacing= '0' style=\"clear:both\" width=\"370\"><tr><td>"  + MLAetal  + "</td><td align=\"right\">"  + addauthortext  + "</td></tr></table>");
    if (style  == "MLA"  && aCount  - editorCount  == 1)MLAetal  = etaltext$('#addauthor').html("<table cellpadding='0' cellspacing= '0' style=\"clear:both\" width=\"370\"><tr><td>"  + MLAetal  + "</td><td align=\"right\">"  + addauthortext  + "</td></tr></table>");
}
function authorbox()  {
    document.getElementById('TextBoxesGroup').className  = "show";
    document.getElementById('addauthor').className  = "show";
    aCount  = parseInt(aCount)  + 1;
    document.getElementById('temp').className  = "hide";
    //delete textboxes
     if (type  === "Book")  {
        edtxt  = "/Editor";
    } else  {
        edtxt  = "";
    };
    mynum  = aCount;
    counter  = aCount;
    etaltext  = "<p style=\"font-size:.8em;\"><input onclick=\"sendToBottom();\" tabindex=\"-1\" type=\"checkbox\" onclick=\"mydisable()\" id=\"etal\" name=\"etal\" value=\"etal\" /><a href='#' tabindex=\"-1\" onclick='return false;' class=\"info\" style=\"font-size:1.0em;\">There are more than three authors<span>In MLA, when a citation has more than 3 authors you have the option to list only one author followed by et al, or you can choose to list every author.</span></a> and I wish to list only the first author followed by the phrase et al.</p> ";
    apaseventext  = "<p style=\"font-size:.8em;\"><input type=\"checkbox\" onclick=\"sendToBottom();\" id=\"ellipsis\" name=\"etal\" value=\"etal\" /><a onclick='return false;' tabindex=\"-1\" href='#' class=\"info\">More than seven authors<span>List first six authors and last author</span></a></p> ";
    addauthortextorig  = "<input id=\"addauthor2\" tabindex=\"-1\" class=\"whitebg\" type=\"button\" style='margin:0;padding:0' value=\"Add Another Author"  + edtxt  + "\" onclick=\"authorbox()\" />";
    addauthortext  = addauthortextorig;
    if (aCount  - editorCount  == 1  && style  == "MLA")  {
        MLAetal  = etaltext;
    } else  {
        MLAetal  = "";
    }
    if (aCount  - editorCount  == 7  && style  == "APA")  {
        addauthortext  = apaseventext;
    }
    if (type  == "Book")  {
        bookcontributor  = "<select onchange=\"countAuthors(this);sendToBottom();recount();\" style=\"padding:0px;margin:0px\" name=\"contributor["  + counter  + "]\" id=\"contributor"  + counter  + "\" size=\"1\"><option value=\"Au\">Author</option><option value=\"Ed\">Editor</option></select>";
    } else  {
        if (counter  == 1)  {
            bookcontributor  = "<span id=\"a"  + counter  + "\"><a onclick='return false;' tabindex=\"-1\" href='#' style=\"font-size:0.9em\" class=\"info\">Authors<span>If no known author leave blank, title will replace author name</span></a> &nbsp;"  + counter  + ".</span>";
        } else  {
            bookcontributor  = "<span style=\"font-size:.9em;padding-left:50px\" id=\"a"  + counter  + "\">"  + counter  + ". </span>"
        }
    }
    if (counter  == 1)  {
        firstnamelabel  = "<span style=\"font-size:0.8em;padding-left:2px\"> First name</span><br />";
        if (style  == "APA")firstnamelabel  = "<span style=\"font-size:0.8em;padding-left:2px\">First initial</span><br />";
        milabel  = "<a onclick='return false;' href='#' style=\"font-size:0.8em;padding-left:2px\" tabindex=\"-1\" class=\"info narrow noline\"> MI<span>middle initial</span></a><br />";
        lastnamelabel  = "<span style=\"font-size:0.8em;padding-left:2px\">Last name/Organisation</span><br />";
        brtag  = "<br />";
        divheight  = "40px";
    } else  {
        firstnamelabel  = "";
        milabel  = "";
        lastnamelabel  = "";
        brtag  = "";
        divheight  = "25px";
    }
    if (style  === "APA")  {
        mysize  = "\"1\" maxlength=\"1\"";
        inputwidth  = "15";
        divwidth  = "width:63px";
        marginleft  = "margin-left:15px;";
    } else  {
        mysize  = "\"12\"";
        inputwidth  = "100";
        marginleft  = "";
        divwidth  = "";
    }
    myAuthorsText  = "<div style=\"clear:both;height:"  + divheight  + "\" id=\"TextBoxDiv"  + counter  + "\"><div style=\"width:75px;float:left\">"  + brtag  + "<span>"  + bookcontributor  + "</span></div>";
    /*firstname */	
	myAuthorsText  = myAuthorsText  + "<div style=\"float:left;"  + divwidth  + "\">"  + firstnamelabel  + "<input style=\"width:"  + inputwidth  + "px;padding:0px;margin:0px;"  + marginleft  + "\" type=\"text\" size="  + mysize  + " name=\"fname["  + counter  + "]\" onkeyup=\"sendToBottom()\" id=\"authorFirst"  + counter  + "\" value=\"\" ></div>";
    /*MI*/			
	myAuthorsText  = myAuthorsText  + "<div style=\"float:left;margin-left:2px;\">"  + milabel  + "<input style=\"padding:0px;width:15px;margin:0px\" type=\"text\" maxlength=\"1\" size=\"1\" name=\"MI["  + counter  + "]\" /*firstname */onkeyup=\"sendToBottom()\" id=\"MI"  + counter  + "\" value=\"\" ></div>";
    myAuthorsText  = myAuthorsText  + "<div style=\"width:179px;float:left;margin-left:2px;\">"  + lastnamelabel  + "<input style=\"padding:0px;margin:0px\" size=\"25\" type=\"text\" name=\"lname["  + counter  + "]\" onkeyup=\"sendToBottom()\" id=\"authorLast"  + counter  + "\" value=\"\" ></div><div id=\"removeButton"  + counter  + "\" style=\"width:18px;float:left\">"  + brtag  + "<a href=\"#\" onclick=\"deleteRows("  + counter  + ");return false;\"><img src=\"images/delete.gif\" width=\"18\" title=\"Remove author\" /></a></div></div>";
    // }
     //newTextBoxDiv.appendTo("#TextBoxesGroup"); 
     	 	 $('#TextBoxesGroup').append(myAuthorsText);
    $('#counterauthor').val(aCount);
    //??? what is this
     // }
     $('#addauthor').html("<table cellpadding='0' cellspacing= '0' style=\"clear:both\" width=\"370\"><tr><td>"  + MLAetal  + "</td><td align=\"right\">"  + addauthortext  + "</td></tr></table>");
    if (myeditting  == true)   {
        document.getElementById('authorFirst'  + counter).style.backgroundColor  = '#DFE8F2';
        document.getElementById('MI'  + counter).style.backgroundColor  = '#DFE8F2';
        document.getElementById('authorLast'  + counter).style.backgroundColor  = '#DFE8F2';
    }
    // }
    
}
$(document).ready(function ()  {
    //Default Action
     $(".tab_content").hide();
    //Hide all content
     $("ul.tabs li:first").addClass("active").show();
    //Activate first tab
     $(".tab_content:first").show();
    //Show first tab content
     //On Click Event
     $("ul.tabs li").click(function ()  {
        if (style  !== "")  {
            //if ($(this).attr('id') == "mytab3") {} else {
             $("ul.tabs li").removeClass("active");
            //Remove any "active" class
             $(this).addClass("active");
            //Add "active" class to selected tab
             $(".tab_content").hide();
            //Hide all tab content
             activeTab  = $(this).find("a").attr("href");
            //Find the rel attribute value to identify the active tab + content
             $(activeTab).fadeIn();
            //Fade in the active content
             if (activeTab  == "#tab1")  {
                popupIntextWindow('popup'  + style,false);
            } else  {
                if (activeTab  == "#tab2")  {
                    popupIntextWindow('popup'  + style,true);
                }
            }
            return false;
        } else alert("Please choose a style");
    });
});
var counter  = 1;
//var count=2;
(function($)  {
    $.fn.disableSelection  = function()  {
        return this.each(function()  {
            $(this).attr('unselectable', 'on').css({
                '-moz-user-select':'none', '-webkit-user-select':'none', 'user-select':'none'
            }).each(function()  {
                this.onselectstart  = function()  {
                    return false;
                };
            });
        });
    };
})

function deleteRows(num)  {
    if (aCount  < 2)  {
        firstnamelabel  = "<span style=\"font-size:0.8em\">First name</span><br />";
        milabel  = "<span style=\"font-size:0.8em\">MI</span><br />";
        lastnamelabel  = "<span style=\"font-size:0.8em\">Last name/Organisation</span><br />";
        document.getElementById('authorFirst'  + num).value  = "";
        document.getElementById('authorLast'  + num).value  = "";
        document.getElementById('MI'  + num).value  = "";
    } else  {
        var d  = document.getElementById('TextBoxDiv'  + num);
        d.parentNode.removeChild(d);
        if (num  !== aCount)  {
            for (i  = (num  + 1);
            i  <= aCount;
            i++) {
                $('#TextBoxDiv'  + i).attr("id", "TextBoxDiv"  + (i  - 1));
                if (type  !== "Book")  {
                    document.getElementById('a'  + i).innerHTML  = (i  - 1)  + ".";
                    $('#a'  + i).attr("id", "a"  + (i  - 1))
                }
                $('#authorFirst'  + i).attr("id", "authorFirst"  + (i  - 1));
                $('#authorLast'  + i).attr("id", "authorLast"  + (i  - 1));
                $('#MI'  + i).attr("id", "MI"  + (i  - 1));
                document.getElementById('removeButton'  + i).id  = "removeButton"  + (i  - 1);
                document.getElementById('removeButton'  + (i  - 1)).innerHTML  = document.getElementById('removeButton'  + (i  - 1)).innerHTML.replace(i,i  - 1);
                if (type  === "Book") $('#contributor'  + i).attr("id", "contributor"  + (i  - 1))
            }
            $('#a1').attr("id", "c1");
        }
        aCount--;
    }
    sendToBottom();
    if (aCount  === 1  && style  === "MLA")  {
        $('#addauthor').html("<table style=\"clear:both\" width=\"370\"><tr><td>"  + etaltext  + "</td><td align=\"right\">"  + addauthortext  + "</td></tr></table>");
    }
    if (aCount  === 6  && style  === "APA")  {
        $('#addauthor').html("<p style=\"font-size:.8em;text-align:right;padding-right:20px\">"  + addauthortextorig  + "</p>")
    }
}

