// Customize Recaptcha
var RecaptchaOptions = { theme : 'white' };
var itumz_menulists = new Array();

// Define Global Functions
function htmlspecialchars(p_string) {
	p_string = p_string.replace(/&/g, '&amp;');
	p_string = p_string.replace(/</g, '&lt;');
	p_string = p_string.replace(/>/g, '&gt;');
	p_string = p_string.replace(/"/g, '&quot;');
//	p_string = p_string.replace(/'/g, '&#039;');
	return p_string;
};

function trim (str) {
    return str.replace(/^\s*/, "").replace(/\s*$/, "");
}


function addslashes( str ) {
    return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}


function trim (str) {
	return str.replace(/^\s+|\s+$/g, '');
}

function delayedFade (block, delay, duration) {
	
	if (delay == null) {
		delay = 2000;
	}
	
	if (duration == null) {
		duration = 500;
	}
	
	block.stop(true).show();
	
	setTimeout(function() {
		block.fadeOut(duration)
	}, delay);
	
}

function toggle(which_option) {
	var weekly = document.getElementById("pick_weekly_div");
	var monthly = document.getElementById("pick_monthly_div");

	if(which_option == "daily") {
    	weekly.style.display = "none";
    	monthly.style.display = "none";
  	}else if (which_option == "weekly"){
    	weekly.style.display = "block";
    	monthly.style.display = "none";
	} else if (which_option == "monthly") {
    	weekly.style.display = "none";
    	monthly.style.display = "block";
	}
}

function fill_in_day(monthly_field, number, day_field){
	var monthly_field_input = document.getElementById(monthly_field);
	if (monthly_field_input.value != ""){
		var select_field = "day" + monthly_field_input.value;
		var select_day = document.getElementById(select_field);
		select_day.className = "";
	}

	monthly_field_input.value = number;
	day_field.className='selected';
}	

function ajaxSuccessError (response, error_heading, suppress_alerts) {
	var error;
	
	if (response['error-inline']) {
		error = response['error-inline'];
	} else if (response['error']) {
		error = response['error'];
	} else {
		return false;
	}
	
	if (suppress_alerts) {
		return true;
	}
	
	if (ajaxErrorBox(error, error_heading)) {
		return true;
	} else {
		alert(error_heading + ": " + error);
		return true;
	}
	
	return false;
}

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	
	return false;
}


function clearAllActive(e) {
	$('.clicked_active').click();
}

function doLogin (obj) {
	
	var u = window.location + "";
	if (obj) {
		if ($(obj).is('a')) {
			u = $(obj).attr('href');
		}
	}
	
	var go = '/login/';
	
	if (u.length) {
		//go += '?next=' + encodeURIComponent(u);
	} else {
		return;
	}
	
	window.location = go;
	
	return false;
}

function doNewList(link) {
	tb_show(null,'#TB_inline?height=270&width=557&inlineId=tb_new-list&modal=true&border=false',false);

	clearAllActive();
	$('#new-list_box .txtInput').focus();
	
	return false;
}





function ajaxErrorBox (message, heading) {
	var box = $('#ajax-error-box');
	
	if (!box.length) {
		return false;
	}
	
	box
		.find('h2')
			.html(heading).end()
		.find('.box-text')
			.html(message).end()
		.show();
	
	$(window).trigger('itumz_redraw');
	
	return true;
}

function makeTooltip (content, append_to) {
	if (!append_to) {
		append_to = $('body');
	}
	
	$('.tooltip').remove();
	
	var tooltip = $('<div class="tooltip closeable-box"></div>')
		.append('<div class="tooltip-tl"><div class="tooltip-tr"></div></div>')
		.append('<div class="tooltip-ml"><div class="tooltip-mr"><div class="tooltip-content"></div></div></div>')
		.append('<div class="tooltip-bl"><div class="tooltip-br"></div></div>')
		.find('.tooltip-content')
			.append('<div class="close-btn" title="Close this"></div>')
			.append(content)
			.end()
		.appendTo(append_to);
	
	return tooltip;
}

