/// <reference path="jquery-1.5.1.js" />
/// <reference path="jquery-ui-1.8.11.min.js" />
/// <reference path="jquery.jgrowl.js" />

/*
* StartUp Script: startup.js
* Version 1.0
* Copyright(c) 2011 Smirne.com. All Rights Reserved.    
*/


// jCarousel Helpers
function jcarousel_itemLoadCallback(carousel, state) {

	var index = carousel.first;
	var params = carousel.list.attr('id').split('_');
	var res = params[1];
	var id = params[2];

	// Check if the requested items already exist
	if (state != 'init' && carousel.has(index)) {
		jcarousel_displayCounter(id, index, carousel.size());
		return;
	}

	carousel.lock();

	jQuery.getJSON(
		'/Home/GetPictureURL',
		{
			key: id,
			resolution: res,
			index: index - 1
		},
		function (data)
		{
			carousel.size(data.total);
			jcarousel_displayCounter(id, index, carousel.size());
			carousel.add(carousel.first, jcarousel_getItemHTML(data.url, res));
			carousel.unlock();
		}
	);
};

// AjaxDialog
function ajaxDialog(title, action, params) {
	var ajaxDialog = $('<div></div>').load(action, params);
	ajaxDialog.dialog({
		title: title,
		position: 'center',
		height: 580,
		width: 550,
		dialogClass: 'ajaxDialog',
		resizable: false,
		modal: true,
		buttons: {
			'Submit': function () {
				var $form = ajaxDialog.find('form:first');
				var action = $form.attr("action");
				var inputs = $form.serialize();
				var btn = $('.ui-dialog-buttonpane').find('button:first');
				var label = btn.button("option", "label");
				var errorWidget = ajaxDialog.children(".errorWidget");

				errorWidget.hide();
				btn.button("option", "label", "Please wait...");
				btn.button("option", "disabled", true);

				$.post(action, inputs, function (data) {

					btn.button("option", "label", label);
					btn.button("option", "disabled", false);

					if (data.returnCode != 0) {
						errorWidget.html(buildMessage('ui-state-error', 'ui-icon-alert', data.message));
						errorWidget.show();
					}
					else {
						ajaxDialog.dialog('close');
						$.jGrowl("Your email has been sent.<BR>Thank you!");
					}
				}, "json");

				return false;
			},
			Cancel: function ()
			{
				$(this).dialog('close').remove();
			}
		},
		close: function ()
		{
			$(this).remove();
		}
	});
};

// Message Construct
function buildMessage(type, icon, message)
{
	var messageBox = "<div style='padding: 0pt 0.7em;' class='" + type + " ui-corner-all'>";
	messageBox += "<div><span style='float: left; margin: 0.3em 0.3em 0 0;' class='ui-icon " + icon + "'></span>";
	messageBox += "<div class='errorMessage'>" + message + "</div></div></div>";
	return messageBox;
}

// jGrowl Settings
$.jGrowl.defaults.position = 'bottom-right';

