/**
 * IDEALIAGroup srl
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@idealiagroup.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this module to newer
 * versions in the future.
 *
 * @category   IG
 * @package    IG_ImgPreview
 * @copyright  Copyright (c) 2010-2011 IDEALIAGroup srl (http://www.idealiagroup.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Riccardo Tempesta <tempesta@idealiagroup.com>
*/

var igImgPreviewConfig = new Object();
var igImgPreviewDiv = null;
var igImgPreviewDivInner = null;

function igImgPreviewShowFade()
{
	igImgPreviewDiv.setStyle({
		'visibility': 'visible',
		'opacity': 0.0
	});
	
	igImgPreviewDivInner.setStyle({
		'visibility': 'visible',
		'opacity': 0.0
	});
	
	new Effect.Opacity(igImgPreviewDiv, {
		duration: parseFloat(igImgPreviewConfig['effect/duration']),
		from: 0.0,
		to: parseFloat(igImgPreviewConfig['display/opacity'])
	});
	
	new Effect.Opacity(igImgPreviewDivInner, {
		duration: parseFloat(igImgPreviewConfig['effect/duration']),
		from: 0.0,
		to: 1.0
	});
}

function igImgPreviewShowBlind()
{
	igImgPreviewDiv.setStyle({
		'display': 'none',
		'opacity': parseFloat(igImgPreviewConfig['display/opacity'])
	});
	
	igImgPreviewDivInner.setStyle({
		'display': 'none',
		'opacity': parseFloat(igImgPreviewConfig['display/opacity'])
	});

	new Effect.BlindDown(igImgPreviewDiv, {
		duration: parseFloat(igImgPreviewConfig['effect/duration'])
	});
	
	new Effect.BlindDown(igImgPreviewDivInner, {
		duration: parseFloat(igImgPreviewConfig['effect/duration'])
	});
}

function igImgPreviewShowSlide()
{
	igImgPreviewDiv.setStyle({
		'display': 'none',
		'opacity': parseFloat(igImgPreviewConfig['display/opacity'])
	});
	
	igImgPreviewDivInner.setStyle({
		'display': 'none',
		'opacity': parseFloat(igImgPreviewConfig['display/opacity'])
	});

	new Effect.SlideDown(igImgPreviewDiv, {
		duration: parseFloat(igImgPreviewConfig['effect/duration'])
	});
	
	new Effect.SlideDown(igImgPreviewDivInner, {
		duration: parseFloat(igImgPreviewConfig['effect/duration'])
	});
}

function igImgPreviewShowNone()
{
	
}

function igImgPreviewImageLoaded()
{
	if ($('ig_imgpreview_loading')) $('ig_imgpreview_loading').setStyle({'display': 'none'});
	if ($('ig_imgpreview_img')) $('ig_imgpreview_img').setStyle({'display': 'block'});
}

function igImgPreviewShow(item)
{
	var size = igImgPreviewConfig['display/size'].split('x');
	var width = parseInt(size[0]);
	var height = parseInt(size[1]);

	igImgPreviewDiv = new Element('div', {'id': 'ig_imgpreview'});
	
	var htmlImg = "<img onload=\"igImgPreviewImageLoaded()\" id=\"ig_imgpreview_img\" style=\"display:none\" width=\"" + width + "\"  height=\"" + height + "\" src=\"" + item.rel + "\" alt=\"Loading...\" />";
	var htmlDsc = "<div>" + item.t + "</div>";
	var html = "<div id=\"ig_imgpreview_loading\" style=\"width: " + width + "px; height: " + height + "px;\">Loading, please wait...</div>";
	
	
	if (igImgPreviewConfig['general/description'] == 'top') 
		html += htmlDsc + htmlImg;
	else if (igImgPreviewConfig['general/description'] == 'bottom')
		html += htmlImg + htmlDsc;
	else
		html += htmlImg;
	
	igImgPreviewDivInner = new Element('div', {
		'id': 'ig_imgpreview_inner'
	}).update(html);
	
	Element.insert($$('body')[0], igImgPreviewDiv);
	Element.insert($$('body')[0], igImgPreviewDivInner);
	
	igImgPreviewDiv.setStyle({'position': 'absolute'});
	igImgPreviewDivInner.setStyle({'position': 'absolute'});
		
	igImgPreviewDiv.setStyle({'width': igImgPreviewDivInner.getWidth()+"px", 'height': igImgPreviewDivInner.getHeight()+"px"});	
	
	if (igImgPreviewConfig['display/corners-radius']) 
	{
		curvySettings = {
			tl: {
				radius: parseInt(igImgPreviewConfig['display/corners-radius'])
			},
			tr: {
				radius: parseInt(igImgPreviewConfig['display/corners-radius'])
			},
			bl: {
				radius: parseInt(igImgPreviewConfig['display/corners-radius'])
			},
			br: {
				radius: parseInt(igImgPreviewConfig['display/corners-radius'])
			},
			antiAlias: true
		};
		
		curvyCorners(curvySettings, "#ig_imgpreview");
	}
	
	switch (igImgPreviewConfig['effect/type'])
	{
		case 'fade':
			return igImgPreviewShowFade();
			
		case 'blind':
			return igImgPreviewShowBlind();
			
		case 'slide':
			return igImgPreviewShowSlide();
	}
	
	return igImgPreviewShowNone();
}

function igImgPreviewHide()
{
	if (igImgPreviewDiv) igImgPreviewDiv.remove();
	if (igImgPreviewDivInner) igImgPreviewDivInner.remove();
}

function igImgPreviewInit()
{	
	igImgPreviewOffsetX = parseInt(igImgPreviewConfig['display/mouse-offset-x']);
	igImgPreviewOffsetY = parseInt(igImgPreviewConfig['display/mouse-offset-y']);
	
	if (igImgPreviewOffsetX < 5) igImgPreviewOffsetX = 5;
	if (igImgPreviewOffsetY < 5) igImgPreviewOffsetY = 5;
	
	$$("a.ig_imgpreview").each(function (item) {
		Event.observe(item, 'mouseover', function(evt) 
		{
			igImgPreviewShow(item);
		});
		
		Event.observe(item, 'mousemove', function(evt) 
		{
			if (igImgPreviewDiv) igImgPreviewDiv.setStyle({'left': (igImgPreviewOffsetX+evt.pageX)+'px', 'top': (igImgPreviewOffsetY+evt.pageY)+'px'});
			if (igImgPreviewDivInner) igImgPreviewDivInner.setStyle({'left': (igImgPreviewOffsetX+evt.pageX)+'px', 'top': (igImgPreviewOffsetY+evt.pageY)+'px'});
		});
		
		Event.observe(item, 'mouseout', function(evt) 
		{
			igImgPreviewHide();
		});

		item.t = item.title;
		item.setAttribute('title', '');
	});
	
	$$("a.ig_imgpreview img").each(function (item) {
		item.setAttribute('title', '');
	});
}

Event.observe(window, 'load', function() {
	igImgPreviewInit();
});