function closeTooltips (delay, tooltip) {
	if (!delay) {
		delay = 0;
	}
	
	if (!tooltip) {
		tooltip = $('.tooltip');
	}
	
	return setTimeout(function() {
		tooltip
			.fadeOut(500)
			.queue(function() {
				$(this)
					.remove()
					.dequeue();
			});
	}, delay);
	
}

function getMenuList (sort_col) {
	if (sort_col) {
		// Sort the list first
		itumz_menulists.sort(function(a, b) {
			var x, y;
			
			switch (sort_col) {
			case 'alpha':
				x = a.name.toLowerCase();
				y = b.name.toLowerCase();
				
				return ((x < y) ? -1 : ((x > y) ? 1 : 0));
			case 'accessed':
				return b.accessed - a.accessed;
			case 'modified':
				return b.modified - a.modified;
			}
		});
	}
	
	var output = '';
	
	for (var i = 0; i < itumz_menulists.length; i++) {
		output += '<li><a href="/editlist/?id=' + itumz_menulists[i].id + '" title="' + addslashes(itumz_menulists[i].name) + '">' + itumz_menulists[i].name + '</a></li>';
	}
	
	return output;
}

// General forms
$(document).ready(function() {
	$('.itumz_form .txtInput')
		.focus(function() {
			$(this).addClass('focus');
		})
		.blur(function() {
			$(this).removeClass('focus');
		});
	
	$('.itumz_form').submit(function() {
		
		var form = this;
		
		$(this).find('.formBtn:not(.ajaxBtn)').each(function() {
			$(form).append('<input type="hidden" name="' + $(this).attr('name') + '" value="' + $(this).attr('value') + '" />');
		
			$(this)
				.attr('disabled','disabled')
				.addClass('disabled');
		});
		
	});
	
});

// Register form
$(document).ready(function() {
	if ($('.register_form #register_type_full').is(':checked')) {
		$('.register_form #register_full_block').show();
	}
	
	$('.register_form input[name=register_type]').click(function() {
		if ($(this).val() == 'full') {
			$('.register_form #register_full_block').show();
		} else {
			$('.register_form #register_full_block').hide();
			
			$('.register_form #register_password').val('');
		}
	});
	
	if ($('.register_form input[name=register_type]').val() != 'full') {
		$('.register_form #register_password').val('');
	}
});

$(document).ready(function() {
	$('.share_block_toggle').click(function() {
		var share_block = $(this).parents('.list_share_box:first').find('.share_more_block');
		
		if (share_block.is(':visible')) {
			$(this).html('Share it!');
			share_block.hide();
		} else {
			$(this).html('Hide sharing');
			share_block.show();
		}
		
		return false;
	});
	
	$('.share_more_block .list_url:not(#edit_list_url) > strong').click(function() {
		// firefox
		if(document.createRange) {
			rangeToSelect = document.createRange();
			rangeToSelect.selectNode(this.firstChild);
			curSelect = window.getSelection();
			curSelect.addRange(rangeToSelect);
			return false;
		}
		// ie
		if(document.body && document.body.createTextRange) {
			range = document.body.createTextRange();
			range.moveToElementText(this);
			range.select();
			return false;
		}
	});
});

$(document).ready(function() {
	
	$(document).click(clearAllActive);
	
	$('#header .button_row .my-lists').each(function() {
		
		var height_adj = false;
		
		$(this).parents('li:first').click(function(e) {
			
			if ($(this).hasClass('clicked_active')) {
				$(this).removeClass('clicked_active');
				$('#my-lists_menu').hide();
				
			} else {
				$(this).addClass('clicked_active');
				$('#my-lists_menu').show();
				
				if (!height_adj) {
					// Adjust the height of the scoller
					var scroll = $('#my-lists_menu .all-lists .all-lists-scroll');
					var scroll_h = scroll.height();
					
					var content = $('#my-lists_menu .all-lists .menu_lists');
					var content_h = content.outerHeight();
					
					if (content_h < scroll_h) {
						scroll.css('height','auto');
					}
					
					height_adj = true;
				}
			}
			
			return false;
			
		});
	});
	
	
	$('#my-lists_menu').click(function(e) {
		e.stopPropagation();
	});
	
	$('#my-lists_menu .sorter a').click(function() {
		if ($(this).hasClass('active')) {
			return false;
		}
		
		$(this)
			.siblings('a')
				.removeClass('active')
				.end()
			.addClass('active');
		
		var list = $('#my-lists_menu .all-lists ul.menu_lists');
		
		if ($(this).hasClass('sort_alpha')) {
			// Sort alphabetically
			list.html(getMenuList('alpha'));
		} else if ($(this).hasClass('sort_accessed')) {
			// Sort by last access time
			list.html(getMenuList('accessed'));
		} else if ($(this).hasClass('sort_modified')) {
			// Sort by created time
			list.html(getMenuList('modified'));
		}
		
		return false;
	});
});

