
paths['admin_base_url']         = paths['base_url'] + 'admin/';
paths['css_admin_folder']       = paths['base_url'] + 'public/admin/css/';
paths['js_admin_folder']        = paths['base_url'] + 'public/admin/js/';
paths['images_admin_folder']    = paths['base_url'] + 'public/admin/images/';
paths['base_url']               = paths['base_url'] + '';
paths['css_front_folder']       = paths['base_url'] + 'public/frontend/css/';
paths['js_front_folder']        = paths['base_url'] + 'public/frontend/js/';
paths['images_front_folder']    = paths['base_url'] + 'public/frontend/images/';

paths['css_shared_folder']      = paths['base_url'] + 'public/shared/css/';
paths['js_shared_folder']       = paths['base_url'] + 'public/shared/js/';
paths['images_shared_folder']   = paths['base_url'] + 'public/shared/images/';
paths['modules_shared_folder']  = paths['base_url'] + 'public/shared/';
paths['users_folder']           = paths['base_url'] + 'public/users/';
paths['groups_folder']          = paths['base_url'] + 'public/groups/';

var vd_ajax_img = Array();

vd_ajax_img['common']   = '<img src="' + paths['images_shared_folder'] + 'ajaxLoader.gif" />';
vd_ajax_img['3d']       = '<img src="' + paths['images_shared_folder'] + '3d-rotation.gif" />';
vd_ajax_img['points']   = '<img src="' + paths['images_shared_folder'] + 'points-indicator.gif" />';
vd_ajax_img['circle']   = '<img src="' + paths['images_shared_folder'] + 'circle-indicator.gif" />';
vd_ajax_img['button'] 	= '<img src="' + paths['images_shared_folder'] + 'loader.gif" />';
vd_ajax_img['button2'] 	= '<img src="' + paths['images_shared_folder'] + 'button-disabled.gif" />';
vd_ajax_img['loader'] 	= '<img src="' + paths['images_shared_folder'] + 'loader.gif"  alt="Please wait, loading..."  style="margin: 30px 90px; " />';


//Preload used images here
jQuery("<img>").attr("src", paths['images_shared_folder'] + 'points-indicator.gif');
jQuery("<img>").attr("src", paths['images_shared_folder'] + 'button-disabled.gif');

//checking for errors in AJAX requests
function vdAjaxError(result, displayError) {
	var errorKeyword = '[error]';
	if(result.indexOf(errorKeyword) == 0) {
		if(displayError) {
			alert(result.substring(errorKeyword.length, result.length).replace(/\\n/i, ' \n\n'));
		}
		return true;
	} else {
		return false;
	}
}

//reference to object that was clicked to open window. Used to hide that element if needed
var jqmWindowObjectCaller;

