(function($) {

	jQuery.rtn = {	};

	jQuery.rtn.dialog = {
		/* Buttons */
		BUTTON_CANCEL: {
			"Cancel": function() { $(this).dialog('destroy'); }
		}, 
		BUTTON_CONTINUE_TO_ARTIST_ADMIN: {
			"" : function() { $(this).dialog('destroy'); }
		}, 
		POST_ANNOUNCEMENT: {
			"Post Announcement": function() { 
				
				var subject = $("#txt1").val();
				var message = $("#msg").val();
				var class_id = $("#class_id").val();
				var user_id = $("#user_id").val();
				var errors = [];
				
				if (message == 'Type away...'){
					errors.push('No message enetered');
				}
				if (message.length > 200){
					errors.push('Message must be less than 200 characters long.');
				}
				if (subject.length > 250){
					errors.push('Subject must be less than 250 characters long.');
				}
				if(errors.length > 0){
					$(this).dialog('destroy');
					$.rtn.dialog.error($.rtn.dialog.BOX_ANNOUNCEMENT_FAILED, errors);
				} else {
					$(this).dialog('destroy');
					$.getJSON('../includes/js/json_announcement.php',{
					u: user_id, c: class_id, s: subject, m: message},
					 function(json){
						$.rtn.dialog.alert($.rtn.dialog.BOX_ANNOUNCEMENT_SUCCESS);
					});
				}
				
				//document.location.href = "../EDU/teacher_course_home.php";
				}
		}, 
		BUTTON_CONTINUE: {
			"Continue": function() { $(this).dialog('destroy'); }
		},
		BUTTON_CLOSE: {
			"Close": function() { $(this).dialog('destroy'); }
		},
		BUTTON_LOGIN: {
			"Continue": function() { document.location.href = "/login.php"; }
		},
        BUTTON_SIGNUP: {
			"Sign Up": function() { document.location.href = "/signup.php"; }
		},
		BUTTON_TRYAGAIN: {
			"Try Again": function() { $(this).dialog('destroy'); }
		},
        BUTTON_CONTINUE_CLICK: {
			"Continue Anyway": function() {
				$('a').unbind('click.protect');
				$.rtn.dialog.followLink($.rtn.dialog.protectEvent);
				$(this).dialog('destroy');
			}
		},
		BUTTON_CONTINUE_CLICK2: {
			"Continue": function() {
				$('a').unbind('click.protect');
				$.rtn.dialog.followLink($.rtn.dialog.protectEvent);
			}
		},
		BUTTON_CONTINUE_CUSTOM: {
			"Continue": function() {
				$('#frm-vehicle-continue').unbind('submit');
				$('#frm-vehicle-continue').submit();
			}
		},
		BUTTON_NEW_WINDOW: {
			"Download File": function() {
				protectEvent = jQuery.rtn.dialog.protectEvent;
				$('a').unbind('click.protect');
				$(protectEvent.target).click();
				window.open($.rtn.dialog.getLink(protectEvent));
				$(this).dialog('destroy');
			}
		},
		BUTTON_ACCEPT_COPYRIGHT_WARNING: {
			"Accept": function() {
				$(this).dialog('destroy');
			}
		},		
		// BUTTON_CANCEL_WARNING: {
		// 	"Cancel": function() { document.location.href = "/login.php"; }
		// },

		/* Templates */
		BOX_EMAIL_IN_USE: {
			title: "Oops!",
			text: "That email address is already in use.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_EMAIL_NOT_VALID: {
			title: "Oops!",
			text: "That email address is invalid.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_POST_ANNOUNCEMENT: {
			title: "<font style='font-size: 16.21px; color: #ffffff; font-family: Trebuchet MS;'><b>Post Announcement</b></font> ",
			text: "<font style='font-size: 12px; color: #666666; font-family: arial;'>Post an announcement to your entire class right here: </font>",
			input_Subject: " <input type='text' style='font-size: 12px; color: #666666; font-family: arial;' size = '80' id='txt1' onfocus=\"if ($('#txt1').val()=='Subject'){$('#txt1').val('');}\" onblur=\"if($('#txt1').val()==''){$('#txt1').val('Subject');}\" value ='Subject' MAXLENGTH = '100' />",
			input_Text: " <textarea class=\"text-area\" style='font-size: 12px; color: #666666; font-family: arial;' rows=\"2\" cols=\"78\" id='msg' onfocus=\"if ($('#msg').val()=='Type away...'){$('#msg').val('');}\" onblur=\"if($('#msg').val()==''){$('#msg').val('Type away...').trigger('keyup');}\">Type away...</textarea> ", 
			arrButtons: ['BUTTON_CANCEL', 'POST_ANNOUNCEMENT']
		},
		BOX_ANNOUNCEMENT_FAILED: {
			title: "Oops!",
			text: "Announcement was NOT posted.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_ANNOUNCEMENT_SUCCESS: {
			title: "Yay!",
			text: "Announcement was posted.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_FIELD_EMPTY: {
			title: "Hold up!",
			text: "You have to create a team name before you continue.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_INVALID_ACCOUNT: {
			title: "Yikes!",
			text: "We are unable to find an account associated with that email address.",
			arrButtons: ['BUTTON_TRYAGAIN', 'BUTTON_SIGNUP']
		},
		BOX_INVALID_ADDRESS: {
			title: "Oops!",
			text: "The location you entered is not valid.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_INVALID_LOGIN: {
			title: "Yikes!",
			text: "We are unable log you in with that email/password combination.",
			arrButtons: ['BUTTON_TRYAGAIN', 'BUTTON_SIGNUP']
		},
		BOX_CONFIRM_EMAIL: {
			title: "Yay!",
			text: "We've sent you a confirmation email! Please confirm your account by clicking on the confirmation link.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_FAVORITE_LOGIN: {
			title: "Hold up!",
			text: "In order to favorite something, you must be signed in!",
			arrButtons: ['BUTTON_LOGIN']
		},
		BOX_SUBSCRIBE_LOGIN: {
			title: "Hold up!",
			text: "In order to update your subscription, you must be signed in!",
			arrButtons: ['BUTTON_LOGIN']
		},
		BOX_SUBSCRIBE_UPDATED: {
			title: "Sweet!",
			text: "Your subscription has been updated!",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_NAVIGATE_AWAY: {
			title: "Hold up!",
			text: "Are you sure you want to navigate away from this page?",
			arrButtons: ['BUTTON_CANCEL','BUTTON_CONTINUE_CLICK']
		},
		BOX_FILE_SUCCESS: {
			title: "Yay!",
			text: "Your file was successfully uploaded.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_FILE_SIZE_FAILED: {
			title: "Whoops!",
			text: "Upload File Size Can Not Be Greater Than 1MB.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_TWITTER_SUCCESS: {
			title: "Yay!",
			text: "Your twitter feed was updated.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		ARTIST_SONG_LIMIT: {
			title: "Whoops!",
			text: "You may only upload six songs at a time.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_FILE_FAILED: {
			title: "Whoops!",
			text: "An error occurred while uploading your file. Please try again.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_MISSING_FIELD: {
			title: "Whoops!",
			text: "You've missed a required field.",
			arrButtons: ['BUTTON_TRYAGAIN']
		},
		BOX_ARTIST_PROFILE_BIO_CHARACTER_LENGTH: {
			title: "Whoops!",
			text: "Your bio may only contain at most 600 characters, including spaces.",
			arrButtons: ['BUTTON_TRYAGAIN']
		},
		BOX_BIO_DESCRIPTION_CHARACTER_LENGTH: {
			title: "Whoops!",
			text: "The about description may only be at most 255 characters, including spaces.",
			arrButtons: ['BUTTON_TRYAGAIN']
		},
		BOX_ALREADY_EMAILED: {
			title: "Hang on!",
			text: "We've already sent an email to that person!",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_CONFIRM_DELETE: {
			title: "Not so fast!",
			text: "Are you sure you want to delete?"
		},
		BOX_CONFIRM_VIDEO_DOWNLOAD: {
			title: "Wait!",
			text: "Before you download this file, please keep in mind all video content can only be uploaded on the Roadtrip Nation Website, not other websites like YouTube, Myspace or other media related sites.",
			arrButtons: ['BUTTON_NEW_WINDOW']
		},
		BOX_EMAIL_SENT: {
			title: "Thanks!",
			text: "Your email has been sent.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_EMAIL_SENT_CAREERS: {
			title			: 'Woohoo! Your e-mail was successfully sent.',
			text			: 'Thanks! Please check your email and verify by clicking on the link!',
			arrButtons		: ['BUTTON_CLOSE'],
			rtnDialogClass	: 'DAG_Contact'
		},
		BOX_EMAIL_VERIFIED_CAREERS: {
			title			: 'Woohoo! Your e-mail was successfully Verified.',
			text			: 'Thank you for your inquiry.<br />We will respond to you just as soon as we can.',
			arrButtons		: ['BUTTON_CLOSE'],
			rtnDialogClass	: 'DAG_Contact'
		},
		BOX_EMAIL_ALREADY_CAREERS: {
			title			: 'You have already signed up!',
			text			: 'This email has already been signed up and verified.',
			arrButtons		: ['BUTTON_CLOSE'],
			rtnDialogClass	: 'DAG_Contact'
		},
		BOX_EMAIL_NOT_SENT: {
			title: "Whoops!",
			text: "Something went wrong. Your email was not sent. Please try again.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_UPDATED: {
			title: "Yay!",
			text: "Your information has been updated.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_BAND_ACCEPTED: {
			title: "Yay!",
			text: "You have been accepted.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_UPDATED_CONTINUE: {
			title: "Yay!",
			text: "Your information has been updated.",
			arrButtons: ['BUTTON_CONTINUE_CLICK2']
		},
		BOX_UPDATED_CUSTOM: {
			title: "Yay!",
			text: "Your information has been updated.",
			arrButtons: ['BUTTON_CONTINUE_CUSTOM']
		},
		BOX_ADDED: {
			title: "Yay!",
			text: "Your information has been added.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_ADDED_CONTINUE: {
			title: "Yay!",
			text: "Your information has been added.",
			arrButtons: ['BUTTON_CONTINUE_CLICK2']
		},
		BOX_ADDED_CUSTOM: {
			title: "Yay!",
			text: "Your information has been added.",
			arrButtons: ['BUTTON_CONTINUE_CUSTOM']
		},
		BOX_INFORMATION_UPDATED: {
			title: "Yay!",
			text: "Your information has been updated.",
			arrButtons: ['BUTTON_CONTINUE']
		},
        BOX_INFORMATION_NOT_UPDATED: {
			title: "Whoops!",
			text: "That team name already exists. Please try again.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_MISSING_STEP: {
			title: "Whoops!",
			text: "You've missed a required step in the application.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_MORE_INFO_NEEDED: {
			title: "Whoops!",
			text: "Looks like your team needs to add some more information to complete your application.",
			arrButtons: ['BUTTON_CONTINUE']
		},
		BOX_INVALID_EDITPROFILE: {
			title: "Whoops!",
			text: "Please complete the required fields.",
			arrButtons: ['BUTTON_TRYAGAIN']
		},
		BOX_COPYRIGHT_WARNING: {
			title: "Warning | Profile Usage & Copyrights",
			text: "You acknowledge and agree that you alone are responsible for any and all text, graphics, videos, images, music, sounds, and any other content that you publish, upload, display or transmit on or through the Roadtrip Nation (“RTN”) Website (collectively “Content”).  By publishing, uploading, displaying or transmitting Content to the RTN Website, you agree to be bound by the RTN Website <a href='../terms_of_service.php' target='_blank'>Terms of Use</a>, including, but not limited to, all provisions related to copyrighted material.  You shall not publish, upload, display or transmit any Content that you did not create and that you do not have permission to publish, upload, display or transmit. <br /><br />By publishing, uploading, displaying or transmitting Content to the RTN Website, you hereby grant to RTN perpetual and unlimited streaming rights with respect to the Content on the Website, without monetary compensation to you.  In exchange, RTN shall, at its sole and exclusive discretion, include the Content on the Website.  By publishing, uploading, displaying or transmitting Content to the RTN Website, you grant, warrant and represent that you are capable of granting, to RTN an unlimited, transferable, irrevocable, exclusive, fully paid and worldwide license (which includes the right to sublicense) to copy, modify, publish, transmit, distribute, publicly perform, publicly display, reverse engineer, create derivative works of or otherwise exploit for any purpose, commercial, advertising or otherwise, any Content posted by you on the RTN Website.  RTN does not claim any ownership rights in your Content; you preserve full ownership rights over any and all Content posted by you on the RTN Website and any intellectual property rights or any other proprietary interests associated with your Content, subject to any and all rights granted to RTN pursuant to the RTN Website <a href='../terms_of_service.php' target='_blank'>Terms of Use</a>.",
			arrButtons: ['BUTTON_ACCEPT_COPYRIGHT_WARNING']
		},
				
		getLink: function(protectEvent) {
			return (protectEvent.target.tagName == 'A') ?
				protectEvent.target.href :
				$(protectEvent.target).parent('a').attr('href');
		},

		followLink: function(protectEvent) {
			// Trigger and bound events
			$(protectEvent.target).click();
			
			// Find parent tag in the case of <a><img/></a>
			document.location.href = $.rtn.dialog.getLink(protectEvent);
		},

		alert: function(options) {
			if (options.arrButtons) {
				options.buttons = {};
				for (btn in options.arrButtons) {
					$.extend(options.buttons, this[options.arrButtons[btn]]);
				}
			}

			settings = {
				width: 500,
				draggable: false,
				modal: true,
				dialogClass : options.rtnDialogClass
			};
			$.extend(settings, options);

			if ($('#dialog').length < 1)
				$('<div id="dialog"></div>').appendTo('body');

			$('#dialog').html(options.text);
			$('#dialog').dialog(settings);
			
			$('.ui-dialog-buttonpane button:eq(0)').focus().blur(); //takes focus off any links in .ui-dialog-content
		
			if (options.input_Subject) {
				$('<div id="subject" style="padding-top: 8px;">'+options.input_Subject+'</div>').appendTo('#dialog');
				//$('#subject').append(options.input_Subject);
			}
			
			if (options.input_Text) {
				$('<div id="message" style="padding-top: 8px;">'+options.input_Text+'<span id="char_count">200</span> Characters Left'+'</div>').appendTo('#dialog');
				$('#msg').startCounting({limit:200});
			}				
		},
		error: function(options, errors) {
			settings = clone(options);
			settings.text += "<br>";
			for (i in errors) {
				settings.text += "<br>" + errors[i];
			}

			this.alert(settings);
		},

		confirm: function(template, options) {
			settings = clone(template);

			$.extend(settings, options);

			settings.buttons = {
				"Cancel":   function() {
					$(this).dialog('destroy');
					if (options.callbackCancel) options.callbackCancel();
				},
				"Continue": function() {
					$(this).dialog('destroy');
					if (options.callbackContinue) options.callbackContinue();
				}
			};
			settings.arrButtons = null;

			this.alert(settings);
		},

		protected: [],
		protect: function(form) {

			var showProtect = function(event) {
				// var prot = $.rtn.dialog.protected;
				
				for (i = 0; i < $.rtn.dialog.protected.length; ++i) {
					if ($.rtn.dialog.protected[i] == this) return;
				}
				
				$.rtn.dialog.protected.push(this);
				
				
				$('a').bind('click.protect', function(event) {
					$.rtn.dialog.protectEvent = event;
					event.preventDefault();
					event.stopPropagation();
					$.rtn.dialog.alert($.rtn.dialog.BOX_NAVIGATE_AWAY);
				});
			}
			
			

			if ($.browser.msie)
				$(':input', form).bind('change.rtn-protect', showProtect);
			else
				form.bind('change.rtn-protect', showProtect);
		},
		
		unprotect: function(form) {
			var protected = $.rtn.dialog.protected;
			
			form.each(function() {
				for (i = 0; i < protected.length; ++i) {
					if (protected[i] == this)
						protected.splice(i, 1);
				}
			});

			// alert(protected.length);
			if (protected.length == 0) {
				$('a').unbind('click.protect');
			}
		}
		
		
	}
})(jQuery);

function Clone() { }
function clone(obj) {
    Clone.prototype = obj;
    return new Clone();
}

(function($) {

$.fn.startCounting = function(options){

var defaults = {
limit: 30
};

options = $.extend( defaults, options);

return $(this).each( function(i) {

var elem = $(this);
elem.after('');
elem.keyup( function(i) {
var limit = options.limit;
var text = elem.val();
var textlength = text.length;


//elem.next('.counting_class').val( limit + ' / ' + limit);
//elem.val(text.substr(0,limit));
$('#char_count').html(limit - textlength);
});
elem.trigger('keyup');
});
};

})(jQuery);