﻿var larguraslider = 0;
var timeoutflash;
$(document).ready (
	function() {

		readCookies();

		$(".boxrecebe").sortable({
			opacity: 0.7,
			revert: true,
			appendTo: 'body',
			handle: $('.boxunitario h4'),
			connectWith: ['.boxrecebe'],
			start: function(e, x) {
				$('#' + x.item[0].id + ' a.abrirwidget').css('display', 'none');
				trocaFlash();
			},
			stop: function(e, x) { 
				restauraFlash();
				$('#' + x.item[0].id + ' a.abrirwidget').css('display', 'block');
				if (this.id == 'divNeston' && countElement('divUsuario') > 4) {
					Close(x.item[0].id);
				}

				if (this.id != document.getElementById(x.item[0].id).parentNode.id) {
					Slider(this.id == 'divNeston');
					saveCookies();
					
				}
			}
		});
		Slider(false);
		
		//Hover dos box de arrastar para exibir div com explicação
		var timeoutHoverBoxArrasta;
		var HoverBoxArrastaWorking = false;
		$('h4.titulo').hover(
			function(obj) {
				if(!HoverBoxArrastaWorking){
					var target = $(this).parent().parent().parent().attr('id'); 
					target = (target == 'divNeston' || target == 'BoxArrasta999' ? $('#alertdown') : $('#alertup'));
					
					var pagx =  obj.pageX - 124;
					if(pagx < 0) pagx = 10; 
					if(pagx + target.width() > $(document).width()){ pagx = $(document).width()-target.width(); }
					ShowTip(target, obj.pageY+25, pagx);
						
					timeoutHoverBoxArrasta = setTimeout(function(){
						HideTip();
					}, 10000);
				}
			},
			function() {
				HideTip();
				clearTimeout(timeoutHoverBoxArrasta);		
			}
		);
	}
 );

function ShowTip(target, top, left){
	HideTip();
	HoverBoxArrastaWorking = true;
	target.css('top', top);
	target.css('left', left);
	target.show();
}
function HideTip(){
	$('.dragalert').hide();
	HoverBoxArrastaWorking = false;
}
function Slider(incrementa) {
            
            if (larguraslider < 0) {
                if (incrementa) {
                    larguraslider += 182;
                }
                else {					
                    if(countElement('divUsuario') < 4)
						larguraslider -= 182;
                }
                freeze();
                $('#divNeston').animate({ marginLeft: larguraslider },800);
            }
            $("#boxslider").easySlider(
                {
                    prevId: 'btnAnt',
                    nextId: 'btnPrx',
                    prevText: 'Anterior',
                    nextText: 'Próximo',
                    speed: 800,
                    slide: 182,
                    start: 'trocaFlash();',
                    stop: 'restauraFlash();'
                }
            );
    }
function Close(elementId) {
    $('#divNeston').append($('#' + elementId));
    saveCookies();
    Slider(false);
}
function Add(elementId) {
    if (countElement('divUsuario') < 4) {
        $('#divUsuario').append($('#' + elementId));
        saveCookies();
        Slider(true);
    }
    else
        alert("Todos os espaços foram preenchidos!");
}
function readCookies() {
    readCookie('UserElements', 'divUsuario');
}
function saveCookies() {
    $.cookie('UserElements', serialize('divUsuario'), { expires: 99999 });
}
function countElement(containterName) {
    return serialize(containterName).split('[]').length - 1;
}
function readCookie(cookieName, containerName) {
    if ($.cookie(cookieName) != null) {
        var coluna1 = $.cookie(cookieName);
        coluna1 = coluna1.replace(/divNeston\[\]=/g, '');
        coluna1 = coluna1.replace(/divUsuario\[\]=/g, '');

        var coluna1 = coluna1.split('&');
        var div_id = '';
        for (var x = 0; x <= coluna1.length; x++) {
            div_id = coluna1[x];
            $('#' + containerName).append($('#' + div_id));
        }
    }
}
function serialize(s) {
    var serial = '';

    $('ul#' + s).children().each(function(i) {
        if (this.className == 'BoxArrasta') {
            serial += s + '[]=' + this.id + '&';
        }
    });
    serial = serial.substring(0, serial.length - 1);
    return serial;
};
function freeze() {
    clearTimeout(timeoutflash);
    trocaFlash();
    timeoutflash = setTimeout('restauraFlash();', 800 * 1.2);
}
