/**
 * Open popup window
 * 
 * @param {String} url
 */
function openPopup(url)
{
	window.open(url+(url.search(/\?/) != -1 ? '&' : '?')+'popupWindow', 'popup', 'width=625, height=500, scrollbars=1').focus(); 
	
	return false;
}

/**
 * Init image preview
 * 
 * @param {String} selector
 */
function initImagePreview(selector)
{
	$(function()
	{
		$(selector).each(function()
		{
			// check if qTip is already registered to element 
			if ($(this).data('qtip'))
			{
				return;
			}
			
			$parent = $(this).parent().parent();
			$code = $('.catalog-list-digest .code', $parent).html();
			
			$content = '<div class="imgPreviewContainer">'; 
			$content += '<p class="catalog-price">'+$('p.catalog-price', $parent).html()+'</p>'; 
			$content += '<div class="digest">'+$('.catalog-list-digest .digest', $parent).html()+'</div>'; 
			$content += '<img src="'+$(this).attr("rel")+'" alt="" />'; 
			$content += '</div>'; 
			
			$(this).qtip({
				content: {
					text: $content,
					title: {
						text: $('h3.catalog-list-title', $parent).text()+($code ? ' ['+$code+']' : '')
					}
				},
				show: { delay: 0, effect: {length: 0} },
				hide: { fixed: false, delay: 0, effect: {length: 0} },
				position: {
					corner: {
						target: 'rightMiddle',
						tooltip: 'leftMiddle'
					},
					adjust: {screen: true }
				},
				style: {
					width: { max: 295 },
					title: { 'background-color': '#CEEC28', 'padding': '3px 3px 7px 3px', 'text-align': 'center'},
					background: '#FFFFFF',
					color: 'black',
					padding: 0, 
					textAlign: 'center',
					border: {
						width: 10,
						radius: 10,
						color: '#CEEC28'
					},
					tip: false
				}
			});
		});
	});
}

/**
 * Init tooltip
 * 
 * @param {String} selector
 */
function initTooltip(selector)
{
	$(function()
	{
		$(selector).each(function()
		{
			// check if qTip is already registered to element 
			if ($(this).data('qtip'))
			{
				return;
			}
		
			$(this).qtip({
				content:
				{
					text: false
				},
				show: { delay: 0, effect: {length: 0} },
				hide: { delay: 0, effect: {length: 0} },
				position: {
					corner: {
						target: 'topMiddle',
						tooltip: 'bottomMiddle'
					}
				},
				style:
				{
					name: 'blueTooltip'
				}
			});
		});
	});
}

/**
 * Init label
 * 
 * @param {String} selector
 */
function initLabel(selector)
{
	$(function()
	{
		if (!$(selector).attr("value"))
		{
			$(selector).labelify({ labelledClass: "input-label" });
		}
	});
}

/**
 * Init password label
 * 
 * @param {String} selector
 */
function initPasswordLabel(selectorPassword, selectorEmpty)
{
	$(function()
	{
		$(selectorEmpty).show();
		$(selectorPassword).hide();
	
		$(selectorEmpty).focus(function() {
			$(selectorEmpty).hide();
			$(selectorPassword).show();
			$(selectorPassword).focus();
		});
		$(selectorPassword).blur(function() {
			if($(selectorPassword).val() == '') {
				$(selectorEmpty).show();
				$(selectorPassword).hide();
			}
		});
	});
}

/**
 * Init basket preview
 * 
 * @param {String} selector
 * @param {String} content
 */
function initBasketPreview(selector, content)
{
	$(function()
	{
		$(selector).qtip(
		{
			content: content,
			show: { delay: 100, effect: {length: 300, type: 'slide'} },
			hide: { fixed: true, delay: 200, effect: {length: 300, type: 'slide'} },
			position: {
				corner: { target: 'leftMiddle', tooltip: 'rightTop' }
			},
			style: {
				border: { width: 10, color: '#F06917', radius: 10 },
				padding: 0,
				tip: { corner: 'rightTop'},
				width: 350
			}
		});
	});
}

// init tooltip styles
$.fn.qtip.styles.blueTooltip = {
	background: '#E00A00',
	color: 'white',
	border:
	{
		width: 5,
		radius: 5,
		color: '#E00A00'
	},
	padding: 2, 
	tip: 'bottomMiddle'
};

//init labels
initLabel("#search-bar input.text");
initLabel("#user-login-login");
initPasswordLabel("#user-login-password", "#user-login-password-empty");

// init tooltip
initTooltip('.tooltip[title]');

//init image preview
initImagePreview('.catalog-box-item a.catalog-image-link, .catalog-recommend a.catalog-image-link');

//init popup link
$(function(){ $('a.linkPopup').click(function(){return openPopup(this.href);});});
