﻿
function common_getParameterByName(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function common_getUrlOnly()
{
    if (window.location.href.indexOf('?') == -1)
        return window.location.href;
        
    return window.location.href.substr(0, window.location.href.indexOf('?'));
}

function common_beginProgress(jqElement, inFrame){
    if (inFrame == null)
        inFrame = false;
        
    var bg = 'url(../images/please-wait.gif) no-repeat scroll center #FAFAFA';
    if (inFrame){
        bg = 'url(../../images/please-wait.gif) no-repeat scroll center #FAFAFA';
    }        
    jqElement.css({
                    width : jqElement.width(),
                    height : jqElement.height(),
                    background: bg
                });
    jqElement.children().css({
                                display:'none',
                                opacity:0
                            });
}

function common_endProgress(jqElement, oldWidth, oldHeight, oldBackground, oldDisplay){
    jqElement.css({
                            width: oldWidth,
                            height: oldHeight,
                            'background' : oldBackground
                    });
    jqElement.children().css({
                                display:oldDisplay,
                                opacity:1
                            });
}

function common_refreshPageByGet(){
    var curLoc = window.location + '';
    if (curLoc.indexOf('?') == -1){
        window.location = curLoc.replace('\?refresh', '') + '?refresh';
    } else {
        window.location = curLoc.replace('\&refresh', '') + '&refresh';
    }
}








/////////////////////////////////////////////////////////////////
var var_staffAtWork_maxCommentLength = 100;

var var_staffAtWork_HandlerPath = "../controls/staffatwork/gallery/StaffAtWorkImagesListHandler.ashx";

function staffAtWork_setComment(commentSection, imageId, comment){
    $.get(var_staffAtWork_HandlerPath, { Command: "comment", ImageID: imageId, Comment: comment},
                    function(result) 
                    { 
                        if (result == 'Success'){
                            
                        }else{
                            alert('Failed to save last comment.');
                        }
                        
                    }); 
}

function staffAtWork_ImageAdded(listingId, thumbnailPath, path, alt, imageId, comment){
    if (comment == null)
        comment = '';

    var newItem = $("<div class='staffatwork-image-item' style='display:none;'><div class='staffatwork-image-item-inner'></div>");
    var newItemImg = $("<div class='img-container'><img/></div>");
    newItemImg.append("<input type='hidden' />");
    newItemImg.find(":input").val(imageId);
    
    var imgLowerSection = $("<div class='staffatwork-img-lower clearfix'><div class='img-del-x'></div><a href='javascript:void(0);'>Comment</a></div>");
    var commentSection = $("<div class='popup-container'></div>")
    .append("<div class='img-comment'>" + comment + "</div>");
    
    newItemImg.find('img').attr('src', thumbnailPath);
    
    imgLowerSection.append(commentSection);
    
    newItem.find('.staffatwork-image-item-inner')
            .append(newItemImg)
            .append(imgLowerSection);

    $('#' + listingId).append(newItem);
    
    staffAtWork_AddHandlers(newItem, listingId);
    
    newItem.animate({opacity: 'show'}, 1000);
    staffAtWork_CheckIfNoImages(listingId);
}

function staffAtWork_CheckIfNoImages(listingId){
    if ($('#' + listingId + ' .staffatwork-image-item').size() == 0){
        $('#' + listingId).html('');
        $('#' + listingId).append($('<div class="no-items"><h4>You have no photos in this section.</h4></div>'));
    } else {
        $('#' + listingId + ' .no-items').remove();
    }
}

function staffAtWork_InitListingItems(listingId){
    $(document).ready(function(){
        staffAtWork_CheckIfNoImages(listingId);

        $('#' + listingId + ' .staffatwork-image-item').each(function(index, value){
            staffAtWork_AddHandlers(value, listingId);
        });
    });
    
}

function staffAtWork_AddHandlers(listingItem, listingId){
    var listingItem = $(listingItem);
    var imgLowerSection = $(listingItem).find('.staffatwork-img-lower');
    var deleteButton = imgLowerSection.find('.img-del-x');
    var imageId = listingItem.find(':input').val();
    var listingItemInner = listingItem.children();
    var commentSection = listingItemInner.find('.popup-container');
    
    deleteButton.unbind('click');
    deleteButton.click(function(){
        common_beginProgress(listingItemInner);
        
        $.get(var_staffAtWork_HandlerPath, { Command: "delete", ImageID: imageId },
                    function(result) 
                    { 
                        if (result == 'Success'){
                            listingItem.animate({opacity: 'hide'}, 1000, 'linear', function() {
                                    listingItem.remove();
                                  staffAtWork_CheckIfNoImages(listingId);
                              });
                        } else {
                            //alert(result);
                        }
                    }); 
    });
    
    imgLowerSection.find('a').click(function(event){
        event.stopPropagation();
        popupInput(commentSection, 
                    commentSection.find('.img-comment').html(), 
                    var_staffAtWork_maxCommentLength, 
                    function(inputValue){ 
                                            staffAtWork_setComment(commentSection.find('img-comment'), imageId, inputValue);
                                            commentSection.find('.img-comment').html(inputValue);
                                        }
                    );
    });
}


/////////////////////////////////////////////////////////////////////////

function sawGallery_InitGallery(galleryId){
    $(document).ready(function(){
        sawGallery_CheckIfNoImages(galleryId);

        $('#' + galleryId + ' .saw-gal-image-item').each(function(index, value){
            sawGallery_initGalleryItem(value, galleryId);
        });
    });
    
}

function sawGallery_CheckIfNoImages(galleryId){
    if ($('#' + galleryId + ' .saw-gal-image-item').size() == 0){
        $('#' + galleryId).html('');
        $('#' + galleryId).append($('<div class="no-items"><h4>There are no photos.</h4></div>'));
    } else {
        $('#' + galleryId + ' .no-items').remove();
    }
}

function sawGallery_initGalleryItem(item, galleryId){
    var item = $(item);
    var thumbImageContainer = item.find('.img-container');
    var imageNumber = item.find(':input').val();
    var initialWidth = thumbImageContainer.width();
    var initialHeight = thumbImageContainer.height();
    
    thumbImageContainer.click(function(event){
        event.stopPropagation();
        sawGalleryShow_begin(imageNumber);
    });
    
//    thumbImageContainer.hover(function() {
//        sawGallery_enlargeImage(thumbImageContainer, initialWidth, initialHeight, initialWidth + 30, initialHeight + 30, 40);
//	}, function() {
//	    sawGallery_minimizeImage(thumbImageContainer, initialWidth, initialHeight, initialWidth + 30, initialHeight + 30, 200);
//    });
}

function sawGallery_enlargeImage(thumbImageContainer, initialWidth, initialHeight, enlargedWidth, enlargedHeight, duration){
    var leftToCenter = initialWidth / 2 - enlargedWidth / 2;
    var topToCenter = initialHeight / 2 - enlargedHeight / 2;
    if (topToCenter < -200)
        topToCenter = -200;
    
    thumbImageContainer.parent().animate({'z-index': 100}, duration);
    thumbImageContainer.css({
                                                                left:0,
                                                                top:0,
                                                                width:initialWidth,
                                                                height:initialHeight,
                                                                border: '1px solid #d0d0d0'
                                                            }).stop().animate({
                                                                left: leftToCenter,
                                                                top: topToCenter,
                                                                width:enlargedWidth,
                                                                height:enlargedHeight
	                                                        }, { "duration": duration}); 
	thumbImageContainer.find('img').animate({width: enlargedWidth, height:enlargedHeight}, duration);
}

function sawGallery_minimizeImage(thumbImageContainer, initialWidth, initialHeight, enlargedWidth, enlargedHeight, duration){
    
    thumbImageContainer.css({border: 'none'}).stop().animate({
        left: 0,
        top: 0,
        width: initialWidth,
        height: initialHeight
	}, duration); 
	thumbImageContainer.find('img').animate({width: initialWidth, height:initialHeight}, duration);
	thumbImageContainer.parent().animate({'z-index': 1}, duration);
}

////////////////////////////////////////
//Staff At Work Gallery Show
///////////////////////////////////////
var var_sawGalleryShow_isLoadingImagePage = false;
var var_sawGalleryShow_root;
var var_sawGalleryShow_seed = 1;
var var_sawGalleryShow_curImageNumber;
var var_sawGalleryShow_imagesPage = [];
var var_sawGalleryShow_imagesPageMinItemsBeforePreLoad = 7;
var var_sawGalleryShow_imagesPageSize = 21;
var var_sawGalleryShow_imagesTotalCount = 0;
var var_sawGalleryShow_slideAnimDuration = 300;
var var_sawGalleryShow_fbAppId = 1;

var const_sawGalleryShow_handlerPath = '../controls/staffatwork/gallery/show/GalleryShowPageLoader.ashx';

function sawGalleryShow_synchronize(imagesListingContainer, images){
    imagesListingContainer = $(imagesListingContainer);
    var imageItems = imagesListingContainer.find('.image-item');
    
    var minCurImageNumber = imageItems.first().attr('imageNumber');
    if (minCurImageNumber == null)
        minCurImageNumber = -2;
    else 
        minCurImageNumber = minCurImageNumber - 0;
    var maxCurImageNumber = imageItems.last().attr('imageNumber');
    if (maxCurImageNumber == null)
        maxCurImageNumber = -1;
    else
        maxCurImageNumber = maxCurImageNumber - 0;
    
    var minReqItemNumber = images[0].imageNumber - 0;
    var maxReqItemNumber = images[images.length - 1].imageNumber - 0;
    
    var i;
    var itemNumber;
    imageItems.each(function(i, item){
        itemNumber = $(item).attr('imageNumber') - 0;
        if (itemNumber < minReqItemNumber || itemNumber > maxReqItemNumber){
            $(item).remove();
        }
    });
    
    var beginItems = [];
    var endItems = [];
    
    for (i = 0; i < images.length; i ++){
        if (images[i].imageNumber < minCurImageNumber){
            beginItems[beginItems.length] = sawGalleryShow_createShowItem(images[i].imageNumber, images[i].imageLink, images[i].userName, images[i].postedDate, images[i].comment, 0, images[i]);
        } else if (images[i].imageNumber > maxCurImageNumber){
            endItems[endItems.length] = sawGalleryShow_createShowItem(images[i].imageNumber, images[i].imageLink, images[i].userName, images[i].postedDate, images[i].comment, 0, images[i]);
        }
    }
    
    for (i = 0; i < beginItems.length; i ++){
        if (i == 0){
            imagesListingContainer.prepend(beginItems[i]);
            continue;
        }
        
        beginItems[i - 1].after(beginItems[i]);
        
        
    }
    
    for (i = endItems.length - 1; i >= 0; i --){
        if (i == endItems.length - 1){
            imagesListingContainer.append(endItems[i]);
            continue;
        }
        
        endItems[i + 1].before(endItems[i]);
        
    }
    
}
function sawGalleryShow_getImagesContainer(){
    return var_sawGalleryShow_root.find('.saw-show-content');
}

function sawGalleryShow_setCurrentImage(imageNumber, duration){
    var curItem;
    sawGalleryShow_getImagesContainer().find('.image-item').each(function(i, item){
        curItem = $(item);
        
        var imageSourceSplitted = curItem.find('img').attr('src').split('/');
        
        if (imageNumber == curItem.attr('imageNumber') - 0){
            var curUrl = common_getUrlOnly();
            curItem.find('.fb-like-area').html('').append(sawGalleryShow_createLike(curUrl.slice(0, curUrl.indexOf('/staff/')) + '/staff-at-work/' + imageSourceSplitted[imageSourceSplitted.length - 1].replace('.', '-') + '/view.aspx'));
        } else {
            curItem.find('.fb-like-area').html('');
        }
        if (parseInt(curItem.attr('imageNumber')) != imageNumber)
            curItem.hide();
        else {
            curItem.show();
            var showInfo = var_sawGalleryShow_root.find('.saw-show-info');
            showInfo.find('.first-line, .second-line, .fb-like-area').remove();
            
            showInfo.find('.progress-bar-outer').after(curItem.find('.first-line, .second-line').clone())
            .before(curItem.find('.fb-like-area').clone());
        }
         
    });
    
    var_sawGalleryShow_curImageNumber = parseInt(imageNumber);
    
    if (var_sawGalleryShow_curImageNumber - var_sawGalleryShow_imagesPage[0].imageNumber < var_sawGalleryShow_imagesPageMinItemsBeforePreLoad && 
            var_sawGalleryShow_imagesPage[0].imageNumber > 1){
            
        sawGalleryShow_reloadImagesPage(var_sawGalleryShow_curImageNumber, var_sawGalleryShow_imagesPageSize, var_sawGalleryShow_seed);
        
    } else if (var_sawGalleryShow_imagesPage[var_sawGalleryShow_imagesPage.length - 1].imageNumber - var_sawGalleryShow_curImageNumber < var_sawGalleryShow_imagesPageMinItemsBeforePreLoad &&
                var_sawGalleryShow_imagesPage[var_sawGalleryShow_imagesPage.length - 1].imageNumber < var_sawGalleryShow_imagesTotalCount){
        
        sawGalleryShow_reloadImagesPage(var_sawGalleryShow_curImageNumber, var_sawGalleryShow_imagesPageSize, var_sawGalleryShow_seed);
        
    }
    else if (!sawGalleryShow__canImageBeShown(imageNumber)){
        sawGalleryShow_reloadImagesPage(var_sawGalleryShow_curImageNumber, var_sawGalleryShow_imagesPageSize, var_sawGalleryShow_seed);
    }
    
    var progressBar = var_sawGalleryShow_root.find('.progress-bar-inner');
    if (var_sawGalleryShow_imagesTotalCount > 0)
        progressBar.stop().animate({width: $(progressBar.parent().parent()).width() * (var_sawGalleryShow_curImageNumber-1)/ (var_sawGalleryShow_imagesTotalCount-1)});
                    
}

function sawGalleryShow_reloadImagesPage(centerImageNumber, pageSize, seed){
    if (var_sawGalleryShow_isLoadingImagePage){
        return;
    }
    
    var_sawGalleryShow_isLoadingImagePage = true;
    
    var needToShowCurrentImage = !sawGalleryShow__canImageBeShown(centerImageNumber);
    
    $.ajax({
                url: const_sawGalleryShow_handlerPath, 
                data: { pageSize: pageSize, pos: centerImageNumber, seed: seed }, 
                dataType: 'json', 
                success: function(data){ 
                                            var_sawGalleryShow_imagesPage = data.imageItems;
                                            var_sawGalleryShow_imagesTotalCount = data.totalImagesCount - 0;
                                            
                                            sawGalleryShow_synchronize(sawGalleryShow_getImagesContainer(), var_sawGalleryShow_imagesPage);
                                            
                                            if (needToShowCurrentImage){
                                                sawGalleryShow_setCurrentImage(centerImageNumber, var_sawGalleryShow_slideAnimDuration);
                                            }
                                            
                                            var_sawGalleryShow_isLoadingImagePage = false;  
                                        },
                error: function(e){ 
                                        alert(e); 
                                        var_sawGalleryShow_isLoadingImagePage = false; 
                                    }
                });

}

function sawGalleryShow_createShowItem(imageNumber, imageLink, userName, postedDate, comment, position, imageInfo){
    var result = $('<div><table><tr><td></td></tr></table></div>');
    result.addClass('image-item');
    result.attr('imageNumber', imageNumber);
    result.css({left:position, top:0, display:'none'});
    
    var viewOnFacebook = '';
    if (imageInfo.facebookImageId != null && imageInfo.facebookImageId != ''){
        viewOnFacebook = "<br/><a style='color:#3B5998;font-family:lucida grande,tahoma,verdana,arial,sans-serif;' href='http://www.facebook.com/photo.php?fbid=" + imageInfo.facebookImageId + "' target='_blank'>View on Facebook</a>";
    }
    
    var footer = $("<div class='image-item-footer'></div>");
    $("<div class='first-line'>Posted by <b>" + userName + "</b> - " + postedDate + "</div>").append(viewOnFacebook).appendTo(footer);
    $("<div class='second-line'>" + comment + "</div>").appendTo(footer);
    $("<div class='fb-like-area'></div>").prependTo(footer);

    var image = $('<img/>');
    image.attr('src', imageLink);
    image.attr('width', null);
    image.attr('height', null);
    result.find('td')
    .append(image)
    .append(footer);
    
    return result;
}

function sawGalleryShow_createLike(imageLink){
    var result = $('<iframe src="http://www.facebook.com/plugins/like.php?href='
                    + encodeURIComponent(imageLink)
                    + '&send=false&layout=button_count&image=test&width=300&show_faces=false&action=like" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:21px;" allowTransparency="true"></iframe>'
                 );
                 
    return result;
}

function sawGalleryShow_getCurImageNumberFromRequest(){
    var index = window.location.href.indexOf('#');
    if (index == -1)
        return -1;
        
    var result = parseInt(window.location.href.substr(index + 1, window.location.href.length - index - 1));
    
    if (!result)
        return -1;
        
    return result;
}

function sawGalleryShow_init(showRoot, seed){
    if (seed != null){
        var_sawGalleryShow_seed = seed;
    }

    $(function(){
        var_sawGalleryShow_root = $(showRoot);
                
        $('.saw-show-bg').remove().appendTo($('body'));
        $('.saw-show-bg').width($(document).width());
        $('.saw-show-bg').height($(document).height());
        var_sawGalleryShow_root.remove().appendTo($('body'));
        
        $('body').click(function() {
           sawGalleryShow_end(); 
        });
        
        var_sawGalleryShow_root.click(function(event){
            event.stopPropagation();
        });
        
        var_sawGalleryShow_root.find('.saw-show-prev').click(function(){
            sawGalleryShow_prev();
        });
        
        var_sawGalleryShow_root.find('.saw-show-next').click(function(){
            sawGalleryShow_next();
        });
        
        //preloading pictures
        var minImageNumber = -1;
        $('.saw-gal-image-item-inner input').each(function(index, value){
            if (parseInt($(value).val()) < minImageNumber || minImageNumber == -1){
                minImageNumber = parseInt($(value).val());
            }
                
        });
        
        var curImNum = sawGalleryShow_getCurImageNumberFromRequest();
        if (curImNum != -1)
            sawGalleryShow_begin(curImNum);
        else if (minImageNumber != -1)
            sawGalleryShow_reloadImagesPage(minImageNumber, var_sawGalleryShow_imagesPageSize, var_sawGalleryShow_seed);
            
        
    });
    
    $(window).scroll(function(){
        if (var_sawGalleryShow_root.height() >= $(window).height())
            return;
    
        var_sawGalleryShow_root.stop().animate({left:$(window).width() / 2 - var_sawGalleryShow_root.width() / 2 + $(window).scrollLeft(),
                        top:$(window).height() / 2 - var_sawGalleryShow_root.height() / 2 + $(window).scrollTop()});
    });   
    
    $(window).resize(function() {
        $('.saw-show-bg').width($(document).width());
        $('.saw-show-bg').height($(document.body).height());
    });  
}

function sawGalleryShow__canImageBeShown(imgOrderNumber){
    if (var_sawGalleryShow_imagesPage.length == 0){
        return false;
    } else if (imgOrderNumber < var_sawGalleryShow_imagesPage[0].imageNumber 
                || imgOrderNumber > var_sawGalleryShow_imagesPage[var_sawGalleryShow_imagesPage.length - 1].imageNumber){
        return false;
    }
    
    return true;
}

function sawGalleryShow_begin(imgOrderNumber){
    sawGalleryShow_getImagesContainer().find('.image-item').each(function(i, item){
        $(item).hide();
    });
    
    var_sawGalleryShow_root.hide();
    var_sawGalleryShow_root.css({left:Math.max($(window).width() / 2 - var_sawGalleryShow_root.width() / 2 + $(window).scrollLeft(), 0),
                        top:$(window).height() / 2 - var_sawGalleryShow_root.height() / 2 + $(window).scrollTop() });               
    
    if (sawGalleryShow__canImageBeShown(imgOrderNumber))
        sawGalleryShow_setCurrentImage(imgOrderNumber, var_sawGalleryShow_slideAnimDuration);
    else
        sawGalleryShow_reloadImagesPage(imgOrderNumber, var_sawGalleryShow_imagesPageSize, var_sawGalleryShow_seed);
    
    $('.saw-show-bg').width($(document).width());
    $('.saw-show-bg').height($(document.body).height());
    $('.saw-show-bg').show();
    var_sawGalleryShow_root.show();
    
    
}

function sawGalleryShow_end(){
    var_sawGalleryShow_root.hide();
    $('.saw-show-bg').hide();
    
    sawGalleryShow_getImagesContainer().find('.image-item').each(function(i, item){
        $(item).hide();
    });
    
    var_sawGalleryShow_curImageNumber = null;
}

function sawGalleryShow_next(){
    if (var_sawGalleryShow_curImageNumber + 1 > var_sawGalleryShow_imagesPage[var_sawGalleryShow_imagesPage.length - 1].imageNumber)
        return;
        
    var_sawGalleryShow_curImageNumber ++;
    
    sawGalleryShow_setCurrentImage(var_sawGalleryShow_curImageNumber, var_sawGalleryShow_slideAnimDuration);
}

function sawGalleryShow_prev(){
    if (var_sawGalleryShow_curImageNumber - 1 < var_sawGalleryShow_imagesPage[0].imageNumber)
        return;
        
    var_sawGalleryShow_curImageNumber --;
    
    sawGalleryShow_setCurrentImage(var_sawGalleryShow_curImageNumber, var_sawGalleryShow_slideAnimDuration);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// app_code/staff/FbStaffUtil.vb
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var facebookConnect_HandlersPath = '../controls/staff/facebook/';
function facebookConnectReqWindow_init(textHtml){
    $(document).ready(function(){
        var windowContent = $("<div>" + textHtml + "</div>");
        var btnAccept = $("<div class='button small silver' style='margin:0px 20px 0px 5px;'><a href='javascript:void(0);'>&nbsp;Yes&nbsp;</a></div>'");
        var btnDecline = $("<div class='button small silver'><a href='javascript:void(0);'>&nbsp;No&nbsp;</a></div>'");
        var dvStatus = $("<div></div>");
        var dvButtonsArea = $("<div></div>");
        dvStatus.hide();
        
        windowContent.append(dvStatus);
        
        dvButtonsArea.append(btnAccept);
        dvButtonsArea.append(btnDecline);
        windowContent.append(dvButtonsArea);
        
        btnAccept.click(function(){ 
            dvButtonsArea.hide();
            facebookConnectReqWindow_showStatus(dvStatus, true, 'Connecting to Facebook...', 'Black');
            
            FB.login(function(response) {
                  if (response.session) {
                    facebookConnectReqWindow_showStatus(dvStatus, true, 'The connection has been successfully established. Saving the results to our server...', 'Green');
                    
                    setTimeout(function(){
                        $.get(facebookConnect_HandlersPath + "FBConnectManagementHandler.ashx?method=setup&access_token=" + response.session.access_token,
                            function(result) 
                            { 
                                if (result == 'success'){
                                    $(windowContent).dialog("close");
                                    common_refreshPageByGet();
                                } else {
                                    $(windowContent).dialog("close");
                                    common_refreshPageByGet();
                                }
                            }); 
                    }, 1000);
                    
                  } else {
                    facebookConnectReqWindow_showStatus(dvStatus, false, 'Connection failed, please try again.', 'Red');
                    dvButtonsArea.show();
                  }
            }, {perms:'publish_stream,offline_access'});
        });
        
        btnDecline.click(function(){
            dvButtonsArea.hide();
            facebookConnectReqWindow_showStatus(dvStatus, true, 'You have declined to connect to Facebook. Please wait...', 'Black');
            
            setTimeout(function(){
                $.get(facebookConnect_HandlersPath + "FBConnectManagementHandler.ashx?method=cancel",
                    function(result) 
                    { 
                        if (result == 'success'){
                            $(windowContent).dialog("close");
                            common_refreshPageByGet();
                        } else {
                            $(windowContent).dialog("close");
                            common_refreshPageByGet();
                        }
                    }); 
            }, 1000);
        });
    
        windowContent.dialog({ modal:true, minHeight:60, width:300, draggable:false, title:"Facebook Connection Setup", resizable:false, closeOnEscape:false});
  
    });
      
}

function facebookConnectReqWindow_showStatus(dvStatusContainer, showProgress, message, color){
    var dvStatusContainer = $(dvStatusContainer);
    
    dvStatusContainer.html(message);
    
    if (showProgress){
        dvStatusContainer.css({'padding': '5px 20px', 'background' : 'url(../images/ajax-loader.gif) left center no-repeat scroll transparent'});
    } else{
        dvStatusContainer.css({'padding-left':0, 'background' : 'transparent' });
    }
    
    
    dvStatusContainer.css('color', color);
    
    dvStatusContainer.show();
    
}

function facebookConnectReqWindow_hideStatus(dvStatusContainer){
    dvStatusContainer.css({display:none});
}

function facebookFbFriendsLiked_init(containerId){
    FB.api('/me', function(response) {
                var query = FB.Data.query('select uid, name, username from user where has_added_app=1 and uid in (select uid2 from friend where uid1={0}) limit 10', response.id);
                query.wait(function(rows) {
                    if (rows.length > 0) {
                        for (var i = 0; i < rows.length; i++) {
                            document.getElementById(containerId).innerHTML += '<a title="' + rows[i].name + '" href="http://www.facebook.com/profile.php?id=' + rows[i].uid + '"><img style="padding-right:4px;" src="http://graph.facebook.com/' + rows[i].uid + '/picture" alt="' + rows[i].name + ' is using StuckForStaff %>" /></a>'
                        }
                    }
                });
    });
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//popup input
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$(document).ready(function(){
    $('body').click(function(){
        $('.popup-input').animate({opacity: 0}, function(){$(this).remove();});
    });
});

function popupInput(containerSelector, initialValue, maxLength, succeedCallback){
    $('.popup-input').animate({opacity: 0}, function(){$(this).remove();});

    var container = $(containerSelector);

    

    var popup = $("<div class='popup-input'><div class='pi-arrow'/><div style='clear:both;padding-bottom:3px;'>Please type your comment</div><div class='popup-error'></div><div class='input-container'><textarea /></div></div>");
    popup.find('textarea').val(initialValue).attr('cols', 20).attr('rows', maxLength / 20 + 1);
    var btnSubmit = $("<div class='popup-ok'>Ok</div>");
    btnSubmit.click(function(){
        succeedCallback(popup.find('textarea').val());
        $('.popup-input').animate({opacity: 0}, function(){$(this).remove();});
    });
    
    popup.append(btnSubmit);
    popup.kernel = this;
    popup.callback = succeedCallback;
    popup.css({opacity: 0, left: 83, top: 18});
    popup.click(function (event){
        event.stopPropagation();
    });
    
    var checkMaxLength = function(){
        if (container.find('textarea').val().length > maxLength - 1){
            popup.find('.popup-error').html('').append("Your comment shouldn't exceed " + maxLength + " characters");
            return false;
        } else {
            popup.find('.popup-error').html('');
            return true;
        }
    }
    
    popup.find('textarea').keypress(function(event){
        if (event.which != '8' && !checkMaxLength())
            event.preventDefault();
        
        if (event.which == '8'){
            popup.find('.popup-error').html('');
        }
    }).change(function(){
    });
    
    
    container.append(popup);
    popup.animate({opacity: 1}, 500);
}

///////

$(function(){
        $('body').click(function(){
            $('.ip-addr-exp div div').hide().css({'z-index':0});
        });

        $('.ip-addr-exp').each(function(index, item){
            var item = $(item);
            
            item.find('a').click(function(){
                item.find('div div').css({'z-index':1});
                item.find('div div').slideToggle(200);
            });
            
            item.find('div div').click(function(event){
                event.stopPropagation();
            });
        });
    });

