$(function(){
				
	// SUPERFISH FOR MENU ( DROP-DOWN )
	$("ul.nav").superfish({
		animation:{
			height: "show",
			width: "show"
		}, speed : 500
	});
					
	// TOOLTIPS
	$(".tooltip").easyTooltip();
		
	// CHECK ALL CHECKBOX WHEN HEADER ONE IS PRESSED
	$('.checkall').click(
		function(){
			$(this).parent().parent().parent().parent().find("input[type='checkbox']").attr('checked', $(this).is(':checked'));   
		}
	);
	
	$('.checkall_once').click(
		function(){
			$(this).parent().parent().find("input[type='checkbox']").attr('checked', $(this).is(':checked'));   
		}
	);
	
	// CLOSE NOTIFICATION MESSAGES WHEN CLICKED
	/*
	$(".close").click(
		function () {
			$(this).fadeTo(400, 0, function () { // Links with the class "close" will close parent
			$(this).slideUp(400);
		});
		return false;
		}
	);
	*/
	$(".close").live('click', function () {
			$(this).fadeTo(400, 0, function () { // Links with the class "close" will close parent
			$(this).slideUp(400);
		});
		return false;
	});

				
	// SORTABLE, PORTLETS
	$(".column").sortable({
		connectWith: '.column'
	});
				
	$(".sort").sortable({
		connectWith: '.sort'
	});		

	$(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
			.find(".portlet-header")
			.addClass("ui-widget-header ui-corner-all")
			.prepend('<span class="ui-icon ui-icon-circle-arrow-s"></span>')
			.end()
			.find(".portlet-content");

	$(".portlet-header .ui-icon").click(function() {
		$(this).toggleClass("ui-icon-minusthick");
		$(this).parents(".portlet:first").find(".portlet-content").toggle();
	});

	$(".column").disableSelection();

	// ACCORDION
	$("#accordion").accordion({ header: "h3" });
	
	// TABS
	$('#tabs').tabs();
	
	// DIALOG			
	$('#dialog').dialog({
		autoOpen: false,
		width: 500,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close"); 
			}, 
			"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
				
	// DIALOG LINK
	$('#dialog_link').click(function(){
		$('#dialog').dialog('open');
		return false;
	});

	// DATEPICKER
	$('#datepicker').datepicker({
		inline: true
	});
				
	// SLIDER
	$('#slider').slider({
		range: true,
		values: [20, 70]
	});
				
	// PROGRESSBAR
	$("#progressbar").progressbar({
		value: 40 
	});
				
	// HOVER STATES ON STATIC WIDGETS
	$('#dialog_link, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
		
	// WYSIWYG EDITOR
	$('textarea.wysiwyg').wysiwyg({
  		controls: {
    		strikeThrough : { visible : true },
    		underline     : { visible : true },
      
   			separator00 : { visible : true },
      
     		justifyLeft   : { visible : true },
      		justifyCenter : { visible : true },
     		justifyRight  : { visible : true },
     		justifyFull   : { visible : true },
      
 			separator01 : { visible : true },
      
   			indent  : { visible : true },
			outdent : { visible : true },
      
     		separator02 : { visible : true },
      
    		subscript   : { visible : true },
    		superscript : { visible : true },
      
   			separator03 : { visible : true },
  	    
  			undo : { visible : true },
   			redo : { visible : true },
      
    		separator04 : { visible : true },
      
     		insertOrderedList    : { visible : true },
    		insertUnorderedList  : { visible : true },
    		insertHorizontalRule : { visible : true },
      
    		h4mozilla : { visible : true && $.browser.mozilla, className : 'h4', command : 'heading', arguments : ['h4'], tags : ['h4'], tooltip : "Header 4" },
     		h5mozilla : { visible : true && $.browser.mozilla, className : 'h5', command : 'heading', arguments : ['h5'], tags : ['h5'], tooltip : "Header 5" },
     		h6mozilla : { visible : true && $.browser.mozilla, className : 'h6', command : 'heading', arguments : ['h6'], tags : ['h6'], tooltip : "Header 6" },
      
     		h4 : { visible : true && !( $.browser.mozilla ), className : 'h4', command : 'formatBlock', arguments : ['<H4>'], tags : ['h4'], tooltip : "Header 4" },
     		h5 : { visible : true && !( $.browser.mozilla ), className : 'h5', command : 'formatBlock', arguments : ['<H5>'], tags : ['h5'], tooltip : "Header 5" },
   			h6 : { visible : true && !( $.browser.mozilla ), className : 'h6', command : 'formatBlock', arguments : ['<H6>'], tags : ['h6'], tooltip : "Header 6" },
      
   			separator07 : { visible : true },
      
     		cut   : { visible : true },
     		copy  : { visible : true },
     		paste : { visible : true }
   		}
  	});

});

function create_notification(msg, title, type) {
	$('<div class="message ' + type + ' close"><h2>' + title + '</h2><p>' + msg + '</p></div>')
	.hide()
	.prependTo("#notifications_list")
	.fadeIn(1500);
	$("html,body").scrollTop($("#notifications_list").position().top);
	if($("#notifications_list .message").length > 4)
		$("#notifications_list .message:gt(3)").remove();
}

$(document).ready(function() {
	$("#wurl_form").submit(function () {
		var wurl = $("input#wurl").val();
		var postStr = 'url='+wurl;
		$.ajax({
			type:"POST",
			url:"api/whitelist/",
			data:postStr,
			success:function(response, statusMsg, ajaxObj) {
				create_notification('Congratulations, your chosen URL has now been whitelisted and you may begin submitting.', 'Success!', 'success');
			},
			error:function(ajaxObj) {
				errMsg = 'Unknown.';
				switch(ajaxObj.status) {
					case 404: errMsg = 'Malformed Request.'; break;
					case 400: errMsg = 'Invalid URL provided, No Backlink Detected or Backlink is Nofollow.'; break;
					case 403: errMsg = 'A registered member whitelisted this domain or you need to login.'; break;
				}
				create_notification('We were Unable to Whitelist your chosen URL for the following reason: ' + errMsg, 'Error!', 'error');
			}
		});
		return false;
	});
	$("#lurl_form").submit(function () {
		var postStr = $("#lurl_form").serialize();
		$.ajax({
			type:"POST",
			url:"api/recipient/",
			data:postStr,
			success:function(response) {
				create_notification('Congratulations, your chosen Domain has now been queued for approval by WL Admins. You will recieve an email when and if accepted.', 'Success!', 'success');
			},
			error:function(ajaxObj) {
				errMsg = 'Unknown.';
				switch(ajaxObj.status) {
					case 400: errMsg = 'Malformed Request, Check the Validity of Your Submitted URL.'; break;
					case 403: errMsg = 'Domain has been listed by another user or you are not logged in.'; break;
				}
				create_notification('We were Unable to Accept your chosen URL for the following reason: ' + errMsg, 'Error!', 'error');
			}
		});
		return false;
	});
	$("#submit_downloads_form").submit(function() {
		var postStr = $(this).serialize();
		$("#submit_downloads_form input[name=submit]").hide();
		$("#submit_downloads_form input[name=loading]").show();
		$.ajax({
			type:"POST",
			url:"api/submit/",
			data:postStr,
			dataType:"json",
			success:function(response) {
				create_notification('Congratulations, in ' + response.executionTime + 's we sent ' + response.submissions + ' Submission(s) to ' + response.recipients.total + ' Sites<br />' +
						    response.recipients.pass + ' Sites Received Your Submissions, ' + response.recipients.fail + ' Responded with an Error', 'Success!', 'success');
				$("#submit_downloads_form input[name=submit]").show();
				$("#submit_downloads_form input[name=loading]").hide();
			},
			error:function(ajaxObj) {
				errMsg = 'Unknown.';
				switch(ajaxObj.status) {
					case 400: errMsg = 'Malformed Request.'; break;
					case 404: errMsg = 'Malformed Request.'; break;
					case 403: errMsg = 'URL is not whitelisted, is suspended within our system, is owned by another user or your download URL host and site URL host did not match.'; break;
				}
				create_notification('We were Unable to Submit Your Downloads due to the following error: ' + errMsg, 'Error!', 'error');
				$("#submit_downloads_form input[name=submit]").show();
				$("#submit_downloads_form input[name=loading]").hide();
			}
		});
		return false;
	});
	$(".type_down_arrow").click(function() {
		var tda_id = $(this).attr('id');
		var tda_id_num = tda_id.substr(16);
		var tda_val = $(".type_list:eq(" + tda_id_num + ")").val();
		$(".type_list:gt(" + tda_id_num + ")").val(tda_val);
		return false;
	});
	$("#save_sites_list").click(function() {
		var slist = $("#sites_list input:checked");
		$.ajax({
			type: "POST",
			url: "api/site_selection/",
			data: slist.serialize(),
			success: function(response) {
				create_notification('Success, your choices have been saved for future usage.', 'Success!', 'success');
			},
			error: function(ajaxObj) {
				errMsg = 'Unknown';
				switch(ajaxObj.status) {
					case 400: errMsg = 'Invalid request parameters.'; break;
					case 403: errMsg = 'Forbidden, you must be logged in.'; break;
				}
				create_notification('Unable to save your choices for the following reason: ' + errMsg, 'Error!', 'error');
			}
		});
		return false;
	});
	$("#change_pass_form").submit(function() {
		var old_pass = $("#old_pass");
		var new_pass = $("#new_pass");
		var new_pass_confirm = $("#new_pass_confirm");
		if(new_pass.val() != new_pass_confirm.val()) {
			create_notification('Your new password and the confirmation password did not match, please try again.', 'Error!', 'error');
			return false;
		}
		$.ajax({
			type:"POST",
			url:"api/change_password/",
			data: $(this).serialize(),
			success:function(response) {
				create_notification('Success, your password has been changed.', 'Success!', 'success');
			},
			error:function(ajaxObj) {
				errMsg = 'Unknown';
				switch(ajaxObj.status) {
					case 403: errMsg = 'Forbidden, either you are not logged in or passwords did not match.'; break;
					case 400: errMsg = 'Malformed Request.'; break;
				}
				create_notification('Unable to change your password due to the following reason: ' + errMsg, 'Error!', 'error');
			}
		});
		return false;
	});
	$("#clear_downloads").click(function() {
		$("#submit_downloads_form input[name=title\\[\\]]").val('');
		$("#submit_downloads_form input[name=url\\[\\]]").val('');
		$("#submit_downloads_form select[name=type\\[\\]]").val('App');
	});
});
