/* Char Count */
// fieldname, warningname, remainingname, maxchars
function CheckFieldLength(fn,wn,rn,mc) {
    var len = fn.value.length;
    if (len > mc) {
        fn.value = fn.value.substring(0,mc);
        len = mc;
    }
    document.getElementById(wn).innerHTML = len;
    document.getElementById(rn).innerHTML = mc - len;
}

/* Chat Minimize */
function scrollChatMinimizeBar()
{
    if($('#ft-minimized-chat').length > 0) {
    // gembelly Scroller
    var gScroller = {
        children: $("#gembellies-cnt .g-item").length,
        itemWidth: $("#gembellies-cnt .g-item").outerWidth(true),
        itemHeight: $("#gembellies-cnt .g-item").outerHeight(true),
        columnItemPerPage: 10,
        columnItemWidth: 30,
        columnItemHeight: 27,
        fullPageWidth: 300,
        columnPageWidth: 852,
        isColumn: 0
    }

    gScroller.widthFull = gScroller.children * gScroller.itemWidth;
    gScroller.widthColumn = Math.ceil(gScroller.children / 10) * gScroller.columnPageWidth;

    $("#gembellies-cnt").width(gScroller.widthFull);

    $('#gallery-right').click(function(e) {
        e.preventDefault();
        if( (Math.abs($("#gembellies-cnt").position()['left']) + (gScroller.isColumn ? gScroller.columnPageWidth : gScroller.fullPageWidth)) >= (gScroller.isColumn ? gScroller.widthColumn : gScroller.widthFull)  ) {
            return false;
        } else {
            $("#gembellies-cnt").css({
                'left': '+=' + (gScroller.isColumn ? -gScroller.columnPageWidth : -gScroller.fullPageWidth)
            });
        }
    });

    $('#gallery-left').click(function(e) {

        e.preventDefault();
        if( (Math.abs($("#gembellies-cnt").position()['left']) - (gScroller.isColumn ? gScroller.columnPageWidth : gScroller.fullPageWidth)) < 0) {
            return false;
        } else {
            $("#gembellies-cnt").css({
                'left': '+=' + (gScroller.isColumn ? gScroller.columnPageWidth : gScroller.fullPageWidth)
            });
        }
    });


    }
}

function replaceAll(strTarget, strSubString, strText)
{
    var intIndexOfMatch = strText.indexOf( strTarget );

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1){
    // Relace out the current instance.
    strText = strText.replace( strTarget, strSubString )

    // Get the index of any next matching substring.
    intIndexOfMatch = strText.indexOf( strTarget );
    }

    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return( strText );
}


