//http://threedubmedia.com/code/event/drag/#demos

/*
 * Posições do pop com os input e tag nuvens
 *
 * popPosiitonX = Posição do pop no eixo X - Vertical
 * popPosiitonY = Posição do pop no eixo Y - Horizontal
 *
 * P = Positivo
 * N = Negativo
 *
 *     Y N
 *      |
 * X N --- X P
 *      |
 *     Y P
 *
 *     popPosiitonX = 'N';
       popPosiitonY = 'N';
 **/

controleOnOff='off';
controleBalao='on';
popPositionY = 'N';
popPositionX = 'N';

jQuery(document).ready(function()
{
        //Valores iniciais da janela de chat
        jQuery("#alicehead").css("left","87%");
        jQuery("#alicehead").css("top","52%");
        jQuery("#pop").css("marginLeft","-680px");
        jQuery("#pop").css("marginTop","-320px");
});


jQuery(function($){
	var $div = $('body');

        //Pegar tamanho maximos
        if (navigator.appName.indexOf("Microsoft")!=-1)// IE MALDITO
        {
                w = document.documentElement.clientWidth;
                h = document.documentElement.clientHeight;
        }
        else
        {
                w = window.innerWidth;
                h = window.innerHeight;
        }


	jQuery('#alicehead')
		.drag("start",function( ev, dd ){
			dd.limit = $div.offset();
			dd.limit.bottom = h;//dd.limit.top + $div.outerHeight() - $( this ).outerHeight();
			dd.limit.right = w;//dd.limit.left + $div.outerWidth() - $( this ).outerWidth();
                        controleBalao='off';
		})
		.drag(function( ev, dd ){
                        $( this ).css({
				top: ev.clientY - 80,//Math.min( dd.limit.bottom, Math.max( 0, dd.offsetY ) ),
				left: ev.clientX - 50 //Math.min( dd.limit.right, Math.max( 0, dd.offsetX ) )
			}); 

                        //top e left
                        reposition( ev.clientY - 80, ev.clientX - 50 );
		},{handle:"#aliceimg"})
                .drag("end",function( ){controleBalao='on';});


                jQuery('#aliceimg').click(function(){
                        jQuery('#balao').fadeOut('fast');
                        if (controleOnOff == 'off')
                                openpop();
                        else if(controleOnOff == 'on')
                                closepop();
                });

                jQuery('#aliceimg').hover(
                        function(){
                                if (controleBalao=='on')
                                        if (controleOnOff!='on')
                                                jQuery('#balao').fadeIn('fast');
                        },
                        function(){
                                jQuery('#balao').fadeOut('fast');
                        }
                );


});

function openpop()
{
        if(navigator.appName=='Microsoft Internet Explorer')
        {
               jQuery('#pop').css('display','block');
        }
        else
        {
               jQuery('#pop').fadeIn('normal');
        }
        controleOnOff = 'on';
}

function closepop()
{
        if(navigator.appName=='Microsoft Internet Explorer')
        {
                jQuery('#pop').css('display','none');
        }
        else
        {
                jQuery('#pop').fadeOut('normal');
        }
        controleOnOff = 'off';
}

/*
 * Função para reposicionar objeto filho do objeto Drag
 * Caso o mesmo chegue nos limites definidos.
 * Se encontrar na lateral direita ele pula para esquerda, no topo pula para baixo e vice-versa
 **/
function reposition(aliceTop,aliceLeft)
{
        if( ! aliceTop)
                aliceTop = 0;

        //Width e Height do objeto drag - alice head
        aliceW = 120;
        aliceH = 160;

        //Width e Height do objeto filho - janela inputs
        popW = popW_global;//780 ou 430
        popH = 390;

        //Limites da tela com relação a area total do pop
        limitTop = (0 + popH) - 120;
        limitBottom = h - (popH - aliceH);
        limitLeft = (0 + popW) - 100;
        limitRight = w - (popW - aliceW);

        if ( aliceTop <= limitTop)
                popPositionY = 'P';//eixo positivo
        if ( aliceTop >= limitBottom)
                popPositionY = 'N';//eixo negativo

        if ( aliceLeft <= limitLeft)
                popPositionX = 'P';
        if ( aliceLeft >= limitRight)
                popPositionX = 'N';
            

        if (popPositionX == 'N')
                jQuery("#pop").css("marginLeft", (popW - (popW * 0.069))*(-1) +"px");
        else if (popPositionX == 'P')
                jQuery("#pop").css("marginLeft","60px");

        if (popPositionY == 'N')
                jQuery("#pop").css("marginTop","-320px");
        else if (popPositionY == 'P')
               jQuery("#pop").css("marginTop","120px");

//debugs
//jQuery("#mod_search_searchword").val(aliceTop+"-"+aliceLeft +" "+ popPosiitonX);
//jQuery("#mod_search_searchword").val((popW - (popW * 0.067))*(-1) +"px");

}

function scalePop(ord)
{
        if (ord == 1)//Ficar Grande
        {
                jQuery('#bt_aumentar').css('display','none');
                jQuery('#bt_diminuir').css('display','block');


                if (popPositionX == 'N')
                        jQuery('#pop').animate({width: "720px", marginLeft: "-680px"}, 500 );
                else if (popPositionX == 'P')
                        jQuery('#pop').animate({width: "720px", marginLeft: "60px"}, 500 );

                jQuery('#pop_fundo').animate({width: "712px"}, 500 );
                jQuery('#pop_sombra').animate({width: "722px"}, 500 );
                jQuery('#bts_pop').animate({marginLeft: "620px"}, 500 );
                jQuery('#tagnuvensDiv').delay(500).fadeIn(500);
                popW_global=720;
        }
        else if (ord == 0)//Ficar Pequeno
        {
                jQuery('#bt_diminuir').css('display','none');
                jQuery('#bt_aumentar').css('display','block');
                jQuery('#tagnuvensDiv').css('display','none');

                if (popPositionX == 'N')
                        jQuery('#pop').animate({width: "500px", marginLeft: "-450px"}, 500 );
                else if (popPositionX == 'P')
                        jQuery('#pop').animate({width: "500px", marginLeft: "50px"}, 500 );

                jQuery('#pop_fundo').animate({width: "480px"}, 500 );
                jQuery('#pop_sombra').animate({width: "480px"}, 500 );
                jQuery('#bts_pop').animate({marginLeft: "390px"}, 500 );
                popW_global=480;

        }

}



/*
window.onresize = function(e)
{
        initDragDrop();

        if ( objX >= __limitDragX ) objX = __limitDragX;
        else if ( objX <= 10 ) objX = 10;
        if ( objY >= __limitDragY ) objY = __limitDragY;
        else if ( objY <= 10 ) objY = 10;

        document.getElementById('alicehead').style.left = objX +'px';
        document.getElementById('alicehead').style.top = objY +'px';
}
*/

