﻿var __carouselStarted = false;
var __lastPicture = null;
var FirstPicture = null;
function pageLoad(sender, args) {
    if ( jQuery.trim(btnComment_ClientId).length > 0 ) {
        jQuery('#' + btnComment_ClientId).click(function(){
            var txtComment = document.getElementById(txtComment_ClientId);
            if ( txtComment ) {
                if ( jQuery.trim(txtComment.value) <= 0 ) {
                    alert('- Por favor insira um comentário para poder enviá-lo.');
                    try { txtComment.focus(); } catch ( e ) { }
                    return false;
                } else {
                    this.value = "Enviando...";
                    this.disabled = true;
                    __doPostBack(btnComment_UniqueId, document.getElementById(txtComment_ClientId).value);
                    return true;
                }
            } else {
                return false;
            }
        });
    }
    
    try {
        jQuery('.comment_limit').maxlength({
            'feedback' : '.comment_charsleft'
        });
    } catch ( e ) {
    }
}
jQuery(document).ready(function(){
    tb_init('a.thickbox, area.thickbox, input.thickbox');
    imgLoader = new Image();
    imgLoader.src = "/images/Eventos/loading.gif";
    
    if ( typeof(FirstPicture) != "undefined" && FirstPicture ) {
        showLoading();
        setTimeout(function() {
            openPicture(true, document.getElementById(FirstPicture[0]), FirstPicture[1], FirstPicture[2]);
        }, 0);
    } else {
        try {
            hideLoading();
        } catch ( e ) {
        }
    }
    jQuery(".jcarousel-list.jcarousel-list-horizontal").css("width", "100%");
    try {
        jQuery("img.eventPicture").contextMenu("alternativaMenu", {
            onContextMenu: function(e) {
                return false;
            }
        });
    } catch ( e ) {
    }
});
function showEvent(eventId, title) {
    //__doPostBack('EXPOSITOR_SHOW_EVENT', eventId);
    //setTimeout(function(){document.getElementById('aShowEvent').click();}, 0);
    var sHeight = -1;
    if ( window.innerHeight ) {
        sHeight = window.innerHeight;
    } else if ( document.documentElement.clientHeight && document.documentElement.clientHeight > 0 ) {
        sHeight = document.documentElement.clientHeight;
    } else {
        sHeight = document.body.clientHeight;
    }
    if ( sHeight <= 0 ) {
        sHeight = 666;
    }
    var nHeight = Math.round(sHeight*0.90);
    var newUrl = ExpositorUrl + "?id=" + eventId + "&header=false&autosize=true&TB_iframe=true&height="+nHeight+"&width=782";
    jQuery("#aShowEvent").attr("href", newUrl);
    jQuery("#aShowEvent").attr("title", title);
    jQuery("#aShowEvent").click();
}
function startCarousel() {
    if ( !__carouselStarted ) {
        jQuery("#expositor").jcarousel({        
            scroll: 1,
            visible: 5,
            initCallback: expositor_initCallback,
            buttonNextHTML: null,        
            buttonPrevHTML: null    
        });
        __carouselStarted = true;
    }
}
function showLoading(){
    var loader = document.getElementById("loader");
    var loading = document.getElementById("loading");
    
    if ( document.documentElement.scrollWidth > 0 ) {
        loader.style.top = "0px";
        loader.style.left = "0px";
        loader.style.width = document.documentElement.scrollWidth + "px";
        loader.style.height = document.documentElement.scrollHeight + "px";
    }
    
    loader.style.display = "block";
    loading.style.display = "block";
}
function hideLoading() {
    var loader = document.getElementById("loader");
    var loading = document.getElementById("loading");
    
    loader.style.display = "none";
    loading.style.display = "none";
}
function openPicture(isFirst, sender, description, url) {
    try {
        if ( __lastPicture ) {
            if ( __lastPicture.DIV ) {
                __lastPicture.DIV.style.display = "none";
            }
            
            if ( __lastPicture.SPAN ) {
                __lastPicture.SPAN.style.display = "none";
            }
            
            __lastPicture.PIC.className = __lastPicture.PIC.className.replace(/ *selected */gi, "");
        }
        
        var picture = document.getElementById(expositor_img_ClientId);
        if ( isFirst ) {
            picture.onload = function(){
                hideLoading();
                setPicture(sender, description, url);
                jQuery(".imageToLoad").each(function() {
                    this.src = this.getAttribute("img_url");
//                    var p = this.parentNode;
//                    var src = this.getAttribute("img_url");
//                    this.outerHTML = "";
//                    var img = new Image();
//                    img.src = src;
//                    p.appendChild(img);
//                    img.style.display = "block";
                });
            }
        } else {
            picture.onload = function(){
                hideLoading();
                setPicture(sender, description, url);
            }
        }
        if ( picture.src != url ) {
            showLoading();
            picture.src = url;
            picture.title = sender.title;
        }
        return false;
    } catch ( e ) {
        setTimeout(function() {
            openPicture(isFirst, sender, description, url);
        }, 50);
    }
}