$(function() {
    
    /* Profile footer */
    $('#ft-messages-opener').click(function(e) {
        e.preventDefault();
        $('#ft-messages').addClass('active');
        
        //Run ajax, update inbox
        update_inbox_1();
    });

    $('#ft-messages-closer').click(function(e) {
        e.preventDefault();
        $('#ft-messages').removeClass('active');
        
        //Run ajax, update inbox
        update_unread_counter();
    });
    
    $('#ft-chat-opener').click(function(e) {
        e.preventDefault();
        $('#ft-chat').addClass('active');
    });

    $('#ft-chat-closer').click(function(e) {
        e.preventDefault();
        $('#ft-chat').removeClass('active');
    });
    
    
    $('#ft-chat-setting-opener').click(function(e) {
        e.preventDefault();
        $('#ft-chat-setting').addClass('active');
    });

    $('#ft-chat-setting-closer').click(function(e) {
        e.preventDefault();
        $('#ft-chat-setting').removeClass('active');
    });


    $('.editable').editable('/ajax-handler', { 
        type      : 'textarea',
        indicator:  '<img src="/img/cbox-loading-1.gif" />',
        submitdata: {
            action: 'inline-save', 
            "is_ajax": 1
        },
        height    : 95,
        cancel    : 'Cancel',
        submit    : 'Save',
        tooltip   : 'Click to edit...'
    });

    $('#new_my_box_title').editable('/ajax-handler', { 
        submitdata: {
            action: 'inline-save', 
            "is_ajax": 1
        },
        indicator:  '<img src="/img/cbox-loading-1.gif" />',
        width: 30,
        submit    : 'Save',
        tooltip   : 'My Dreams, Family, Friends...'
    });



    /*MSG Tabs*/
    $('#ft-message-tabs li a').click(function() {
        $('#ft-message-tabs li').removeClass('active');
        $(this).parent().addClass('active');
	
        $('.ft-msg-tab').removeClass('active');
        $($(this).attr('href')).addClass('active');
        
        if($(this).attr('href')=='#msg-new')
        {
            $('#bff-message-form').fadeIn(1000);
            $('#bff-message-success').hide();
            ResetFormMessage();
        }
        else if($(this).attr('href')=='#msg-sent')
        {
            update_inbox();
        }
        else if($(this).attr('href')=='#msg-inbox')
        {
            update_inbox_1();
        }
    });

//Script will attach on html
/*
    $('.msg-from, .msg-subject').click(function(e) {
        var doAjaxRead = false;
        if( !$(this).parents('.msg:first').hasClass('read')) {
            doAjaxRead = true;
        }
        
        $(this).parents('.msg:first').toggleClass('active').addClass('read');
        
        if( doAjaxRead ) {
            $.ajax({
                url: '/ajax-handler/',
                dataType: 'json',
                data: {
                    'action': 'message-read', 
                    'msg': $(this).parents('.msg:first').attr('data-id'), 
                    'user_id': $('#current_user').val(), 
                    'is_ajax': true
                },
                complete:function() {
                },
                success: function (data, status) {
                    if(!data.status) {
                    } else {
                        unread=$('#unread').html();
                        unread=unread.replace('+', "");
                        unread=parseInt(unread);
                        unread=unread-1;
                        if(unread==0) $('#unread').html('');
                        else $('#unread').html(unread+'+');
                }
                },
                error: function() {
                }
            });
        }
    });

    $('.msg-content .reply').click(function() {
        $('#ft-message-tabs li').removeClass('active');
        $('.ft-message-tabs li:last').addClass('active');
	$('.ft-msg-tab').removeClass('active');
        $('#msg-new' ).addClass('active');
        $('.chzn-select').val($(this).attr('sender_id'));
        $('.chzn-select').trigger('liszt:updated');
        $('.chzn-select').trigger('change');
        parent = $(this).parent().parent().parent().clone();
        $('#send_subject').val("RE: "+ parent.find('div').html());
    });
    
    $('.msg-content .forward').click(function() {
        $('#ft-message-tabs li').removeClass('active');
        $('.ft-message-tabs li:last').addClass('active');
	$('.ft-msg-tab').removeClass('active');
        $('#msg-new').addClass('active');
        
        parent = $(this).parent().parent().parent().clone();
        
        $('#send_subject').val("FW: "+ parent.find('div').html());
        parent.find('div p').remove();
        $('#bff_message_content').text(parent.find('div:.msg-content').text().trim());
    });
*/

    /*Scroll*/
    $('.message_codes-cnt').jScrollPane({
        autoReinitialise: true
    });
    $('#ft-chat-container').jScrollPane({
        autoReinitialise: true
    });
    
    $('.ft-msg-tab').jScrollPane({
        autoReinitialise: true
    });
    $('.editable textarea').jScrollPane({
        autoReinitialise: true
    });
    $('#things-box-cnt').jScrollPane({
        autoReinitialise: true
    });
    $('#history-box-cnt').jScrollPane({
        autoReinitialise: true
    });
    $('#div_my_gemz').jScrollPane({
        autoReinitialise: true
    });


    /* Shop */
    if($('.shop-tabs').length > 0) {
        $('#shop-tabs li').click(function(e) {
            e.preventDefault();
            var who =$(this).attr('id').replace('-tab', '');
		
            $('#shop-tabs li').removeClass('selected');
            $('#shop-tabs-cnt > div').removeClass('selected');

            $('#' + who + '-tab').addClass('selected');
            $('#' + who + '-cnt').addClass('selected');
        });

        $('#close-tabs').click(function(e) {
            e.preventDefault();
            $('#shop-tabs li').removeClass('selected');
            $('#shop-tabs-cnt > div').removeClass('selected');
        });
    }

    if($('#shop-gallery').length > 0) {
        // gembelly Scroller
        var gScroller = {
            children: $("#gembellies-cnt .g-item").length,
            itemWidth: $("#gembellies-cnt .g-item").outerWidth(true),
            itemHeight: $("#gembellies-cnt .g-item").outerHeight(true),
            columnItemPerPage: 12,
            columnItemWidth: 107,
            columnItemHeight: 145,
            fullPageWidth: 441,
            columnPageWidth: 852,
            isColumn: 0
        }
	
        gScroller.widthFull = gScroller.children * gScroller.itemWidth;
        gScroller.widthColumn = Math.ceil(gScroller.children / 12) * gScroller.columnPageWidth;
	
        $("#gembellies-cnt").width(gScroller.widthFull);

        $('#show-more').click(function(e) {
            e.preventDefault();
            $('#shop-gallery').addClass('all');
            $("#gembellies-cnt").width(gScroller.widthColumn).css({
                left:0
            });
	
            gScroller.isColumn = 1;
		
            $('#gembellies-cnt .g-item').each(function(ix ){
                $(this).css({
                    'position': 'absolute', 
                    'left': Math.floor(ix / gScroller.columnItemPerPage) * gScroller.columnPageWidth + (ix % 6) * gScroller.itemWidth, 
                    'top': Math.floor(ix / 6) % 2 * gScroller.itemHeight
                });
            });
        });

        $('#show-less').click(function(e) {
            e.preventDefault();
            $('#shop-gallery').removeClass('all');
            $("#gembellies-cnt").width(gScroller.widthFull).css({
                left:0
            });

            gScroller.isColumn = 0;

            $('#gembellies-cnt .g-item').each(function(ix ){
                $(this).css({
                    'position': 'relative', 
                    'top': 'auto', 
                    'left': 'auto'
                });
            });
        });


        $('#gallery-right').click(function(e) {
            e.preventDefault();
            if( (Math.abs($("#gembellies-cnt").position()['left']) + (gScroller.isColumn ? gScroller.columnPageWidth : gScroller.fullPageWidth)) >= (gScroller.isColumn ? gScroller.widthColumn : gScroller.widthFull)  ) {
                return false;
            } else {
                $("#gembellies-cnt").css({
                    'left': '+=' + (gScroller.isColumn ? -gScroller.columnPageWidth : -gScroller.fullPageWidth)
                });
            }
        });

        $('#gallery-left').click(function(e) {

            e.preventDefault();
            if( (Math.abs($("#gembellies-cnt").position()['left']) - (gScroller.isColumn ? gScroller.columnPageWidth : gScroller.fullPageWidth)) < 0) {
                return false;
            } else {
                $("#gembellies-cnt").css({
                    'left': '+=' + (gScroller.isColumn ? gScroller.columnPageWidth : gScroller.fullPageWidth)
                });
            }
        });
		
	
    }
    
    scrollChatMinimizeBar();
    
    /* Login Form */
    var logsupcnt = $('#logsup-cnt');
    $('#log-sign-up-btn').click(function() {
        logsupcnt.css({
            top: "-45px"
        });
    });
	
    $('#back-to-login').click(function(){
        logsupcnt.css({
            top: "0px"
        });
    });
	
    $('#go-to-step2').click(function() {
        logsupcnt.css({
            top: "-90px"
        });
    });

    $('.username').click(function(){
        $('.menu-user-settings').animate({
            height:'toggle'
        });
    });

    /*
	$('.sign-in').click(function() {
		$('#login').css('display','block');
		$(this).css('display','none');
	});

	$('#close-sign-up, #close-step-one, #close-step-two').click(function() {
		$('#login').css('display','none');
		$('#sign-up').css('display', 'none');		
		$('.sign-in').css('display','block');
	});

	$('#sign-up-button').click(function(){
		$('#login').css('display','none');
		$('#sign-up').css('display', 'block');		
		$('.step-one').css('display', 'block');		
		$('#sign-up-arrow-back2').css('display','none');
	});

	$('#sign-up-arrow-next').click(function(){
		$('#sign-up').css('display', 'block');		
		$('.step-one').css('display', 'none');		
		$('.step-two').css('display', 'block');		
		$('#sign-up-arrow-back2').css('display','block');
	});

	$('#sign-up-arrow-back').click(function(){
		$('#sign-up').css('display', 'none');		
		$('#login').css('display','block');
		$('.step-one').css('display', 'none');		
	});

	$('#sign-up-arrow-back2').click(function(){
		$('#step-two').css('display', 'none');		
		$('.step-one').css('display', 'block');		
		$('#sign-up-arrow-back2').css('display','none');
	});

*/

    /* Shop */
    /*
	$('#choose-amount').change(function(){
		$('#amount').attr('value', $("#choose-amount option:selected").val()); 
		$('#item_name').attr('value', $("#choose-amount option:selected").attr('title')); 
		$('#a3').attr('value', $("#choose-amount option:selected").val()); 
	});
	$('#how-often').change(function(){
		$('#p3').attr('value', $("#how-often option:selected").val()); 
	});


	$(".select-style-dl-child dd a").click(function() {
		$("#theme_color").remove();
		$('<link href="' + $(this).attr("rel") + '" id="theme_color" rel="stylesheet" type="text/css" />').appendTo('head');
		return false;
	});

	$(".select-wallpaper-dl-child dd a").click(function() {
		$("#wallpaper_color").remove();
		$('<link href="' + $(this).attr("rel") + '" id="wallpaper_color" rel="stylesheet" type="text/css" />').appendTo('head');
		return false;
	});
*/

    /* Pulsate */
    function runEffect() {
        $( "#create-button" ).effect( "pulsate", 1000, runEffect );		
    };
    runEffect();


    /* Inline */
    $("a[rel^='prettyPhoto']").prettyPhoto({
        'social_tools': '', 
        theme: 'light_square'
    });

    $("#creator-accordion").accordion();
    $('#select-wallpaper-ul-parent').prettyGallery();
    $('#select-style-ul-parent').prettyGallery();
    $('#slider').anythingSlider();
    $(".add-thumb").colorbox();
    $(".add-status").colorbox();
    $(".add-story").colorbox();
    $(".add-my-family-status").colorbox();
    $("#add-new-friend").colorbox( {width:'320px',height:'240px'});
    $("#add-new-friend2").colorbox( {width:'320px',height:'240px'});
    		

    /*
	$("#karma").colorbox();
*/
    $("#add-things-i-love").colorbox();
    $(".img-link").colorbox({
        rel: 'group-pics'
    });
    $("#manage-pictures").colorbox();
	
    if($('#h-gallery')) {
        var 	scrollCnt = $('.h-gallery-items ul'),
        imgCnts = $('.h-gallery-items ul li'),
        thumbs = $('.h-gallery-thumbs .h-gallery-thumb .h-gallery-t-link'),
        numThumbs = thumbs.length,
        autoRunTime = 4000,
        timeoutID;

        $('.h-gallery-t-link').click(function(e) {
            if(timeoutID) {
                clearTimeout(timeoutID);
            }

            e.preventDefault();
            $('.h-gallery-t-link').removeClass('active');
            $(this).addClass('active');

            //			$(scrollCnt).animate({left: $(this).attr('data-pos')});
            imgCnts.removeClass('active');
            $('#' + $(this).attr('data-pos')).addClass('active');

            timeoutID = setTimeout(autoRun, autoRunTime);

            return false;
        });
	
        function autoRun() {
            var nextPar, next, actParent = $('.h-gallery-t-link.active').parent();

            $('.h-gallery-t-link').removeClass('active');
            if( (nextPar = actParent.next('.h-gallery-thumb')).length > 0 ) {
                next = $('.h-gallery-t-link', nextPar);
            } else {
                next = $('.h-gallery-thumbs .h-gallery-thumb:first .h-gallery-t-link')
            }
            next.addClass('active');
            //			$(scrollCnt).animate({left: next.attr('data-pos')});
            imgCnts.removeClass('active');
            $('#' + next.attr('data-pos')).addClass('active');
			
            timeoutID = setTimeout(autoRun, autoRunTime);
        }

        timeoutID = setTimeout(autoRun, autoRunTime);


    }

});


  
