﻿/*

www.asapinformatica.com
A.S.A.P. Informatica di Stefano Porrino
Software and website development
Sviluppo Software e Soluzioni internet

*/

var asapImgIsLoaded = 0;
var asapAlreadyGo = 0;

// Record browser tyle
var Browser = {
    find: function() {
        // Get the user agent to get the browser identifier
        var useragent = navigator.userAgent;
        // Match with the keyword to find out the browser type
        this.isKHTML = /Konqueror|Safari|KHTML/.test(useragent);
        this.isGecko = (/Gecko/.test(useragent) && !this.isKHTML);
        this.isOpera = /Opera/.test(useragent);
        this.isMSIE = (/MSIE/.test(useragent) && !this.isOpera);
        this.isMSIE7 = this.isMSIE && !(/MSIE 6\./.test(useragent) && !this.isOpera);
    }
}
// Bubble function
function bubble(id, left, top, delay, anispeed) {

    // exists id?
    if (document.getElementById(id) == undefined) return;

    // Cache the bubble object to speed up the process
    var obj = $("#" + id);

    // Get current width and height
    var width, height, w, h;
    width = obj.css("width");
    height = obj.css("height");
    // strip "px" to get the pixel value
    //alert(width);
    w = width.split("px")[0];
    h = height.split("px")[0];

    // shrink it to 0,0, and move it to target position
    obj.css({ opacity: 0, visibility: "visible", display: "block", width: "0px", height: "0px", left: left, top: top });
    // Hide the object
    obj.find("p").hide();
    // Set the timer to trigger the animation
    setTimeout(
	function() {
	    // To save new top and left
	    var left, top;
	    // Set all objects
	    obj.each(
		function() {
		    // Get current left and 
		    left = $(this).offset().left - (w / 2);
		    left = left + "px";
		    top = $(this).offset().top - (h / 2);
		    top = top + "px";
		    // Animate the object. Animate opacity and size, adjuct left and top also to keep object
		    // center. jQuery Effect.scale function is having bug that fails to keep the position
		    // after animation, so manually getting that effect
		    $(this).animate(
			{
			    width: width,
			    height: height,
			    opacity: 1,
			    left: left,
			    top: top
			},
			anispeed,
			function()	// This is callback function at the end of animation to make text visible
			{
			    $("p", this).slideDown(200);
			});
		});
	}, delay);
}

function asapGo() {
    //alert("qui");
    if (asapAlreadyGo == 1) return;
    asapAlreadyGo = 1;
    
    var scrW = screen.availWidth;
    var scrH = screen.availHeight;
    //alert(scrH);
    var hPlus = 0;
    if (scrH > 780) hPlus = 100;
    var circleW = 282;
    var scrMiddle = scrW / 2;
    if (scrMiddle < 500) scrMiddle = 500;

    bubble('asap1-bubble', scrMiddle - circleW, 300+hPlus, 200, 2100);
    bubble('asap2-bubble', scrMiddle, 430 + hPlus, 400, 2100);
    bubble('asap3-bubble', scrMiddle + circleW, 300 + hPlus, 600, 2100);
}

$(document).ready(function() {
    //alert("StartBubble");
    // Hack for IE6
    Browser.find();
    if (Browser.isMSIE && !Browser.isMSIE7) {
        $(".shot1").attr("src", "_autostima/movimento.png");
        $(".shot2").attr("src", "_autostima/mente.png");
        $(".shot3").attr("src", "_autostima/salute.png");
    }

    var scrW = screen.availWidth;
    var circleW = 282;
    var scrMiddle = scrW / 2;
    if (scrMiddle < 500) scrMiddle = 500;

    // solo x befree logo
    if (document.getElementById('divLogo') != null) {
        // 253 = logo width
        document.getElementById('divLogo').style.left = scrMiddle - 253 / 2 + "px";
    }

    // Set bubble
    // ID, left, top, delay, animation delay
    //window.setTimeout('asapGo();', 3000);
    //bubble('downloads-bubble', 800, 300, 800, 1100);
});

//       function MM_preloadImages() { //v3.0
//           //alert("start-pre-load");
//           var d = document; if (d.images) {
//               if (!d.MM_p) d.MM_p = new Array();
//               var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
//               for (i = 0; i < a.length; i++) {
//                   //if (a[i].indexOf("#") != 0) {
//                   d.MM_p[j] = new Image;
//                   d.MM_p[j].onLoad = imgOnLoad(a[i]);
//                   d.MM_p[j].src = a[i];
//                   j++;

//                       //alert(a[i]);
//                   //}
//               }
//           }
//           //alert("end-pre-load");
//       }
////       function asapImgPreload(fName) {
//           var img = new Image;
//           img.onLoad = imgOnLoad(fName);
//           img.src = fName;
//       }
function asapImgPreload(src) {
    //alert("pre=" + src);
    $('<img />').load(function() {
        //alert("pre2=" + src);
        //$(this).show();
        //console.log('loaded %s!', src);
        asapImgIsLoaded--;
        if (asapImgIsLoaded <= 0) {
            //alert("immagini caricate");
            asapGo();
        }
    })
    .attr('src', src);
    //.hide()
    //.appendTo('body');
}

//       function imgOnLoad(fName)
//       {
//           alert(fName);
//           asapImgIsLoaded = true;
//       }
window.setTimeout("asapGo();", 3000);
asapImgIsLoaded = 4;

asapImgPreload('http://www.personaltraining-coaching.com/_autostima/logo-be-free.gif');
asapImgPreload('http://www.personaltraining-coaching.com/_autostima/movimento.png');
asapImgPreload('http://www.personaltraining-coaching.com/_autostima/mente.png');
asapImgPreload('http://www.personaltraining-coaching.com/_autostima/salute.png');
asapImgPreload('http: //www.personaltraining-coaching.com/_autostima/elica3.gif');
    //MM_preloadImages('http://www.personaltraining-coaching.com/_autostima/logo-be-free.gif', 'http://www.personaltraining-coaching.com/_autostima/movimento.png', 'http://www.personaltraining-coaching.com/_autostima/mente.png', 'http://www.personaltraining-coaching.com/_autostima/salute.png');