function getX(obj) {
  var curleft = 0;

  if(obj.offsetParent) {
    while(1) {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
  } else if(obj.x) {
    curleft += obj.x;
  }

  obj.style.position = "static";

  return curleft;
}

function getY(obj) {
  var curtop = 0;

  if(obj.offsetParent) {
    while(1) {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
  } else if(obj.y) {
    curtop += obj.y;
  }

  return curtop;
}

function setPicture(sender, description, url, rep) {
    try { 
        var description = document.getElementById(description);
        var divDescription = document.getElementById("divDescription");
        var picture = document.getElementById(expositor_img_ClientId);
        if ( !divDescription ) {
            divDescription = document.createElement("DIV");
        }
        if ( description && jQuery.trim(description.innerHTML).length > 0 ) {
            picture.parentNode.appendChild(divDescription); //document.body.appendChild(divDescription);
            picture.parentNode.appendChild(description); //document.body.appendChild(description);
            description.className = "picture_description_text";
            divDescription.className = "picture_description";
            divDescription.id = "divDescription";
            
            if ( document.all ) {
                divDescription.style.position = "absolute";
                divDescription.style.left = getX(picture) + 15 + "px";
                divDescription.style.top = getY(picture) + picture.offsetHeight - 10  + "px";
                divDescription.style.width = picture.offsetWidth + "px";
                divDescription.title = description.innerHTML;
                
                description.style.position = "absolute";
                description.style.left = getX(picture) + 25 + "px";
                description.style.top = getY(picture) + picture.offsetHeight - 20 + "px";
                description.style.width = picture.offsetWidth - 20 + "px";
                description.title = description.innerHTML;
            } else {
                divDescription.style.position = "absolute";
                divDescription.style.left = getX(picture) - 1 + "px";
                divDescription.style.top = getY(picture) + picture.offsetHeight - 25  + "px";
                divDescription.style.width = picture.offsetWidth + "px";
                divDescription.title = description.innerHTML;
                
                description.style.position = "absolute";
                description.style.left = getX(picture) + 5 + "px";
                description.style.top = getY(picture) + picture.offsetHeight - 35 + "px";
                description.style.width = picture.offsetWidth - 10 + "px";
                description.title = description.innerHTML;
            }
            divDescription.style.display = "block";
            description.style.display = "block";   
        }
            
        sender.className += " selected";
        
        if ( !rep ) {
            __lastPicture = {PIC:sender,DIV:divDescription,SPAN:description};
        }
        
        if ( !document.all && !rep ) {
            setTimeout(function() {
                setPicture(sender, description.id, url, true);
            }, 0);
        }
    } catch ( e ) {
        setTimeout(function() {
            setPicture(sender, description, url);
        }, 50);
    }
}
function gotoComment() {
    if ( document.getElementById(txtComment_ClientId) ) {
        document.getElementById('comentar').focus();
        document.getElementById(txtComment_ClientId).focus();
    }
    return false;
}
/** * We use the initCallback callback * to assign functionality to the controls */
function expositor_initCallback(carousel) {    
    jQuery('#expositor-control a').bind('click', function() {        
        carousel.scroll(jQuery.jcarousel.intval(this.id));        
        return false;    
    });    
    
    jQuery('.jcarousel-scroll select').bind('change', function() {        
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);        
        return false;    
    });    
    
    jQuery('#expositor-next').bind('click', function() {        
        carousel.next();        
        return false;    
    });    
    
    jQuery('#expositor-prev').bind('click', function() {        
        carousel.prev();        
        return false;    
    });
};

jQuery(document).ready(function() {
    jQuery("#close").click(tb_remove);
});