$(document).ready(function() {

    $('.fnc-onload').removeClass('fnc-onload');

    // fadeout after 5 sec
    setTimeout(function(){
         $(".info-help.green:not(.fnc-nofadeout), .fnc-fadeout").fadeOut("slow", function () {
            $(".info-help.green:not(.fnc-nofadeout), .fnc-fadeout").remove();
        });
    }, 5000);
    
    $(".fnc-closeinfo").click(function() {
        $(this).parent().fadeOut();
    });
    
	$("a.fnc-wait").click(function(){ 
        $(this).parent().html(vd_ajax_img['points']);
    });
 	//common actions
 	$(".hideable").click(function(){ 
        $(this).parent('div').prepend(vd_ajax_img['button']);
        $(this).hide();
		
    });


    $(".fnc-hideable").click(function(){        
         $(this).before(vd_ajax_img['button2']).hide();
    });

    $(".fnc-hideable-points").click(function(){
         $(this).before(vd_ajax_img['points']).hide();
    });
			
	$('a.fnc-remove-friend').click(function(){ 
		if(confirm('Are you sure you want to remove this friend?')) {
			$('#fnc-send-msg-' + $(this).attr('href') ).fadeOut();
            $(this).fadeOut().load(paths['base_url'] + 'friends/ajax_remove_friend/' + $(this).attr('href'));
		}
		return false;
    });

    $('.fnc-save-pref').click(function(){
        $(this).html(vd_ajax_img['points']);
        $.ajax({
            url: paths['base_url'] + 'profile/ajax_save_preference/' + $(this).attr('href')
        });
        $(this).ajaxStop(function() {
            $('.fnc-pref-container').slideUp();
        });
		return false;
    });
	
	$('a.edit-tags').click(function(){
		$('#modal-windows').empty().html('<div id="jqmTags" class="jqmWindow">' + vd_ajax_img['loader'] + '</div>'); 		
		$('#jqmTags').jqm({ ajax: paths['module_url'] + "ajax_edit_tags/" + $(this).attr('href') }).jqmShow();
	    return false;
	});
	
	$("a.edit-description").click(function(){
		$('#modal-windows').empty().html('<div id="jqmDescription" class="jqmWindow">' + vd_ajax_img['loader'] + '</div>'); 		
		$('#jqmDescription').jqm({ ajax: paths['module_url'] + "ajax_edit_" + currentItemType + "_description/" + $(this).attr('href') }).jqmShow();
	    return false;
	});
	
	$("a.fnc-add-friend").click(function(){
	    if ( ! me['logged']) {
	        alert('You need to be registered and logged in to use this feature.');
	        return false;
	    }
	    
	    if ( ! me['published']) {
	        alert('You must publish your profile in order to use this feature.');
	        return false;
	    }
	    	    
		jqmWindowObjectCaller = $(this);
		$('#modal-windows').empty().html('<div id="jqmFriendAdd" class="jqmWindow">' + vd_ajax_img['loader'] + '</div>'); 		
		$('#jqmFriendAdd').jqm({ ajax: paths['base_url'] + "friends/ajax_add_friend/" + $(this).attr('href') }).jqmShow();
	    return false;
	});
	
	$("a.fnc-add-associate").click(function(){
        /* don't check this anymore ***RECHECK
        if ( me['level'] == 52 && me['id_plan'] == 0) {
	        alert('You need to upgrade your account to premium to use this feature.');
	        return false;
	    }
	    */
		jqmWindowObjectCaller = $(this);
		$('#modal-windows').empty().html('<div id="jqmAssociateAdd" class="jqmWindow">' + vd_ajax_img['loader'] + '</div>'); 		
		$('#jqmAssociateAdd').jqm({ ajax: paths['base_url'] + "associates/ajax_add_friend/" + $(this).attr('href') }).jqmShow();
	    return false;
	});
	
	$("a.fnc-send-msg").click(function(){
	    if ( ! me['logged']) {
	        alert('You need to be registered and logged in to use this feature.');
	        return false;
	    }
	    
		jqmWindowObjectCaller = $(this);
		$('#modal-windows').empty().html('<div id="jqmSendMsg" class="jqmWindow">' + vd_ajax_img['loader'] + '</div>'); 		
		$('#jqmSendMsg').jqm({ ajax: paths['base_url'] + "messages/ajax_send_message/" + $(this).attr('href') }).jqmShow();
	    return false;
	});
	
	
	$("a.fnc-forgot-pass").click(function(){
		jqmWindowObjectCaller = $(this);
		$('#modal-windows').empty().html('<div id="jqmForgotPass" class="jqmWindow">' + vd_ajax_img['loader'] + '</div>'); 		
		$('#jqmForgotPass').jqm({ ajax: paths['base_url'] + "login/ajax_forgot_pass/"  }).jqmShow();
	    return false;
	});
    
    /*$("a.fnc-group-join").click(function(){
        return (confirm('Are you sure you want to join this group?'));
    }); */
    
    $("a.fnc-group-leave").click(function(){
        return (confirm('Are you sure you want to leave this group?'));
    });
    
	$('a.fnc-add-favorites').click(function() { 
	    if ( ! me['logged']) {
	        alert('You need to be registered and logged in to use this feature.');
	        return false;
	    }
		if(confirm('Add to Favorites?')) {
			var company_data =  $(this).attr('href');
            $.get(paths['base_url'] + "favorites/ajax_add_to_favorites/" + company_data);
            $(this).fadeOut();
		} 
		return false;
	});
    
    $(".fnc-see-video").click(function(){
		jqmWindowObjectCaller = $(this);
		$('#modal-windows').empty().html('<div id="jqmSeeVideo" class="jqmWindow">' + vd_ajax_img['loader'] + '</div>'); 		
		$('#jqmSeeVideo').jqm({ ajax: paths['base_url'] + "videos/ajax_get_profile_video_code/" + $(this).attr('rel') }).jqmShow();
	    return false;
	});

     $(".fnc-see-profile-video").click(function(){
        var id = $(this).attr('rel');
        var type= $(this).attr('rev');
        var profileLink = function(){            
            $('#jqmSeeVideo').append('<a href="' + paths['base_url'] + type + '/' + id + '" id="jqmProfileLink" class="more-link" title="View Profile">View Profile</a>');
        };
        jqmWindowObjectCaller = $(this);
        $('#modal-windows').empty().html('<div id="jqmSeeVideo" class="jqmWindow">' + vd_ajax_img['loader'] + '</div>');
        $('#jqmSeeVideo').jqm({ ajax: paths['base_url'] + "videos/ajax_get_profile_video_code/" + id, onLoad: profileLink }).jqmShow();
        return false;
     });

    $(".fnc-see-help-video").click(function(){
		jqmWindowObjectCaller = $(this);
		$('#modal-windows').empty().html('<div id="jqmSeeVideo" class="jqmWindow">' + vd_ajax_img['loader'] + '</div>');
		$('#jqmSeeVideo').jqm({ ajax: paths['base_url'] + "tutorials/ajax_get_video_code/" + $(this).attr('rel') }).jqmShow();
	    return false;
	});
	
	$('a.fnc-logged-only').click(function() { 
	    if ( ! me['logged']) {
	        alert('You need to be registered and logged in to use this feature.');
	        return false;
	    }
	});


    //Wishlists
    $('.fnc-add-to-wishlist').click(function() {

        if ( ! me['logged']) {
	        alert('You need to be registered and logged in to use this feature.');
	        return false;
	    }

        var id = $(this).attr('href');
        var id_product = id.split('/');
        $.ajax({
            url: paths['base_url'] + "products/ajax_add_to_wishlist/" + id
        });

        $(this).ajaxStop(function() {
            $(this).hide();
            $("#fnc-fav-r-" + id_product[0]).show();
        });

        return false;
    });



    // remove from wishlist
    $('.fnc-remove-from-wishlist').click(function() {

        if ( ! me['logged']) {
	        alert('You need to be registered and logged in to use this feature.');
	        return false;
	    }

        var id = $(this).attr('href');

        $.ajax({
            url: paths['base_url'] + "products/ajax_remove_from_wishlist/" + id
        });

        $(this).ajaxStop(function() {
            $(this).hide();
            $("#fnc-fav-a-" + id).show();
        });

        return false;
    });

    // box toggle
    $('.fnc-box-handler h3').click(function(){
                var box =  $(this).parents('.standard-box');

                box.find('.box-content').toggle();
                box.find('.box-footer').toggle();
                box.find('.search').toggle();
                box.find('.additional-menu').toggle();
                box.toggleClass('closed');

                var closedFlag = $.cookie($(this).parents('.standard-box').attr('id')) == 1 ? 0 : 1;
                $.cookie($(this).parents('.standard-box').attr('id'), closedFlag, { expires: 180 });
                return false;
            });
      $(".fnc-box-handler").parents('.standard-box').each(function(){
                 if($.cookie($(this).attr('id')) == 1) {
                    $('#' + $(this).attr('id')).find('.box-content').toggle();
                    $('#' + $(this).attr('id')).find('.box-footer').toggle();
                    $('#' + $(this).attr('id')).find('.search').toggle();
                    $('#' + $(this).attr('id')).find('.additional-menu').toggle();
                    $('#' + $(this).attr('id')).toggleClass('closed');
                }
        });


        

        //Invitation modal
        $(".fnc-invitation").click(function(e){            
            
            // external or internal invite tabs? (internal => show all 3 tabs; external => hide 'WTGE friends' tab)
            var external = $(this).attr('rel') == 'external' ? 'external' : '';           
            if ($(this).attr('name') != '') external = $(this).attr('name'); // ID_ITEM override! meaning: if we want to invite our friends to group or photo or whatever, set ID_GROUP in name attribute, and since we don't need 'external' parameter anyway in this case, just override it with ID_ITEM of whatever you're inviting user to
            
            // which controller function should we call.. regular "invite page" or "invite to couple page"..
            var page_name = 'show_page'; // regular invitation page
            if ( $(this).attr('id') == 'fnc-show-couple-page' )         page_name = 'couple_page'; // show couple page instead (this is acctually call to different CONTROLER FUNCTION, which will pass some other required params etc.. so don't screw this up..)
            if ( $(this).attr('id') == 'fnc-show-wparty-page' )         page_name = 'wparty_page'; // show wparty invite page instead...
            if ( $(this).attr('id') == 'fnc-show-group-page' )          page_name = 'group_page'; 
            if ( $(this).attr('id') == 'fnc-show-association-page' )    page_name = 'association_page';  
            
            e.preventDefault();           
            $.nyroModalManual({
                url: paths['base_url'] + 'invite/' + page_name + '/' + external,       
                bgColor: '#081F27',
                css: {
                    bg: { height: $('body').height() + 400  },
                    content: { overflow: 'hidden' }
                }
            });
             
            return false;
        });
	   
	   
	   // CPL account switch in header
        $('.account-switch .change-link').click(function(){
            $('.account-switch .menu').slideToggle();
            return false;
        });
    
    
    
    
    // block global search to allow empty search string
    $('.fnc-validate-form').submit(function() {
        
        if ( $('.fnc-required-nonempty-field').val() == '') {
            alert('Please enter a search word.');
            return false;
        } else {
            return true;
        }
    });



});


jQuery.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback);
};	

function user_folder(id_user) {
    var prefix = id_user % 1000; // prep folder last 3 digits

    if (prefix > 99) { }
    else if (prefix > 9) prefix = "0" + prefix;
    else prefix = "00" + prefix;

    return prefix + "/" + id_user;
}