
/* Add new class */
var classCount = 0;

jQuery.fn.checkQuarterVal = function() {

        var i = 0;
        var j = 0;
        var found = 0;

		var course = $(this).text().replace(" ", "");
		var quarter = $(this).parent().attr('id');
        quarter = quarter.charAt(2); // Get the quarter

        /* Find the course! */
        while(i < courseArray.length)
        {
            if(course.toLowerCase() == (courseArray[i].name.replace(" ", "")).toLowerCase())
            {
                found++;
                break;
            }
            else {
                i++;
            }
        }

		// Convert to text
		switch(quarter)
		{
			case '1': // Fall
				quarter="f";
				break;
			case '2': // Winter
				quarter="w";
				break;
			case '3': // Spring
				quarter="s";
				break;
			case '4': // Summer
				quarter="r";
				break;
		}

        /* If found check quarter! */
        if(found) {
            /* Search for the quarter char */
            while(j < courseArray[i].offered.length) {
                if(courseArray[i].offered.charAt(j) == quarter.charAt(0)) {
                    return true;
                } else {
                    j++;
                }
            }
			return false;
        }
    return true;
}


jQuery.fn.addNewClass = function() {
    var newName = $("#newCourseName").val().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    var mystring = '<div id="EX' + classCount.toString() + '" class="sortableitem inplace" style="background-color:#ffffff">' + newName + '</div>';
    $("#extras").append(mystring);
    classCount++;


                $('#extras').Sortable(
                {
                        accept: 'sortableitem',
                        helperclass: 'sorthelper',
                        activeclass :   'sortableactive',
                        hoverclass :    'sortablehover',
                        opacity:                0.8,
                        fx:                             200,
                        opacity:                0.4,
                        revert:                 true,
                        onChange : function(ser)
                        {
                        },
                        onStart : function()
                        {
                                $.iAutoscroller.start(this, document.getElementsByTagName('body'));
                        },
                        onStop : function()
                        {
                                $.iAutoscroller.stop();
								$('#trashbin').html("<center>drag here to delete</center>");

								// Quarter Validation
								if($(this).checkQuarterVal() == false) {
									//Not allowed
									$(this).addClass("invalidQuarter");
								} else {
									$(this).removeClass("invalidQuarter");
								}
                        }
                });

                  $('.inplace').editInPlace({
                        params: "ajax=yes",
                        bg_over: "#cff",
                        saving_image: "img/indicator.gif",
                        callback: function(original_element, html) {
                                //Insert AJAX here
                                return(html);
                                }
                });
};


/* htmlToText */
jQuery.fn.htmlToText = function(input, ID) {
};

/* testToHtml */
jQuery.fn.textToHtml = function(input, ID) {
};

function serialize(s)
{
	serial = $.SortSerialize(s);
	alert(serial.hash);
};

function makeText(input, divobject)
{
	('divobject').text(input);
};

function hideToggle() {
		if($(".summer").css("visibility")=="hidden") {
			$(".summer").css("visibility", "visible");
			/*$(".summer").removeClass("quarterWrapper");*/
			$(".yearWrapper").css("width", "465px");
			$(".yearWrapper2").css("width", "465px");
			$("#tbl").css("width", "465px");
		}
		else {
			$(".summer").css("visibility", "hidden");
			/*$(".summer").addClass("quarterWrapper");*/
			$(".yearWrapper").css("width", "360px");
			$(".yearWrapper2").css("width", "360px");
			$("#tbl").css("width", "360px");
		}	
};



/* OnLoad */
$(document).ready(function() {

	var classOrder =$.cookie('dragndropr');
	var pHelp = 0; // 1 = firefox; 2 = ie;

	$("hideClick").click(function () {
     		$("#Fr4").toggleClass("quarterWrapper");
    	});
	
		
	$('div.quarterWrapper').Sortable(
		{
			accept: 'sortableitem',
			helperclass: 'sorthelper',
			activeclass : 	'sortableactive',
			hoverclass : 	'sortablehover',
			opacity:		0.8,
			fx:				200,
			opacity:		0.4,
			revert:			true,
			onChange : function(ser)
			{
			},
			onStart : function()
			{
				$.iAutoscroller.start(this, document.getElementsByTagName('body'));
			},
			onStop : function()
			{
				$.iAutoscroller.stop();
				$('#trashbin').html("<center>drag here to delete</center>");

				// Quarter Validation
				if($(this).checkQuarterVal() == false) {
					//Not allowed
					$(this).css("background-color", "#ffcccc");
				} else {
					$(this).css("background-color", "#ffffff");
				}
			}
		});
	
		$('#extras').Sortable(
		{
			accept: 'sortableitem',
			helperclass: 'sorthelper',
			activeclass : 	'sortableactive',
			hoverclass : 	'sortablehover',
			opacity:		0.8,
			fx:				200,
			opacity:		0.4,
			revert:			true,
			onChange : function(ser)
			{
			},
			onStart : function()
			{
				$.iAutoscroller.start(this, document.getElementsByTagName('body'));
			},
			onStop : function()
			{
				$.iAutoscroller.stop();
				$('#trashbin').html("<center>drag here to delete</center>");

                // Quarter Validation
                if($(this).checkQuarterVal() == false) {
                    //Not allowed
                    $(this).addClass("invalidQuarter");
                } else {
                    $(this).removeClass("invalidQuarter");
                }

			}
		});

		  $('.inplace').editInPlace({
			params: "ajax=yes",
			bg_over: "#cff",
			saving_image: "img/indicator.gif",
			callback: function(original_element, html) {
				//Insert AJAX here
				return(html);
				}
		});

		$('#trashbin').Sortable (
		{
            accept: 'sortableitem',
            helperclass: 'sorthelper',
            activeclass :   'sortableactive',
            hoverclass :    'sortablehover',
            opacity:        0.8,
            fx:             200,
            opacity:        0.4,
            revert:         true,
            onChange : function(ser)
            {
            },
            onStart : function()
            {
                $.iAutoscroller.start(this, document.getElementsByTagName('body'));
            },
            onStop : function()
            {
                $.iAutoscroller.stop();
            }
		});

		$('#ffHelp').click(function() {
			var ffhelpinfo = "firefox help:<br>printing backgrounds<br><br>1) file -><br>2) page setup -><br>3) check print background (colors & images)";
			if(pHelp == 0) { // show vis + add text
				$('#printDisplay').css("visibility", "visible");
				$('#printDisplay').html(ffhelpinfo);
				pHelp = 1;
			} else if( pHelp == 1) { // hide vis
				$('#printDisplay').css("visibility", "hidden");
				pHelp = 0;
			} else { // change text
				$('#printDisplay').html(ffhelpinfo);
				pHelp = 1;
			}
		});

		$('#ieHelp').click(function() {
			var iehelpinfo = "internet explorer help:<br>printing backgrounds<br><br>1) tools -><br>2) internet options -><br>3) click the advanced tab<br>4) scroll to printer options -><br>5) check print background colors and images";
			if(pHelp == 0) { // show vis + add text
				$('#printDisplay').css("visibility", "visible");
				$('#printDisplay').html(iehelpinfo);
				pHelp = 2;
			} else if(pHelp == 2) { // hide vis
				$('#printDisplay').css("visibility", "hidden");
				pHelp = 0;
				
			} else { // change text
				$('#printDisplay').html(iehelpinfo);
				pHelp = 2;
			}
		});

		$('#newCourseName').keypress(function(e){
			if(e.which == 13) {
				$(this).addNewClass();
			}
		});

		$('#addClassArea').click(function () {
			$(this).addNewClass();
		});

});

