/**
	KintekCMS Javascript Common
   
	VERSION:			1.0
	LAST MODIFIED:		24 - 09 - 09
	MODIFIED BY:		Madhava
   
*/

/**
	Version 1.0
	
	Documentation:
	
	- Auto lineup_icons for famfamfam icons
	- auto delete confirms
	- live confirm class which now uses attr('confirm')
	
	Future Improvements
	- Make alertMessage use Notices
*/

// All praise be to jQuery
$(function() {

	
	// default dialogs
	$("#errorMessage").dialog({
		autoOpen: false,
		width: 310,
		close: function(){
			$("#errorMessage").dialog('option', 'width', 310);
			$("#errorMessage").html("");
			$("#errorMessage").dialog('option', 'buttons', {});
			}
	});
	
	$('#noticeMessage').dialog({
		autoOpen: false,
		width: 310,
		close: function(){
			$("#noticeMessage").dialog('option', 'width', 310);
			$("#noticeMessage").dialog('option', 'buttons', {});
			$("#noticeMessage").html("");
			}
	});
	
	// Fix famfam silk icons alignment
	$('img[src*=images/icons/]').addClass('lineup_icon');
	
	// Confirm certain link clicks
	$('.confirm').live('click', function(e) {
		link = $(this);
		if (typeof(link.attr('confirm')) != 'undefined') {
			return confirm(link.attr('confirm'));
		} else {
			return confirm('Really?');
		}
	});

	// Apply delete links
	$(".delete").live('click',attachAjaxDeleteEvent);

	// Focus on username in login form
	$("#loginForm").find("#user_name").focus();

	// Submit login form
	$("#loginLink").click(function(e) {
		e.preventDefault();
		$("#loginForm").submit();
	});

	// Basic Ajax Links
	$('.ajaxLink').click(function(e) {
		e.preventDefault();
		$.post($(this).attr('href'), function(d) {
			alert(d);
		});
	});
	
	// Apply date inputs	
	$('.datepicker').datepicker({ dateFormat: 'dd-mm-yy' });
	

	// Very simple Ajax link
	$(".ajax").click(function(e) {
		e.preventDefault();
		$.post($(this).attr("href"), function(d) {
			alert(d);
		});
	});
	
	// Apply tablesorters
	if ($("table.sortableTable").length) {
		$("table.sortableTable").tablesorter({
			sortList: [[1,0]]
		});
	}
	
	// Help!
	$("#helpLink").click(function(e) {
		e.preventDefault();
		$("#info,.info").slideToggle();
	});
	
	$('table.rowme').each(function() {
		$(this).find('tr:even').addClass('stripe');
	});
	
}); 


function attachAjaxDeleteEvent(e) {
	e.preventDefault();
	var parentRow;
	if ($(this).parents("tr")[0]) {
		parentRow = $(this).parents("tr")[0];
	}
	else if ($(this).parents("li")[0]) {
		parentRow = $(this).parents("li")[0];
	}
	if (confirm("Really Delete?")) {
		$.get($(this).attr("href"), function(d) {
			if (d=="Success") {
				$(parentRow).hide();
			}
			else alert("UNEXPECTED RESPONSE FROM SERVER: "+d);
		});
	}
}

function noticeMessage(notices)
{
	var noticeIcon = '<span class="ui-icon ui-icon-check" style="float: left; margin-right: 0.3em; margin-top:2px;"/>';
	$('#noticeMessage').html('');
	for(var i in notices)
	{
		$('#noticeMessage').append('<p>' + noticeIcon + notices[i] + '</p>');
	}
	$('#noticeMessage').dialog({
		close: function() { $(this).dialog('destroy') }
	});
}

function errorMessage(errors)
{
	var errorIcon = '<span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em; margin-top:2px;"/>';
	$('#errorDialog').html('');
	for(var i in errors)
	{
		if(errors[i].field)
		{
			$('#errorDialog').append('<p>' + errorIcon + errors[i].field +errors[i].reason + '</p>');
		}
		else{
			$('#errorDialog').append('<p>' + errorIcon + errors[i]);
		}
	}
	$('#errorDialog').dialog({
		close: function() { 
			$('#errorDialog').html(''); 
			$(this).dialog('destroy'); 
		}
	});	
}

function alertMessage(message) {
	alert(message);
}
