if (window != window.top) window.top.location = window.location;
$(function() {

	var qtipDefaults = {
		position: {
			viewport: $(window)
		},
		show: {
			delay: 300,
			effect: function(api) { $(this).slideDown(undefined, function () {$(this).dequeue();}); },
			solo: true
		},
		hide: {
			effect: function(api) { $(this).slideUp(undefined, function () {$(this).dequeue();}); }
		},
		style: {
			tip: true,
			classes: "ui-tooltip-blue ui-tooltip-rounded ui-tooltip-shadow"
		}
	};


	$('a[title], img[title]').each(function() {
		var qtipSettings = {}, title = $(this).attr('title');
		if (title) {
			title = simpleFormatTitle(title);
			$.extend(true, qtipSettings, qtipDefaults, { content: { text: title } } );
			$(this)
				.removeAttr('title')
				.qtip(qtipSettings);
		}
	});
	$('a.actionSubmitForm').click(function(event) {
		var formId = $(this).attr('href').split('#')[1];
		if (formId) {
			event.preventDefault();
			if (!$(this).data('submitted')) {
				$('#' + formId).submit();
				$(this).data('submitted', true);
			}
		}
	});
	$('#searchForm input.question').autocomplete({
		source: function(request, response) {
			$.ajax({
				url: '/search.php',
				dataType: 'json',
				data: {
					action: 'json',
					q: request.term,
					limit: 5
				},
				success: function( data ) {
					if (data) {
						response( $.map( data.terms, function( item ) {
							return {
								label: item,
								value: item
							}
						}));
					}
				}
			});
		},
		minLength: 3
	});
	$('a.searchSubmit').click(function(){
		$('#searchForm').submit();
	});
	$('a.exactCreate').click(function(e){
		e.preventDefault();
		var params = objFromRel(this);
		if(!$('#sfGadgetCreate').length)
			$('<form>', {
				id: 'sfGadgetCreate',
				method: 'post'
		}).appendTo('body');
		$('#sfGadgetCreate input').remove();
		for (param in params) {
			var name = (param == 'quick_gadget_create') ? param : 'gadget_create[gadget][' + param + ']';
			$('<input>', {
				name: name,
				type: 'hidden'
			}).appendTo('#sfGadgetCreate').val(params[param]);
		}
		if ($.sfWindow) $.sfWindow.overlay();
		$('#sfGadgetCreate')
			.attr('action', $(this).attr('href'))
			.submit();
	});
	$('a.actionQuickCodeActivation').each(function() {
		if (!$.sfWindow) script.get('/imp/js/jquery/plugins/jquery.sfwindow.js');
		$(this).click(function(e) {
			e.preventDefault();
			$('#quickCodeActivation').remove();
			var code = $(this).attr('rel'),
				quickCodeActivation = $('<div>', { id: 'quickCodeActivation' }).appendTo(document.body).data('url', $(this).attr('href'));
			if (code) {
				$.sfWindow.overlay();
				$.sfJson.load('/cart_json.php', function(data) {
					$.sfWindow.overlayDestroy();
					if (data.user && data.user.loggedIn == false) {
						var url = '/login.php?redirect_to=' + escape(window.location.pathname),
							buttons = {};
						buttons[i18n.iface.button.cancel] = function() { $(this).dialog('close'); };
						buttons[i18n.iface.button.login] = function() { window.location = url; $(this).dialog('close'); };
						$.sfWindow.alert(i18n.iface.info, i18n.codes.loginAlert, { buttons: buttons });
					} else {
						if (data.status == 1) {
							$.sfWindow.alert(i18n.iface.info, i18n.codes.activationSuccess);
							var url = $('#quickCodeActivation').data('url');
							if (!url.match(/#/)) window.location = url;
						} else {
							var alertMsg = fillVariables(i18n.codes.activationFailure, { code: data.status, status: i18n.codes.status[data.status] })
							$.sfWindow.alert(i18n.iface.alert, alertMsg);
						}
					}
				}, { action: 'activateCode', code: code });
			}
		});
	});
});