// Intro Box
$(document).ready(function() {
	
	var ss = new Array('tour-ss2', 'tour-ss3');
	
	function swapScreen() {
		var cur = $('#introbox .screenshot:visible');
		
		var next = ss.shift();
		ss.push(cur.attr('id'));
		
		var over = $('#introbox #' + next).hide();
		
		cur.after(over);
		
		over.fadeIn(1000);
		
		cur
			.fadeOut(1000)
			.queue(function() {
				setTimeout(swapScreen,10000);
				$(this).dequeue();
			});
	}
	
	setTimeout(swapScreen,10000);
	
	$('#introbox input[name=list_name]').val('Type in your email to sign up...');
	
	$('#introbox input[name=list_name]')
		.focus(function() {
			if ($(this).hasClass('placeholder')) {
				$(this).removeClass('placeholder').val('');
			}
		})
		.blur(function() {
			if (!$(this).val().length) {
				$(this).addClass('placeholder').val('Type in your email to sign up...');
			}
		});
	
	$('#introbox form').submit(function() {
		if ($('#introbox input[name=list_name]').hasClass('placeholder')) {
			ajaxErrorBox('Make sure you type in a name for the list your want to create.','Oops!');
			return false;
		}
	});
});

/* List Browsing and Preview */
$(document).ready(function() {
	// Sorting
	$('.sort_controls select.sort_list').change(function() {
		window.location.href = location.pathname + '?sort=' + $(this).val();
	});
	
	// Preview link
	$('.list_preview_link').click(function() {
		
		var link = this;
		var block = $(this).parents('.list_preview:first');
		var ul = block.find('ul');
		
		if (ul.length) {
			ul.toggle();
			return false;
		}
		
		var list_id = parseInt(block.attr('id').replace('list-preview-',''));
		
		if (!list_id) {
			return false;
		}
		
		// Construct the AJAX function
		$.ajax({
			type: 'POST',
			url: '/services.php?run=listitems',
			data: { 'list_id' : list_id },
			dataType: 'json',
			success: function(response) {
				if (ajaxSuccessError(response) || !response.preview_list.length) {
					return false;
				}
				
				// Append the items to the block
				block.append(response.preview_list);
			}
		});
		
		return false;
	});
});

/* Boxes and Alerts */
$(document).ready(function() {
	$('.closeable-box .close-btn').live('click',function() {
		$(this)
			.parents('.closeable-box')
			.fadeOut(500)
			.queue(function() {
				$(window).trigger('itumz_redraw');
				
				$(this).dequeue();
			});
		
		return false;
	});
	
	$('.disable-alert').live('click',function(e) {
		
		var id = $(this).attr('id');
		var link = this;
		
		if (id.search('disable-alert_') == -1) {
			return false;
		}
		
		id = id.replace('disable-alert_','');
		
		if (!id.length) {
			return false;
		}
		
		$.ajax({
			type: 'POST',
			url: '/services.php?run=setalert',
			data: { 'alert' : id, 'set' : 0 },
			dataType: 'json',
			error: function() {
				// Close the box if it's closeable
				$(link).parents('.closeable-box:first').find('.close-btn').click();
			},
			success: function(response) {
				// Close the box if it's closeable
				$(link).parents('.closeable-box:first').find('.close-btn').click();
				
				if (ajaxSuccessError(response,"Unable to save settings")) {
					return false;
				}
			}
		});
		
		e.preventDefault();
	});
});