/* GLOBALS ********************************************************************/

var pulldown_visible = false;
var sliding = false;

/* ONLOAD *********************************************************************/

// cross-browser onLoad event handler
function addLoadEvent(func) {
    var oldonload = window.onload;

    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

// onLoad
addLoadEvent(pageInit);

function pageInit()
{
	// Check if script.aculo.us Sortables should be initated
	if( (typeof(window['sortable_list']) != "undefined") && (typeof(window['sortable_list'] != "object")) )
	{
		Sortable.create(sortable_list);
	}
   
    if(document.getElementById('pulldown-wrapper').style.display != "none")
    {
        pulldown_visible = true;
		if(document.getElementById('pulldown-switch') != undefined) // if page has pulldown show/hide link
		{
			document.getElementById('pulldown-switch').innerHTML = "Hide Pull-Down";
        	document.getElementById('pulldown-switch-icon').src = document.location.protocol + "//www.mymasterclass.com/images/icon_collapse.gif"; // absolute URL b/c 404 misbehaves o/w if dir is specified
        	// document.location.protocol prevents the "secure and unsercure items" message
        }
    }
}

/* PULLDOWN *******************************************************************/

function Activate_Pulldown()
{
    if(!sliding)
    {
        if(!pulldown_visible)
        {
            sliding = true;
            setTimeout('sliding = false;', 1000);
            setTimeout('pulldown_visible = true;',1000);
            new Effect.SlideDown(document.getElementById('pulldown-wrapper', {duration: 1.0}));
            document.getElementById('pulldown-switch').innerHTML = "Hide Pull-Down";
            document.getElementById('pulldown-switch-icon').src = document.location.protocol + "//www.mymasterclass.com/images/icon_collapse.gif"; // absolute URL b/c 404 misbehaves o/w if a dir is specified
			// document.location.protocol prevents the "secure and unsercure items" message  
        }
        else
        {
            Hide_Pulldown();
        }
    }
}

function Hide_Pulldown()
{
    if(!sliding)
    {
        if(pulldown_visible)
        {
            sliding = true;
            setTimeout('sliding = false;', 1000);
            setTimeout('pulldown_visible = false;',1000);
            new Effect.SlideUp(document.getElementById('pulldown-wrapper', {duration: 1.0}));
            document.getElementById('pulldown-switch').innerHTML = "Show Pull-Down";
            document.getElementById('pulldown-switch-icon').src = document.location.protocol + "//www.mymasterclass.com/images/icon_expand.gif"; // absolute URL b/c 404 misbehaves o/w if a dir is specified
            // document.location.protocol prevents the "secure and unsercure items" message
        }
    }
}

/* TEXT BOX MAX LENGTH ********************************************************/

function Check_Length(name, max)
{
	var textbox = document.getElementById(name);
	if(textbox.value.length > max)
	if(textbox.value.length > max)
		textbox.value = textbox.value.substring(0, max);
}

/* ADMIN EMAIL ****************************************************************/
/* Generate custom confirmation message for admin_email page */

function Confirm_Send_Email()
{
	var message = '';
	switch(document.getElementById('form-ae-recipients').options[document.getElementById('form-ae-recipients').selectedIndex].value)
	{
		case '0': // all users
			message = 'This email will be sent to ALL USERS of the MyMasterClass system.  Are you sure you want to continue?';
			break;
		case '1': // all students
			message = 'This email will be sent to ALL STUDENTS of the MyMasterClass system.  Are you sure you want to continue?';
			break;
		case '2': // all instructors
			message = 'This email will be sent to ALL INSTRUCTORS of the MyMasterClass system.  Are you sure you want to continue?';
			break;
		case '3': // all admin
			message = 'This email will be sent to ALL ADMINISTRATORS of the MyMasterClass system.  Are you sure you want to continue?';
			break;
		case '4': // by class
			classname = document.getElementById('form-ae-classes').options[document.getElementById('form-ae-classes').selectedIndex].text
			message = 'This email will be sent to ALL STUDENTS enrolled in the class \'' + classname + '\'.  Are you sure you want to continue?';
			break;
		default: // by email address, other
			message = 'Are you sure you wish to send this email to the specified recipients?';
			break;
	}
	
	return confirm(message);
}

/* QUERY STRING ***************************************************************/

// Client-side access to querystring name=value pairs
// Version 1.2.3
// June 22, 2005
// Adam Vandenberg (http://adamv.com/dev/javascript/querystring)

// qs = querystring to parse (no ?) (optional)
function Querystring(qs)
{
	this.params = new Object()
	this.get = Querystring_get
	
	if (qs == null)
		qs = location.search.substring(1,location.search.length)

	if (qs.length == 0)
		return

	// Turn <plus> back to <space>
	// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
	// split out each name=value pair
	for (var i=0;i<args.length;i++)
	{
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_)
{
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value = this.params[key]
	if (value == null)
		value = default_;
	
	return value
}

/* SORTABLES ******************************************************************/
/* Drag and Drop for Admin Index page */

function Save_List()
{
	var qs = new Querystring();
	
	var sort_type = qs.get("sort_type");
	if(!sort_type || sort_type == "instructors")
	{
		listArray = Serialize_List('instructor-index', 'instructor');
		var category = document.getElementById('form-ai-categories').value;
		window.location = "admin_index.php?sort_type=instructors&category=" + category + "&order=" + listArray;
	}
	else
	{		
		listArray = Serialize_List('class-index', 'class');
		var instructor = document.getElementById('form-ai-instructors').value;
		window.location = "admin_index.php?sort_type=classes&instructor=" + instructor + "&order=" + listArray;
	}
}

function Serialize_List(sortable_list, list_name)
{
	list_order = Sortable.serialize(sortable_list);
	
	switch(list_name)
	{
		case 'instructor':
			var order = Sortable.serialize(sortable_list, {tag:'li', name:'instructor'});
			order = order.replace(/instructor\[\]=/g, '');
			break;
		case 'class':
			var order = Sortable.serialize(sortable_list, {tag:'li', name:'class'});
			order = order.replace(/class\[\]=/g, '');
			break;
	}
	
	list_array = order.split('&');
	
	return list_array;
}

/* ADMIN_INDEX ****************************************************************/

function Show_Instructor_List()
{
	document.getElementById('instructor-list').style.display = "block";
	document.getElementById('instructor-instructions').style.display = "block";
}

/* ADMIN_EMAIL ****************************************************************/

function Toggle_Recipient_Details()
{
	// Send to students in a specific class
	if(document.getElementById('form-ae-recipients').value == '4')
		document.getElementById('class-list').style.display = "block";
	else
		document.getElementById('class-list').style.display = "none";
	
	// Send to a specific address
	if(document.getElementById('form-ae-recipients').value == '5')
		document.getElementById('email-addresses').style.display = "block";
	else
		document.getElementById('email-addresses').style.display = "none";
}

function Toggle_Reply_To_Details()
{
	// Specify email address
	if(document.getElementById('form-ae-reply').value == '3')
		document.getElementById('reply-address').style.display = "inline";
	else
		document.getElementById('reply-address').style.display = "none";
}

/* ADMIN_CLASSES / ADMIN_USERS ************************************************/

function Toggle_Booklet_Type()
{
	if(document.getElementById('booklet-published').style.display == "block")
	{
		document.getElementById('booklet-published').style.display = "none";
		document.getElementById('booklet-video').style.display = "block";
	}
	else
	{
		document.getElementById('booklet-published').style.display = "block";
		document.getElementById('booklet-video').style.display = "none";
	}
}

function Show_Upload_Loading(page)
{
	if(page == 'admin_classes')
	{
		document.getElementById('preview-upload').style.display = "none";
		document.getElementById('booklet-upload').style.display = "none";
		document.getElementById('loading1').style.display = "block";
		document.getElementById('loading2').style.display = "block";
	}
	else if(page == 'admin_users')
	{
		document.getElementById('instructor-upload').style.display = "none";
		document.getElementById('instructor-bio-upload').style.display = "none";
		document.getElementById('loading').style.display = "block";
	}
}

function Toggle_Instructor_Uploads()
{
	if(document.getElementById('form-au-acc').selectedIndex == 2)
		document.getElementById('instructor-upload').style.display = "block";
	else
		document.getElementById('instructor-upload').style.display = "none";
}


