// Set up the name spaces
var maxshop = {};
maxshop.admin = {};
maxshop.admin.advert = {};
maxshop.admin.tree = {};

/*
 * Add an item to the wishlist
 * @param {int} styleCode
 */
maxshop.addToWishlist = function(styleCode)
{
	$.getJSON(maxshop.urlPrefix + "/style/wishlist/" + styleCode + "/add/", function (data)
	{
		if (data.success == false)
		{
			alert("Failed to add to favourites.");
		}
		else
		{
			var style = new maxshop.style(data.style);

			var newFav= $('<li></li>');
			newFav.attr('id', 'things_' + styleCode);

			var favLink = $('<a href="' + style.getUrl() + '" ></a>');
			favLink.append(style.getThumbnail(100, 128));

			var favRemove = $('<a class="remove" href="javascript:maxshop.removeFromWishlist(' + styleCode + ')">X</a>');
			
			newFav.append(favLink);
			newFav.append(favRemove);
			newFav.css('display', 'none');
			$('#favourite_heart_' + styleCode).addClass('like');
			$('#things-i-love').append(newFav);
			newFav.fadeIn();
//			newFav.children('a').children('img').popup(
//			{
//				text: '/style/' + styleId + '/json/',
//				formatData: maxshop.formatPopup,
//				top: 10
//			});

			maxshop.addThinsILoveHelp();
			maxshop.initScrollPane();
			/*@cc_on
			   /*@if (@_jscript_version <= 6)
					$(".item_container").css("zoom","1");
			   /*@end
			@*/
		}
	});
}

maxshop.removeFromWishlist = function (styleCode)
{
	$.getJSON(maxshop.urlPrefix + "/style/wishlist/" + styleCode + "/remove/", function (data)
	{
		if (data.success == false)
		{
			alert("Failed to remove from favourites.");
		}
		else
		{
			// Delete it from display.
			$('#things_' + styleCode).fadeOut(300, function()
			{
				$('#things_' + styleCode).remove();
				maxshop.addThinsILoveHelp();
				maxshop.initScrollPane();
			});
			
			$('#favourite_heart_' + styleCode).removeClass('like');
			/*@cc_on
			   /*@if (@_jscript_version <= 6)
					$(".item_container").css("zoom","1");
			   /*@end
			@*/
		}

		$('.jqueryPopup').remove();
	});
}

maxshop.wishlistToggleHeart = function (styleCode)
{
	if( $('#favourite_heart_' + styleCode).hasClass('like') )
	{
		maxshop.removeFromWishlist(styleCode);
	}
	else
	{
		maxshop.addToWishlist(styleCode);
	}
}

maxshop.getCollectionList = function(callback)
{
	$.getJSON(maxshop.urlPrefix + '/shop-for/ajax/list/', function(data)
	{
		callback(data.children);
	});
}

maxshop.formatPopup = function (data)
{
	var style = new maxStyleClass(data);

	var text = '<div class="popup_text">';
	text += '<p class="title"><a href="' + style.getUrl() + '" title="view item" >' + style.getDescription() + '</a></p>';
	text += '<p class="style_number">STYLE NUMBER:' + style.getCode() + '</p>';
	text += '<p class="price">' + style.getPrice() + '</p>';
	text += '<p>' + style.getLongDescription(100) + '</p>';
	text += '</div>';

	return text;
}

maxshop.initScrollPane = function()
{
	$('.scroll-pane').jScrollHorizontalPane(
		{
			scrollbarHeight:13,
			scrollbarMargin:0,
			reinitialiseOnImageLoad:false,
			minimumWidth:565,
			dragMinWidth:391,
			dragMaxWidth:391,
			maintainPosition: false,
			reset: false
		});

	if( $('#favourites_tab li.first').hasClass('ui-tabs-selected'))
	{
		if( $('ul#things-i-love').children().length < 6 )
		{
			$('.jScrollPaneTrack').hide();
		}
		else
		{
			$('.jScrollPaneTrack').show();
		}
	}
	else
	{
		if( $('ul#recently-viewed-items').children().length < 6 )
		{
			$('.jScrollPaneTrack').hide();
		}
		else
		{
			$('.jScrollPaneTrack').show();
		}
	}
}

maxshop.autocomplete = function(inputSelect, options)
{
	if( !options )
	{
		options = {};
	}

	if( !options.format)
	{
		options.format = function(style)
		{
			return "<div>" + style.getCode() + " (" + style.getDescription() + ")</div>";
		}
	}

	if( !options.extraParams )
	{
		options.extraParams = {};
	}

	$(inputSelect).autocomplete(maxshop.urlPrefix + "/search/ajax/",
	{
		extraParams: options.extraParams,
		selectFirst: false,
		matchContains: true,
		minChars: 2,
		mustMatch: false,
		autoFill: false,
		dataType:"json",
		max:20,
		parse: function(data)
		{
			var formatedArray = new Array()
			for( var i in data )
			{
				var rawStyle = data[i];
				if( rawStyle )
				{
					var style = new maxshop.style(rawStyle.style);

					// data (all the data for the row), value (just the value displayed), and result (the formatted value)
					formatedArray[formatedArray.length] = {
						data:style,
						value:style.getDescription(),
						result:style.getCode()
					};
				}
			}
			return formatedArray;
		},
		formatItem: options.format
	}).result(function(event, item)
	{
		if( options.callback )
		{
			options.callback(item);
		}
	});
}


maxshop.clearSearch = function()
{
	if( $('#search > input:text').val() == "Enter Search" )
	{
		$('#search > input:text').val('');
	}
	$('#search > input:text').focus();
}

/*
 * @class An exception
 * An exception class
 */
maxshop.exception = function(msg, name)
{
	this.message = msg;
	this.name = name;
}
maxshop.exception = Error;

/**
 * Add an error to the dialog.
 * @param dialog Either a jquery object or a jquery selector to the dialog.
 * @param msg The message that needs to be displayed
 */
maxshop.dialogError = function(dialog, msg)
{
	var error = $('<span class="error">' + msg + '</span>');
	error.css(
	{
		position: 'absolute',
		right: '10px',
		bottom: '60px'
	});

	$(dialog).prepend(error);
	error.fadeOut(4000);
}

/**
 * Add help text to the things i love pane.
 */
maxshop.addThinsILoveHelp = function()
{
	$('.things-i-love-help').remove();

	if( $('#things-i-love').children().length < 5 )
	{
		var li = $('<li class="things-i-love-help" style="display: none;"><img src="/images/help-things-i-love-panel.jpg" alt="things i love help" height="130px" width="100px" /></li>');
		$('#things-i-love').append(li);
		li.fadeIn();
	}
}

maxshop.urlPrefix = "";
