var reload_content = null;

function log(msg){
    if (window.console) 
        console.log(msg);
}

// Seems this function isn't available for some reason. Copied from bali.js.
function bali_url(url) {
	// Get the base element.
	var base = $('base').attr('href');

	// Check the base URL has a trailing slash.
	if (base.substr(base.length -1) != '/') {
		base += '/';
	}

	// Ensure the URL we were passed doesn't have a leading slash.
	if (url.substr(0, 1) == '/') {
		url = url.substr(1);
	}

	return base+url;
}

function ajaxFileUpload(path, element_id, listener){
    //starting setting some animation when the ajax starts and completes
    $("#loading").ajaxStart(function(){
        $(this).show();
    }).ajaxComplete(function(){
        $(this).hide();
    });
    
    /*
     prepareing ajax file upload
     url: the url of script file handling the uploaded files
     fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
     dataType: it support json, xml
     secureuri:use secure protocol
     success: call back function when the ajax complete
     error: callback function when the ajax failed
     
     */
    $.ajaxFileUpload({
        url: bali_url(path),
        secureuri: false,
        fileElementId: element_id,
        dataType: 'json',
        success: function(data, status){
            if (typeof(data.error) != 'undefined') {
                if (data.error != '') {
                    alert('Error: ' + data.error);
                }
                else {
					listener(data);
                }
            }
        },
        error: function(data, status, e){
            alert(e);
        }
    });
    
    return false;
    
}

function setup_acount_edits() {
    $('a#logo_edit').click(function(){
        $('a#logo_edit, div.logo_upload_container').toggle();
        return false;
    });
    
    $('#logo_save').click(function(){
        ajaxFileUpload('/members/account/upload_logo', 'member_logo', function(data){
			if (typeof(data.logo) != 'undefined') {
				$('div.tablemember_logo img').attr('src', data.logo + '?ts' + new Date().getTime());
			}
			
            $('a#logo_edit, div.logo_upload_container').toggle();
        });
		
        return false;
    });
	
	/**
	 * Member Image
	 */
    $('a#image_edit').click(function(){
        $('a#image_edit, div.image_upload_container').toggle();
        return false;
    });
    
    $('#image_save').click(function(){
        ajaxFileUpload('/members/account/upload_image', 'member_image', function(data){
			if (typeof(data.image) != 'undefined') {
				$('#member_img').attr('src', data.image + '?ts' + new Date().getTime());
			}
			
            $('a#image_edit, div.image_upload_container').toggle();
        });
		
        return false;
    });
    
    /**
     * Affiliate Logo
     */
    $('a#affiliate_logo_edit').click(function() {
    	$('a#affiliate_logo_edit, div.affiliate_logo_upload_container').toggle();
    	return false;
    });
    
    $('#affiliate_logo_save').click(function() {
    	ajaxFileUpload('/members/account/upload_affiliate_image', 'affiliate_logo', function(data) {
    		if (typeof(data.image) != 'undefined') {
				$('.affiliate_logo_img').attr('src', data.image + '?ts' + new Date().getTime());
    		}
    		
	    	$('a#affiliate_logo_edit, div.affiliate_logo_upload_container').toggle();
    	});
    	
    	return false;
    });

	$('a.edit_link').colorbox({opacity: 0.6});

	$().bind('cbox_closed', function(){
		log('cbox_closed: ' + reload_content);
		setTimeout(reload_content_onclose, 500);
	});	
	// trap popup form submits
	$('input.submit_btn').live('click', function() {
		var $this = $(this);
		var rel = $this.attr('rel');
		var form = $this.parents('form');

		$.post(
			$(form).attr('action'),
			$('input, select, textarea').serialize(),
			function (data) {
				if ('save' != $this.attr('name')) {
					$('#cboxLoadedContent').empty().html(data);
					return;
				}
				$.fn.colorbox.close();
				
				if ('edit' == rel) {
					$('#contact_content').html(data);
				} else if ('overview' == rel) {
					$('#member_description').empty().html(data);
				} else if ('other_details' == rel) {
					$('#other_details_content').empty().html(data);
				} else if ('disciplines' == rel) {
					$('#disciplines_content').empty().html(data);
				} else if ('linked_in' == rel) {
					$('#linked_in_content').empty().html(data);
					$('#linked_in_content a.edit_link').colorbox({opacity: 0.6});
				}
				
				reload_content = null;
			}
		);
		return false;
	});
}
function remove_discipline(id, obj) {
	if (confirm('Are you sure you want to remove this discipline?')) {
		$.get('/members/account/remove_discipline/' + id + '?confirm=true', function(){
			$(obj).parents('tr').remove();
			reload_content = 'disciplines';
		});
	}
}

function reload_content_onclose() {
	if ('disciplines' == reload_content) {
		$.get('/members/account/disciplines/reload', function (data) {
			$('#disciplines_content').empty().html(data);
		});
	}
	reload_content = null;
}

