// JavaScript Document

var mousex = 0;
var mousey = 0;
var is_over = false;
var is_info_loaded = false;
var infoimg_num = 4;
var infoimg_counter = 0;

this.imagePreview = function(){
	/* CONFIG */

		yOffset = 10;
		xOffset = 30;

		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result

	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = $(this).attr('title');
		$(this).attr('title',''); //this.title = "";
		var c = (this.t != "") ? "<br/>" + this.t : "";
		
		var imgobj = $(this).children('img')[0];
		
		$("body").append("<p id='preview'><img src='"+ imgobj.src +"' alt='Image preview' /></p>"); //"+ c +"
		$("#preview").css({top:0,left:0});
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		$(this).attr('title',this.t); //this.title = this.t;
		$("#preview").remove();
    });
	$("a.preview").mousemove(function(e){
	    if ($("#preview").length > 0) {
		    var topval = e.pageY - yOffset;
		    var leftval = e.pageX + xOffset;

		    var scrtop = $(window).scrollTop();
		    var scrsize = getScreenSize();

		    if (topval + $("#preview")[0].offsetHeight > scrtop + scrsize.h) {
		        topval = e.pageY - yOffset - $("#preview")[0].offsetHeight;
			}

			if (leftval + $("#preview")[0].offsetWidth > scrsize.w) {
		        leftval = e.pageX - xOffset - $("#preview")[0].offsetWidth;
			}

			$("#preview")
				.css("top",(topval) + "px")
				.css("left",(leftval) + "px");
		}
	});
};

$(document).ready(function(){

    imagePreview();

	/*$(document).mousemove(function(e){
    	//$('#status').html(e.pageX +', '+ e.pageY);
    	mousex = e.pageX;
    	mousey = e.pageY;
    	
    	if (is_over) {
    		var zoomer = $('#imgzoomer')[0];

			var y = mousey - (zoomer.offsetHeight + 20);
			
			if (y < $(window).scrollTop() -20) {
			    y = mousey + 20;
			}
    		
    		$(zoomer).css({
				'top':(y) +'px',
				'left': (mousex - (zoomer.offsetWidth/2)) +'px'
			});
    		//popup.style.left = (mousex - (zoomer.offsetWidth + 60)) +'px';
		}
   	});

    $("ul.gallery li img").hover(
		function() {
        	is_over = true;
        	//$('#imgzoomer').empty();
        	$('#imgzoomer').html('<img src="' + $(this).attr('src') + '" alt="" />');
        	$('#imgzoomer').show();
        	//$('#imgzoomer').css('height',this.offsetHeight + 'px');
    	},
    	function() {
        	is_over = false;
        	$('#imgzoomer').hide();
        	$('#imgzoomer').css({'top':0,'left':0});
		}
	);*/
	
	$('input.button-info-tec').click(function(){
	    openInfoBox();
	    return false;
	});
	
	//$('#info-box div.content').jScrollPane({scrollbarWidth:10});

//Larger thumbnail preview
/*$("ul.gallery li").hover(function() {
	$(this).css({'z-index' : '90'});
	$(this).find('img').addClass("hover").stop()
		.animate({
			marginTop: '-110px',
			marginLeft: '-110px',
			top: '50%',
			left: '50%',
			width: '541px',

			padding: '20px'
		}, 200);

	} , function() {
	$(this).css({'z-index' : '0'});
	$(this).find('img').removeClass("hover").stop()
		.animate({
			marginTop: '0',
			marginLeft: '0',
			top: '0',
			left: '0',
			width: '120px',
			height: '78px',
			padding: '2px'

		}, 400);
});*/

//Swap Image on Click

});

function openInfoBox() {
	if (!is_info_loaded) {
	    $.ajax({
	        type: "POST",
	        url: "aj_dialer.php",
	        dataType: 'json',
	        data: 'action=getinfo',
	        //Evento di riuscita
	        success: function(ret) {
				if (ret.msg_type == 'ok') {
				    $('#info-box').show();
					$('#info-box div.container').html(ret.data);
					$('#info-box div.container img').load(function() {
					    infoimg_counter++
					    if (infoimg_counter == infoimg_num) {
							$('#info-box div.content').jScrollPane();
						}
					});
					$('#info-box a.close').click(function() {
					    $('#info-box').hide();
					    return false;
					});
				}
	        }
	    });
	    
	    is_info_loaded = true;
    } else {
        $('#info-box').show();
	}
}

function getScreenSize() {
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    /*window.alert( 'Width = ' + myWidth );
    window.alert( 'Height = ' + myHeight );*/

    ret = new Object();
    ret.w = myWidth;
    ret.h = myHeight;

    return ret;
}
